From 285ad0a1bfe13b2460653a02ad40243184e7d80a Mon Sep 17 00:00:00 2001 From: Marco Cesarato Date: Mon, 18 Jan 2021 19:40:17 +0100 Subject: [PATCH] feat: add not tag option and add errors on commit and tagging --- src/Changelog.php | 27 ++++++++++++++++++++++----- src/DefaultCommand.php | 1 + src/Git.php | 13 ++++++++++--- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/Changelog.php b/src/Changelog.php index 32a8c12..dfe7f50 100644 --- a/src/Changelog.php +++ b/src/Changelog.php @@ -32,6 +32,7 @@ public function generate(InputInterface $input, SymfonyStyle $output) $root = $input->getArgument('path'); // Root $autoCommit = $input->getOption('commit'); // Commit once changelog is generated + $autoTag = !$input->getOption('no-tag'); // Tag release once is committed $amend = $input->getOption('amend'); // Amend commit $hooks = !$input->getOption('no-verify'); // Verify git hooks $fromDate = $input->getOption('from-date'); @@ -246,14 +247,30 @@ public function generate(InputInterface $input, SymfonyStyle $output) // Save new changelog prepending the current one file_put_contents($file, $mainHeader . "{$changelogNew}{$changelogCurrent}"); - $output->success("Changelog generated to: {$file}"); + $output->success('Changelog generated!'); + $output->writeln(" > Changelog file: {$file}"); - // Create commit and add tag + // Create commit if ($autoCommit) { - Git::commit("chore(release): {$newVersion}", [$file], $amend, $hooks); - Git::tag('v' . $newVersion); + $result = Git::commit("chore(release): {$newVersion}", [$file], $amend, $hooks); + if ($result !== false) { + $output->success('Release committed!'); + // Create tag + if ($autoTag) { + $result = Git::tag('v' . $newVersion); + if ($result !== false) { + $output->success("Release tagged with success! New version: v{$newVersion}"); + } else { + $output->error('An error occurred tagging the release!'); + + return Command::FAILURE; + } + } + } else { + $output->error('An error occurred committing the release!'); - $output->success("Committed new version with tag: v{$newVersion}"); + return Command::FAILURE; + } } return Command::SUCCESS; diff --git a/src/DefaultCommand.php b/src/DefaultCommand.php index e5042de..a34f743 100644 --- a/src/DefaultCommand.php +++ b/src/DefaultCommand.php @@ -61,6 +61,7 @@ protected function configure() new InputOption('no-chores', null, InputOption::VALUE_NONE, 'Exclude chores type from changelog'), new InputOption('no-refactor', null, InputOption::VALUE_NONE, 'Exclude refactor type from changelog'), new InputOption('no-verify', null, InputOption::VALUE_NONE, 'Bypasses the pre-commit and commit-msg hooks'), + new InputOption('no-tag', null, InputOption::VALUE_NONE, 'Disable release auto tagging'), ]); } diff --git a/src/Git.php b/src/Git.php index 05d4b4c..9bcbc91 100644 --- a/src/Git.php +++ b/src/Git.php @@ -113,7 +113,11 @@ public static function getTags() * Commit. * * @param $message - * @param $files + * @param array $files + * @param false $amend + * @param bool $verify + * + * @return string */ public static function commit($message, $files = [], $amend = false, $verify = true) { @@ -128,16 +132,19 @@ public static function commit($message, $files = [], $amend = false, $verify = t if (!$verify) { $command .= ' --no-verify'; } - system($command); + + return exec($command); } /** * Tag. * * @param $name + * + * @return string */ public static function tag($name) { - system("git tag {$name}"); + return exec("git tag {$name}"); } }