From b23655a139e4e2c19ad99b466bce3ac00ab27bac Mon Sep 17 00:00:00 2001 From: sebprt Date: Thu, 5 Oct 2023 17:37:03 +0200 Subject: [PATCH 1/4] Created new option to copy file from local into docker image --- src/Adapter/Docker/Configuration.php | 8 +++++++ src/Adapter/Docker/Factory.php | 23 +++++++++++++++++++ .../FileOrDirectoryNotFoundException.php | 7 ++++++ 3 files changed, 38 insertions(+) create mode 100644 src/Adapter/Docker/FileOrDirectoryNotFoundException.php diff --git a/src/Adapter/Docker/Configuration.php b/src/Adapter/Docker/Configuration.php index c4770b35..dfee1bd4 100644 --- a/src/Adapter/Docker/Configuration.php +++ b/src/Adapter/Docker/Configuration.php @@ -21,6 +21,14 @@ public function getConfigTreeBuilder(): TreeBuilder ->arrayNode('tags') ->scalarPrototype()->end() ->end() + ->arrayNode('copy') + ->arrayPrototype() + ->children() + ->scalarNode('from')->isRequired()->end() + ->scalarNode('to')->isRequired()->end() + ->end() + ->end() + ->end() ->end() ; diff --git a/src/Adapter/Docker/Factory.php b/src/Adapter/Docker/Factory.php index 3ba0ecfc..7d6d88be 100644 --- a/src/Adapter/Docker/Factory.php +++ b/src/Adapter/Docker/Factory.php @@ -32,6 +32,26 @@ public function __invoke(array $configuration): Configurator\SatelliteBuilderInt if (isset($configuration['docker']['tags'])) { $builder->withTags(...$configuration['docker']['tags']); } + if (isset($configuration['docker']['copy'])) { + foreach ($configuration['docker']['copy'] as $copy) { + if (!file_exists($copy['from'])) { + throw new FileOrDirectoryNotFoundException( + strtr( + 'Unable to find the file or the directory at path %path%', + [ + 'path' => $copy['from'] + ] + ) + ); + } + + if (is_file($copy['from'])) { + $builder->withFile(new Packaging\File($copy['from'], new Packaging\Asset\LocalFile($copy['from'])), $copy['to']); + } else { + $builder->withDirectory(new Packaging\Directory($copy['from']), $copy['to']); + } + } + } if (isset($configuration['docker']['include']) && is_iterable($configuration['docker']['include'])) { foreach ($configuration['docker']['include'] as $path) { @@ -60,6 +80,9 @@ public function __invoke(array $configuration): Configurator\SatelliteBuilderInt } else { $builder->withComposerFile(new Packaging\Asset\LocalFile('composer.json')); } + if (file_exists('vendor')) { + $builder->withDirectory(new Packaging\Directory('vendor/')); + } } if (\array_key_exists('autoload', $configuration['composer']) && \array_key_exists('psr4', $configuration['composer']['autoload'])) { diff --git a/src/Adapter/Docker/FileOrDirectoryNotFoundException.php b/src/Adapter/Docker/FileOrDirectoryNotFoundException.php new file mode 100644 index 00000000..dfce1b2b --- /dev/null +++ b/src/Adapter/Docker/FileOrDirectoryNotFoundException.php @@ -0,0 +1,7 @@ + Date: Fri, 6 Oct 2023 09:06:44 +0200 Subject: [PATCH 2/4] Removed include script in factory because the option does not exist --- src/Adapter/Docker/Factory.php | 20 -------------------- src/Adapter/Docker/Satellite.php | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/Adapter/Docker/Factory.php b/src/Adapter/Docker/Factory.php index 7d6d88be..80600a6f 100644 --- a/src/Adapter/Docker/Factory.php +++ b/src/Adapter/Docker/Factory.php @@ -53,26 +53,6 @@ public function __invoke(array $configuration): Configurator\SatelliteBuilderInt } } - if (isset($configuration['docker']['include']) && is_iterable($configuration['docker']['include'])) { - foreach ($configuration['docker']['include'] as $path) { - if (is_dir($path)) { - $builder->withDirectory(new Packaging\Directory($path)); - } else { - $builder->withFile(new Packaging\Asset\LocalFile($path)); - } - } - } - - if (isset($configuration['include'])) { - foreach ($configuration['include'] as $path) { - if (is_dir($path)) { - $builder->withDirectory(new Packaging\Directory($path)); - } else { - $builder->withFile(new Packaging\Asset\LocalFile($path)); - } - } - } - if (\array_key_exists('composer', $configuration)) { if (\array_key_exists('from_local', $configuration['composer']) && true === $configuration['composer']['from_local']) { if (file_exists('composer.lock')) { diff --git a/src/Adapter/Docker/Satellite.php b/src/Adapter/Docker/Satellite.php index a3fec7e0..90863069 100644 --- a/src/Adapter/Docker/Satellite.php +++ b/src/Adapter/Docker/Satellite.php @@ -56,6 +56,7 @@ public function dependsOn(string ...$dependencies): self public function build( LoggerInterface $logger, ): void { + $this->sortFiles(); $archive = new TarArchive($this->dockerfile, ...$this->files); $iterator = function (iterable $tags) { @@ -85,4 +86,19 @@ public function build( throw new \RuntimeException('Process exited unexpectedly.'); } } + + private function sortFiles(): void + { + uksort($this->files, function ($a, $b) { + if (is_numeric($a) && is_numeric($b)) { + return $a - $b; + } elseif (is_numeric($a)) { + return -1; + } elseif (is_numeric($b)) { + return 1; + } else { + return strcmp($a, $b); + } + }); + } } From ddb8ae75e13fe1f8d88c27c67c0e1f289aef5f35 Mon Sep 17 00:00:00 2001 From: sebprt Date: Fri, 6 Oct 2023 09:08:42 +0200 Subject: [PATCH 3/4] Ran php-cs-fixer --- src/Adapter/Docker/Factory.php | 9 +-------- .../Docker/FileOrDirectoryNotFoundException.php | 6 +++--- src/Adapter/Docker/Satellite.php | 10 ++++++---- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/Adapter/Docker/Factory.php b/src/Adapter/Docker/Factory.php index 80600a6f..5c47ae94 100644 --- a/src/Adapter/Docker/Factory.php +++ b/src/Adapter/Docker/Factory.php @@ -35,14 +35,7 @@ public function __invoke(array $configuration): Configurator\SatelliteBuilderInt if (isset($configuration['docker']['copy'])) { foreach ($configuration['docker']['copy'] as $copy) { if (!file_exists($copy['from'])) { - throw new FileOrDirectoryNotFoundException( - strtr( - 'Unable to find the file or the directory at path %path%', - [ - 'path' => $copy['from'] - ] - ) - ); + throw new FileOrDirectoryNotFoundException(strtr('Unable to find the file or the directory at path %path%', ['path' => $copy['from']])); } if (is_file($copy['from'])) { diff --git a/src/Adapter/Docker/FileOrDirectoryNotFoundException.php b/src/Adapter/Docker/FileOrDirectoryNotFoundException.php index dfce1b2b..c3a2e14b 100644 --- a/src/Adapter/Docker/FileOrDirectoryNotFoundException.php +++ b/src/Adapter/Docker/FileOrDirectoryNotFoundException.php @@ -1,7 +1,7 @@ files, function ($a, $b) { if (is_numeric($a) && is_numeric($b)) { return $a - $b; - } elseif (is_numeric($a)) { + } + if (is_numeric($a)) { return -1; - } elseif (is_numeric($b)) { + } + if (is_numeric($b)) { return 1; - } else { - return strcmp($a, $b); } + + return strcmp($a, $b); }); } } From 13c55cb016417b67fa0b96c4b454da8475e90ca9 Mon Sep 17 00:00:00 2001 From: sebprt Date: Fri, 6 Oct 2023 11:09:14 +0200 Subject: [PATCH 4/4] Extract values instead of filter files --- src/Adapter/Docker/Satellite.php | 20 +------------------- 1 file changed, 1 insertion(+), 19 deletions(-) diff --git a/src/Adapter/Docker/Satellite.php b/src/Adapter/Docker/Satellite.php index c0eb6975..e5742f2a 100644 --- a/src/Adapter/Docker/Satellite.php +++ b/src/Adapter/Docker/Satellite.php @@ -56,8 +56,7 @@ public function dependsOn(string ...$dependencies): self public function build( LoggerInterface $logger, ): void { - $this->sortFiles(); - $archive = new TarArchive($this->dockerfile, ...$this->files); + $archive = new TarArchive($this->dockerfile, ...array_values($this->files)); $iterator = function (iterable $tags) { foreach ($tags as $tag) { @@ -86,21 +85,4 @@ public function build( throw new \RuntimeException('Process exited unexpectedly.'); } } - - private function sortFiles(): void - { - uksort($this->files, function ($a, $b) { - if (is_numeric($a) && is_numeric($b)) { - return $a - $b; - } - if (is_numeric($a)) { - return -1; - } - if (is_numeric($b)) { - return 1; - } - - return strcmp($a, $b); - }); - } }