File Coverage

blib/lib/Dist/Zilla/Role/InsertVersion.pm
Criterion Covered Total %
statement 32 39 82.0
branch 12 20 60.0
condition 4 11 36.3
subroutine 6 6 100.0
pod 0 1 0.0
total 54 77 70.1


line stmt bran cond sub pod time code
1 4     4   2103 use strict;
  4         11  
  4         120  
2 4     4   21 use warnings;
  4         8  
  4         241  
3             package # hide from PAUSE
4             Dist::Zilla::Role::InsertVersion;
5             # vim: set ts=8 sts=4 sw=4 tw=115 et :
6              
7             our $VERSION = '0.008';
8              
9 4     4   23 use Moose::Role;
  4         10  
  4         33  
10 4     4   19365 use Scalar::Util 'blessed';
  4         10  
  4         220  
11 4     4   26 use namespace::autoclean;
  4         9  
  4         37  
12              
13             =pod
14              
15             =for Pod::Coverage insert_version
16              
17             =cut
18              
19             has _ourpkgversion => (
20             is => 'ro', isa => 'Dist::Zilla::Plugin::OurPkgVersion',
21             lazy => 1,
22             default => sub {
23             my $self = shift;
24             (my $name = blessed($self)) =~ s/^Dist::Zilla::Plugin:://;
25             Dist::Zilla::Plugin::OurPkgVersion->new(
26             zilla => $self->zilla,
27             plugin_name => 'OurPkgVersion, via ' . $name,
28             );
29             },
30             predicate => '_used_ourpkgversion',
31             );
32             has _pkgversion => (
33             is => 'ro', isa => 'Dist::Zilla::Plugin::PkgVersion',
34             lazy => 1,
35             default => sub {
36             my $self = shift;
37             require Dist::Zilla::Plugin::PkgVersion;
38             Dist::Zilla::Plugin::PkgVersion->VERSION('5.010'); # one line, no braces
39             (my $name = blessed($self)) =~ s/^Dist::Zilla::Plugin:://;
40             Dist::Zilla::Plugin::PkgVersion->new(
41             zilla => $self->zilla,
42             plugin_name => 'PkgVersion, via ' . $name,
43             die_on_existing_version => 1,
44             die_on_line_insertion => 0,
45             );
46             },
47             predicate => '_used_pkgversion',
48             );
49              
50             sub insert_version
51             {
52 16     16 0 343 my ($self, $file, $version, $trial) = @_;
53              
54             # $version is the bumped post-release version; fool the plugins into using
55             # it rather than the version we released with
56 16         497 my $release_version = $self->zilla->version;
57 16 100       833 $self->zilla->version($version) if $release_version ne $version;
58              
59             MUNGE_FILE: {
60 16         1062 my $replaced;
  16         42  
61 16         100 my $content = $file->content;
62 16 50 33     9318 if ($content =~ /\x{23} VERSION/ and eval { require Dist::Zilla::Plugin::OurPkgVersion; 1 })
  0         0  
  0         0  
63             {
64 0         0 my $orig_content = $content;
65 0         0 $self->_ourpkgversion->munge_file($file);
66 0         0 $content = $file->content;
67 0 0       0 last MUNGE_FILE if $content eq $orig_content;
68              
69             # [OurPkgVersion] uses $self->zilla->is_trial, which we cannot override
70 0 0 0     0 $replaced =
    0          
71             ($self->zilla->is_trial xor $trial) ? $content =~ s/ # TRIAL VERSION//mg
72             : $trial ? $content =~ s/ # TRIAL VERSION/ # TRIAL/mg
73             : $content =~ s/ # VERSION$//mg;
74             }
75             else
76             {
77 16         53 my $orig_content = $content;
78 16         689 $self->_pkgversion->munge_perl($file);
79 16         370289 $content = $file->content;
80 16 100       1398 last MUNGE_FILE if $content eq $orig_content;
81              
82             # [PkgVersion] uses $self->zilla->is_trial, which we cannot override
83 12 100 75     401 my $trial_str = ($self->zilla->is_trial xor $trial) ? ' # TRIAL' : '';
84              
85 12         1005 $replaced = $content =~ s/^\$\S+::(VERSION = '$version';)$trial_str/our \$$1/mg;
86             }
87              
88 12 100       124 $self->log(
    50          
89             !$replaced
90             ? [ q{failed to insert our $VERSION = '%s'; into %s}, $version, $file->name ]
91             : $replaced == 1
92             ? [ 'inserted $VERSION statement into %s', $file->name ]
93             : [ 'inserted %d $VERSION statements into %s', $replaced, $file->name ]
94             );
95              
96 12         6487 $file->content($content);
97             }
98              
99             # restore zilla version, in case other plugins still need it
100 16 100       3359 $self->zilla->version($release_version) if $release_version ne $version;
101              
102 16         1239 return 1;
103             }
104              
105             around dump_config => sub
106             {
107             my ($orig, $self) = @_;
108             my $config = $self->$orig;
109              
110             $config->{'Dist::Zilla::Plugin::OurPkgVersion'} = $self->_ourpkgversion->dump_config
111             if $self->_used_ourpkgversion;
112             $config->{'Dist::Zilla::Plugin::PkgVersion'} = $self->_pkgversion->dump_config
113             if $self->_used_pkgversion;
114              
115             return $config;
116             };
117              
118             1;