File Coverage

blib/lib/ShipIt/ProjectType/Perl.pm
Criterion Covered Total %
statement 87 131 66.4
branch 12 44 27.2
condition n/a
subroutine 23 27 85.1
pod 1 6 16.6
total 123 208 59.1


line stmt bran cond sub pod time code
1             package ShipIt::ProjectType::Perl;
2 2     2   39875 use strict;
  2         4  
  2         213  
3 2     2   11 use base 'ShipIt::ProjectType';
  2         2  
  2         942  
4 2     2   11 use File::Spec;
  2         4  
  2         49  
5 2     2   11 use ShipIt::Util qw(slurp write_file);
  2         4  
  2         90  
6 2     2   1139 use ShipIt::ProjectType::Perl::MakeMaker;
  2         3  
  2         59  
7 2     2   863 use ShipIt::ProjectType::Perl::ModuleBuild;
  2         3  
  2         1522  
8              
9             # factory when called directly.
10             # returns undef if not a perl project, otherwise returns
11             # ::MakeMaker or ::ModuleBuild instance.
12             sub new {
13 4     4 0 26 my ($class) = @_;
14 4 100       13 if ($class eq "ShipIt::ProjectType::Perl") {
15 2 50       21 return ShipIt::ProjectType::Perl::ModuleBuild->new if -e "Build.PL";
16 2 50       40 return ShipIt::ProjectType::Perl::MakeMaker->new if -e "Makefile.PL";
17 0         0 return undef;
18             }
19 2         7 return bless {}, $class;
20             }
21              
22             # fields:
23             # version -- if defined, cached current version
24             # ver_from -- if Makefile.PL says so, what file our $VERSION comes from
25              
26             sub current_version {
27 0     0 0 0 my $self = shift;
28 0 0       0 return $self->{version} if defined $self->{version};
29              
30 0 0       0 if (-e "Build.PL") {
31 0         0 return $self->{version} = $self->current_version_from_buildpl;
32             } else {
33 0         0 return $self->{version} = $self->current_version_from_makefilepl;
34             }
35             }
36              
37             sub current_version_from_makefilepl {
38 0     0 0 0 my $self = shift;
39 0 0       0 open (my $fh, "Makefile.PL") or die "Can't open Makefile.PL: $!\n";
40 0         0 while (<$fh>) {
41             # MakeMaker
42 0 0       0 if (/VERSION_FROM.+([\'\"])(.+?)\1/) {
43 0         0 $self->{ver_from} = $2;
44 0         0 last;
45             }
46             # Module::Install
47 0 0       0 if (/(?:(?:all|version)_from|reference_module)(?:\s*\(|\s+)([\'\"])(.+?)\1/) {
48 0         0 $self->{ver_from} = $2;
49 0         0 last;
50             }
51 0 0       0 if (/\bVERSION\b.+([\'\"])(.+?)\1/) {
52 0         0 return $2;
53             }
54             }
55 0         0 close($fh);
56 0         0 return $self->version_from_file;
57             }
58              
59             sub current_version_from_buildpl {
60 0     0 0 0 my $self = shift;
61 0 0       0 open (my $fh, "Build.PL") or die "Can't open Build.PL: $!\n";
62 0         0 while (<$fh>) {
63 0 0       0 if (/\bdist_version_from\b.+([\'\"])(.+?)\1/) {
64 0         0 $self->{ver_from} = $2;
65 0         0 last;
66             }
67 0 0       0 if (/\bmodule_name\b.+([\'\"])(.+?)\1/) {
68 0         0 $self->{ver_from} = $self->_module_to_file($2);
69             # no last since we prefer dist_version_from
70             }
71 0 0       0 if (/\bdist_version\b.+([\'\"])(.+?)\1/) {
72 0         0 return $2;
73             }
74             }
75 0         0 close($fh);
76 0         0 return $self->version_from_file;
77             }
78              
79             sub _module_to_file {
80 0     0   0 my ($self, $mod) = @_;
81              
82 0         0 my @parts = split /::/, $mod;
83 0         0 $parts[-1] .= q{.pm};
84              
85 0 0       0 unshift @parts, 'lib' if -d 'lib';
86              
87 0         0 return File::Spec->catfile(@parts);
88             }
89              
90             sub _versioncode_from_string {
91 114     114   4307 my ($self, $string) = @_;
92              
93 114 100       466 if ($string =~ /
94             (
95             ( use \s* version \s* ; \s* )?
96             (our)? \s* \$VER SION \s* = \s* # trick PAUSE from parsing this line
97             (
98             ['"] v?[\d\.\_]+ ['"]
99             | q{1,2}\( \s* v?[\d\.\_]+ \s* \)
100             | [\d\.\_]+
101             | qv\( \s* ['"] v? [\d\.\_]+ ['"] \s* \)
102             )
103             )
104             /xms) {
105 39         135 return $1;
106             }
107              
108 75         206 return 0;
109             }
110              
111             # returns $VERSION from a file, assuming $self->{ver_from} is already set
112             sub version_from_file {
113 21     21 0 1875 my $self = shift;
114 21 50       52 my $file = $self->{ver_from} or die "no ver_from set";
115 21 50       565 open (my $fh, $file) or die "Failed to open $file: $!\n";
116 21         211 while (my $line = <$fh>) {
117 93 100       121 if (my $versionpart = $self->_versioncode_from_string($line)) {
118 21         22 eval {
119             package __ShipIt_Temp_Package;
120 2     2   12 use vars qw($VERSION);
  2         3  
  2         553  
121 1     1   577 eval $versionpart;
  1     1   1735  
  1     1   6  
  1     1   5  
  1     1   1  
  1     1   4  
  1     1   6  
  1     1   2  
  1     1   5  
  1     1   4  
  1     1   1  
  1     1   4  
  1         7  
  1         2  
  1         5  
  1         5  
  1         2  
  1         4  
  1         4  
  1         1  
  1         4  
  1         4  
  1         2  
  1         4  
  1         5  
  1         41  
  1         8  
  1         4  
  1         1  
  1         5  
  1         6  
  1         1  
  1         6  
  1         4  
  1         1  
  1         4  
  21         1296  
122             };
123 21 50       200 next if $@;
124 21         373 return $__ShipIt_Temp_Package::VERSION;
125             }
126             }
127 0         0 die "No \$VERSION found in file $file\nMaybe, you forgot to quote \$VERSION?";
128             }
129              
130             sub update_version {
131 7     7 1 1774 my ($self, $newver) = @_;
132              
133 7 50       21 if (my $file = $self->{ver_from}) {
134 7         24 my $contents = slurp($file);
135              
136 7         14 my $versionpart = $self->_versioncode_from_string($contents);
137 7         7 my $newversionpart = $versionpart;
138 7         12 my $version_withoutv = $self->{version};
139 7         34 $version_withoutv =~ s/^v//;
140 7         133 $newversionpart =~ s/ v? $version_withoutv /$newver/xms;
141              
142 7         17 my ($x, $y) = (quotemeta($versionpart), $newversionpart);
143 7         68 $contents =~ s/$x/$y/;
144 7         21 write_file($file, $contents);
145              
146 7         24 return 1;
147             }
148              
149 0 0         if (-e "Makefile.PL") {
150 0           my $file = "Makefile.PL";
151 0           my $contents = slurp($file);
152 0 0         $contents =~ s/(\bVERSION\b.+)([\'\"])(.+?)\2/$1$2$newver$2/
153             or die "Failed to replace VERSION in MakeFile.PL\n";
154 0           write_file($file, $contents);
155 0           return 1;
156              
157             }
158              
159 0           die "perl update not done";
160             }
161              
162             1;