App/sigfix.pm untested

filter: Statementstmt 0.0% 0 / 89 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 156 CC 10 SCAR 26.4
CC
10
Cov
0%
options
30.0
process_source
20.0
process_file
6.0
CRAP 14.0
1 package App::sigfix;
2  
3 use strictures 2;
4 use Babble::Plugin::CoreSignatures;
5 use Babble::Match;
6 use Getopt::Long;
7  
8 our $VERSION = '0.001001';
9  
10 sub stripspace {
11 my ($text) = @_;
12 $text =~ /^(\s+)/ && $text =~ s/^$1//mg;
13 $text;
14 }
15  
16 sub usage {
17 print <<'USAGE';
18 sigfix [OPTIONS] [FILES]
19  
20 sigfix -t signatures perl526file >perl528file
21 sigfix -t oldsignatures perl528file >perl526file
22 sigfix -t plain perlfile >perl58file
23  
24 sigfix -i -t signatures upgradethisfile
25  
26 sigfix -t signatures --pmc lib/My/Module.pm >lib/My/Module.pmc
27 sigfix -i -t signatures --pmc lib/My/Module.pm
28  
29 sigfix -i --pmc eval $(find lib -name '*.pm') # multi-target fat pmc
30  
31 Options:
32  
33 -t, --target Target: 'signatures', 'oldsignatures', 'plain'
34 -i, --inplace Modify target file(s) in place
35 --pmc Generate .pmc (without -t, triple target pmc)
36 -h This usage message
37 USAGE
38  
39 exit(255);
40 }
41  
42 {
43 my $cs = Babble::Plugin::CoreSignatures->new;
44  
45 my @target_selectors = (
46 [ 'signatures', '($] >= 5.020 and $] < 5.022) or $] >= 5.028' ],
47 [ 'oldsignatures', '$] >= 5.022 and $] < 5.028' ],
48 [ 'plain' ],
49 );
50  
51 sub process_source {
52 my ($source, $opt) = @_;
53  
54 if (my $target = $opt->{target}) {
55  
56 my $top = Babble::Match->new(
57 top_rule => 'Document',
58 text => $source,
59 );
60  
61 my $transform = "transform_to_${target}";
62  
63 unless ($cs->can($transform)) {
64 print STDERR "Invalid transform target ${target}";
65 usage;
66 }
67 $cs->$transform($top);
68  
69 return $top->text;
70 }
71  
72 # must be in pmc mode without target
73 my $preamble = stripspace <<' PREAMBLE';
74 # This code generated by App::sigfix
75 BEGIN {
76 my %options;
77 PREAMBLE
78  
79 my @segments;
80  
81 foreach my $target (map $_->[0], @target_selectors) {
82  
83 my $top = Babble::Match->new(
84 top_rule => 'Document',
85 text => $source,
86 );
87  
88 $cs->${\"transform_to_${target}"}($top);
89  
90 my $data = $top->text;
91  
92 # indent for heredoc and ensure trailing newline
93 s/^/ /mg, s/(?<!\n)\z/\n/ for $data;
94  
95 my $doc_name = uc join '_', 'CODE_FOR', $target;
96  
97 push @segments, join '',
98 qq{ \$options{'${target}'} = '#line '.(1+__LINE__).' "'.__FILE__."\\"\\n".<<'$doc_name';\n},
99 $data, $doc_name, "\n";
100 }
101  
102 my $select = join "\n",
103 ' my $chosen =',
104 (map {
105 my ($choice, $cond) = @$_;
106 if ($cond) {
107 " $cond ? '$choice' :"
108 } else {
109 " '$choice';"
110 }
111 } @target_selectors),
112 '';
113 my $postamble = stripspace <<' POSTAMBLE';
114 require Filter::Util::Call;
115 Filter::Util::Call::filter_add(sub {
116 Filter::Util::Call::filter_del();
117 1 while Filter::Util::Call::filter_read();
118 $_ = $options{$chosen};
119 });
120 POSTAMBLE
121 $postamble =~ s/^/ /mg;
122 return join '',
123 $preamble,
124 @segments,
125 $select,
126 $postamble,
127 "}\n0; # should never get here\n";
128 }
129 }
130  
131 sub process_file {
132 my ($file, $opt) = @_;
133  
134 my $source = do {
135 open my $in, '<', $file or die "Couldn't open ${file} for read: $!\n";
136 local $/; <$in>;
137 };
138  
139 # Process first so if that fails, we didn't eat the original file
140 my $processed = process_source $source, $opt;
141  
142 # ... or overwrite the old .pmc
143 my $out_file = $file.($opt->{pmc} ? 'c' : '');
144  
145 open my $out, '>', $out_file
146 or die "Couldn't open ${out_file} for write: $!\n";
147  
148 print $out $processed;
149  
150 return;
151 }
152  
153 sub options {
154 my %opt;
155  
156 GetOptions(
157 't|target=s', \$opt{target},
158 'i|inplace', \$opt{inplace},
159 'pmc', \$opt{pmc},
160 'help', \$opt{help}
161 );
162  
163 if ($opt{help}) {
164 usage;
165 }
166 if (not $opt{target} and not $opt{pmc}) {
167 print STDERR "Must supply at least one of -t/--target or --pmc\n";
168 usage;
169 }
170 if ($opt{inplace} and not @ARGV) {
171 print STDERR "In-place operation requires a list of files\n";
172 usage;
173 }
174 if ($opt{inplace} and $opt{pmc} and grep !/\.pm$/, @ARGV) {
175 print STDERR "In-place pmc generation requires a list of .pm files\n";
176 usage;
177 }
178 return \%opt;
179 }
180  
181 sub main {
182 my $opt = options;
183  
184 if ($opt->{inplace}) {
185 foreach my $file (@ARGV) {
186 process_file $file, $opt;
187 }
188 return;
189 }
190  
191 (my $source = do { local $/; <> }) ||= '';
192 print process_source $source, $opt;
193  
194 return;
195 }
196  
197 1;
198  
199 __END__