From baaf3bf16b094a86dbf8b8e6a2fc8b589c067525 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Thu, 23 Nov 2023 08:24:34 +0000 Subject: [PATCH] refactor: add `isDocker` and `isFlake` to the ...Configuration DTO --- CHANGELOG.md | 1 + src/Action/CreateFinalConfigurationData.php | 5 ++++ src/Action/CreateListOfFilesToGenerate.php | 33 +++++---------------- src/DataTransferObject/Config.php | 4 +++ templates/astro/run.twig | 2 -- templates/fractal/run.twig | 3 -- 6 files changed, 18 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8100656..8aba875 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ ### Changed * Replace `set -ueo` in Git hook templates to use the long names and be consistent with `run` scripts. +* Add `isDocker` and `isFlake` to the Configuration DTO and remove duplicate variables within templates. ## 2023-11-22 diff --git a/src/Action/CreateFinalConfigurationData.php b/src/Action/CreateFinalConfigurationData.php index dd306d6..e9bf5eb 100644 --- a/src/Action/CreateFinalConfigurationData.php +++ b/src/Action/CreateFinalConfigurationData.php @@ -10,6 +10,11 @@ use Symfony\Component\Yaml\Yaml; final class CreateFinalConfigurationData { public function handle(string $configFile, \Closure $next) { + // Perform some initial checks before the defaults are merged. + $configurationData = Yaml::parseFile(filename: $configFile); + $configurationData['isDocker'] = isset($configurationData['dockerfile']); + $configurationData['isFlake'] = isset($configurationData['flake']); + $configurationData = array_replace_recursive( Yaml::parseFile(filename: __DIR__ . '/../../resources/build.defaults.yaml'), Yaml::parseFile(filename: $configFile), diff --git a/src/Action/CreateListOfFilesToGenerate.php b/src/Action/CreateListOfFilesToGenerate.php index 263bf6d..c8a22a4 100644 --- a/src/Action/CreateListOfFilesToGenerate.php +++ b/src/Action/CreateListOfFilesToGenerate.php @@ -19,15 +19,12 @@ final class CreateListOfFilesToGenerate * @var Config $configurationDataDto, * @var array $configurationData */ - [$configurationData, $configurationDataDto] = $configurationDataAndDto; - - $isDocker = static::isDocker($configurationData); - $isFlake = static::isFlake($configurationData); + [$configurationData, $configurationDataDTO] = $configurationDataAndDto; /** @var Collection */ $filesToGenerate = collect(); - switch (strtolower($configurationDataDto->type)) { + switch (strtolower($configurationDataDTO->type)) { case (strtolower(ProjectType::Astro->name)): $filesToGenerate = collect([ new TemplateFile(data: 'astro/.envrc', name: '.envrc'), @@ -44,14 +41,14 @@ final class CreateListOfFilesToGenerate new TemplateFile(data: 'fractal/run', name: 'run'), ]); - if ($isDocker) { + if ($configurationDataDTO->isDocker) { $filesToGenerate->push(new TemplateFile(data: 'fractal/.env.example', name: '.env.example')); $filesToGenerate->push(new TemplateFile(data: 'fractal/.dockerignore', name: '.dockerignore')); $filesToGenerate->push(new TemplateFile(data: 'fractal/.hadolint.yaml', name: '.hadolint.yaml')); $filesToGenerate->push(new TemplateFile(data: 'fractal/.yarnrc', name: '.yarnrc')); $filesToGenerate->push(new TemplateFile(data: 'fractal/Dockerfile', name: 'Dockerfile')); $filesToGenerate->push(new TemplateFile(data: 'fractal/docker-compose.yaml', name: 'docker-compose.yaml')); - } elseif ($isFlake) { + } elseif ($configurationDataDTO->isFlake) { $filesToGenerate->push(new TemplateFile(data: 'fractal/.envrc', name: '.envrc')); $filesToGenerate->push(new TemplateFile(data: 'fractal/flake.nix', name: 'flake.nix')); } @@ -85,15 +82,15 @@ final class CreateListOfFilesToGenerate )); } - if ($configurationDataDto->php['phpcs'] !== false) { + if ($configurationDataDTO->php['phpcs'] !== false) { $filesToGenerate->push(new TemplateFile(data: 'drupal/phpcs.xml.dist', name: 'phpcs.xml.dist')); } - if ($configurationDataDto->php['phpstan'] !== false) { + if ($configurationDataDTO->php['phpstan'] !== false) { $filesToGenerate->push(new TemplateFile(data: 'drupal/phpstan.neon.dist', name: 'phpstan.neon.dist')); } - if ($configurationDataDto->php['phpunit'] !== false) { + if ($configurationDataDTO->php['phpunit'] !== false) { $filesToGenerate->push(new TemplateFile(data: 'drupal/phpunit.xml.dist', name: 'phpunit.xml.dist')); } @@ -156,7 +153,7 @@ final class CreateListOfFilesToGenerate ); } - return $next([$configurationData, $configurationDataDto, $filesToGenerate]); + return $next([$configurationData, $configurationDataDTO, $filesToGenerate]); } private static function isCaddy(?string $webServer): bool @@ -168,20 +165,6 @@ final class CreateListOfFilesToGenerate return strtoupper($webServer) === WebServer::Caddy->value; } - private static function isDocker(array $configurationData): bool - { - // This should return `false` if there is no explicit `dockerfile` key - // in the build.yaml file. This is currently not the case, I assume - // because of default values being added. - // For now, if it's not a Flake, it's Docker. - return !static::isFlake($configurationData); - } - - private static function isFlake(array $configurationData): bool - { - return Arr::get($configurationData, 'flake') !== null; - } - private static function isNginx(?string $webServer): bool { if (is_null($webServer)) { diff --git a/src/DataTransferObject/Config.php b/src/DataTransferObject/Config.php index f47b05f..6b05922 100644 --- a/src/DataTransferObject/Config.php +++ b/src/DataTransferObject/Config.php @@ -273,4 +273,8 @@ final class Config ]), ])] public array $web; + + public bool $isDocker; + + public bool $isFlake; } diff --git a/templates/astro/run.twig b/templates/astro/run.twig index c9ebd45..8d509e0 100755 --- a/templates/astro/run.twig +++ b/templates/astro/run.twig @@ -5,8 +5,6 @@ set -o errexit set -o pipefail -{% set isFlake = flake is defined %} - PATH="$PATH:./node_modules/.bin" {% if not isFlake %} diff --git a/templates/fractal/run.twig b/templates/fractal/run.twig index 630b8bd..d3ad745 100755 --- a/templates/fractal/run.twig +++ b/templates/fractal/run.twig @@ -5,9 +5,6 @@ set -o errexit set -o pipefail -{% set isDocker = flake is not defined %} -{% set isFlake = flake is defined %} - PATH="$PATH:./node_modules/.bin" {% if isDocker %}