1# -- 2# Copyright (C) 2001-2020 OTRS AG, https://otrs.com/ 3# -- 4# This software comes with ABSOLUTELY NO WARRANTY. For details, see 5# the enclosed file COPYING for license information (GPL). If you 6# did not receive this file, see https://www.gnu.org/licenses/gpl-3.0.txt. 7# -- 8 9package Kernel::System::Console::Command::Dev::Package::Build; 10 11use strict; 12use warnings; 13 14use parent qw(Kernel::System::Console::BaseCommand); 15 16our @ObjectDependencies = ( 17 'Kernel::System::Main', 18 'Kernel::System::Package', 19); 20 21sub Configure { 22 my ( $Self, %Param ) = @_; 23 24 $Self->Description('Create an OTRS package (opm) file from an OTRS package source (sopm) file.'); 25 $Self->AddOption( 26 Name => 'version', 27 Description => "Specify the version to be used (overrides version from sopm file).", 28 Required => 0, 29 HasValue => 1, 30 ValueRegex => qr/^\d{1,4}[.]\d{1,4}[.]\d{1,4}$/smx, 31 ); 32 $Self->AddOption( 33 Name => 'module-directory', 34 Description => 35 "Specify the directory containing the module sources (otherwise the OTRS home directory will be used).", 36 Required => 0, 37 HasValue => 1, 38 ValueRegex => qr/.*/smx, 39 ); 40 $Self->AddArgument( 41 Name => 'source-path', 42 Description => "Specify the path to an OTRS package source (sopm) file that should be built.", 43 Required => 1, 44 ValueRegex => qr/.*/smx, 45 ); 46 $Self->AddArgument( 47 Name => 'target-directory', 48 Description => "Specify the directory where the generated package should be placed.", 49 Required => 1, 50 ValueRegex => qr/.*/smx, 51 ); 52 53 return; 54} 55 56sub PreRun { 57 my ( $Self, %Param ) = @_; 58 59 my $SourcePath = $Self->GetArgument('source-path'); 60 if ( !-r $SourcePath ) { 61 die "File $SourcePath does not exist / cannot be read.\n"; 62 } 63 64 my $TargetDirectory = $Self->GetArgument('target-directory'); 65 if ( !-d $TargetDirectory ) { 66 die "Directory $TargetDirectory does not exist.\n"; 67 } 68 69 my $ModuleDirectory = $Self->GetOption('module-directory'); 70 if ( $ModuleDirectory && !-d $ModuleDirectory ) { 71 die "Directory $ModuleDirectory does not exist.\n"; 72 } 73 74 return; 75} 76 77sub Run { 78 my ( $Self, %Param ) = @_; 79 80 $Self->Print("<yellow>Building package...</yellow>\n"); 81 82 my $FileString; 83 my $SourcePath = $Self->GetArgument('source-path'); 84 my $ContentRef = $Kernel::OM->Get('Kernel::System::Main')->FileRead( 85 Location => $SourcePath, 86 Mode => 'utf8', # optional - binmode|utf8 87 Result => 'SCALAR', # optional - SCALAR|ARRAY 88 ); 89 if ( !$ContentRef || ref $ContentRef ne 'SCALAR' ) { 90 $Self->PrintError("File $SourcePath is empty / could not be read."); 91 return $Self->ExitCodeError(); 92 } 93 $FileString = ${$ContentRef}; 94 95 my %Structure = $Kernel::OM->Get('Kernel::System::Package')->PackageParse( 96 String => $FileString, 97 ); 98 99 # just build it if PackageIsDownloadable flag is enable 100 if ( 101 defined $Structure{PackageIsDownloadable} 102 && !$Structure{PackageIsDownloadable}->{Content} 103 ) 104 { 105 $Self->PrintError("Package cannot be built.\n"); 106 return $Self->ExitCodeError(); 107 } 108 109 if ( $Self->GetOption('version') ) { 110 $Structure{Version}->{Content} = $Self->GetOption('version'); 111 } 112 113 # build from given package directory, if any (otherwise default to OTRS home) 114 if ( $Self->GetOption('module-directory') ) { 115 $Structure{Home} = $Self->GetOption('module-directory'); 116 } 117 118 my $Filename = $Structure{Name}->{Content} . '-' . $Structure{Version}->{Content} . '.opm'; 119 my $Content = $Kernel::OM->Get('Kernel::System::Package')->PackageBuild(%Structure); 120 if ( !$Content ) { 121 $Self->PrintError("Package build failed.\n"); 122 return $Self->ExitCodeError(); 123 } 124 my $File = $Kernel::OM->Get('Kernel::System::Main')->FileWrite( 125 Location => $Self->GetArgument('target-directory') . '/' . $Filename, 126 Content => \$Content, 127 Mode => 'utf8', # binmode|utf8 128 Type => 'Local', # optional - Local|Attachment|MD5 129 Permission => '644', # unix file permissions 130 ); 131 if ( !$File ) { 132 $Self->PrintError("File $File could not be written.\n"); 133 return $Self->ExitCodeError(); 134 } 135 136 $Self->Print("<green>Done.</green>\n"); 137 return $Self->ExitCodeOk(); 138} 139 140# sub PostRun { 141# my ( $Self, %Param ) = @_; 142# 143# # This will be called after Run() (even in case of exceptions). Perform any cleanups here. 144# 145# return; 146# } 147 1481; 149