File Coverage

lib/Dist/Zilla/Plugin/Author/KENTNL/MinimumPerl.pm
Criterion Covered Total %
statement 23 71 32.3
branch 0 30 0.0
condition 0 3 0.0
subroutine 8 13 61.5
pod 1 1 100.0
total 32 118 27.1


line stmt bran cond sub pod time code
1 1     1   972 use 5.006; # our
  1         3  
2 1     1   5 use strict;
  1         2  
  1         20  
3 1     1   5 use warnings;
  1         2  
  1         73  
4              
5             package Dist::Zilla::Plugin::Author::KENTNL::MinimumPerl;
6              
7             # ABSTRACT: The MinimumPerl Plugin with a few hacks
8              
9             our $VERSION = '2.025010';
10              
11             our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
12              
13              
14              
15              
16              
17              
18              
19              
20              
21              
22              
23              
24              
25 1     1   822 use Moose qw( has extends override around );
  1         467513  
  1         9  
26 1     1   6779 use Dist::Zilla::Util::ConfigDumper qw( config_dumper );
  1         1255  
  1         4  
27 1     1   927 use Dist::Zilla::Plugin::MinimumPerl 1.004;
  1         668115  
  1         55  
28             extends 'Dist::Zilla::Plugin::MinimumPerl';
29 1     1   12 use namespace::autoclean;
  1         2  
  1         12  
30              
31             around dump_config => config_dumper( __PACKAGE__, { attrs => [ 'detected_perl', 'fiveten' ] } );
32              
33             has 'detected_perl' => (
34             is => 'rw',
35             isa => 'Object',
36             lazy_build => 1,
37             );
38              
39             has 'fiveten' => (
40             isa => 'Bool',
41             is => 'rw',
42             default => sub { undef },
43             );
44              
45             override register_prereqs => sub {
46             my ($self) = @_;
47              
48             my $minperl = $self->minperl;
49              
50             $self->log_debug( [ 'Minimum Perl is v%s', $minperl ] );
51             $self->zilla->register_prereqs( { phase => 'runtime' }, perl => $minperl->stringify, );
52             };
53              
54 1     1   218 no Moose;
  1         2  
  1         8  
55             __PACKAGE__->meta->make_immutable;
56              
57             sub _3part_check {
58 0     0     my ( $self, $file, $pmv, $minver ) = @_;
59 0           my $perl_required = version->parse('5.10.0');
60 0 0         return $minver if $minver >= $perl_required;
61 0           my $document = $pmv->Document;
62             my $version_declaration = sub {
63 0 0   0     $_[1]->isa('PPI::Token::Symbol') and $_[1]->content =~ /::VERSION\z/msx;
64 0           };
65             my $version_match = sub {
66 0 0   0     'PPI::Token::Quote::Single' eq $_[1]->class and $_[1]->parent->find_any($version_declaration);
67 0           };
68 0 0         my (@versions) = @{ $document->find($version_match) || [] };
  0            
69 0           for my $versiondecl (@versions) {
70             next
71 0 0         if $minver >= $perl_required;
72             ## no critic (ProhibitStringyEval)
73 0           my $v = eval $versiondecl;
74 0 0         if ( $v =~ /\A\d+[.]\d+[.]/msx ) {
75 0           $minver = $perl_required;
76 0           $self->log_debug( [ 'Upgraded to %s due to %s having x.y.z', $minver, $file->name ] );
77             }
78             }
79 0           return $minver;
80             }
81              
82             sub _build_detected_perl {
83 0     0     my ($self) = @_;
84 0           my $minver;
85              
86 0           foreach my $file ( @{ $self->found_files } ) {
  0            
87              
88             # TODO should we scan the content for the perl shebang?
89             # Only check .t and .pm/pl files, thanks RT#67355 and DOHERTY
90 0 0         next unless $file->name =~ /[.](?:t|p[ml])\z/imsx;
91              
92             # TODO skip "bad" files and not die, just warn?
93 0           my $pmv = Perl::MinimumVersion->new( \$file->content );
94 0 0         if ( not defined $pmv ) {
95 0           $self->log_fatal( [ 'Unable to parse \'%s\'', $file->name ] );
96             }
97 0           my $ver = $pmv->minimum_version;
98 0 0         if ( not defined $ver ) {
99 0           $self->log_fatal( [ 'Unable to extract MinimumPerl from \'%s\'', $file->name ] );
100             }
101 0 0 0       if ( ( not defined $minver ) or $ver > $minver ) {
102 0           $self->log_debug( [ 'Increasing perl dep to %s due to %s', $ver, $file->name ] );
103 0           $minver = $ver;
104             }
105 0 0         if ( $self->fiveten ) {
106 0           $ver = $self->_3part_check( $file, $pmv, $minver );
107 0 0         if ( "$ver" ne "$minver" ) {
108 0           $self->log_debug( [ 'Increasing perl dep to %s due to 3-part in %s', $ver, $file->name ] );
109 0           $minver = $ver;
110             }
111             }
112             }
113              
114             # Write out the minimum perl found
115 0 0         if ( defined $minver ) {
116 0           return $minver;
117             }
118 0           return $self->log_fatal('Found no perl files, check your dist?');
119             }
120              
121              
122              
123              
124              
125              
126              
127             sub minperl {
128 0     0 1   require version;
129 0           my $self = shift;
130 0 0         if ( not $self->_has_perl ) {
131 0           return $self->detected_perl;
132             }
133 0           my ($x) = version->parse( $self->perl );
134 0           my ($y) = $self->detected_perl;
135 0 0         if ( $x > $y ) {
136 0           return $x;
137             }
138 0           return $y;
139             }
140              
141             1;
142              
143             __END__
144              
145             =pod
146              
147             =encoding UTF-8
148              
149             =head1 NAME
150              
151             Dist::Zilla::Plugin::Author::KENTNL::MinimumPerl - The MinimumPerl Plugin with a few hacks
152              
153             =head1 VERSION
154              
155             version 2.025010
156              
157             =head1 METHODS
158              
159             =head2 C<minperl>
160              
161             Returns the maximum of either the version requested for Perl, or the version detected for Perl.
162              
163             =begin MetaPOD::JSON v1.1.0
164              
165             {
166             "namespace":"Dist::Zilla::Plugin::Author::KENTNL::MinimumPerl",
167             "interface":"class",
168             "inherits":"Dist::Zilla::Plugin::MinimumPerl"
169             }
170              
171              
172             =end MetaPOD::JSON
173              
174             =head1 AUTHOR
175              
176             Kent Fredric <kentnl@cpan.org>
177              
178             =head1 COPYRIGHT AND LICENSE
179              
180             This software is copyright (c) 2015 by Kent Fredric <kentfredric@gmail.com>.
181              
182             This is free software; you can redistribute it and/or modify it under
183             the same terms as the Perl 5 programming language system itself.
184              
185             =cut