diff --git a/lib/App/Changelord/Command/Bump.pm b/lib/App/Changelord/Command/Bump.pm index 27a2f73..aaf25e3 100644 --- a/lib/App/Changelord/Command/Bump.pm +++ b/lib/App/Changelord/Command/Bump.pm @@ -16,6 +16,7 @@ with 'App::Changelord::Role::ChangeTypes'; has changelog => ( is => 'lazy' ); with 'App::Changelord::Role::Versions'; +with 'App::Changelord::Role::Stats'; sub _build_changelog ($self) { $self->parent_command->changelog } @@ -50,6 +51,12 @@ sub run ($self) { $self->changelog->{releases}[0]{date} = sprintf "%d-%02d-%02d", $time[5] + 1900, $time[4], $time[3]; + if( $self->changelog->{project}{with_stats} ) { + push $self->changelog->{releases}[0]{changes}->@*, { + type => 'stats', desc => $self->stats + }; + } + my $change = $self->changelog; Bless($change)->keys( [ uniq qw/ diff --git a/lib/App/Changelord/Command/Init.pm b/lib/App/Changelord/Command/Init.pm index 148fdcc..d806822 100644 --- a/lib/App/Changelord/Command/Init.pm +++ b/lib/App/Changelord/Command/Init.pm @@ -49,6 +49,8 @@ sub run ($self) { project => { name => undef, homepage => undef, + with_stats => 'true', + ticket_url => undef, }, change_types => $self->change_types, releases => [ diff --git a/lib/App/Changelord/Command/changelog-schema.yml b/lib/App/Changelord/Command/changelog-schema.yml index e817d4b..4578e31 100644 --- a/lib/App/Changelord/Command/changelog-schema.yml +++ b/lib/App/Changelord/Command/changelog-schema.yml @@ -21,6 +21,8 @@ properties: examples: - s!GH(\d+)!https://github.com/yanick/App-Changelord/issue/$1/ - /^\d+$/ ? "https://.../$_" : undef + with_stats: + description: if true, add git statistics when bumping the version. change_types: type: array items: diff --git a/lib/App/Changelord/Role/ChangeTypes.pm b/lib/App/Changelord/Role/ChangeTypes.pm index e661894..b680539 100644 --- a/lib/App/Changelord/Role/ChangeTypes.pm +++ b/lib/App/Changelord/Role/ChangeTypes.pm @@ -18,6 +18,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' ] }, + { title => 'Statistics' , level => 'patch', keywords => [ 'stats' ] }, ] } diff --git a/lib/App/Changelord/Role/Stats.pm b/lib/App/Changelord/Role/Stats.pm new file mode 100644 index 0000000..c80b99e --- /dev/null +++ b/lib/App/Changelord/Role/Stats.pm @@ -0,0 +1,48 @@ +package App::Changelord::Role::Stats; + +use v5.36.0; + +use Git::Repository; + +use Moo::Role; + +use feature 'try'; no warnings qw/ experimental /; + +requires 'changelog'; + +# stolen from Dist::Zilla::Plugin::ChangeStats::Git + +has repo => ( + is => 'ro', + default => sub { Git::Repository->new( work_tree => '.' ) }, +); + +has stats => ( + is => 'lazy' ); + +sub _build_stats ($self) { + my $comparison_data = $self->_get_comparison_data or return; + + my $stats = 'code churn: ' . $comparison_data; + return $stats =~ s/\s+/ /gr; +} + +sub _get_comparison_data($self) { + + # HEAD versus previous release + # What are we diffing against? :) + my $previous = $self->changelog->{releases}->@* > 1 + ? $self->changelog->{releases}[1]{version} + : '4b825dc642cb6eb9a060e54bf8d69288fbee4904'; # empty tree + + my $output = eval { + $self->repo->run( 'diff', '--shortstat', $previous, 'HEAD') + }; + + + warn "could not gather stats: $@\n" if $@; + + return $output; +} + +1; diff --git a/t/stats.t b/t/stats.t new file mode 100644 index 0000000..c448aa7 --- /dev/null +++ b/t/stats.t @@ -0,0 +1,27 @@ +use Test2::V0; + +use v5.36.0; + +package TestMe { + use Moo; + + has changelog => ( + is => 'ro', + default => sub {{ + project => { ticket_url => undef }, + releases => [ + { }, + ] + }} + ); + + with 'App::Changelord::Role::Stats'; + with 'App::Changelord::Role::ChangeTypes'; + +} + +my $test = TestMe->new; + +like $test->stats => qr/code churn: /; + +done_testing;