diff --git a/lib/App/Changelord.pm b/lib/App/Changelord.pm index 2b21767..f845df6 100644 --- a/lib/App/Changelord.pm +++ b/lib/App/Changelord.pm @@ -19,13 +19,11 @@ option source => ( default => 'CHANGELOG.yml', ); -has changelog => ( - lazy => 1, - is => 'ro', - default => sub($self) { - return YAML::LoadFile($self->source) - } -); +has changelog => ( is => 'lazy' ); + +sub _build_changelog($self) { + return YAML::LoadFile($self->source) +} with 'App::Changelord::Role::ChangeTypes'; @@ -142,9 +140,7 @@ sub run($self) { print $self->as_markdown; } -subcommand 'schema' => 'App::Changelord::Command::Schema'; -subcommand 'validate' => 'App::Changelord::Command::Validate'; -subcommand 'version' => 'App::Changelord::Command::Version'; -subcommand 'bump' => 'App::Changelord::Command::Bump'; +subcommand $_ => 'App::Changelord::Command::' . ucfirst $_ + for qw/ schema validate version bump init/; -'end of App::Changeman'; +1; diff --git a/lib/App/Changelord/Command/Init.pm b/lib/App/Changelord/Command/Init.pm new file mode 100644 index 0000000..148fdcc --- /dev/null +++ b/lib/App/Changelord/Command/Init.pm @@ -0,0 +1,63 @@ +package App::Changelord::Command::Init; + +use 5.36.0; + +use Moo; +use CLI::Osprey desc => 'initialize new changelog source file'; + +use Path::Tiny; +use JSON; +use YAML qw/ Bless /; +use List::AllUtils qw/ first min uniq /; +use Version::Dotted::Semantic; + +with 'App::Changelord::Role::ChangeTypes'; + +has changelog => ( is => 'lazy' ); + +with 'App::Changelord::Role::Versions'; + +sub _build_changelog ($self) { $self->parent_command->changelog } + +sub serialize_changelog($self, $changelog = undef) { + + $changelog //= $self->changelog; + + Bless($changelog)->keys( + [ uniq qw/ + project releases change_types + /, sort keys %$changelog + ] ); + Bless( $changelog->{project} )->keys( + [ uniq qw/ + name homepage + /, sort keys $changelog->{project}->%* + ] ); + + for ( grep { ref } $changelog->{releases}->@* ) { + Bless($_)->keys( [ uniq qw/ version date changes /, sort keys %$_ ] ); + } + + return YAML::Dump($changelog); +} + +sub run ($self) { + my $src = $self->parent_command->source; + die "file '$src' already exists, aborting\n" if -f $src; + + my $change = { + project => { + name => undef, + homepage => undef, + }, + change_types => $self->change_types, + releases => [ + { version => 'NEXT', changes => [] } + ] + }; + path($src)->spew( $self->serialize_changelog($change) ); + + say "file '$src' created, enjoy!"; +} + +'end of App::Changelog::Command::Init'; diff --git a/lib/App/Changelord/Role/ChangeTypes.pm b/lib/App/Changelord/Role/ChangeTypes.pm index 68bbae9..e661894 100644 --- a/lib/App/Changelord/Role/ChangeTypes.pm +++ b/lib/App/Changelord/Role/ChangeTypes.pm @@ -17,6 +17,7 @@ sub _build_change_types($self) { } || [ { title => 'Features' , level => 'minor', keywords => [ 'feat' ] } , { title => 'Bug fixes' , level => 'patch', keywords => [ 'fix' ] }, + { title => 'Package maintenance' , level => 'patch', keywords => [ 'chore', 'maint', 'refactor' ] }, ] }