File Coverage

blib/lib/Dist/Zilla/Plugin/ReversionOnRelease.pm
Criterion Covered Total %
statement 60 75 80.0
branch 12 22 54.5
condition 1 3 33.3
subroutine 14 15 93.3
pod 0 6 0.0
total 87 121 71.9


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