Plugin/NextVersion/Semantic.pm

filter: Statementstmt 88.8% 144 / 162 Branchbran 65.3% 17 / 26 Conditioncond 85.7% 6 / 7 MC/DCmcdc 75.0% 3 / 4 Subroutinesub 90.3% 28 / 31 Podpod 0.0% 0 / 10 totaltotal 82.5% 198 / 240 CC 22 SCAR 13.4
CC
22
Cov
86%
after_release
12.0
increment_version
9.0
__ANON__
5.2
CRAP 3.8
↑ Summary Plugin/PreviousVersion/Changelog.pm »
1 package Dist::Zilla::Plugin::NextVersion::Semantic;
2 3 our $AUTHORITY = 'cpan:YANICK';
3 # ABSTRACT: update the next version, semantic-wise
4 3 $Dist::Zilla::Plugin::NextVersion::Semantic::VERSION = '0.2.6';
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶use CPAN::Changes 0.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Perl::Version;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use List::AllUtils qw/ any min /;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 with qw/
17 Dist::Zilla::Role::Plugin
18 Dist::Zilla::Role::FileMunger
19 Dist::Zilla::Role::TextTemplate
20 Dist::Zilla::Role::BeforeRelease
21 Dist::Zilla::Role::AfterRelease
22 Dist::Zilla::Role::VersionProvider
23 Dist::Zilla::Plugin::NextVersion::Semantic::Incrementer
24 /;
25  
26 3 ▶use Moose::Util::TypeConstraints;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 3 subtype 'ChangeCategory',
29 as 'ArrayRef[Str]';
30  
31 coerce ChangeCategory =>
32 from 'Str',
33 0 ▶ via {
Subroutine __ANON__: not called CC=1 SCAR=6.9
34 0 [ split /\s*,\s*/, $_ ]
35 3 };
36  
37  
38 3 has change_file => ( is => 'ro', isa=>'Str', default => 'Changes' );
39  
40  
41 3 has numify_version => ( is => 'ro', isa => 'Bool', default => 0 );
42  
43  
44 3 has format => (
45 is => 'ro',
46 isa => 'Str',
47 default => '%d.%3d.%3d',
48 );
49  
50  
51 has major => (
52 is => 'ro',
53 isa => 'ChangeCategory',
54 coerce => 1,
55 8 ▶ default => sub { [ 'API CHANGES' ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
56 3 traits => ['Array'],
57 handles => { major_groups => 'elements' },
58 );
59  
60  
61 has minor => (
62 is => 'ro',
63 isa => 'ChangeCategory',
64 coerce => 1,
65 8 ▶ default => sub { [ 'ENHANCEMENTS', 'UNGROUPED' ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
66 3 traits => ['Array'],
67 handles => { minor_groups => 'elements' },
68 );
69  
70  
71 has revision => (
72 is => 'ro',
73 isa => 'ChangeCategory',
74 coerce => 1,
75 8 ▶ default => sub { [ 'BUG FIXES', 'DOCUMENTATION' ] },
Subroutine __ANON__: called CC=1 SCAR=0.0
76 3 traits => ['Array'],
77 handles => { revision_groups => 'elements' },
78 );
79  
80 1 ▶sub before_release {
Subroutine before_release: called CC=2 SCAR=6.9
Pod undocumented
81 1 my $self = shift;
82  
83 1 my ($changes_file) = grep { $_->name eq $self->change_file } @{ $self->zilla->files };
84  
85 1 my $changes = CPAN::Changes->load_string(
86 $changes_file->content,
87 next_token => qr/\{\{\$NEXT\}\}/
88 );
89  
90 1 my( $next ) = reverse $changes->releases;
91  
92 1 my @changes = values %{ $next->changes };
93  
94 1 ▶ $self->log_fatal("change file has no content for next version")
Branch: 1 branch
BranchTrueFalseTotal
unless @changes 1 0 1
95 unless @changes;
96  
97 }
98  
99 0 ▶sub after_release {
Subroutine after_release: not called CC=3 SCAR=24.8
Pod undocumented
100 0 my ($self) = @_;
101 0 my $filename = $self->change_file;
102  
103 0 my $changes = CPAN::Changes->load(
104 $self->change_file,
105 next_token => qr/\{\{\$NEXT\}\}/
106 );
107  
108 # remove empty groups
109 0 $changes->delete_empty_groups;
110  
111 0 my ( $next ) = reverse $changes->releases;
112  
113 0 $next->add_group( grep { $_ ne 'UNGROUPED' } $self->all_groups );
114  
115 0 $self->log_debug([ 'updating contents of %s on disk', $filename ]);
116  
117 # and finally rewrite the changelog on disk
118 0 ▶ open my $out_fh, '>', $filename
Branch: 1 branch
BranchTrueFalseTotal
open my $out_fh, ">", $filename or Carp::croak("can't open $filename for writing: $!") 0 0 0
119 or Carp::croak("can't open $filename for writing: $!");
120  
121 0 print $out_fh $changes->serialize;
122  
123 0 ▶ close $out_fh or Carp::croak("error closing $filename: $!");
Branch: 1 branch
BranchTrueFalseTotal
close $out_fh or Carp::croak("error closing ${filename}: $!") 0 0 0
124 }
125  
126 0 ▶sub all_groups {
Subroutine all_groups: not called CC=1 SCAR=6.9
Pod undocumented
127 0 my $self = shift;
128  
129 0 return map { $self->$_ } map { $_.'_groups' } qw/ major minor revision /
130 }
131  
132 has previous_version => (
133 is => 'ro',
134 lazy => 1,
135 7 ▶ default => sub {
Subroutine __ANON__: called CC=5 SCAR=16.6
136 7 my $self = shift;
137  
138 7 my $plugins =
139 $self->zilla->plugins_with('-YANICK::PreviousVersionProvider');
140  
141 7 ▶ $self->log_fatal(
Branch: 1 branch
BranchTrueFalseTotal
unless ref $plugins and @$plugins >= 1 1 6 7
Condition66%2/3
ref $plugins and @$plugins >= 1
!ll&&!rl&&r
016
MC/DC50%1/2
ref $plugins and @$plugins >= 1
ref $plugins @$plugins >= 1
Truth table66%2/3
A and B
A= ref $pluginsB= @$plugins >= 1
ABresult
0X0
100
111
142 "at least one plugin with the role PreviousVersionProvider",
143 "must be referenced in dist.ini"
144 ) unless ref $plugins and @$plugins >= 1;
145  
146 6 for my $plugin ( @$plugins ) {
147 6 my $version = $plugin->provide_previous_version;
148  
149 6 ▶ return $version if defined $version;
Branch: 1 branch
BranchTrueFalseTotal
if defined $version 6 0 6
150 }
151  
152 0 return undef;
153 },
154 3 );
155  
156 8 ▶sub provide_version {
Subroutine provide_version: called CC=2 SCAR=6.9
Pod undocumented
157 8 my $self = shift;
158  
159 # override (or maybe needed to initialize)
160 8 ▶ return $ENV{V} if exists $ENV{V};
Branch: 1 branch
BranchTrueFalseTotal
if exists $ENV{"V"} 1 7 8
161  
162 7 my $new_ver = $self->next_version( $self->previous_version);
163  
164 6 $self->zilla->version("$new_ver");
165 }
166  
167 6 ▶sub next_version {
Subroutine next_version: called CC=2 SCAR=6.9
Pod undocumented
168 6 my( $self, $last_version ) = @_;
169  
170  
171 6 my $new_ver = $self->increment_version( $self->increment_level );
172  
173 6 ▶ $new_ver = Perl::Version->new( $new_ver )->numify if $self->numify_version;
Branch: 1 branch
BranchTrueFalseTotal
if $self->numify_version 1 5 6
174  
175 3 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
176 6 $self->log("Bumping version from $last_version to $new_ver");
177 6 return $new_ver;
178 }
179  
180 6 ▶sub increment_level {
Subroutine increment_level: called CC=3 SCAR=11.0
Pod undocumented
181 6 my ( $self ) = @_;
182  
183 6 ▶ my ($changes_file) = grep { $_->name eq $self->change_file } @{ $self->zilla->files }
Branch: 1 branch
BranchTrueFalseTotal
my($changes_file) = grep({$_->name eq $self->change_file;} @{$self->zilla->files;}) or die "no changelog file found\n" 6 0 6
184 or die "no changelog file found\n";
185  
186 6 my $changes = CPAN::Changes->load_string( $changes_file->content,
187 next_token => qr/\{\{\$NEXT\}\}/ );
188  
189 6 my ($changelog) = reverse $changes->releases;
190  
191 my %category_map = (
192 6 map( { $_ => 0 } $self->major_groups ),
193 6 map( { $_ => 1 } $self->minor_groups ),
194 );
195  
196 6 $category_map{''} = $category_map{UNGROUPED};
197  
198 3 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
199  
200 my $increment_level = min
201 6 grep { defined }
202 6 map { $category_map{$_} }
203 6 grep { scalar @{ $changelog->changes($_) } } # only groups with items
204 $changelog->groups;
205  
206 6 ▶ return (qw/MAJOR MINOR PATCH/)[$increment_level//2];
Condition100%2/2
$increment_level // 2
l!l
33
MC/DC100%1/1
$increment_level // 2
$increment_level
Truth table100%2/2
A
A= $increment_level
Aresult
11
00
207 }
208  
209 7 ▶sub munge_files {
Subroutine munge_files: called CC=3 SCAR=11.0
Pod undocumented
210 7 my ($self) = @_;
211  
212 7 my ($file) = grep { $_->name eq $self->change_file } @{ $self->zilla->files };
213 7 ▶ return unless $file;
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 7 7
214  
215 7 my $changes = CPAN::Changes->load_string( $file->content,
216 next_token => qr/\{\{\$NEXT\}\}/
217 );
218  
219 7 my ( $next ) = reverse $changes->releases;
220  
221 7 $next->delete_group($_) for grep { !@{$next->changes($_)} } $next->groups;
222  
223 7 $self->log_debug([ 'updating contents of %s in memory', $file->name ]);
224 7 $file->content($changes->serialize);
225 }
226  
227  
228 3 __PACKAGE__->meta->make_immutable;
229 3 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
230  
231 {
232 package Dist::Zilla::Plugin::NextVersion::Semantic::Incrementer;
233 3 our $AUTHORITY = 'cpan:YANICK';
234 3 $Dist::Zilla::Plugin::NextVersion::Semantic::Incrementer::VERSION = '0.2.6';
235 3 ▶use List::AllUtils qw/ first_index any /;
Subroutine BEGIN: called CC=1 SCAR=0.0
236  
237 3 ▶ use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
238  
239 3 requires 'previous_version', 'format';
240  
241 29 ▶ sub nbr_version_levels {
Subroutine nbr_version_levels: called CC=1 SCAR=0.0
Pod undocumented
242 29 my @tokens = $_[0]->format =~ /(%\d*d)/g;
243 29 return scalar @tokens;
244 }
245  
246 25 ▶ sub version_lenghts {
Subroutine version_lenghts: called CC=1 SCAR=0.0
Pod undocumented
247 25 return $_[0]->format =~ /%0*(\d*)/g;
248 }
249  
250 25 ▶ sub increment_version {
Subroutine increment_version: called CC=9 SCAR=22.0
Pod undocumented
251 25 my( $self, $level ) = @_;
252 25 ▶ $level ||= 'PATCH';
Condition100%2/2
$level ||= "PATCH"
l!l
214
MC/DC100%1/1
$level ||= "PATCH"
$level
Truth table100%2/2
A
A= $level
Aresult
11
00
253  
254 25 my @version = (0,0,0);
255  
256 # initial version is special
257 25 ▶ if ( my $previous = $self->previous_version ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $previous = $self->previous_version) 22 3 25
258 22 my $regex = quotemeta $self->format;
259 22 $regex =~ s/\\%0(\d+)d/(\\d{$1})/g;
260 22 $regex =~ s/\\%(\d+)d/(\\d{1,$1})/g;
261 22 $regex =~ s/\\%d/(\\d+)/g;
262  
263 22 ▶ @version = $previous =~ m/$regex/
Branch: 1 branch
BranchTrueFalseTotal
@version = $previous =~ /$regex/ or die "previous version '${previous}' doesn't match format '@{[$self->format];}'" 22 0 22
264 0 or die "previous version '$previous' doesn't match format '@{[$self->format]}'" ;
265 }
266  
267 25 my @levels = qw/ MAJOR MINOR PATCH /;
268 25 ▶ my $index = first_index { $level eq $_ } @levels;
Subroutine __ANON__: called CC=1 SCAR=0.0
269  
270 25 $version[$index]++;
271 25 $version[$_] = 0 for $index+1..2;
272  
273 # if the incremental level is below the number of levels we
274 # have, increment the lowest level we consider
275 25 ▶ if( any { $version[$_] > 0 } $self->nbr_version_levels..2 ) {
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if (&any(sub { $version[$_] > 0; } , $self->nbr_version_levels .. 2)) 2 23 25
276 2 $version[ $self->nbr_version_levels -1 ]++;
277 2 $version[$_] = 0 for $self->nbr_version_levels..2;
278 }
279  
280 # exceeding sizes?
281 25 my @sizes = $self->version_lenghts;
282 25 for my $i ( grep { $sizes[$_] } reverse 0..2 ) {
283 38 ▶ if ( length( $version[$i] ) > $sizes[$i] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $version[$i] > $sizes[$i]) 1 37 38
284 1 $version[$i-1]++;
285 1 $version[$i] = 0;
286 }
287 }
288  
289 3 ▶ no warnings; # possible redundant args for sprintf
Subroutine BEGIN: called CC=1 SCAR=0.0
290  
291 25 my $version = sprintf $self->format, @version;
292 25 $version =~ y/ //d;
293  
294 25 return $version;
295 }
296  
297  
298 }
299  
300  
301 3 1;
302  
303 __END__
↑ Summary Plugin/PreviousVersion/Changelog.pm »