Skip to content

Commit

Permalink
Docker tags corresponding to version
Browse files Browse the repository at this point in the history
  • Loading branch information
kduma committed Nov 29, 2022
1 parent 119ec74 commit 7cabbf2
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
/.vagrant
.phpunit.result.cache
.DS_Store
/builds
4 changes: 2 additions & 2 deletions app/Actions/PdfPagesSplitterAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down
4 changes: 2 additions & 2 deletions app/Actions/ScanBarcodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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,
);
Expand Down
4 changes: 3 additions & 1 deletion app/Actions/Tools/BuildDockerImageAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

class BuildDockerImageAction
{
use GetsDockerImageTag;

public function execute(string $tag, string $path)
{
$command = $this->getCommand($tag, $path);
Expand All @@ -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)
);
Expand Down
13 changes: 13 additions & 0 deletions app/Actions/Tools/DetectsPharArchives.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Actions\Tools;

use Phar;

trait DetectsPharArchives
{
protected function isPhar(): bool
{
return strlen(Phar::running()) > 0;
}
}
37 changes: 37 additions & 0 deletions app/Actions/Tools/GetsDockerImageTag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Actions\Tools;

use Illuminate\Support\Str;

trait GetsDockerImageTag
{
use DetectsPharArchives;

protected function getImageTag(string $dockerImageName): string
{
return $dockerImageName . ':' . $this->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';
}
}
7 changes: 4 additions & 3 deletions app/Actions/Tools/RunDockerContainerAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down
6 changes: 1 addition & 5 deletions app/Actions/Tools/TemporaryDirectoryCreatorAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
13 changes: 11 additions & 2 deletions app/Commands/BuildDockerImagesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 7cabbf2

Please sign in to comment.