File Coverage

blib/lib/ShipIt/Step/ChangePodVersion.pm
Criterion Covered Total %
statement 20 43 46.5
branch 6 20 30.0
condition 0 3 0.0
subroutine 5 8 62.5
pod 2 2 100.0
total 33 76 43.4


line stmt bran cond sub pod time code
1             package ShipIt::Step::ChangePodVersion;
2              
3 3     3   103708 use strict;
  3         8  
  3         134  
4 3     3   19 use base 'ShipIt::Step';
  3         7  
  3         3457  
5 3     3   3577 use ShipIt::Util qw(slurp write_file);
  3         180870  
  3         1004  
6 3     3   7609 use File::Find::Rule;
  3         59741  
  3         32  
7              
8             our $VERSION = '0.04';
9             $VERSION = eval $VERSION;
10              
11             ################################################################################
12             sub init {
13 0     0 1 0 my ($self, $conf) = @_;
14 0         0 my $all_from_config = $conf->value("ChangePodVersion.all");
15 0 0       0 $self->{change_pod_version_all} = defined $all_from_config ? $all_from_config : 1;
16             }
17              
18              
19             ################################################################################
20             # no check for dry run, because nothing gets destroyed
21             sub run {
22 0     0 1 0 my ($self, $state) = @_;
23              
24 0         0 $state->pt->current_version; #should create $self->{ver_from} for us
25 0         0 my $new_version = $state->version;
26              
27             # change Distribution Module
28 0         0 my $dist_file = $state->pt->{ver_from};
29 0 0       0 die "no file for distribution found", $self->{ver_from} unless ($dist_file);
30 0         0 my $changed_file = $self->_change_pod_version(slurp($dist_file), $new_version);
31 0         0 write_file($dist_file, $changed_file);
32              
33             # change other modules
34 0 0       0 if ($self->{change_pod_version_all}) {
35 0         0 for my $module ( File::Find::Rule->name('*.pm')->in('lib') ) {
36 0 0       0 next if ($module eq $dist_file); # already treated
37 0         0 my $version = $self->_version_from_file($module);
38 0 0 0     0 next unless (defined $version && length $version);
39              
40 0         0 my $changed_module = $self->_change_pod_version(slurp($module), $version);
41 0         0 write_file($module, $changed_module);
42             }
43             }
44              
45              
46 0         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             {
55 0     0   0 my $self = shift;
56 0         0 my $file = shift;
57              
58 0 0       0 open my $fh, '<', $file
59             or die "Failed to open $file: $!\n";
60              
61 0         0 while (<$fh>) {
62 0 0       0 return $2 if /\$VERSION\s*=\s*([\'\"])(.+?)\1/;
63             }
64             }
65              
66             ################################################################################
67             sub _change_pod_version {
68 5     5   1391 my ($self, $file_content, $new_version) = @_;
69              
70             # if we find a VERSION section, we change version, otherwise add one
71 5 100       24 if ($file_content =~ /^=head\d VERSION/m) {
72              
73             # replace version
74 3 100       48 if ($file_content !~ s/(^=head\d VERSION[^\d=]*)[\d._]+/$1$new_version/sm) {
75 1         7 die ('there is a POD VERSION section, but the version cannot be parsed');
76             }
77              
78             } else {
79              
80 2         19 my $version = "=head1 VERSION\n\n$new_version\n\n";
81             # add it after NAME section, everybody has one, right?
82 2 100       27 if ($file_content !~ s/(^=head\d NAME.*?(?=^=))/$1$version/sm) {
83 1         9 die ('trying to add a POD VERSION section after NAME Section, but there is none');
84             }
85              
86             }
87              
88 3         13 return $file_content;
89             }
90              
91             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 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<< >>
165              
166             =head1 BUGS
167              
168             Please report any bugs or feature requests to C, or through
169             the web interface at L. 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
186              
187             =item * AnnoCPAN: Annotated CPAN documentation
188              
189             L
190              
191             =item * CPAN Ratings
192              
193             L
194              
195             =item * Search CPAN
196              
197             L
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