From 523a34426882a653cea96d9a03dc558cebabccd5 Mon Sep 17 00:00:00 2001 From: Simon Gilli <25326036+gilbertsoft@users.noreply.github.com> Date: Wed, 17 Nov 2021 15:02:05 +0100 Subject: [PATCH] [FEATURE] Introduce --dry-run option to commands Resolves #40 --- src/Command/AbstractClientRequestCommand.php | 12 ++++- .../Extension/SetExtensionVersionCommand.php | 47 ++++++++++++------- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/src/Command/AbstractClientRequestCommand.php b/src/Command/AbstractClientRequestCommand.php index f882231..9865527 100644 --- a/src/Command/AbstractClientRequestCommand.php +++ b/src/Command/AbstractClientRequestCommand.php @@ -47,8 +47,10 @@ abstract class AbstractClientRequestCommand extends Command protected function configure(): void { - // General option to get a raw result. Can be used for further processing. - $this->addOption('raw', 'r', InputOption::VALUE_OPTIONAL, 'Return result as raw object (e.g. json)', false); + $this + // General option to get a raw result. Can be used for further processing. + ->addOption('raw', 'r', InputOption::VALUE_OPTIONAL, 'Return result as raw object (e.g. json)', false) + ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Outputs the operations but will not execute anything'); } protected function execute(InputInterface $input, OutputInterface $output): int @@ -68,6 +70,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int ->setRaw($input->getOption('raw') !== false) ->setDefaultAuthMethod($this->defaultAuthMethod); + // In case of a dry-run the request to TER is skipped. + if ($input->getOption('dry-run') === true) { + $io->note(sprintf('Would have sent the command "%s %s", skipping', $requestConfiguration->getMethod(), $requestConfiguration->getEndpoint())); + return 0; + } + // RequestService returns a boolean for whether the request was successful or not. // Since we have to return an exit code, this must be negated and casted to return // 0 on success and 1 on failure. diff --git a/src/Command/Extension/SetExtensionVersionCommand.php b/src/Command/Extension/SetExtensionVersionCommand.php index 2c1f3bb..d242bc1 100644 --- a/src/Command/Extension/SetExtensionVersionCommand.php +++ b/src/Command/Extension/SetExtensionVersionCommand.php @@ -39,7 +39,8 @@ protected function configure(): void ->setDescription('Update the extensions ext_emconf.php version to a specific version. Useful in CI environments') ->addArgument('version', InputArgument::REQUIRED, 'The version to publish, e.g. 1.2.3. Must have three digits.') ->addOption('path', '', InputOption::VALUE_OPTIONAL, 'Path to the extension folder', getcwd() ?: './') - ->addOption('no-docs', '', InputOption::VALUE_OPTIONAL, 'Disable version update in documentation settings', false); + ->addOption('no-docs', '', InputOption::VALUE_OPTIONAL, 'Disable version update in documentation settings', false) + ->addOption('dry-run', null, InputOption::VALUE_NONE, 'Outputs the operations but will not execute anything'); } protected function execute(InputInterface $input, OutputInterface $output): int @@ -58,6 +59,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 1; } + $dryRun = ($input->getOption('dry-run') === true); + $emConfFile = rtrim($path, '/') . '/ext_emconf.php'; if (!file_exists($emConfFile)) { $io->error(sprintf('No \'ext_emconf.php\' found in the given path %s.', $path)); @@ -66,11 +69,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int $versionReplacer = new VersionReplacer($version); - try { - $versionReplacer->setVersion($emConfFile, self::EMCONF_PATTERN); - } catch (\InvalidArgumentException $e) { - $io->error(sprintf('An error occurred while setting the ext_emconf.php version to %s.', $version)); - return 1; + if (!$dryRun) { + try { + $versionReplacer->setVersion($emConfFile, self::EMCONF_PATTERN); + } catch (\InvalidArgumentException $e) { + $io->error(sprintf('An error occurred while setting the ext_emconf.php version to %s.', $version)); + return 1; + } + } else { + $io->note(sprintf('Would have set version in ext_emconf.php to %s, skipping', $version)); } if ($input->getOption('no-docs') === null @@ -90,18 +97,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int return 0; } - try { - $versionReplacer->setVersion($documentationSettingsFile, self::DOCUMENTATION_RELEASE_PATTERN); - } catch (\InvalidArgumentException $e) { - $io->error(sprintf('An error occurred while updating the release number in %s', $documentationSettingsFile)); - return 1; - } - - try { - $versionReplacer->setVersion($documentationSettingsFile, self::DOCUMENTATION_VERSION_PATTERN, 2); - } catch (\InvalidArgumentException $e) { - $io->error(sprintf('An error occurred while updating the version number in %s', $documentationSettingsFile)); - return 1; + if (!$dryRun) { + try { + $versionReplacer->setVersion($documentationSettingsFile, self::DOCUMENTATION_RELEASE_PATTERN); + } catch (\InvalidArgumentException $e) { + $io->error(sprintf('An error occurred while updating the release number in %s', $documentationSettingsFile)); + return 1; + } + + try { + $versionReplacer->setVersion($documentationSettingsFile, self::DOCUMENTATION_VERSION_PATTERN, 2); + } catch (\InvalidArgumentException $e) { + $io->error(sprintf('An error occurred while updating the version number in %s', $documentationSettingsFile)); + return 1; + } + } else { + $io->note(sprintf('Would have set version in Documentation/Settings.cfg to %s, skipping', $version)); } return 0;