File Coverage

blib/lib/ShipIt/Step/ChangeAllVersions.pm
Criterion Covered Total %
statement 71 73 97.2
branch 17 22 77.2
condition 4 6 66.6
subroutine 12 12 100.0
pod 1 5 20.0
total 105 118 88.9


line stmt bran cond sub pod time code
1             package ShipIt::Step::ChangeAllVersions;
2              
3 3     3   230459 use 5.008_001;
  3         14  
  3         113  
4 3     3   17 use strict;
  3         5  
  3         87  
5 3     3   13 use warnings;
  3         10  
  3         151  
6              
7             our $VERSION = '1.001';
8              
9 3     3   785 use parent qw(ShipIt::Step);
  3         292  
  3         21  
10 3     3   5640 use Fatal qw(open close rename opendir closedir);
  3         32899  
  3         22  
11 3     3   6133 use File::Find qw(find);
  3         6  
  3         2958  
12              
13             my $looks_like_perl = qr/\. p(?: [ml]c? | od | erl ) \z/xms;
14              
15             sub collect_files {
16 3     3 0 18 my @files;
17              
18             # from subdir
19             find sub {
20 14 100 66 14   895 return if not ( -f $_ && not -l $_ );
21              
22 7 100       55 if($_ =~ $looks_like_perl) {
23 3         206 push @files, $File::Find::name;
24             }
25             else {
26 4         115 open my $in, '<', $_;
27 4         250 read $in, my($buff), 2; # I don't care whether it fail or not
28              
29 4 100 66     26 if($buff && $buff eq '#!'){
30 2         6 push @files, $File::Find::name;
31             }
32 4         118 close $in;
33             }
34 3         24 }, grep{ -d } qw(lib script bin);
  9         422  
35              
36             # from toplevel
37 3         215 opendir my $dirh, '.';
38 3         201 push @files, grep { ! -l $_ } grep{ $_ =~ $looks_like_perl } readdir($dirh);
  4         59  
  34         108  
39 3         78 closedir $dirh;
40              
41 3         71 return @files;
42             }
43              
44             sub run {
45 2     2 1 6541 my ($self, $state) = @_;
46              
47 2         12 my $dry_run = $state->dry_run;
48              
49 2 50       17 if($dry_run){
50 0         0 $self->log("*** DRY RUN, not actually updating versions.");
51             }
52              
53 2         9 my $current_version = quotemeta $state->pt->{version};
54 2         23 my $new_version = $state->version;
55              
56 2         20 foreach my $module ($self->collect_files) {
57 8         200 open my $in, '<', $module;
58 8         371 binmode $in;
59              
60 8         11 my $out;
61 8 50       22 if(!$dry_run){
62 8         189 open $out, '>', "$module.tmp";
63 8         708 binmode $out;
64             }
65              
66 8         13 my $need_replace = 0;
67              
68 8         103 while (<$in>) {
69              
70             # update $VERSION variable
71 90 100       436 if (s/(\$VERSION .+) \b $current_version \b/$1$new_version/xms) {
72 6         27 $self->{changed_version_variable}{$module}++;
73 6         28 $self->log("Update \$VERSION in $module.");
74 6         9 $need_replace++;
75             }
76              
77             # update the VERSION section which says,
78             # "This is Foo version $ver.",
79             # or "This document descrives Foo version $ver."
80 90 100       242 if (/\A =head1 \s+ VERSION\b/xms ... /\A =\w+/xms) {
81 30 100       208 if (s/(version \b .+) $current_version/$1$new_version/xms) {
82 6         20 $self->{changed_version_section}{$module}++;
83 6         27 $self->log("Update the VERSION section in $module.");
84 6         10 $need_replace++;
85             }
86             }
87              
88 90 50       467 print $out $_ if defined $out;
89             }
90              
91 8         205 close $in;
92              
93 8 50       170 next if $dry_run;
94              
95 8         212 close $out;
96              
97 8 50       505 if($need_replace){
98 8         194 rename $module => "$module~";
99 8         646 rename "$module.tmp" => $module;
100              
101 8         955 unlink "$module~";
102             }
103             else{
104 0         0 unlink "$module.tmp";
105             }
106             }
107              
108 2         14 return 1;
109             }
110              
111             sub changed_version_variable { # for testing
112 6     6 0 594 return $_[0]->{changed_version_variable};
113             }
114              
115             sub changed_version_section { # for testing
116 6     6 0 1014 return $_[0]->{changed_version_section};
117             }
118              
119             sub log {
120 12     12 0 25 my $self = shift;
121 12         68 print @_, "\n";
122             }
123              
124             1;
125             __END__