Skip to content

Commit 7cabbf2

Browse files
committed
Docker tags corresponding to version
1 parent 119ec74 commit 7cabbf2

9 files changed

+74
-15
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
/.vagrant
55
.phpunit.result.cache
66
.DS_Store
7+
/builds

app/Actions/PdfPagesSplitterAction.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ public function execute(string|SplFileInfo $input_file, string|SplFileInfo $outp
3131
->runDockerContainerAction
3232
->withTemporaryDirectory($temporaryDirectory);
3333

34-
$action->execute(dockerImageName: 'ghcr.io/kduma-oss/cli-pdf-scan-splitter/pdf-page-extractor:master',output: $output, return: $return);
34+
$action->execute(dockerImageName: 'ghcr.io/kduma-oss/cli-pdf-scan-splitter/pdf-page-extractor',output: $output, return: $return);
3535

3636
if (0 != $return) {
3737
$temporaryDirectory->delete();
3838
throw new PdfPageContentsExtractorException(
39-
command: $action->getCommand(dockerImageName: 'ghcr.io/kduma-oss/cli-pdf-scan-splitter/pdf-page-extractor:master'),
39+
command: $action->getCommand(dockerImageName: 'ghcr.io/kduma-oss/cli-pdf-scan-splitter/pdf-page-extractor'),
4040
code: $return,
4141
output: $output,
4242
);

app/Actions/ScanBarcodes.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function execute(string|SplFileInfo $input_file): Collection
2828
->runDockerContainerAction
2929
->withTemporaryDirectory($temporaryDirectory);
3030

31-
$action->execute(dockerImageName: 'ghcr.io/kduma-oss/cli-pdf-scan-splitter/barcode-scanner:master', output: $output, return: $return);
31+
$action->execute(dockerImageName: 'ghcr.io/kduma-oss/cli-pdf-scan-splitter/barcode-scanner', output: $output, return: $return);
3232

3333
if (0 != $return) {
3434
$temporaryDirectory->delete();
@@ -37,7 +37,7 @@ public function execute(string|SplFileInfo $input_file): Collection
3737
return collect();
3838

3939
throw new PdfPageContentsExtractorException(
40-
command: $action->getCommand(dockerImageName: 'ghcr.io/kduma-oss/cli-pdf-scan-splitter/barcode-scanner:master'),
40+
command: $action->getCommand(dockerImageName: 'ghcr.io/kduma-oss/cli-pdf-scan-splitter/barcode-scanner'),
4141
code: $return,
4242
output: $output,
4343
);

app/Actions/Tools/BuildDockerImageAction.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
class BuildDockerImageAction
66
{
7+
use GetsDockerImageTag;
8+
79
public function execute(string $tag, string $path)
810
{
911
$command = $this->getCommand($tag, $path);
@@ -15,7 +17,7 @@ public function getCommand(string $tag, string $path): string
1517
{
1618
return sprintf(
1719
'docker build --tag %s --file %s %s',
18-
escapeshellarg($tag),
20+
escapeshellarg($this->getImageTag($tag)),
1921
escapeshellarg($path . '/Dockerfile'),
2022
escapeshellarg($path)
2123
);
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace App\Actions\Tools;
4+
5+
use Phar;
6+
7+
trait DetectsPharArchives
8+
{
9+
protected function isPhar(): bool
10+
{
11+
return strlen(Phar::running()) > 0;
12+
}
13+
}
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Actions\Tools;
4+
5+
use Illuminate\Support\Str;
6+
7+
trait GetsDockerImageTag
8+
{
9+
use DetectsPharArchives;
10+
11+
protected function getImageTag(string $dockerImageName): string
12+
{
13+
return $dockerImageName . ':' . $this->getDockerBranch();
14+
}
15+
16+
protected function getDockerBranch(): string
17+
{
18+
$version = config('app.version');
19+
if(!Str::startsWith($version, 'v'))
20+
$version = 'latest';
21+
22+
$environment = config('app.env');
23+
24+
if($this->isPhar() || $environment == 'production')
25+
return $version;
26+
27+
try {
28+
$composer_version = \Composer\InstalledVersions::getPrettyVersion('kduma/pdf-scan-splitter-tool');
29+
if(Str::startsWith($composer_version, 'dev-'))
30+
$composer_version = Str::after($composer_version, 'dev-');
31+
} catch (\OutOfBoundsException $e) {
32+
return 'master';
33+
}
34+
35+
return $composer_version ?? 'master';
36+
}
37+
}

app/Actions/Tools/RunDockerContainerAction.php

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22

33
namespace App\Actions\Tools;
44

5-
use Illuminate\Support\Str;
65
use Spatie\TemporaryDirectory\TemporaryDirectory;
76

87
class RunDockerContainerAction
98
{
9+
use GetsDockerImageTag;
10+
1011
private TemporaryDirectory|string|null $temporaryDirectory = null;
1112
private ?string $name = null;
1213

@@ -30,7 +31,7 @@ public function withName(?string $name): self
3031
public function execute(string $dockerImageName, string $arguments = '', array &$output = null, int &$return = null): string
3132
{
3233
$command = $this->getCommand($dockerImageName, arguments: $arguments, interactive: false);
33-
34+
dump($command);
3435
return exec($command, $output, $return);
3536
}
3637

@@ -67,7 +68,7 @@ public function getCommand(string $dockerImageName, string $arguments = '', bool
6768
$command = sprintf(
6869
'docker run %s %s %s 2>&1',
6970
$options,
70-
escapeshellarg($dockerImageName),
71+
escapeshellarg($this->getImageTag($dockerImageName)),
7172
$arguments
7273
);
7374
return $command;

app/Actions/Tools/TemporaryDirectoryCreatorAction.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22

33
namespace App\Actions\Tools;
44

5-
use Phar;
65
use Spatie\TemporaryDirectory\TemporaryDirectory;
76

87
class TemporaryDirectoryCreatorAction
98
{
10-
protected function isPhar(): bool
11-
{
12-
return strlen(Phar::running()) > 0;
13-
}
9+
use DetectsPharArchives;
1410

1511
public function get(): TemporaryDirectory
1612
{

app/Commands/BuildDockerImagesCommand.php

+11-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
namespace App\Commands;
44

55
use App\Actions\Tools\BuildDockerImageAction;
6+
use App\Actions\Tools\GetsDockerImageTag;
67
use App\Actions\Tools\RunDockerContainerAction;
78
use Illuminate\Console\Command;
89

910
class BuildDockerImagesCommand extends Command
1011
{
12+
use GetsDockerImageTag;
13+
1114
/**
1215
* The name and signature of the console command.
1316
*
@@ -36,9 +39,15 @@ class BuildDockerImagesCommand extends Command
3639
*/
3740
public function handle(BuildDockerImageAction $builder, RunDockerContainerAction $runner)
3841
{
42+
$this->info('Docker tag: '.$this->getDockerBranch());
43+
if($this->isPhar()) {
44+
$this->error('This command is not available when run from phar archive');
45+
return 1;
46+
}
47+
3948
collect([
40-
'ghcr.io/kduma-oss/cli-pdf-scan-splitter/pdf-page-extractor:master' => base_path('bin/pdf-page-extractor/'),
41-
'ghcr.io/kduma-oss/cli-pdf-scan-splitter/barcode-scanner:master' => base_path('bin/barcode-scanner/'),
49+
'ghcr.io/kduma-oss/cli-pdf-scan-splitter/pdf-page-extractor' => base_path('bin/pdf-page-extractor/'),
50+
'ghcr.io/kduma-oss/cli-pdf-scan-splitter/barcode-scanner' => base_path('bin/barcode-scanner/'),
4251
])->each(function ($path, $tag) use ($builder, $runner) {
4352
$this->info($builder->getCommand($tag, $path));
4453
$builder->execute($tag, $path);

0 commit comments

Comments
 (0)