From d4ad82747175e53cf1a7d781ce5d5fb720222efa Mon Sep 17 00:00:00 2001 From: Yanick Champoux Date: Tue, 19 Jul 2022 16:18:58 -0400 Subject: [PATCH] add 'add' command --- lib/App/Changelord.pm | 2 +- lib/App/Changelord/Command/Add.pm | 68 +++++++++++++++++++ lib/App/Changelord/Command/Schema.pm | 7 +- .../Changelord/Command/changelog-schema.yml | 31 ++++++--- 4 files changed, 93 insertions(+), 15 deletions(-) create mode 100644 lib/App/Changelord/Command/Add.pm diff --git a/lib/App/Changelord.pm b/lib/App/Changelord.pm index f845df6..91cb85f 100644 --- a/lib/App/Changelord.pm +++ b/lib/App/Changelord.pm @@ -141,6 +141,6 @@ sub run($self) { } subcommand $_ => 'App::Changelord::Command::' . ucfirst $_ - for qw/ schema validate version bump init/; + for qw/ schema validate version bump init add/; 1; diff --git a/lib/App/Changelord/Command/Add.pm b/lib/App/Changelord/Command/Add.pm new file mode 100644 index 0000000..9074fb7 --- /dev/null +++ b/lib/App/Changelord/Command/Add.pm @@ -0,0 +1,68 @@ +package App::Changelord::Command::Add; + +use 5.36.0; + +use Moo; +use CLI::Osprey desc => 'add a change to the changelog'; + +use PerlX::Maybe; +use Path::Tiny; +use App::Changelord::Command::Init; + +has changelog => ( is => 'lazy' ); + +sub _build_changelog ($self) { $self->parent_command->changelog } + +# TODO validate the type +option type => ( + format => 's', + doc => 'type of change', + is => 'ro', +); + +option ticket => ( + format => 's', + doc => 'associated ticket', + is => 'ro', +); + +sub is_next($self,$release) { + my $version = $release->{version}; + return !$version || $version eq 'NEXT'; +} + +sub next_release($self) { + my $changelog = $self->changelog; + + my $release = $changelog->{releases}[0]; + + unless( $self->is_next($release) ) { + unshift $changelog->{releases}->@*, + $release = { + version => 'NEXT', + changes => [], + }; + } + + return $release; +} + +sub save_changelog($self) { + my $src = $self->parent_command->source; + + path($src)->spew( App::Changelord::Command::Init::serialize_changelog($self) ); +} + +sub run ($self) { + my $version = $self->next_release; + + push $version->{changes}->@*, { + maybe type => $self->type, + maybe ticket => $self->ticket, + desc => join ' ', @ARGV, + }; + + $self->save_changelog; +} + +'end of App::Changelog::Command::Add'; diff --git a/lib/App/Changelord/Command/Schema.pm b/lib/App/Changelord/Command/Schema.pm index b573be2..dd65e3b 100644 --- a/lib/App/Changelord/Command/Schema.pm +++ b/lib/App/Changelord/Command/Schema.pm @@ -18,12 +18,9 @@ option json => ( sub run($self) { - my $schema = path(__FILE__)->sibling('changelog-schema.yml')->slurp; - - $schema = JSON->new->pretty->encode(YAML::Load($schema)); - - print $schema; + my $schema = YAML::Load(path(__FILE__)->sibling('changelog-schema.yml')->slurp); + print $self->json ? JSON->new->pretty->encode(YAML::Load($schema)) : YAML::Dump($schema); } 'end of App::Changelog::Command::Schema'; diff --git a/lib/App/Changelord/Command/changelog-schema.yml b/lib/App/Changelord/Command/changelog-schema.yml index 52431da..d4958a8 100644 --- a/lib/App/Changelord/Command/changelog-schema.yml +++ b/lib/App/Changelord/Command/changelog-schema.yml @@ -6,30 +6,43 @@ properties: additionalProperties: false properties: homepage: - type: string + type: [ string, 'null' ] description: url of the project's homepage examples: - https://github.com/yanick/app-changelord name: - type: string + type: [ 'null', string ] description: name of the project examples: - App::Changelord - type: + change_types: type: array items: type: object - # properties: - # type: object - # properties: - # level: { enum: [ major, minor, patch ] } - # title: { type: string } + additionalProperties: false + properties: + keywords: + type: array + items: { type: string } + level: { enum: [ major, minor, patch ] } + title: { type: string } releases: type: array items: oneOf: - type: string - type: object + additionalProperties: false properties: version: { type: string } - date: { type: string } + date: { type: ['null',string] } + changes: { type: 'array', items: { $ref: '#/$defs/change' } } +$defs: + change: + type: object + required: [ desc ] + additionalProperties: false + properties: + desc: { type: string } + ticket: { type: [ string, 'null' ] } + type: { type: [ string, 'null' ] }