blib/lib/Dist/Zilla/Plugin/OurPkgVersion.pm

filter: Statementstmt 96.8% 93 / 96 Branchbran 84.0% 37 / 44 Conditioncond 66.6% 8 / 12 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 89.7% 158 / 176 CC 30 SCAR 24.9
CC
30
Cov
91%
munge_file
21.7
_identify_version_value_token
7.0
munge_files
3.0
CRAP 12.1
↑ Summary
1 package Dist::Zilla::Plugin::OurPkgVersion;
2 10 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 10 our $VERSION = '0.21'; # VERSION
7  
8 10 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 10 with (
10 'Dist::Zilla::Role::FileMunger',
11 'Dist::Zilla::Role::FileFinderUser' => {
12 default_finders => [
13 ':InstallModules',
14 ':PerlExecFiles',
15 ],
16 },
17 'Dist::Zilla::Role::PPI',
18 );
19  
20 10 ▶use Carp qw( confess );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 10 ▶use PPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 10 ▶use MooseX::Types::Perl qw( LaxVersionStr );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 10 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 10 has underscore_eval_version => (
26 is => 'ro',
27 isa => 'Bool',
28 default => 0,
29 );
30  
31 10 has semantic_version => (
32 is => 'ro',
33 isa => 'Bool',
34 default => 0,
35 );
36  
37 10 has skip_main_module => (
38 is => 'ro',
39 isa => 'Bool',
40 default => 0,
41 );
42  
43 10 has overwrite => (
44 is => 'ro',
45 isa => 'Bool',
46 default => 0,
47 );
48  
49 10 has no_critic => (
50 is => 'ro',
51 isa => 'Bool',
52 default => 0,
53 );
54  
55 8 ▶sub munge_files {
Subroutine munge_files: called CC=3 SCAR=11.0
Pod documented
56 8 my $self = shift;
57  
58 8 $self->munge_file($_) for
59 25 ▶ grep { $self->skip_main_module ? $_->name ne $self->zilla->main_module->name : 1 }
Branch: 1 branch
BranchTrueFalseTotal
$self->skip_main_module ? : 2 23 25
60 8 @{ $self->found_files };
61  
62 8 return;
63 }
64  
65 9 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.0
Pod documented
66 9 my $self = shift;
67  
68 9 ▶ if ($self->underscore_eval_version && $self->semantic_version) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->underscore_eval_version and $self->semantic_version) 1 8 9
Condition100%3/3
$self->underscore_eval_version and $self->semantic_version
!ll&&!rl&&r
711
MC/DC100%2/2
$self->underscore_eval_version and $self->semantic_version
$self->underscore_eval_version $self->semantic_version
Truth table100%3/3
A and B
A= $self->underscore_eval_versionB= $self->semantic_version
ABresult
0X0
100
111
69 1 confess 'You cannot setup both underscore_eval_version and semantic_version';
70 }
71  
72 8 return 1;
73 }
74  
75 24 ▶sub munge_file { ## no critic (Subroutines::ProhibitExcessComplexity)
Subroutine munge_file: called CC=20 SCAR=30.8
Pod documented
76 24 my ( $self, $file ) = @_;
77  
78 24 ▶ if ( $file->name =~ m/\.pod$/ixms ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->name =~ /\.pod$/imsux) 0 24 24
79 0 $self->log_debug( 'Skipping: "' . $file->name . '" is pod only');
80 0 return;
81 }
82  
83 24 my $version = $self->zilla->version;
84  
85 24 ▶ confess 'invalid characters in version'
Branch: 1 branch
BranchTrueFalseTotal
unless LaxVersionStr()->check($version) 0 24 24
86 unless LaxVersionStr->check( $version ); ## no critic (Modules::RequireExplicitInclusion)
87  
88 24 my $doc = $self->ppi_document_for_file($file);
89  
90 24 ▶ return unless defined $doc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc 0 24 24
91  
92 24 ▶ $doc->index_locations if $self->overwrite; # optimize calls to check line numbers
Branch: 1 branch
BranchTrueFalseTotal
if $self->overwrite 1 23 24
93 24 my $comments = $doc->find('PPI::Token::Comment');
94  
95 24 my $version_regex
96 = q{
97 ^
98 (\s*) # capture leading whitespace for whole-line comments
99 (
100 \#\#?\s*VERSION # capture # VERSION or ## VERSION
101 \b # and ensure it ends on a word boundary
102 [ # conditionally
103 [:print:] # all printable characters after VERSION
104 \s # any whitespace including newlines - GH #5
105 ]* # as many of the above as there are
106 )
107 $ # until the EOL}
108 ;
109  
110 24 my $munged_version = 0;
111 24 ▶ if ( ref($comments) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $comments eq "ARRAY") 22 2 24
112 22 foreach ( @{ $comments } ) {
113 38 ▶ if ( /$version_regex/xms ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$version_regex/msux) 24 14 38
114 24 my ( $ws, $comment ) = ( $1, $2 );
115 24 ▶ $comment =~ s/(?=\bVERSION\b)/TRIAL /x if $self->zilla->is_trial;
Branch: 1 branch
BranchTrueFalseTotal
if $self->zilla->is_trial 10 14 24
116 24 my $code
117 = "$ws"
118 . q{our $VERSION = '}
119 . $version
120 . qq{'; $comment}
121 ;
122  
123 24 ▶ if ( $self->semantic_version ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->semantic_version) 1 23 24
124 1 ▶ confess 'Invalid semantic version syntax declaration in INI file' unless ( $version =~ /^v\d+\.\d+\.\d+$/x );
Branch: 1 branch
BranchTrueFalseTotal
unless $version =~ /^v\d+\.\d+\.\d+$/ux 0 1 1
125 1 $code = "use version;\n" . $code;
126 }
127  
128 # If the comment is not a whole-line comment, and if the user wants to overwrite
129 # existing "our $VERSION=...;", then find the other tokens from this line, looking
130 # for our $VERSION = $VALUE. If found, edit only the VALUE.
131 24 ▶ if ( $self->overwrite && !$_->line ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->overwrite and not $_->line) 3 21 24
Condition66%2/3
$self->overwrite and not $_->line
!ll&&!rl&&r
2103
MC/DC50%1/2
$self->overwrite and not $_->line
$self->overwrite not $_->line
Truth table66%2/3
A and not(B)
A= $self->overwriteB= not $_->line
ABresult
0X0
100
111
132 3 my $line_no = $_->line_number;
133 3 ▶ my $nodes = $doc->find( sub { $_[1]->line_number == $line_no } );
Subroutine __ANON__: called CC=1 SCAR=0.0
134 3 ▶ my $version_value_token = $nodes && $self->_identify_version_value_token(@$nodes);
Condition33%1/3
$nodes && $self->_identify_version_value_token(@$nodes)
!ll&&!rl&&r
003
MC/DC0%0/2
$nodes && $self->_identify_version_value_token(@$nodes)
$nodes $self->_identify_version_value_token(@$nodes)
Truth table33%1/3
A && B
A= $nodesB= $self->_identify_version_value_token(@$nodes)
ABresult
0X0
100
111
135 3 ▶ if ( $version_value_token ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version_value_token) 3 0 3
136 3 $version_value_token->set_content(qq{'$version'});
137 3 $code = $ws . $comment;
138 3 $munged_version++;
139 }
140 }
141  
142 24 ▶ if ( $version =~ /_/ && $self->underscore_eval_version ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version =~ /_/u and $self->underscore_eval_version) 1 23 24
Condition66%2/3
$version =~ /_/u and $self->underscore_eval_version
!ll&&!rl&&r
2301
MC/DC50%1/2
$version =~ /_/u and $self->underscore_eval_version
$version =~ /_/u $self->underscore_eval_version
Truth table66%2/3
A and B
A= $version =~ /_/uB= $self->underscore_eval_version
ABresult
0X0
100
111
143 1 my $eval = "\$VERSION = eval \$VERSION;";
144 1 ▶ $eval .= " ## no critic (BuiltinFunctions::ProhibitStringyEval)"
Branch: 1 branch
BranchTrueFalseTotal
if $self->no_critic 0 1 1
145 if $self->no_critic;
146 1 ▶ $code .= $_->line? "$eval\n" : "\n$eval";
Branch: 1 branch
BranchTrueFalseTotal
$_->line ? : 1 0 1
147 }
148  
149 24 $_->set_content($code);
150 24 $munged_version++;
151 }
152 }
153 }
154  
155 24 ▶ if ( $munged_version ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($munged_version) { } 20 4 24
156 20 $self->save_ppi_document_to_file( $doc, $file);
157 20 $self->log_debug([ 'adding $VERSION assignment to %s', $file->name ]);
158 }
159 else {
160 4 $self->log( 'Skipping: "'
161 . $file->name
162 . '" has no "# VERSION" comment'
163 );
164 }
165 24 return;
166 }
167  
168 3 ▶sub _identify_version_value_token {
Subroutine _identify_version_value_token: called CC=7 SCAR=19.5
169 3 my ( $self, @tokens ) = @_;
170 3 my $val_tok;
171 3 my @want = ('our', '$VERSION', '=', undef, ';');
172 3 my $i = 0;
173 3 for ( @tokens ) {
174 39 ▶ next if $_->isa('PPI::Token::Whitespace');
Branch: 1 branch
BranchTrueFalseTotal
if $_->isa("PPI::Token::Whitespace") 12 27 39
175 # If the next thing we want is "undef", this is where we capture the value token.
176 27 ▶ if (!defined $want[$i]) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $want[$i]) { } 3 24 27
elsif ($want[$i] eq $_->content) { } 13 11 24
elsif ($i) { } 1 10 11
177 3 $val_tok = $_;
178 3 ++$i;
179 }
180 # Else if the token matches the current step in the sequence, advance the sequence
181 # If sequence completely matched, return.
182 elsif ($want[$i] eq $_->content) {
183 13 ++$i;
184 13 ▶ return $val_tok if $i >= $#want;
Branch: 1 branch
BranchTrueFalseTotal
if $i >= $#want 3 10 13
185 }
186 # A mismatch restarts the search
187 elsif ($i) {
188 1 $i = 0;
189 }
190 }
191 0 return; # no match
192 }
193  
194 10 __PACKAGE__->meta->make_immutable;
195 10 1;
196 # ABSTRACT: No line insertion and does Package version with our
197  
198 __END__
↑ Summary