blib/lib/ShipIt/Step/ChangePodVersion.pm

filter: Statementstmt 50.0% 23 / 46 Branchbran 27.2% 6 / 22 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 62.5% 5 / 8 Podpod 100% 2 / 2 totaltotal 43.3% 36 / 83 CC 14 SCAR 31.8
CC
14
Cov
41%
run
56.0
_version_from_file
20.0
init
6.0
CRAP 24.0
↑ Summary
1 package ShipIt::Step::ChangePodVersion;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use base 'ShipIt::Step';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use ShipIt::Util qw(slurp write_file);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use File::Find::Rule;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $VERSION = '0.04';
9 3 $VERSION = eval $VERSION;
10  
11 ################################################################################
12 0 ▶sub init {
Subroutine init: not called CC=2 SCAR=17.9
Pod documented
13 0 my ($self, $conf) = @_;
14 0 my $all_from_config = $conf->value("ChangePodVersion.all");
15 0 ▶ $self->{change_pod_version_all} = defined $all_from_config ? $all_from_config : 1;
Branch: 1 branch
BranchTrueFalseTotal
defined $all_from_config ? : 0 0 0
16 }
17  
18  
19 ################################################################################
20 # no check for dry run, because nothing gets destroyed
21 0 ▶sub run {
Subroutine run: not called CC=7 SCAR=40.3
Pod documented
22 0 my ($self, $state) = @_;
23  
24 0 $state->pt->current_version; #should create $self->{ver_from} for us
25 0 my $new_version = $state->version;
26  
27 # change Distribution Module
28 0 my $dist_file = $state->pt->{ver_from};
29 0 ▶ die "no file for distribution found", $self->{ver_from} unless ($dist_file);
Branch: 1 branch
BranchTrueFalseTotal
unless $dist_file 0 0 0
30 0 my $changed_file = $self->_change_pod_version(slurp($dist_file), $new_version);
31 0 write_file($dist_file, $changed_file);
32  
33 # change other modules
34 0 ▶ if ($self->{change_pod_version_all}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"change_pod_version_all"}) 0 0 0
35 0 for my $module ( File::Find::Rule->name('*.pm')->in('lib') ) {
36 0 ▶ next if ($module eq $dist_file); # already treated
Branch: 1 branch
BranchTrueFalseTotal
if $module eq $dist_file 0 0 0
37 0 my $version = $self->_version_from_file($module);
38 0 ▶ next unless (defined $version && length $version);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version and length $version 0 0 0
Condition0%0/3
defined $version and length $version
!ll&&!rl&&r
000
MC/DC0%0/2
defined $version and length $version
defined $version length $version
Truth table0%0/3
A and B
A= defined $versionB= length $version
ABresult
0X0
100
111
39  
40 0 my $changed_module = $self->_change_pod_version(slurp($module), $version);
41 0 write_file($module, $changed_module);
42 }
43 }
44  
45  
46 0 return 1;
47  
48 }
49  
50 ################################################################################
51 # Copied from ProjectType::Perl, which operates only on $self->{ver_from}
52 # Copied again from ShipIt::Step::CheckVersionsMatch
53 sub _version_from_file
54 0 ▶{
Subroutine _version_from_file: not called CC=4 SCAR=30.0
55 0 my $self = shift;
56 0 my $file = shift;
57  
58 0 ▶ open my $fh, '<', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or die "Failed to open ${file}: $!\n" 0 0 0
59 or die "Failed to open $file: $!\n";
60  
61 0 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 0 0 0
62 0 ▶ return $2 if /\$VERSION\s*=\s*([\'\"])(.+?)\1/;
Branch: 1 branch
BranchTrueFalseTotal
if /\$VERSION\s*=\s*([\'\"])(.+?)\1/ 0 0 0
63 }
64 }
65  
66 ################################################################################
67 5 ▶sub _change_pod_version {
Subroutine _change_pod_version: called CC=4 SCAR=13.9
68 5 my ($self, $file_content, $new_version) = @_;
69  
70 # if we find a VERSION section, we change version, otherwise add one
71 5 ▶ if ($file_content =~ /^=head\d VERSION/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file_content =~ /^=head\d VERSION/m) { } 3 2 5
72  
73 # replace version
74 3 ▶ if ($file_content !~ s/(^=head\d VERSION[^\d=]*)[\d._]+/$1$new_version/sm) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file_content =~ s/(^=head\d VERSION[^\d=]*)[\d._]+/$1$new_version/ms) 1 2 3
75 1 die ('there is a POD VERSION section, but the version cannot be parsed');
76 }
77  
78 } else {
79  
80 2 my $version = "=head1 VERSION\n\n$new_version\n\n";
81 # add it after NAME section, everybody has one, right?
82 2 ▶ if ($file_content !~ s/(^=head\d NAME.*?(?=^=))/$1$version/sm) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($file_content =~ s/(^=head\d NAME.*?(?=^=))/$1$version/ms) 1 1 2
83 1 die ('trying to add a POD VERSION section after NAME Section, but there is none');
84 }
85  
86 }
87  
88 3 return $file_content;
89 }
90  
91 3 1;
92  
93 =head1 NAME
94  
95 ShipIt::Step::ChangePodVersion - Keep VERSION in your Pod in sync with $VERSION
96  
97 =head1 VERSION
98  
99 Version 0.04
100  
101 =begin readme
102  
103 =head1 INSTALLATION
104  
105 To install this module, run the following commands:
106  
107 perl Build.PL
108 ./Build
109 ./Build test
110 ./Build install
111  
112 =end readme
113  
114 =head1 SYNOPSIS
115  
116 Just add it to the ShipIt config, maybe after the ChangeVersion-Step
117  
118 steps = FindVersion, ChangeVersion, ChangePodVersion, CheckChangeLog, ...
119  
120 And make sure you have a VERSION or at least a NAME section in your Pod.
121  
122 =head1 VERSION
123  
124 version 123
125  
126 =head1 DESCRIPTION
127  
128 This is a Step for ShipIt to keep your Pod VERSION in sync with your $VERSION.
129 If a VERSION section is discovered in your Pod, it is tried to find and replace
130 numbers or "." or "_" within this section with your new version.
131  
132 You can write whatever you want before your version-number, but make sure it
133 does not contain numbers or "." or "_".
134  
135 In case no VERSION section is found, a VERSION section is created after the
136 NAME section. If no NAME section is found, we die.
137  
138 By default all your modules' Pod VERSION sections are updated to the files'
139 $VERSION. Add ChangePodVersion.all to your shipit config and set it to 0 to
140 change only the Pod of your distribution package.
141  
142 In case no $VERSION is found in your package, we don't die, but continue with
143 other packages.
144  
145 =head1 CONFIG
146  
147 =head2 ChangePodVersion.all
148  
149 B<DEFAULT> 1
150  
151 Set this config value to 0 to deactivate VERSION Changes for all your dists
152 modules. Only the dist-packages' Pod VERSION will be changed then.
153  
154 =head1 WARNING
155  
156 The code to change all Modules' Pod VERSION is not automatically tested yet,
157 because it is hard to write tests for it. I use it with HTTP::Exception now and
158 didn't notice any problems, although automatic testing is better than having not
159 experienced any problems. If you encounter problems, just deactivate it with
160 ChangePodVersion.all = 0 and drop me an email.
161  
162 =head1 AUTHOR
163  
164 Thomas Mueller, C<< <tmueller at cpan.org> >>
165  
166 =head1 BUGS
167  
168 Please report any bugs or feature requests to C<bug-shipit-step-changepodversion at rt.cpan.org>, or through
169 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=ShipIt-Step-ChangePodVersion>. I will be notified, and then you'll
170 automatically be notified of progress on your bug as I make changes.
171  
172 =head1 SUPPORT
173  
174 You can find documentation for this module with the perldoc command.
175  
176 perldoc ShipIt::Step::ChangePodVersion
177  
178  
179 You can also look for information at:
180  
181 =over 4
182  
183 =item * RT: CPAN's request tracker
184  
185 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=ShipIt-Step-ChangePodVersion>
186  
187 =item * AnnoCPAN: Annotated CPAN documentation
188  
189 L<http://annocpan.org/dist/ShipIt-Step-ChangePodVersion>
190  
191 =item * CPAN Ratings
192  
193 L<http://cpanratings.perl.org/d/ShipIt-Step-ChangePodVersion>
194  
195 =item * Search CPAN
196  
197 L<http://search.cpan.org/dist/ShipIt-Step-ChangePodVersion/>
198  
199 =back
200  
201 =head1 LICENSE AND COPYRIGHT
202  
203 Copyright 2010 Thomas Mueller.
204  
205 This program is free software; you can redistribute it and/or modify it
206 under the terms of either: the GNU General Public License as published
207 by the Free Software Foundation; or the Artistic License.
208  
209 See http://dev.perl.org/licenses/ for more information.
210  
211  
212 =cut
↑ Summary