From cd3e0e8562e2a3ec014461a005ac646bea71765a Mon Sep 17 00:00:00 2001 From: Marco Cesarato Date: Sun, 17 Jan 2021 20:58:55 +0100 Subject: [PATCH] refactor: split changelog command class into default command and changelog class --- conventional-changelog | 4 +- src/{ChangelogCommand.php => Changelog.php} | 42 ++-------------- src/DefaultCommand.php | 53 +++++++++++++++++++++ 3 files changed, 60 insertions(+), 39 deletions(-) rename src/{ChangelogCommand.php => Changelog.php} (83%) create mode 100644 src/DefaultCommand.php diff --git a/conventional-changelog b/conventional-changelog index 13a099c..32b43e3 100644 --- a/conventional-changelog +++ b/conventional-changelog @@ -1,7 +1,7 @@ #!/usr/bin/env php getName(); // Run application single command diff --git a/src/ChangelogCommand.php b/src/Changelog.php similarity index 83% rename from src/ChangelogCommand.php rename to src/Changelog.php index 8bca783..f1e6c14 100644 --- a/src/ChangelogCommand.php +++ b/src/Changelog.php @@ -4,13 +4,10 @@ use DateTime; use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; -use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; -class ChangelogCommand extends Command +class Changelog { /** * Changelog filename. @@ -50,38 +47,11 @@ class ChangelogCommand extends Command ]; /** - * Configure. - * - * @return void - */ - protected function configure() - { - $this - ->setName('changelog') - ->setDescription('Generate changelogs and release notes from a project\'s commit messages' . - 'and metadata and automate versioning with semver.org and conventionalcommits.org') - ->setDefinition([ - new InputArgument('path', InputArgument::OPTIONAL, 'Define the path directory where generate changelog', getcwd()), - new InputOption('commit', 'c', InputOption::VALUE_NONE, 'Commit the new release once changelog is generated'), - new InputOption('first-release', null, InputOption::VALUE_NONE, 'Run at first release (if --ver isn\'t specified version code it will be 1.0.0)'), - new InputOption('from-date', null, InputOption::VALUE_REQUIRED, 'Get commits from specified date [YYYY-MM-DD]'), - new InputOption('to-date', null, InputOption::VALUE_REQUIRED, 'Get commits last tag date (or specified on --from-date) to specified date [YYYY-MM-DD]'), - new InputOption('major', 'maj', InputOption::VALUE_NONE, 'Major release (important changes)'), - new InputOption('minor', 'min', InputOption::VALUE_NONE, 'Minor release (add functionality)'), - new InputOption('patch', 'p', InputOption::VALUE_NONE, 'Patch release (bug fixes) [default]'), - new InputOption('ver', null, InputOption::VALUE_REQUIRED, 'Define the next release version code (semver)'), - new InputOption('history', null, InputOption::VALUE_NONE, 'Generate the entire history of changes of all releases'), - 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'), - ]); - } - - /** - * Execute command. + * Generate changelog. * * @return int */ - protected function execute(InputInterface $input, OutputInterface $output) + public function generate(InputInterface $input, SymfonyStyle $output) { $root = $input->getArgument('path'); // Root @@ -280,18 +250,16 @@ protected function execute(InputInterface $input, OutputInterface $output) $changelogNew .= $this->getMarkdownChanges($changes); } - $io = new SymfonyStyle($input, $output); - // Save new changelog prepending the current one file_put_contents($file, self::$header . "{$changelogNew}{$changelogCurrent}"); - $io->success("Changelog generated to: {$file}"); + $output->success("Changelog generated to: {$file}"); // Create commit and add tag if ($autoCommit) { Git::commit("chore(release): {$newVersion}", [$file]); Git::tag('v' . $newVersion); - $io->success("Committed new version with tag: v{$newVersion}"); + $output->success("Committed new version with tag: v{$newVersion}"); } return Command::SUCCESS; diff --git a/src/DefaultCommand.php b/src/DefaultCommand.php new file mode 100644 index 0000000..5e09efe --- /dev/null +++ b/src/DefaultCommand.php @@ -0,0 +1,53 @@ +setName('changelog') + ->setDescription('Generate changelogs and release notes from a project\'s commit messages' . + 'and metadata and automate versioning with semver.org and conventionalcommits.org') + ->setDefinition([ + new InputArgument('path', InputArgument::OPTIONAL, 'Define the path directory where generate changelog', getcwd()), + new InputOption('commit', 'c', InputOption::VALUE_NONE, 'Commit the new release once changelog is generated'), + new InputOption('first-release', null, InputOption::VALUE_NONE, 'Run at first release (if --ver isn\'t specified version code it will be 1.0.0)'), + new InputOption('from-date', null, InputOption::VALUE_REQUIRED, 'Get commits from specified date [YYYY-MM-DD]'), + new InputOption('to-date', null, InputOption::VALUE_REQUIRED, 'Get commits last tag date (or specified on --from-date) to specified date [YYYY-MM-DD]'), + new InputOption('major', 'maj', InputOption::VALUE_NONE, 'Major release (important changes)'), + new InputOption('minor', 'min', InputOption::VALUE_NONE, 'Minor release (add functionality)'), + new InputOption('patch', 'p', InputOption::VALUE_NONE, 'Patch release (bug fixes) [default]'), + new InputOption('ver', null, InputOption::VALUE_REQUIRED, 'Define the next release version code (semver)'), + new InputOption('history', null, InputOption::VALUE_NONE, 'Generate the entire history of changes of all releases'), + 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'), + ]); + } + + /** + * Execute. + * + * @return int + */ + protected function execute(InputInterface $input, OutputInterface $output) + { + $changelog = new Changelog(); + $outputStyle = new SymfonyStyle($input, $output); + + return $changelog->generate($input, $outputStyle); + } +}