Skip to content

Commit

Permalink
refactor: split changelog command class into default command and chan…
Browse files Browse the repository at this point in the history
…gelog class
  • Loading branch information
marcocesarato committed Jan 17, 2021
1 parent c891783 commit cd3e0e8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 39 deletions.
4 changes: 2 additions & 2 deletions conventional-changelog
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env php
<?php

use ConventionalChangelog\ChangelogCommand;
use ConventionalChangelog\DefaultCommand;
use Symfony\Component\Console\Application;

// Autoload
Expand All @@ -21,7 +21,7 @@ foreach ($files as $file) {
error_reporting(2039);

// Command
$command = new ChangelogCommand();
$command = new DefaultCommand();
$commandName = $command->getName();

// Run application single command
Expand Down
42 changes: 5 additions & 37 deletions src/ChangelogCommand.php → src/Changelog.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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;
Expand Down
53 changes: 53 additions & 0 deletions src/DefaultCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace ConventionalChangelog;

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 DefaultCommand 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.
*
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$changelog = new Changelog();
$outputStyle = new SymfonyStyle($input, $output);

return $changelog->generate($input, $outputStyle);
}
}

0 comments on commit cd3e0e8

Please sign in to comment.