From 7cabbf243459bab2a08215be20032e6d59ee2caf Mon Sep 17 00:00:00 2001 From: Krystian Duma Date: Wed, 30 Nov 2022 00:39:39 +0100 Subject: [PATCH] Docker tags corresponding to version --- .gitignore | 1 + app/Actions/PdfPagesSplitterAction.php | 4 +- app/Actions/ScanBarcodes.php | 4 +- app/Actions/Tools/BuildDockerImageAction.php | 4 +- app/Actions/Tools/DetectsPharArchives.php | 13 +++++++ app/Actions/Tools/GetsDockerImageTag.php | 37 +++++++++++++++++++ .../Tools/RunDockerContainerAction.php | 7 ++-- .../Tools/TemporaryDirectoryCreatorAction.php | 6 +-- app/Commands/BuildDockerImagesCommand.php | 13 ++++++- 9 files changed, 74 insertions(+), 15 deletions(-) create mode 100644 app/Actions/Tools/DetectsPharArchives.php create mode 100644 app/Actions/Tools/GetsDockerImageTag.php diff --git a/.gitignore b/.gitignore index fcea151..4576147 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ /.vagrant .phpunit.result.cache .DS_Store +/builds diff --git a/app/Actions/PdfPagesSplitterAction.php b/app/Actions/PdfPagesSplitterAction.php index 22d36c7..8c5edf3 100644 --- a/app/Actions/PdfPagesSplitterAction.php +++ b/app/Actions/PdfPagesSplitterAction.php @@ -31,12 +31,12 @@ public function execute(string|SplFileInfo $input_file, string|SplFileInfo $outp ->runDockerContainerAction ->withTemporaryDirectory($temporaryDirectory); - $action->execute(dockerImageName: 'ghcr.io/kduma-oss/cli-pdf-scan-splitter/pdf-page-extractor:master',output: $output, return: $return); + $action->execute(dockerImageName: 'ghcr.io/kduma-oss/cli-pdf-scan-splitter/pdf-page-extractor',output: $output, return: $return); if (0 != $return) { $temporaryDirectory->delete(); throw new PdfPageContentsExtractorException( - command: $action->getCommand(dockerImageName: 'ghcr.io/kduma-oss/cli-pdf-scan-splitter/pdf-page-extractor:master'), + command: $action->getCommand(dockerImageName: 'ghcr.io/kduma-oss/cli-pdf-scan-splitter/pdf-page-extractor'), code: $return, output: $output, ); diff --git a/app/Actions/ScanBarcodes.php b/app/Actions/ScanBarcodes.php index 7c712c5..5880ab6 100644 --- a/app/Actions/ScanBarcodes.php +++ b/app/Actions/ScanBarcodes.php @@ -28,7 +28,7 @@ public function execute(string|SplFileInfo $input_file): Collection ->runDockerContainerAction ->withTemporaryDirectory($temporaryDirectory); - $action->execute(dockerImageName: 'ghcr.io/kduma-oss/cli-pdf-scan-splitter/barcode-scanner:master', output: $output, return: $return); + $action->execute(dockerImageName: 'ghcr.io/kduma-oss/cli-pdf-scan-splitter/barcode-scanner', output: $output, return: $return); if (0 != $return) { $temporaryDirectory->delete(); @@ -37,7 +37,7 @@ public function execute(string|SplFileInfo $input_file): Collection return collect(); throw new PdfPageContentsExtractorException( - command: $action->getCommand(dockerImageName: 'ghcr.io/kduma-oss/cli-pdf-scan-splitter/barcode-scanner:master'), + command: $action->getCommand(dockerImageName: 'ghcr.io/kduma-oss/cli-pdf-scan-splitter/barcode-scanner'), code: $return, output: $output, ); diff --git a/app/Actions/Tools/BuildDockerImageAction.php b/app/Actions/Tools/BuildDockerImageAction.php index bede99e..68596b2 100644 --- a/app/Actions/Tools/BuildDockerImageAction.php +++ b/app/Actions/Tools/BuildDockerImageAction.php @@ -4,6 +4,8 @@ class BuildDockerImageAction { + use GetsDockerImageTag; + public function execute(string $tag, string $path) { $command = $this->getCommand($tag, $path); @@ -15,7 +17,7 @@ public function getCommand(string $tag, string $path): string { return sprintf( 'docker build --tag %s --file %s %s', - escapeshellarg($tag), + escapeshellarg($this->getImageTag($tag)), escapeshellarg($path . '/Dockerfile'), escapeshellarg($path) ); diff --git a/app/Actions/Tools/DetectsPharArchives.php b/app/Actions/Tools/DetectsPharArchives.php new file mode 100644 index 0000000..0df1c8b --- /dev/null +++ b/app/Actions/Tools/DetectsPharArchives.php @@ -0,0 +1,13 @@ + 0; + } +} diff --git a/app/Actions/Tools/GetsDockerImageTag.php b/app/Actions/Tools/GetsDockerImageTag.php new file mode 100644 index 0000000..781606c --- /dev/null +++ b/app/Actions/Tools/GetsDockerImageTag.php @@ -0,0 +1,37 @@ +getDockerBranch(); + } + + protected function getDockerBranch(): string + { + $version = config('app.version'); + if(!Str::startsWith($version, 'v')) + $version = 'latest'; + + $environment = config('app.env'); + + if($this->isPhar() || $environment == 'production') + return $version; + + try { + $composer_version = \Composer\InstalledVersions::getPrettyVersion('kduma/pdf-scan-splitter-tool'); + if(Str::startsWith($composer_version, 'dev-')) + $composer_version = Str::after($composer_version, 'dev-'); + } catch (\OutOfBoundsException $e) { + return 'master'; + } + + return $composer_version ?? 'master'; + } +} diff --git a/app/Actions/Tools/RunDockerContainerAction.php b/app/Actions/Tools/RunDockerContainerAction.php index 05e9177..43ed270 100644 --- a/app/Actions/Tools/RunDockerContainerAction.php +++ b/app/Actions/Tools/RunDockerContainerAction.php @@ -2,11 +2,12 @@ namespace App\Actions\Tools; -use Illuminate\Support\Str; use Spatie\TemporaryDirectory\TemporaryDirectory; class RunDockerContainerAction { + use GetsDockerImageTag; + private TemporaryDirectory|string|null $temporaryDirectory = null; private ?string $name = null; @@ -30,7 +31,7 @@ public function withName(?string $name): self public function execute(string $dockerImageName, string $arguments = '', array &$output = null, int &$return = null): string { $command = $this->getCommand($dockerImageName, arguments: $arguments, interactive: false); - + dump($command); return exec($command, $output, $return); } @@ -67,7 +68,7 @@ public function getCommand(string $dockerImageName, string $arguments = '', bool $command = sprintf( 'docker run %s %s %s 2>&1', $options, - escapeshellarg($dockerImageName), + escapeshellarg($this->getImageTag($dockerImageName)), $arguments ); return $command; diff --git a/app/Actions/Tools/TemporaryDirectoryCreatorAction.php b/app/Actions/Tools/TemporaryDirectoryCreatorAction.php index 1998a1f..47ac27e 100644 --- a/app/Actions/Tools/TemporaryDirectoryCreatorAction.php +++ b/app/Actions/Tools/TemporaryDirectoryCreatorAction.php @@ -2,15 +2,11 @@ namespace App\Actions\Tools; -use Phar; use Spatie\TemporaryDirectory\TemporaryDirectory; class TemporaryDirectoryCreatorAction { - protected function isPhar(): bool - { - return strlen(Phar::running()) > 0; - } + use DetectsPharArchives; public function get(): TemporaryDirectory { diff --git a/app/Commands/BuildDockerImagesCommand.php b/app/Commands/BuildDockerImagesCommand.php index 4cd295a..6bdd841 100644 --- a/app/Commands/BuildDockerImagesCommand.php +++ b/app/Commands/BuildDockerImagesCommand.php @@ -3,11 +3,14 @@ namespace App\Commands; use App\Actions\Tools\BuildDockerImageAction; +use App\Actions\Tools\GetsDockerImageTag; use App\Actions\Tools\RunDockerContainerAction; use Illuminate\Console\Command; class BuildDockerImagesCommand extends Command { + use GetsDockerImageTag; + /** * The name and signature of the console command. * @@ -36,9 +39,15 @@ class BuildDockerImagesCommand extends Command */ public function handle(BuildDockerImageAction $builder, RunDockerContainerAction $runner) { + $this->info('Docker tag: '.$this->getDockerBranch()); + if($this->isPhar()) { + $this->error('This command is not available when run from phar archive'); + return 1; + } + collect([ - 'ghcr.io/kduma-oss/cli-pdf-scan-splitter/pdf-page-extractor:master' => base_path('bin/pdf-page-extractor/'), - 'ghcr.io/kduma-oss/cli-pdf-scan-splitter/barcode-scanner:master' => base_path('bin/barcode-scanner/'), + 'ghcr.io/kduma-oss/cli-pdf-scan-splitter/pdf-page-extractor' => base_path('bin/pdf-page-extractor/'), + 'ghcr.io/kduma-oss/cli-pdf-scan-splitter/barcode-scanner' => base_path('bin/barcode-scanner/'), ])->each(function ($path, $tag) use ($builder, $runner) { $this->info($builder->getCommand($tag, $path)); $builder->execute($tag, $path);