File Coverage

blib/lib/Dist/Zilla/Plugin/ReversionOnRelease.pm
Criterion Covered Total %
statement 59 74 79.7
branch 12 22 54.5
condition 1 3 33.3
subroutine 14 15 93.3
pod 0 6 0.0
total 86 120 71.6


line stmt bran cond sub pod time code
1             package Dist::Zilla::Plugin::ReversionOnRelease;
2              
3 2     2   3709019 use strict;
  2         3  
  2         54  
4 2     2   49 use 5.008_005;
  2         4  
5             our $VERSION = '0.06';
6              
7 2     2   7 use version;
  2         3  
  2         11  
8 2     2   873 use Version::Next;
  2         1935  
  2         10  
9 2     2   242 use Moose;
  2         3  
  2         11  
10             with(
11             'Dist::Zilla::Role::FileMunger',
12             'Dist::Zilla::Role::FileFinderUser' => {
13             default_finders => [ ':InstallModules', ':ExecFiles' ],
14             },
15             );
16              
17             has 'prompt' => (is => 'ro', isa => 'Bool', default => 0);
18              
19              
20             qr/ ( (?i: Revision: \s+ ) | v | )
21             ( \d+ (?: [.] \d+)* )
22             ( (?: _ \d+ )? ) /x;
23             # from perl-reversion
24             my $VersionRegexp =
25             qr{ ^ (
26             .*? [\$\*] (?: \w+ (?: :: | ' ) )* VERSION \s* = \D*?
27             |
28             \s* package \s+ [\w\:\']+ \s+
29             )
30             ( (?i: Revision: \s+ ) | v | )
31             ( \d+ (?: [.] \d+)* )
32             ( (?: _ \d+ )? )
33             ( .* ) $ }x;
34              
35             sub munge_files {
36 10     10 0 288896 my $self = shift;
37              
38 10 50       40 return unless $ENV{DZIL_RELEASING};
39              
40 10         31 my $version = $self->reversion;
41              
42 10 50       365 if ($self->prompt) {
43             my $given_version = $self->zilla->chrome->prompt_str(
44             "Next release version? ", {
45             default => $version,
46             check => sub {
47 0     0   0 eval { version->parse($_[0]); 1 },
  0         0  
  0         0  
48             },
49             },
50 0         0 );
51              
52 0         0 $version = $given_version;
53             }
54              
55 10         13 $self->munge_file($_, $version) for @{ $self->found_files };
  10         49  
56 10         2123 $self->zilla->version($version);
57              
58 10         729 return;
59             }
60              
61             sub reversion {
62 10     10 0 15 my $self = shift;
63              
64 10         283 my $new_ver = $self->zilla->version;
65              
66 10 100       314 if ($ENV{V}) {
    50          
67 4         26 $self->log("Overriding VERSION to $ENV{V}");
68 4         841 $new_ver = $ENV{V};
69             } elsif ($self->is_released($new_ver)) {
70 6         32 $self->log_debug("$new_ver is released. Bumping it");
71 6         314 $new_ver = Version::Next::next_version($new_ver);
72             } else {
73 0         0 $self->log_debug("$new_ver is not released yet. No need to bump");
74             }
75              
76 10         402 $new_ver;
77             }
78              
79             sub is_released {
80 6     6 0 12 my($self, $new_ver) = @_;
81              
82 6         16 my $changes_file = 'Changes';
83              
84 6 50       99 if (! -e $changes_file) {
85 6         38 $self->log("No $changes_file found in your directory: Assuming $new_ver is released.");
86 6         1310 return 1;
87             }
88              
89 0         0 my $changelog = Dist::Zilla::File::OnDisk->new({ name => $changes_file });
90              
91 0         0 grep /^$new_ver(?:-TRIAL)?(?:\s+|$)/,
92             split /\n/, $changelog->content;
93             }
94              
95             sub filter_pod {
96 10     10 0 14 my($self, $cb) = @_;
97              
98 10         12 my $in_pod;
99              
100             return sub {
101 38     38   38 my $line = shift;
102              
103 38 50       53 if ($in_pod) {
104 0 0       0 /^=cut/ and do { $in_pod = 0; return };
  0         0  
  0         0  
105             } else {
106 38 50       70 /^=(?!cut)/ and do { $in_pod = 1; return };
  0         0  
  0         0  
107 38         52 return $cb->($line);
108             }
109 10         38 };
110             }
111              
112             sub rewrite_version {
113 10     10 0 34 my($self, $file, $pre, $ver, $post, $new_ver) = @_;
114              
115 10         261 my $current = $self->zilla->version;
116              
117 10 50 33     293 if (defined $current && $current ne $ver) {
118 0         0 $self->log([ 'Skipping: "%s" has different $VERSION: %s != %s', $file->name, $ver, $current ]);
119 0         0 return $pre . $ver . $post;
120             }
121              
122 10         35 $self->log([ 'Bumping $VERSION in %s to %s', $file->name, "$new_ver" ]);
123              
124 10         2823 return $pre . $new_ver . $post;
125             }
126              
127             sub munge_file {
128 10     10 0 12048 my($self, $file, $new_ver) = @_;
129              
130             my $scanner = $self->filter_pod(sub {
131 38     38   277 s{$VersionRegexp}{
132 10         64 $self->rewrite_version($file, $1, $2.$3.$4, $5, $new_ver)
133             }e;
134 10         80 });
135              
136 10         11 my $munged;
137              
138 10         40 my @content = split /\n/, $file->content, -1;
139 10         3707 for (@content) {
140 38 100       51 $scanner->($_) && $munged++;
141             }
142              
143 10 50       57 $file->content(join("\n", @content)) if $munged;
144             }
145              
146             __PACKAGE__->meta->make_immutable;
147 2     2   9439 no Moose;
  2         3  
  2         11  
148             1;
149             __END__
150              
151             =encoding utf-8
152              
153             =head1 NAME
154              
155             Dist::Zilla::Plugin::ReversionOnRelease - Bump and reversion $VERSION on release
156              
157             =head1 SYNOPSIS
158              
159             [VersionFromModule]
160             [ReversionOnRelease]
161             prompt = 1
162             [CopyFilesFromRelease]
163             match = \.pm$
164              
165             =head1 DESCRIPTION
166              
167             This is a Dist::Zilla plugin that bumps version (a la C<perl-reversion
168             -bump>) in-place with the .pm files inside C<lib>. You should most
169             likely use this plugin in combination with
170             L<Dist::Zilla::Plugin::VersionFromModule> so that current VERSION is
171             taken out of your main module, and then the released file is written
172             back after the release with L<Dist::Zilla::Plugin::CopyFilesFromRelease>.
173              
174             Unlike C<perl-reversion>, this module uses L<Version::Next> to get
175             more naturally incremented version, instead of a little strict 3-digit
176             rules in L<Perl::Version>.
177              
178             You B<should not> use this plugin with any code munging or Pod::Weaver
179             plugins.
180              
181             By default, this plugin bumps version by the smallest possible
182             increase - if you have 0.001, the next version is 0.002. You can
183             override that by either running the plugin with C<prompt> option to
184             give the desired value from the prompt, or by setting the environment
185             variable C<V>:
186              
187             > V=1.001000 dzil release
188              
189             =head1 AUTHOR
190              
191             Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
192              
193             =head1 COPYRIGHT
194              
195             Copyright 2013- Tatsuhiko Miyagawa
196              
197             =head1 LICENSE
198              
199             This library is free software; you can redistribute it and/or modify
200             it under the same terms as Perl itself.
201              
202             =head1 SEE ALSO
203              
204             =over 4
205              
206             =item * L<Dist::Milla>
207              
208             =item * L<Version::Next>
209              
210             =item * L<Dist::Zilla::Plugin::BumpVersion>
211              
212             =item * L<Dist::Zilla::Plugin::RewriteVersion> - also takes $VERSION from the main module; ensures all $VERSIONs are consistent
213              
214             =item * L<Dist::Zilla::Plugin::BumpVersionAfterRelease> - edits the $VERSION in the repository code to reflect the new version, after release
215              
216             =item * L<Dist::Zilla::Plugin::RewriteVersion::Transitional> - like L<Dist::Zilla::Plugin::RewriteVersion>, but munges the version in if it was not already present
217              
218             =item * L<Dist::Zilla::Plugin::BumpVersionAfterRelease::Transitional> - like L<Dist::Zilla::Plugin::BumpVersionAfterRelease>, but also adds the $VERSION into the repository code if it was not already present
219              
220             =back
221              
222             =cut