App-Changelord/lib/App/Changelord/Role/Versions.pm
2022-07-17 13:24:43 -04:00

40 lines
932 B
Perl

package App::Changelord::Role::Versions;
use v5.36.0;
use List::AllUtils qw/ first min /;
use Version::Dotted::Semantic;
use Moo::Role;
use feature 'try';
requires 'changelog';
sub latest_version($self){
first { $_ } grep { $_ ne 'NEXT' } map { eval { $_->{version} } } $self->changelog->{releases}->@*;
}
sub next_version($self) {
my $version = Version::Dotted::Semantic->new($self->latest_version // '0.0.0');
my $upcoming = $self->changelog->{releases}[0];
if( $upcoming->{version} and $upcoming->{version} ne 'NEXT') {
$upcoming = { changes => [] };
}
my %mapping = map {
my $level = $_->{level};
map { $_ => $level } $_->{keywords}->@*
} $self->change_types->@*;
my $bump =min 2, map { $_ eq 'major' ? 0 : $_ eq 'minor' ? 1 : 2 } map { $mapping{$_->{type}} || 'patch' } $upcoming->{changes}->@*;
$version->bump($bump);
return $version->normal;
}
1;