Makerelease/Step/Modify.pm untested

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 105 CC 7 SCAR 27.1
CC
7
Cov
0%
test
30.0
get_files
12.0
step
2.0
CRAP 15.1
1 package Makerelease::Step::Modify;
2  
3 use strict;
4 use Makerelease::Step;
5 use IO::File;
6  
7 our $VERSION = '0.1';
8  
9 our @ISA=qw(Makerelease::Step);
10  
11 sub get_files {
12 my ($self, $modify) = @_;
13  
14 my @files;
15  
16 my $files = $modify->{'files'};
17  
18 # XXX: fix this... should be more accepting
19 if (ref($files) eq 'ARRAY') {
20 $files = $files->[0];
21 }
22 if (ref($files) eq 'HASH') {
23 foreach my $fileref (@{$self->ensure_array($files->{'file'})}) {
24 push @files, glob($self->expand_parameters($fileref));
25 }
26 } else {
27 foreach my $fileref ($self->expand_parameters($modify->{'files'})) {
28 push @files, glob($fileref);
29 }
30 }
31  
32 return \@files;
33 }
34  
35 sub test {
36 my ($self, $step, $parentstep, $counter) = @_;
37 return 1 if ($self->require_piece($step, $parentstep, $counter,
38 'modifications', 'modify'));
39 my $result = 0;
40 for (my $i = 0; $i <= $#{$step->{'modifications'}[0]{'modify'}}; $i++) {
41 $result =
42 $self->WARN($step, "modification #$i contains no find clause")
43 if (!exists($step->{'modifications'}[0]{'modify'}[$i]{'find'}) ||
44 $step->{'modifications'}[0]{'modify'}[$i]{'find'} eq '');
45 $result =
46 $self->WARN($step, "modification #$i contains no replace clause")
47 if (!exists($step->{'modifications'}[0]{'modify'}[$i]{'replace'}) ||
48 $step->{'modifications'}[0]{'modify'}[$i]{'replace'} eq '');
49 $result = 1
50 if ($self->require_piece($step->{'modifications'}[0]{'modify'}[$i],
51 $parentstep, $counter, 'files', 'file'));
52 }
53 }
54  
55 sub step {
56 my ($self, $step, $parentstep, $counter) = @_;
57  
58 foreach my $modify (@{$step->{'modifications'}[0]{'modify'}}) {
59 my $files = $self->get_files($modify);
60 my $findregex = $self->expand_parameters($modify->{'find'});
61 my $replaceregex = $self->expand_parameters($modify->{'replace'});
62  
63 my $asub = eval "sub { s/$findregex/$replaceregex/; }";
64  
65 foreach my $file (@$files) {
66  
67 $self->output("modifying $file");
68  
69 my $in = new IO::File();
70 my $out = new IO::File();
71 $in->open("<$file") || die "ack: couldn't open $file";
72 $out->open(">$file.mrnew") || die "ack: couldn't open $file.mrnew";
73  
74 while (<$in>) {
75 $asub->();
76 print $out $_;
77 }
78  
79 $in->close();
80 $out->close();
81
82 rename("$file","$file.bak");
83 rename("$file.mrnew","$file");
84 }
85 }
86 }
87  
88 sub document_step {
89 my ($self, $step, $parentstep, $counter) = @_;
90  
91 foreach my $modify (@{$step->{'modifications'}[0]{'modify'}}) {
92 my $findregex = $self->expand_parameters($modify->{'find'});
93 my $replaceregex = $self->expand_parameters($modify->{'replace'});
94 $self->output("Modifying files:");
95 $self->output(" replacing: '$findregex' with: '$replaceregex'\n");
96 $self->output(" files: glob=" .
97 $self->expand_parameters($modify->{'files'}));
98 my $files = $self->get_files($modify);
99 foreach my $file (@$files) {
100 $self->output(" " . $file);
101 }
102 $self->output("");
103 }
104 }
105  
106 1;
107