From ea43e67da87fd6f3a31401e96d8dd9abf58fd315 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Fri, 1 Mar 2024 15:19:46 +0000 Subject: [PATCH] Don't generate files if the `--dry-run` option is set * Add the --dry-run option. * Add a warning if --dry-run is used saying no files have been generated. --- src/Action/GenerateConfigurationFiles.php | 6 +++++- src/Command/GenerateCommand.php | 14 +++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/Action/GenerateConfigurationFiles.php b/src/Action/GenerateConfigurationFiles.php index a5e6c5f..674a4aa 100644 --- a/src/Action/GenerateConfigurationFiles.php +++ b/src/Action/GenerateConfigurationFiles.php @@ -16,6 +16,7 @@ final class GenerateConfigurationFiles private Filesystem $filesystem, private Environment $twig, private string $outputDir, + private bool $isDryRun = false, ) { } @@ -28,7 +29,10 @@ final class GenerateConfigurationFiles */ [$configurationData, $configurationDataDto, $filesToGenerate] = $filesToGenerateAndConfigurationData; - $filesToGenerate->each(function(TemplateFile $templateFile) use ($configurationData): void { + if ($this->isDryRun) { + return $next([$configurationDataDto, $filesToGenerate]); + } + if ($templateFile->path !== null) { if (!$this->filesystem->exists($templateFile->path)) { $this->filesystem->mkdir("{$this->outputDir}/{$templateFile->path}"); diff --git a/src/Command/GenerateCommand.php b/src/Command/GenerateCommand.php index 937282a..d2360a4 100644 --- a/src/Command/GenerateCommand.php +++ b/src/Command/GenerateCommand.php @@ -51,6 +51,11 @@ class GenerateCommand extends Command description: 'The directory to create files in', default: '.', ) + ->addOption( + name: 'dry-run', + mode: InputOption::VALUE_NONE, + description: 'Whether to generate files or not', + ) ; } @@ -60,18 +65,17 @@ class GenerateCommand extends Command $configFile = $input->getOption(name: 'config-file'); $outputDir = $input->getOption(name: 'output-dir'); + $isDryRun = $input->getOption(name: 'dry-run'); $pipelines = [ new CreateFinalConfigurationData(), - new ValidateConfigurationData(), - new CreateListOfFilesToGenerate(), - new GenerateConfigurationFiles( $this->filesystem, $this->twig, $outputDir, + $isDryRun, ), ]; @@ -86,6 +90,10 @@ class GenerateCommand extends Command $io->info("Building configuration for {$configurationData->name}."); + if ($isDryRun === true) { + $io->warning('This is a dry run, no files have been generated.'); + } + $io->write('Generated files:'); $io->listing(static::getListOfFiles(filesToGenerate: $generatedFiles)->toArray());