From 465c3418f95b78ab1c87c4ccb78c3e4d5230d74c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Planchat?= Date: Thu, 31 Aug 2023 12:21:32 +0200 Subject: [PATCH 1/2] Fixed issues related to jq clearing te composer.json when adding composer autoloads --- src/PHP/ComposerAutoload.php | 61 ++++++++++++++++++++++++------------ 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/src/PHP/ComposerAutoload.php b/src/PHP/ComposerAutoload.php index 3d44880..130bc9d 100644 --- a/src/PHP/ComposerAutoload.php +++ b/src/PHP/ComposerAutoload.php @@ -6,16 +6,14 @@ use Kiboko\Component\Dockerfile\Dockerfile; -final class ComposerAutoload implements Dockerfile\LayerInterface +final readonly class ComposerAutoload implements Dockerfile\LayerInterface, \Stringable { - /** - * @param array>> $autoloads - */ - private array $autoloads; - - public function __construct(array $autoloads) - { - $this->autoloads = $autoloads; + public function __construct( + /** + * @param array>> $autoloads + */ + private array $autoloads + ) { } private static function command(string ...$command): string @@ -50,22 +48,45 @@ private static function pipe(string ...$commands): string return implode(' | ', $commands); } - public function __toString() + private static function indirection(string $command, string $destination): string + { + return $command . ' > ' . self::escapeArgument($destination); + } + + private static function and(string ...$commands): string { - $commands = implode(' \\' . PHP_EOL . ' && ', array_map(fn ($type, $autoload) => match ($type) { - 'psr4' => self::pipe( - self::command('cat', 'composer.json'), - self::command('jq', '--indent', '4', sprintf('.autoload."psr-4" |= . + %s', json_encode($autoload))), - self::command('tee', 'composer.json') + return implode(' \\'.\PHP_EOL.' && ', $commands); + } + + public function __toString(): string + { + if (\count($this->autoloads) <= 0) { + return ''; + } + + $commands = self::and( + ...array_map( + fn ($type, $autoload) => match ($type) { + 'psr4' => self::and( + self::pipe( + self::command('cat', 'composer.json'), + self::command('jq', '--indent', '4', sprintf('.autoload."psr-4" |= . + %s', json_encode($autoload, \JSON_THROW_ON_ERROR))), + self::command('tee', 'composer.temp') + ), + self::indirection(self::command('cat', 'composer.temp'), 'composer.json'), + self::command('rm', 'composer.temp'), + ) + }, + array_keys($this->autoloads), + array_values($this->autoloads) ) - }, array_keys($this->autoloads), array_values($this->autoloads))); + ); - $d = $commands; return (string) new Dockerfile\Run( << Date: Fri, 8 Sep 2023 10:57:16 +0200 Subject: [PATCH 2/2] run cs-fixer --- src/PHP/ComposerAutoload.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PHP/ComposerAutoload.php b/src/PHP/ComposerAutoload.php index 130bc9d..3e96cdf 100644 --- a/src/PHP/ComposerAutoload.php +++ b/src/PHP/ComposerAutoload.php @@ -50,7 +50,7 @@ private static function pipe(string ...$commands): string private static function indirection(string $command, string $destination): string { - return $command . ' > ' . self::escapeArgument($destination); + return $command.' > '.self::escapeArgument($destination); } private static function and(string ...$commands): string