File Coverage

blib/lib/ShipIt/Step/ChangeAllVersions.pm
Criterion Covered Total %
statement 76 78 97.4
branch 17 22 77.2
condition 4 6 66.6
subroutine 13 13 100.0
pod 1 5 20.0
total 111 124 89.5


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