blib/lib/Module/Husbandry.pm

filter: Statementstmt 21.0% 115 / 547 Branchbran 10.3% 37 / 356 Conditioncond 2.2% 3 / 135 MC/DCmcdc 1.5% 1 / 66 Subroutinesub 34.0% 17 / 50 Podpod 94.7% 18 / 19 totaltotal 16.2% 191 / 1173 CC 251 SCAR 54.9
CC
251
Cov
15%
mvpm
870.0
cppm
506.0
rmpm
506.0
CRAP 241.7
↑ Summary
1 package Module::Husbandry;
2  
3 1 $VERSION = 0.002;
4  
5 1 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 @ISA = qw( Exporter );
7 1 @EXPORT_OK = qw(
8 cppm
9 install_file
10 mvpm
11 newpm
12 newpmbin
13 newpmdist
14 parse_cli
15 parse_module_specs
16 parse_dist_specs
17 reconfigure_dist
18 rmpm
19 skeleton_files
20 test_scripts_for
21 templates_for
22 usage
23 );
24 1 %EXPORT_TAGS = ( all => \@EXPORT_OK );
25  
26  
27 =head1 NAME
28  
29 Module::Husbandry - build and manage perl modules in a Perl module distribution
30  
31 =head1 SYNOPSIS
32  
33 =head1 DESCRIPTION
34  
35 =head1 FUNCTION
36  
37 =over
38  
39 =cut
40  
41 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use Fatal qw( mkdir close );
Subroutine BEGIN: called CC=1 SCAR=0.0
43 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
44  
45 0 ▶sub _x { ## "eXception"
Subroutine _x: not called CC=3 SCAR=24.8
46 0 ▶ my $options = ref $_[-1] ? pop : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] ? : 0 0 0
47  
48 0 ▶ if ( $options->{describe} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'describe'}) { } 0 0 0
49 0 warn @_;
50 }
51 else {
52 0 die @_;
53 }
54 }
55  
56 0 ▶sub _d { ## "describe". Return 1 if in describe-only mode.
Subroutine _d: not called CC=6 SCAR=37.4
57 0 my $options = pop;
58 0 my $msg = join "", @_;
59 0 ▶ 1 while chomp $msg;
Branch: 1 branch
BranchTrueFalseTotal
chomp $msg and do { '???' } 0 0 0
60 0 ▶ if ( defined $options->{_prog_name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"_prog_name"}) 0 0 0
61 0 ▶ my $sep = $options->{_prog_name_sep} || ":";
Condition0%0/2
$options->{'_prog_name_sep'} || ':'
l!l
00
MC/DC0%0/1
$options->{'_prog_name_sep'} || ':'
$options->{'_prog_name_sep'}
Truth table0%0/2
A
A= $options->{'_prog_name_sep'}
Aresult
11
00
62 0 $sep .= " ";
63 0 ▶ $sep = "" if $msg =~ /^\W/;
Branch: 1 branch
BranchTrueFalseTotal
if $msg =~ /^\W/ 0 0 0
64 0 $msg =~ s/^/$options->{_prog_name}$sep/gm;
65 }
66 0 $msg .= "\n";
67 0 ▶ print $msg unless $options->{quiet};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"quiet"} 0 0 0
68 0 $options->{describe};
69 }
70  
71  
72 0 ▶sub _d_c { # "describe command".
Subroutine _d_c: not called CC=1 SCAR=6.9
73 0 my $options = $_[-1];
74 0 local $options->{_prog_name_sep} = '$';
75 0 _d @_;
76 }
77  
78 0 ▶sub _rel($) {
Subroutine _rel: not called CC=1 SCAR=6.9
79 0 my ( $p ) = @_;
80 0 require File::Spec;
81 0 $p = File::Spec->abs2rel( $p );
82 }
83  
84  
85 0 ▶sub _mkdir {
Subroutine _mkdir: not called CC=3 SCAR=24.8
86 0 my ( $dir, $options ) = @_;
87 0 ▶ unless ( -d $dir or _d_c "mkdir -p ", _rel $dir, $options ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir or _d_c("mkdir -p ", &_rel($dir), $options)) 0 0 0
Condition0%0/3
-d $dir or _d_c("mkdir -p ", &_rel($dir), $options)
l!l&&r!l&&!r
000
MC/DC0%0/2
-d $dir or _d_c("mkdir -p ", &_rel($dir), $options)
-d $dir _d_c("mkdir -p ", &_rel($dir), $options)
Truth table0%0/3
A or B
A= -d $dirB= _d_c("mkdir -p ", &_rel($dir), $options)
ABresult
1X1
011
000
88 0 require File::Path;
89 0 File::Path::mkpath( [ $dir ] );
90 }
91 }
92  
93  
94 0 ▶sub _mkparentdir {
Subroutine _mkparentdir: not called CC=1 SCAR=6.9
95 0 my ( $fn, $options ) = @_;
96 0 _mkdir( (fileparse $fn)[1], $options );
97 }
98  
99  
100 0 ▶sub _chdir {
Subroutine _chdir: not called CC=4 SCAR=30.0
101 0 my ( $dir, $options ) = @_;
102 0 require Cwd;
103 0 ▶ return if $dir eq Cwd::cwd();
Branch: 1 branch
BranchTrueFalseTotal
if $dir eq Cwd::cwd() 0 0 0
104 0 _mkdir $dir, $options;
105 0 ▶ chdir $dir or die "$!: $dir\n"
Branch: 1 branch
BranchTrueFalseTotal
unless _d_c("chdir ", &_rel($dir), $options) 0 0 0
Condition0%0/5
_d_c('chdir ', &_rel($dir), $options) || (chdir $dir || die("$!: $dir\n"))
l!l&&r!l&&!r
000
chdir $dir or die "$!: $dir\n"
l!l
00
MC/DC0%0/2
_d_c('chdir ', &_rel($dir), $options) || (chdir $dir || die("$!: $dir\n"))
_d_c('chdir ', &_rel($dir), $options) chdir $dir
Truth table0%0/3
A || B
A= _d_c('chdir ', &_rel($dir), $options)B= chdir $dir
ABresult
1X1
011
000
106 unless _d_c "chdir ", _rel $dir, $options;
107 }
108
109  
110 =item parse_module_specs
111  
112 my @specs = parse_module_specs @ARGV, \%options;
113  
114 Parses a module specification, one of:
115  
116 Foo
117 Foo::Bar
118 lib/Foo.pm
119 lib/Foo/Bar.pm
120 lib/Foo/Bar.pod
121  
122 and returns the package name (C<Foo::Bar>) and the path to the
123 file (C<lib/Foo/Bar.pm>) for each parameter in a hash. The result HASHes
124 look like:
125  
126 {
127 Filename => "lib/Foo/Bar.pm",
128 Package => "Foo::Bar",
129 Spec => $spec, ## What was passed in
130 };
131  
132 Any name containing characters other that A-Z, 0-9, :, or ' are assumed
133 to be filenames. Filenames should begin with lib/ (or LIB/ on Win32)
134 or will be warned about.
135  
136 The only option provided is:
137  
138 as_dir Set this to 1 to suppress the add "/" instead of ".pm"
139 the Filename when a module name is converted to a filename.
140 Does not affect anything when a filename is parsed. This
141 is used by mvpm's recurse option.
142  
143 =cut
144  
145 13 ▶sub parse_module_specs {
Subroutine parse_module_specs: called CC=8 SCAR=20.9
Pod documented
146 13 ▶ my $options = @_ && ref $_[-1] ? pop : {};
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[-1] ? : 3 10 13
Condition66%2/3
@_ and ref $_[-1]
!ll&&!rl&&r
0103
MC/DC50%1/2
@_ and ref $_[-1]
@_ ref $_[-1]
Truth table66%2/3
A and B
A= @_B= ref $_[-1]
ABresult
0X0
100
111
147  
148 map {
149 13 my $spec = $_;
150  
151 my ( $pkg, $fn ) = $spec =~ /[^\w:']/
152 ? do {
153 6 require File::Spec;
154 6 my $p = File::Spec->canonpath( $spec );
155 6 for ( $p ) {
156 6 s{^(\.[\\/]+)+}{};
157 6 ▶ ( $^O =~ /Win32/
Branch: 2 branches
BranchTrueFalseTotal
$^O =~ /Win32/ ? s[^lib[\\/]+][]i : s[^lib[\\/]+][] or warn "Module spec '${spec}' does not begin with lib/\n" 6 0 6
$^O =~ /Win32/ ? : 0 6 6
158 ? s{^lib[\\/]+}{}i
159 : s{^lib[\\/]+}{}
160 ) or warn "Module spec '$spec' does not begin with lib/\n";
161 6 s{[\\/]+}{::}g;
162 6 s{\..*\z}{};
163 }
164 6 ( $p, $spec );
165 }
166 16 ▶ : do {
Branch: 1 branch
BranchTrueFalseTotal
$spec =~ /[^\w:']/ ? : 6 10 16
167 10 ( my $p = $spec ) =~ s{::}{/}g;
168 10 ▶ ( $spec, $options->{as_dir} ? "lib/$p" : "lib/$p.pm" );
Branch: 1 branch
BranchTrueFalseTotal
$options->{'as_dir'} ? : 2 8 10
169 };
170  
171 {
172 16 Filename => $fn,
173 Package => $pkg,
174 Spec => $spec,
175 };
176 } @_;
177 }
178  
179  
180 =item parse_bin_specs
181  
182 my @specs = parse_bin_specs @ARGV, \%options;
183  
184 Parses specifications for a "bin" program, like:
185  
186 foo
187 bin/foo
188  
189 and returns the program name (C<foo>) and the path to the
190 file (C<bin/foo>) for each parameter in a hash. The result HASHes
191 look like:
192  
193 {
194 Filename => "bin/foo",
195 Program => "foo",
196 Spec => $spec, ## What was passed in
197 };
198  
199 If a spec has no directory separators, "bin/" is prepended.
200 If a spec has directory separator, no "bin/" is prepended.
201  
202 =cut
203  
204 0 ▶sub parse_bin_specs {
Subroutine parse_bin_specs: not called CC=4 SCAR=30.0
Pod documented
205 0 ▶ my $options = @_ && ref $_[-1] ? pop : {};
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[-1] ? : 0 0 0
Condition0%0/3
@_ and ref $_[-1]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and ref $_[-1]
@_ ref $_[-1]
Truth table0%0/3
A and B
A= @_B= ref $_[-1]
ABresult
0X0
100
111
206  
207 map {
208 0 my $spec = $_;
209  
210 0 require File::Spec;
211 0 my @names = File::Spec->splitdir( $spec );
212 0 ▶ unshift @names, "bin" if @names == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @names == 1 0 0 0
213 0 my $fn = File::Spec->canonpath( File::Spec->catdir( @names ) );
214 0 my $program = (fileparse $fn);
215  
216 {
217 0 Filename => $fn,
218 Program => $program,
219 Spec => $spec,
220 };
221 } @_;
222 }
223  
224  
225 =item parse_dist_specs
226  
227 Takes a list of distributions specs (Foo::Bar, Foo-Bar) and returns a hash
228 like
229  
230 {
231 Package => "Foo::Bar",
232 Spec => $spec,
233 }
234  
235 =cut
236  
237 4 ▶sub parse_dist_specs {
Subroutine parse_dist_specs: called CC=1 SCAR=0.0
Pod documented
238 map {
239 4 my $spec = $_;
240  
241 5 ( my $pkg = $spec ) =~ s{-}{::}g;
242 5 ( my $dn = $spec ) =~ s{::}{-}g;
243  
244 {
245 5 Spec => $spec,
246 Package => $pkg,
247 DistName => $dn,
248 };
249 } @_;
250 }
251  
252  
253 =item reconfigure_dist
254  
255 Runs perl Makefile.PL using the current Perl.
256  
257 TODO: Support Module::Build methodology.
258  
259 =cut
260  
261 0 ▶sub reconfigure_dist {
Subroutine reconfigure_dist: not called CC=12 SCAR=50.5
Pod documented
262 0 my ( $options ) = @_;
263  
264 0 ▶ if ( -f "Makefile" ) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f 'Makefile') { } 0 0 0
elsif (-f 'Makefile.PL') { } 0 0 0
265 0 ▶ if ( -f "Makefile.PL" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f "Makefile.PL") 0 0 0
266 0 ▶ unless ( _d_c "touch Makefile.PL", $options ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_d_c("touch Makefile.PL", $options)) 0 0 0
267 0 my $time = time;
268 0 ▶ utime $time, $time, "Makefile.PL"
Branch: 1 branch
BranchTrueFalseTotal
utime $time, $time, "Makefile.PL" or warn "$! touching Makefile.PL" 0 0 0
269 or warn "$! touching Makefile.PL";
270 0 ▶ if ( (stat "Makefile")[9] >= $time
Branch: 1 branch
BranchTrueFalseTotal
if ((stat "Makefile")[9] >= $time and not _d_c("untouch Makefile", $options)) 0 0 0
Condition0%0/3
(stat "Makefile")[9] >= $time and not _d_c("untouch Makefile", $options)
!ll&&!rl&&r
000
MC/DC0%0/2
(stat "Makefile")[9] >= $time and not _d_c("untouch Makefile", $options)
(stat "Makefile")[9] >= $time not _d_c("untouch Makefile", $options)
Truth table0%0/3
A and not(B)
A= (stat "Makefile")[9] >= $timeB= not _d_c("untouch Makefile", $options)
ABresult
0X0
100
111
271 && ! _d_c "untouch Makefile", $options
272 ) {
273 0 ▶ utime $time - 1, $time - 1, "Makefile"
Branch: 1 branch
BranchTrueFalseTotal
utime $time - 1, $time - 1, "Makefile" or warn "$! touching Makefile.PL" 0 0 0
274 or warn "$! touching Makefile.PL";
275 }
276 }
277 }
278  
279 0 ▶ system "make Makefile" unless _d_c "make Makefile\n", $options;
Branch: 1 branch
BranchTrueFalseTotal
unless _d_c("make Makefile\n", $options) 0 0 0
280 }
281 elsif ( -f "Makefile.PL" ) {
282 0 ▶ system $^X, "Makefile.PL" unless _d_c "$^X Makefile\n", $options;
Branch: 1 branch
BranchTrueFalseTotal
unless _d_c("$^X Makefile\n", $options) 0 0 0
283 }
284 else {
285 warn "Can't reconfigure distribution, no Makefile or Makefile.PL found\n"
286 0 ▶ unless $options->{describe};
Branch: 1 branch
BranchTrueFalseTotal
unless $options->{"describe"} 0 0 0
287 }
288  
289 }
290  
291  
292 =item add_to_MANIFEST
293  
294 add_to_MANIFEST "foo", "bar";
295  
296 Adds one or more files to the MANIFEST.
297  
298 =cut
299  
300 ## TODO: back up to the backup dirs used elsewhere.
301  
302 0 ▶sub _backup_and_read_MANIFEST {
Subroutine _backup_and_read_MANIFEST: not called CC=10 SCAR=47.0
303 0 ▶ my $options = @_ && ref $_[-1] ? pop : {};
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[-1] ? : 0 0 0
Condition0%0/3
@_ and ref $_[-1]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and ref $_[-1]
@_ ref $_[-1]
Truth table0%0/3
A and B
A= @_B= ref $_[-1]
ABresult
0X0
100
111
304  
305 0 my @manifest;
306 0 ▶ if ( -e "MANIFEST" && ! _d_c "cp MANIFEST MANIFEST.old", $options ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e "MANIFEST" and not _d_c("cp MANIFEST MANIFEST.old", $options)) 0 0 0
Condition0%0/3
-e "MANIFEST" and not _d_c("cp MANIFEST MANIFEST.old", $options)
!ll&&!rl&&r
000
MC/DC0%0/2
-e "MANIFEST" and not _d_c("cp MANIFEST MANIFEST.old", $options)
-e "MANIFEST" not _d_c("cp MANIFEST MANIFEST.old", $options)
Truth table0%0/3
A and not(B)
A= -e "MANIFEST"B= not _d_c("cp MANIFEST MANIFEST.old", $options)
ABresult
0X0
100
111
307 0 ▶ open MANIFEST, "<MANIFEST"
Branch: 1 branch
BranchTrueFalseTotal
open MANIFEST, "<MANIFEST" or die "$!: MANIFEST\n" 0 0 0
308 or die "$!: MANIFEST\n";
309 @manifest = grep length, map {
310 0 ▶ 1 while chomp;
Branch: 1 branch
BranchTrueFalseTotal
chomp $_ and do { '???' } 0 0 0
311 0 $_;
312 } <MANIFEST>;
313 0 close MANIFEST;
314 0 ▶ unlink "MANIFEST.old" or die "$!: MANIFEST.old"
Branch: 1 branch
BranchTrueFalseTotal
if -e "MANIFEST.old" 0 0 0
Condition0%0/5
-e 'MANIFEST.old' && (unlink('MANIFEST.old') || die("$!: MANIFEST.old"))
!ll&&!rl&&r
000
unlink "MANIFEST.old" or die "$!: MANIFEST.old"
l!l
00
MC/DC0%0/2
-e 'MANIFEST.old' && (unlink('MANIFEST.old') || die("$!: MANIFEST.old"))
-e 'MANIFEST.old' unlink "MANIFEST.old"
Truth table0%0/3
A && B
A= -e 'MANIFEST.old'B= unlink "MANIFEST.old"
ABresult
0X0
100
111
315 if -e "MANIFEST.old";
316 0 ▶ rename "MANIFEST", "MANIFEST.old"
Branch: 1 branch
BranchTrueFalseTotal
rename "MANIFEST", "MANIFEST.old" or die "$! while renaming MANIFEST to MANIFEST.old\n" 0 0 0
317 or die "$! while renaming MANIFEST to MANIFEST.old\n";
318 0 $options->{clean_up_MANIFEST} = 1;
319 }
320  
321 0 return \@manifest;
322 }
323  
324 0 ▶sub _write_MANIFEST {
Subroutine _write_MANIFEST: not called CC=8 SCAR=42.8
325 0 my ( $manifest, $options ) = @_;
326  
327 0 ▶ unless ( $options->{describe} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->{"describe"}) 0 0 0
328 ## TODO: Also add other files not in MANIFEST.SKIP by default?
329 ## Normally, this is done by the skeleton MANIFEST.
330 0 ▶ push @$manifest, "MANIFEST" unless @$manifest;
Branch: 1 branch
BranchTrueFalseTotal
unless @$manifest 0 0 0
331  
332 0 ▶ open MANIFEST, ">MANIFEST" or die "$!: MANIFEST";
Branch: 1 branch
BranchTrueFalseTotal
open MANIFEST, ">MANIFEST" or die "$!: MANIFEST" 0 0 0
333 0 my %seen;
334 0 ▶ print MANIFEST map "$_\n", sort grep !$seen{$_}++, @$manifest
Branch: 1 branch
BranchTrueFalseTotal
print MANIFEST map(("$_\n"), sort(grep((!$seen{$_}++), @$manifest))) or die "$! writing MANIFEST" 0 0 0
335 or die "$! writing MANIFEST";
336 0 close MANIFEST;
337 }
338  
339 unlink "MANIFEST.old" or warn "$! MANIFEST.old\n"
340 0 ▶ if $options->{clean_up_MANIFEST} && ! _d_c "rm MANIFEST.old", $options;
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"clean_up_MANIFEST"} and not _d_c("rm MANIFEST.old", $options) 0 0 0
Condition0%0/8
$options->{'clean_up_MANIFEST'} && !_d_c('rm MANIFEST.old', $options) && (unlink('MANIFEST.old') || warn("$! MANIFEST.old\n"))
!ll&&!rl&&r
000
$options->{"clean_up_MANIFEST"} and not _d_c("rm MANIFEST.old", $options)
!ll&&!rl&&r
000
unlink "MANIFEST.old" or warn "$! MANIFEST.old\n"
l!l
00
MC/DC0%0/3
$options->{'clean_up_MANIFEST'} && !_d_c('rm MANIFEST.old', $options) && (unlink('MANIFEST.old') || warn("$! MANIFEST.old\n"))
$options->{"clean_up_MANIFEST"} not _d_c("rm MANIFEST.old", $options) unlink "MANIFEST.old"
Truth table0%0/4
A and not(B) && C
A= $options->{"clean_up_MANIFEST"}B= not _d_c("rm MANIFEST.old", $options)C= unlink "MANIFEST.old"
ABCresult
0XX0
10X0
1100
1111
341 }
342  
343  
344 0 ▶sub add_to_MANIFEST {
Subroutine add_to_MANIFEST: not called CC=4 SCAR=30.0
Pod documented
345 0 ▶ my $options = @_ && ref $_[-1] ? pop : {};
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[-1] ? : 0 0 0
Condition0%0/3
@_ and ref $_[-1]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and ref $_[-1]
@_ ref $_[-1]
Truth table0%0/3
A and B
A= @_B= ref $_[-1]
ABresult
0X0
100
111
346  
347 0 my $manifest = _backup_and_read_MANIFEST $options;
348 0 ▶ unless ( _d_c
Branch: 1 branch
BranchTrueFalseTotal
unless (_d_c("echo ", join(" ", map(("'${_}'"), @_)), " >> MANIFEST ## and sort it", $options)) 0 0 0
349 "echo ",
350 join( " ", map "'$_'", @_ ),
351 " >> MANIFEST ## and sort it",
352 $options
353 ) {
354 0 push @$manifest, @_;
355 }
356  
357 0 _write_MANIFEST $manifest, $options;
358 }
359  
360  
361 =item rm_from_MANIFEST
362  
363 rm_from_MANIFEST "foo", "bar";
364  
365 Remove one or more files to the MANIFEST.
366  
367 =cut
368  
369 0 ▶sub rm_from_MANIFEST {
Subroutine rm_from_MANIFEST: not called CC=4 SCAR=30.0
Pod documented
370 0 ▶ my $options = @_ && ref $_[-1] ? pop : {};
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[-1] ? : 0 0 0
Condition0%0/3
@_ and ref $_[-1]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and ref $_[-1]
@_ ref $_[-1]
Truth table0%0/3
A and B
A= @_B= ref $_[-1]
ABresult
0X0
100
111
371  
372 0 my $manifest = _backup_and_read_MANIFEST $options;
373  
374 0 ▶ unless ( _d_c
Branch: 1 branch
BranchTrueFalseTotal
unless (_d_c("cat MANIFEST.old | grep -v '", join("|", map("$_", @_)), "' >> MANIFEST", $options)) 0 0 0
375 "cat MANIFEST.old | grep -v '",
376 join( "|", map "$_", @_ ),
377 "' >> MANIFEST",
378 $options
379 ) {
380 0 my %doomed = map { ( $_ => 1 ) } @_;
381 0 @$manifest = grep ! exists $doomed{$_}, @$manifest;
382 }
383  
384 0 _write_MANIFEST $manifest, $options;
385 }
386  
387  
388 =item install_file
389  
390 install_file $from_file_hash, $to_file_hash, \%macros;
391  
392 Locates the approptiate file in the .newpm directory and copies it,
393 instantiating any <%macros%> needed.
394  
395 Reads <%meta foo bar %> and <%meta foo=bar %> tags.
396  
397 Meta tags
398 =========
399 <%meta chmod 0755 %> chmod the resulting file (numeric only)
400  
401 Any unrecognized meta or macro tags are ignored with a warning.
402  
403 Adds file to MANIFEST.
404  
405 TODO: adapt to Module::Build's manifesting procedures.
406  
407 =cut
408  
409 0 ▶sub install_file {
Subroutine install_file: not called CC=14 SCAR=53.5
Pod documented
410 0 ▶ my $options = @_ > 3 ? pop : {};
Branch: 1 branch
BranchTrueFalseTotal
@_ > 3 ? : 0 0 0
411 0 my ( $from, $to, $macros ) = @_;
412  
413 return if _d_c "install ",
414 0 ▶ basename( $from->{Filename} ),
Branch: 1 branch
BranchTrueFalseTotal
if _d_c("install ", basename($from->{"Filename"}), qq[ $to->{"Filename"}\n], $options) 0 0 0
415 " $to->{Filename}\n", $options;
416  
417 0 _mkparentdir $to->{Filename}, $options;
418  
419 0 ▶ open F, $from->{Filename} or die "$!: $from->{Filename}\n";
Branch: 1 branch
BranchTrueFalseTotal
open F, $from->{"Filename"} or die qq[$!: $from->{"Filename"}\n] 0 0 0
420 0 ▶ open T, ">$to->{Filename}" or die "$!: $to->{Filename}\n";
Branch: 1 branch
BranchTrueFalseTotal
open T, qq[>$to->{"Filename"}] or die qq[$!: $to->{"Filename"}\n] 0 0 0
421 0 my %meta;
422 0 ▶ while (<F>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline F)) 0 0 0
423 0 for my $macro ( keys %$macros ) {
424 0 s/<%\s*$macro\s*%>/$macros->{$macro}/gi;
425 }
426 s{
427 <%\s*META\s*([a-z]\w+)\s*(?:=\s*)?(.*?)\s*%>
428 }{
429 0 $meta{lc $1} = $2;
430 0 "";
431 0 }geix;
432 warn "install: WARNING: macro $1 in $from->{Filename} line $. ignored.\n"
433 0 for /(<%.*?%>)/g;
434 0 ▶ print T $_ or die "$! writing to $to->{Filename}\n";
Branch: 1 branch
BranchTrueFalseTotal
print T $_ or die qq[$! writing to $to->{"Filename"}\n] 0 0 0
435 }
436 0 close F;
437 0 close T;
438  
439 0 ▶ if ( my $perms = delete $meta{chmod} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $perms = delete $meta{"chmod"}) 0 0 0
440 0 ▶ unless ( _d_c "chmod $perms $to->{Filename}", $options ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_d_c(qq[chmod $perms $to->{"Filename"}], $options)) 0 0 0
441 0 ▶ $perms = oct $perms if substr( $perms, 0, 1 ) eq "0";
Branch: 1 branch
BranchTrueFalseTotal
if substr($perms, 0, 1) eq 0 0 0 0
442 chmod $perms, $to->{Filename}
443 0 ▶ or warn "$! chmod( $perms )ing $to->{Filename}\n";
Branch: 1 branch
BranchTrueFalseTotal
chmod $perms, $to->{"Filename"} or warn qq[$! chmod( $perms )ing $to->{"Filename"}\n] 0 0 0
444 }
445 }
446  
447 warn "install: WARNING: ignoring META setting",
448 " $_ $meta{$_} in $from->{Filename}\n"
449 0 for sort keys %meta;
450  
451 0 add_to_MANIFEST $to->{Filename}, $options;
452 }
453  
454  
455 =item templates_for
456  
457 my @from_files = templates_for @to_files;
458  
459 Given a list of files to write to, find the appropriate source files.
460  
461 =cut
462  
463 {
464 1 ▶ use vars qw( $template_dir );
Subroutine BEGIN: called CC=1 SCAR=0.0
465
466 2 ▶ sub template_dir {
Subroutine template_dir: called CC=4 SCAR=21.7
Pod undocumented
467 2 ▶ if ( ! defined $template_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Module::Husbandry::template_dir) 0 2 2
468 $template_dir = File::Spec->catdir(
469 $^O =~ /Win32/
470 ? "C:\\etc"
471 : $ENV{HOME},
472 0 ▶ $^O =~ /Win32/
Branch: 2 branches
BranchTrueFalseTotal
$^O =~ /Win32/ ? : 0 0 0
$^O =~ /Win32/ ? : 0 0 0
473 ? "newpm"
474 : ".newpm"
475 );
476 }
477 2 $template_dir;
478 }
479 }
480
481  
482 1 ▶sub templates_for {
Subroutine templates_for: called CC=1 SCAR=0.0
Pod documented
483 1 require File::Spec;
484 map {
485 1 my $fn = $_->{Filename};
486 2 $fn =~ s{\A[^.]*(\.|\z)}{Template$1};
487 {
488 2 Filename => File::Spec->catfile( template_dir, $fn ),
489 };
490 } @_;
491 }
492  
493 =item test_scripts_for
494  
495 my @test_scripts = test_scripts_for @modules;
496  
497 Returns test scripts for any .pm and .pl file in @modules:
498  
499 {
500 Filename => "t/Foo.t",
501 }
502  
503 where @modules is an array of HASHes returned by parse_module_specs.
504  
505 =cut
506  
507 5 ▶sub test_scripts_for {
Subroutine test_scripts_for: called CC=3 SCAR=11.0
Pod documented
508 map {
509 5 ( $_->{Filename} =~
510 ( ( $^O =~ /Win32/ )
511 ? qr/\.p[ml]\z/i
512 : qr/\.p[ml]\z/
513 ) )
514 6 ▶ ? do {
Branch: 2 branches
BranchTrueFalseTotal
$_->{'Filename'} =~ ($^O =~ /Win32/ ? qr/\.p[ml]\z/i : qr/\.p[ml]\z/) ? : 5 1 6
$^O =~ /Win32/ ? : 0 6 6
515 5 ( my $fn = $_->{Package} ) =~ s{::}{-}g;
516  
517 {
518 5 Filename => "t/$fn.t",
519 };
520 }
521 : ();
522 } @_;
523 }
524  
525 =item skeleton_files
526  
527 my %skel_map = skeleton_files $target_dir;
528  
529 Returns a list of from/to files to install from the skeleton directory.
530  
531 =cut
532  
533 0 ▶sub skeleton_files {
Subroutine skeleton_files: not called CC=2 SCAR=17.9
Pod documented
534 0 my ( $target_dir ) = @_;
535  
536 0 require File::Find;
537 0 require File::Spec;
538  
539 0 my $skel_dir = File::Spec->catdir( template_dir, "skel" );
540  
541 0 ▶ die "$skel_dir not found\n" unless -e $skel_dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $skel_dir 0 0 0
542  
543 0 my @files;
544 File::Find::find(
545 {
546 0 ▶ wanted => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
547 0 ▶ return unless -f;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $_ 0 0 0
548 0 my $to_fn = File::Spec->abs2rel( $_, $skel_dir );
549 0 $to_fn =~ s{\A(\.[\\/]+)+}{}g;
550 0 my $from_fn = File::Spec->catfile( $skel_dir, $to_fn );
551 0 push @files, [
552 { # From file
553 Filename => $from_fn,
554 },
555 { # To file
556 Filename => $to_fn,
557 },
558 ];
559 },
560 0 no_chdir => 1,
561 },
562 $skel_dir
563 );
564  
565 0 return @files;
566 }
567  
568 =item cppm
569  
570 cppm $from, $to, \%options
571  
572 Copies a file in a distribution and a related test suite (if found).
573  
574 TODO: Don't rewrite changelogs. Not sure how best to recognize them; this
575 could be an option for the mythical .newpmrc.
576  
577 TODO: Make the filename substitutions patterns case insensitive on Win32?
578  
579 =cut
580  
581 0 ▶sub cppm {
Subroutine cppm: not called CC=22 SCAR=62.3
Pod documented
582 0 ▶ my $options = @_ && ref $_[-1] ? pop : {};
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[-1] ? : 0 0 0
Condition0%0/3
@_ and ref $_[-1]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and ref $_[-1]
@_ ref $_[-1]
Truth table0%0/3
A and B
A= @_B= ref $_[-1]
ABresult
0X0
100
111
583  
584 0 my @copies;
585 0 require File::Find;
586 0 require File::Spec;
587  
588 0 my %substs; ## Strings to substitute as a result of the name change
589 my $substs_pat; ## The re that looks for things to substitute
590  
591 0 ▶ if ( $options->{recurse} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'recurse'}) { } 0 0 0
592 0 die "Sorry, -r not implemented yet.\n";
593 0 my ( $from, $to ) = parse_module_specs @_, { as_dir => 1 };
594  
595 ### SET from_pat, to_name
596  
597 0 my ( $bn, $dn ) = fileparse $from->{Filename};
598 File::Find::find(
599 {
600 no_chdir => 1,
601 0 ▶ wanted => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
602 0 my $p = File::Spec->abs2rel( $_, $dn );
603 ## TODO
604 },
605 },
606 0 $dn
607 );
608 }
609 else {
610 0 my ( $from, $to ) = parse_module_specs @_;
611  
612 _x "$from->{Filename} not found\n", $options
613 0 ▶ unless -e $from->{Filename};
Branch: 1 branch
BranchTrueFalseTotal
unless -e $from->{"Filename"} 0 0 0
614  
615 0 ▶ _x "$from->{Filename} is not a file\n", $options
Branch: 1 branch
BranchTrueFalseTotal
unless -f _ 0 0 0
616 unless -f _;
617  
618 _x "$to->{Filename} exists (and is a directory), not copying module\n", $options
619 0 ▶ if -d $to->{Filename};
Branch: 1 branch
BranchTrueFalseTotal
if -d $to->{"Filename"} 0 0 0
620  
621 0 ▶ _x "$to->{Filename} exists, not copying module\n", $options
Branch: 1 branch
BranchTrueFalseTotal
if -e _ 0 0 0
622 if -e _;
623  
624 0 push @copies, [ $from, $to ];
625  
626 %substs = (
627 $from->{Package} => $to->{Package},
628 $from->{Filename} => $to->{Filename},
629 0 );
630  
631 0 my ( $test_script_from ) = test_scripts_for $from;
632 0 my ( $test_script_to ) = test_scripts_for $to;
633  
634 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($test_script_from and $test_script_to and -f $test_script_from->{"Filename"}) 0 0 0
Condition0%0/6
$test_script_from and $test_script_to and -f $test_script_from->{"Filename"}
!ll&&!rl&&r
000
$test_script_from and $test_script_to
!ll&&!rl&&r
000
MC/DC0%0/3
$test_script_from and $test_script_to and -f $test_script_from->{"Filename"}
$test_script_from $test_script_to -f $test_script_from->{"Filename"}
Truth table0%0/4
A and B and C
A= $test_script_fromB= $test_script_toC= -f $test_script_from->{"Filename"}
ABCresult
0XX0
10X0
1100
1111
635 $test_script_from
636 && $test_script_to
637 && -f $test_script_from->{Filename}
638 ) {
639 0 push @copies, [ $test_script_from, $test_script_to ];
640 $substs{$test_script_from->{Filename}}
641 0 = $test_script_to->{Filename};
642 }
643  
644 0 $substs_pat = join(
645 join( "|", map quotemeta, sort keys %substs ),
646 "\\b(",
647 ")\\b"
648 );
649 0 $substs_pat = qr/$substs_pat/;
650 }
651  
652 0 require File::Copy;
653  
654 0 my ( $from_w, $to_w ) = ( 0, 0 ); # for pretty-printing
655  
656 0 for ( @copies ) {
657 0 my ( $from, $to ) = @$_;
658 $from_w = length $from->{Filename}
659 0 ▶ if length $from->{Filename} > $from_w;
Branch: 1 branch
BranchTrueFalseTotal
if length $from->{"Filename"} > $from_w 0 0 0
660 $to_w = length $to->{Filename}
661 0 ▶ if length $to->{Filename} > $to_w;
Branch: 1 branch
BranchTrueFalseTotal
if length $to->{"Filename"} > $to_w 0 0 0
662 }
663  
664 {
665 0 my $f_w = ( 0, 0 );
666 0 for ( keys %substs ) {
667 0 ▶ $f_w = length $_ if length $_ > $f_w;
Branch: 1 branch
BranchTrueFalseTotal
if length $_ > $f_w 0 0 0
668 }
669 0 my $f = "# subst: %-${f_w}s => %s\n";
670 0 _d sprintf( $f, $_, $substs{$_} ), $options for sort keys %substs;
671 }
672  
673 0 for ( @copies ) {
674 0 my $from_fn = $_->[0]->{Filename};
675 0 my $to_fn = $_->[1]->{Filename};
676  
677 0 ▶ unless ( _d_c
Branch: 1 branch
BranchTrueFalseTotal
unless (_d_c(sprintf("munge %-${from_w}s > %s\n", $from_fn, $to_fn), $options)) 0 0 0
678 sprintf( "munge %-${from_w}s > %s\n", $from_fn, $to_fn ),
679 $options
680 ) {
681 0 ▶ open FROM, "<$from_fn" or die "$!: $from_fn";
Branch: 1 branch
BranchTrueFalseTotal
open FROM, "<$from_fn" or die "$!: $from_fn" 0 0 0
682 0 ▶ open TO, ">$to_fn" or die "$!: $to_fn";
Branch: 1 branch
BranchTrueFalseTotal
open TO, ">$to_fn" or die "$!: $to_fn" 0 0 0
683  
684 0 ▶ while (<FROM>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FROM)) 0 0 0
685 0 s/$substs_pat/$substs{$1}/sge;
686 0 print TO $_;
687 }
688  
689 0 close FROM;
690 0 close TO;
691 }
692  
693 0 add_to_MANIFEST $to_fn, $options;
694 }
695  
696 0 reconfigure_dist $options;
697 }
698  
699 =item newpm
700  
701 Create new modules in ./lib/... and, if it's a .pm module,
702 a test suite in ./t/...
703  
704 Does not build the make file.
705  
706 =cut
707  
708 {
709 1 ▶ use vars qw( $time );
Subroutine BEGIN: called CC=1 SCAR=0.0
710 0 ▶ sub _time {
Subroutine _time: not called CC=2 SCAR=17.9
711 0 ▶ $time = time unless defined $time;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Module::Husbandry::time 0 0 0
712 0 $time;
713 }
714 }
715  
716  
717 1 ▶sub _newpm_installs {
Subroutine _newpm_installs: not called CC=7 SCAR=39.2
718 0 ▶ my $options = ref $_[-1] ? pop : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] ? : 0 0 0
719 0 my @modules = parse_module_specs @_;
720  
721 0 my @errors;
722  
723 my @installs;
724  
725 0 for my $module ( @modules ) {
726 my %macros = (
727 PackageName => $module->{Package},
728 ModulePath => $module->{Filename},
729 0 Date => scalar localtime( _time ),
730 Year => 1900 + (localtime( _time ))[5],
731 );
732  
733 push @errors, "$module->{Filename} found, can't overwrite\n"
734 0 ▶ if -e $module->{Filename};
Branch: 1 branch
BranchTrueFalseTotal
if -e $module->{"Filename"} 0 0 0
735  
736 0 my ( $template ) = templates_for $module;
737  
738 0 push @installs, [ $template, $module, \%macros ];
739  
740 0 my ( $test_script ) = test_scripts_for $module;
741  
742 0 ▶ if ( defined $test_script ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $test_script) 0 0 0
743 0 my ( $test_script_template ) = templates_for $test_script;
744 _x "$test_script->{Filename} found, can't overwrite.\n", $options
745 0 ▶ if -e $test_script->{Filename};
Branch: 1 branch
BranchTrueFalseTotal
if -e $test_script->{"Filename"} 0 0 0
746 0 push @installs, [ $test_script_template, $test_script, \%macros ];
747 }
748 }
749 0 ▶ _x @errors, $options if @errors;
Branch: 1 branch
BranchTrueFalseTotal
if @errors 0 0 0
750  
751 0 @installs;
752 }
753  
754  
755 0 ▶sub newpm {
Subroutine newpm: not called CC=3 SCAR=24.8
Pod documented
756 0 ▶ my $options = ref $_[-1] ? pop : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] ? : 0 0 0
757 0 install_file @$_, $options for _newpm_installs @_, $options;
758 0 reconfigure_dist $options;
759 }
760  
761 =item newpmbin
762  
763 Create new script files in bin/. Does not add a test script
764 (since there's no safe way to test an arbitrary program).
765  
766 =cut
767  
768 0 ▶sub _newpmbin_installs {
Subroutine _newpmbin_installs: not called CC=5 SCAR=34.0
769 0 ▶ my $options = ref $_[-1] ? pop : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] ? : 0 0 0
770 0 my @programs = parse_bin_specs @_;
771  
772 0 my @errors;
773  
774 my @installs;
775  
776 0 for my $program ( @programs ) {
777 my %macros = (
778 ProgramName => $program->{Program},
779 ProgramPath => $program->{Filename},
780 0 Date => scalar localtime( _time ),
781 Year => 1900 + (localtime( _time ))[5],
782 );
783  
784 push @errors, "$program->{Filename} found, can't overwrite\n"
785 0 ▶ if -e $program->{Program};
Branch: 1 branch
BranchTrueFalseTotal
if -e $program->{"Program"} 0 0 0
786  
787 0 my ( $template ) = templates_for $program;
788  
789 0 push @installs, [ $template, $program, \%macros ];
790  
791 # my ( $test_script ) = test_scripts_for $program;
792 #
793 # if ( defined $test_script ) {
794 # my ( $test_script_template ) = templates_for $test_script;
795 # _x "$test_script->{Filename} found, can't overwrite.\n", $options
796 # if -e $test_script->{Filename};
797 # push @installs, [ $test_script_template, $test_script, \%macros ];
798 # }
799 }
800 0 ▶ _x @errors, $options if @errors;
Branch: 1 branch
BranchTrueFalseTotal
if @errors 0 0 0
801  
802 0 @installs;
803 }
804  
805  
806 0 ▶sub newpmbin {
Subroutine newpmbin: not called CC=3 SCAR=24.8
Pod documented
807 0 ▶ my $options = ref $_[-1] ? pop : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] ? : 0 0 0
808 0 install_file @$_, $options for _newpmbin_installs @_, $options;
809 0 reconfigure_dist $options;
810 }
811  
812 =item newpmdist
813  
814 Create a new distribution in . and populate it from the skeleton
815 files. newpm() a new module.
816  
817 =cut
818  
819 0 ▶sub newpmdist {
Subroutine newpmdist: not called CC=7 SCAR=40.3
Pod documented
820 0 ▶ my $options = ref $_[-1] ? pop : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] ? : 0 0 0
821 0 my @installs;
822  
823 0 for my $dist ( parse_dist_specs @_ ) {
824 0 my ( $module ) = parse_module_specs $dist->{Package};
825  
826 my %macros = (
827 PackageName => $dist->{Package},
828 ProgramName => $dist->{Program},
829 DistName => $dist->{DistName},
830 ModulePath => $module->{Filename},
831 0 Date => scalar localtime( _time ),
832 Year => 1900 + (localtime( _time ))[5],
833 );
834  
835 0 my @files = skeleton_files $dist->{DistName};
836  
837 0 ▶ _x "No skeleton files found for dist $dist->{Spec}\n", $options
Branch: 1 branch
BranchTrueFalseTotal
unless @files 0 0 0
838 unless @files;
839  
840 0 push @installs, $dist->{DistName};
841 0 push @installs, map [ @$_, \%macros ], sort @files;
842 0 push @installs, _newpm_installs $module->{Package};
843 0 push @installs, "reconfigure!";
844 }
845  
846 0 require Cwd;
847 0 my $d = Cwd::cwd();
848  
849 0 for ( @installs ) {
850 0 ▶ if ( ref $_ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_) { } 0 0 0
elsif ($_ eq 'reconfigure!') { } 0 0 0
851 0 install_file @$_, $options;
852 }
853 elsif ( $_ eq "reconfigure!" ) {
854 0 reconfigure_dist $options;
855 }
856 else {
857 0 my $dir = File::Spec->catdir( $d, $_ );
858 0 _chdir $dir, $options;
859 }
860 }
861  
862 0 _chdir $d, $options;
863 }
864  
865 =item mvpm
866  
867 mvpm $from, $to, \%options
868  
869 Changes the name of a file in a distribution and all occurences of the
870 file's name (and, if applicable, package name) in it and in all other
871 files.
872  
873 A backup of any files changed is placed in .newpm/bak_0000 (where 0000
874 increments each time).
875  
876 TODO: some kind of locking so simultaneous mvpms don't happen to choose
877 the same backup directory name.
878  
879 TODO: Don't rewrite changelogs. Not sure how best to recognize them; this
880 could be an option for the mythical .newpmrc.
881  
882 TODO: Make the filename substitutions patterns case insensitive on Win32?
883  
884 =cut
885  
886 {
887 1 ▶ use vars qw( $workdir );
Subroutine BEGIN: called CC=1 SCAR=0.0
888 0 ▶ sub _workdir {
Subroutine _workdir: not called CC=2 SCAR=17.9
889 0 ▶ $workdir = ".mvpm.d" unless defined $workdir;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Module::Husbandry::workdir 0 0 0
890 0 $workdir;
891 }
892 }
893  
894  
895 1 ▶sub _mk_bak_dir {
Subroutine _mk_bak_dir: not called CC=6 SCAR=32.9
896 0 my $options = pop;
897  
898 0 my $wd = _workdir;
899  
900 0 require File::Spec;
901 0 my $max = 0;
902 0 ▶ for ( glob( "$wd/bak_*" ) ) {
Subroutine BEGIN: called CC=1 SCAR=0.2
903 0 ▶ /\bbak_(\d+)/ or warn "Unusual backup dir name: '$_'\n";
Branch: 1 branch
BranchTrueFalseTotal
/\bbak_(\d+)/ or warn "Unusual backup dir name: '${_}'\n" 0 0 0
904 0 ▶ my $n = $1 || 0;
Condition0%0/2
$1 || 0
l!l
00
MC/DC0%0/1
$1 || 0
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
905 0 ▶ $max = $n if $n > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $n > $max 0 0 0
906 }
907  
908 0 my $bd = sprintf "$wd/bak_%04d", $max + 1;
909  
910 0 ▶ die "BUG: trying to reuse backup dir $bd" if -e $bd;
Branch: 1 branch
BranchTrueFalseTotal
if -e $bd 0 0 0
911  
912 0 _mkdir $bd, $options;
913  
914 0 return $bd;
915 }
916  
917  
918 0 ▶sub mvpm {
Subroutine mvpm: not called CC=29 SCAR=67.7
Pod documented
919 0 ▶ my $options = @_ && ref $_[-1] ? pop : {};
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[-1] ? : 0 0 0
Condition0%0/3
@_ and ref $_[-1]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and ref $_[-1]
@_ ref $_[-1]
Truth table0%0/3
A and B
A= @_B= ref $_[-1]
ABresult
0X0
100
111
920  
921 0 my @moves;
922 0 require File::Find;
923 0 require File::Spec;
924  
925 0 my %substs; ## Strings to substitute as a result of the name change
926 my $substs_pat; ## The re that looks for things to substitute
927  
928 0 ▶ if ( $options->{recurse} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'recurse'}) { } 0 0 0
929 0 die "Sorry, -r not implemented yet.\n";
930 0 my ( $from, $to ) = parse_module_specs @_, { as_dir => 1 };
931  
932 ### SET from_pat, to_name
933  
934 0 my ( $bn, $dn ) = fileparse $from->{Filename};
935 File::Find::find(
936 {
937 no_chdir => 1,
938 0 ▶ wanted => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
939 0 my $p = File::Spec->abs2rel( $_, $dn );
940 ## TODO
941 },
942 },
943 0 $dn
944 );
945 }
946 else {
947 0 my ( $from, $to ) = parse_module_specs @_;
948  
949 _x "$from->{Filename} not found\n", $options
950 0 ▶ unless -e $from->{Filename};
Branch: 1 branch
BranchTrueFalseTotal
unless -e $from->{"Filename"} 0 0 0
951  
952 0 ▶ _x "$from->{Filename} is not a file\n", $options
Branch: 1 branch
BranchTrueFalseTotal
unless -f _ 0 0 0
953 unless -f _;
954  
955 _x "$to->{Filename} exists (and is a directory), not moving module\n", $options
956 0 ▶ if -d $to->{Filename};
Branch: 1 branch
BranchTrueFalseTotal
if -d $to->{"Filename"} 0 0 0
957  
958 0 ▶ _x "$to->{Filename} exists, not moving module\n", $options
Branch: 1 branch
BranchTrueFalseTotal
if -e _ 0 0 0
959 if -e _;
960  
961 0 push @moves, [ $from, $to ];
962  
963 %substs = (
964 $from->{Package} => $to->{Package},
965 $from->{Filename} => $to->{Filename},
966 0 );
967  
968 0 my ( $test_script_from ) = test_scripts_for $from;
969 0 my ( $test_script_to ) = test_scripts_for $to;
970  
971 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($test_script_from and $test_script_to and -f $test_script_from->{"Filename"}) 0 0 0
Condition0%0/6
$test_script_from and $test_script_to and -f $test_script_from->{"Filename"}
!ll&&!rl&&r
000
$test_script_from and $test_script_to
!ll&&!rl&&r
000
MC/DC0%0/3
$test_script_from and $test_script_to and -f $test_script_from->{"Filename"}
$test_script_from $test_script_to -f $test_script_from->{"Filename"}
Truth table0%0/4
A and B and C
A= $test_script_fromB= $test_script_toC= -f $test_script_from->{"Filename"}
ABCresult
0XX0
10X0
1100
1111
972 $test_script_from
973 && $test_script_to
974 && -f $test_script_from->{Filename}
975 ) {
976 0 push @moves, [ $test_script_from, $test_script_to ];
977 $substs{$test_script_from->{Filename}}
978 0 = $test_script_to->{Filename};
979 }
980  
981 0 $substs_pat = join(
982 join( "|", map quotemeta, sort keys %substs ),
983 "\\b(",
984 ")\\b"
985 );
986 0 $substs_pat = qr/$substs_pat/;
987  
988 0 require Cwd;
989 0 my $cwd = Cwd::cwd();
990  
991 File::Find::find(
992 {
993 no_chdir => 1,
994 0 ▶ wanted => sub {
Subroutine __ANON__: not called CC=18 SCAR=58.3
995 0 my $p = File::Spec->abs2rel( $_, $cwd );
996 0 my $is_d = -d;
997  
998 0 ▶ $File::Find::prune =
Condition0%0/18
$p eq template_dir() || $p eq _workdir() || $p eq 'blib' || $p eq 'pm_to_blib' || $p =~ /^change/ || $is_d && substr($p, 0, 1) eq '.'
l!l&&r!l&&!r
000
$p eq template_dir() or $p eq _workdir() or $p eq "blib" or $p eq "pm_to_blib" or $p =~ /^change/
l!l&&r!l&&!r
000
$p eq template_dir() or $p eq _workdir() or $p eq "blib" or $p eq "pm_to_blib"
l!l&&r!l&&!r
000
$p eq template_dir() or $p eq _workdir() or $p eq "blib"
l!l&&r!l&&!r
000
$p eq template_dir() or $p eq _workdir()
l!l&&r!l&&!r
000
$is_d and substr($p, 0, 1) eq "."
!ll&&!rl&&r
000
MC/DC0%0/7
$p eq template_dir() || $p eq _workdir() || $p eq 'blib' || $p eq 'pm_to_blib' || $p =~ /^change/ || $is_d && substr($p, 0, 1) eq '.'
$p eq template_dir() $p eq _workdir() $p eq "blib" $p eq "pm_to_blib" $p =~ /^change/ $is_d substr($p, 0, 1) eq "."
Truth table0%0/8
A or B or C or D or E || F and G
A= $p eq template_dir()B= $p eq _workdir()C= $p eq "blib"D= $p eq "pm_to_blib"E= $p =~ /^change/F= $is_dG= substr($p, 0, 1) eq "."
ABCDEFGresult
1XXXXXX1
01XXXXX1
001XXXX1
0001XXX1
00001XX1
00000111
000000X0
00000100
999 $p eq template_dir
1000 || $p eq _workdir
1001 || $p eq "blib"
1002 || $p eq "pm_to_blib"
1003 || $p =~ /^change/
1004 || ( $is_d && substr( $p, 0, 1 ) eq "." );
1005  
1006 0 ▶ if ( $File::Find::prune ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($File::Find::prune) 0 0 0
1007 0 ▶ _d "# ignoring $p", $is_d ? "/..." : (), $options;
Branch: 1 branch
BranchTrueFalseTotal
$is_d ? : 0 0 0
1008 0 return;
1009 }
1010  
1011 0 ▶ return if $is_d;
Branch: 1 branch
BranchTrueFalseTotal
if $is_d 0 0 0
1012  
1013 0 ▶ if ( -B ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-B $_) 0 0 0
1014 0 _d "# ignoring binary file $_", $options;
1015 0 return;
1016 }
1017  
1018 return if $p eq $from->{Filename}
1019 || (
1020 $test_script_from
1021 && $p eq $test_script_from->{Filename}
1022 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
if $p eq $from->{"Filename"} or $test_script_from and $p eq $test_script_from->{"Filename"} 0 0 0
Condition0%0/6
$p eq $from->{"Filename"} or $test_script_from and $p eq $test_script_from->{"Filename"}
l!l&&r!l&&!r
000
$test_script_from and $p eq $test_script_from->{"Filename"}
!ll&&!rl&&r
000
MC/DC0%0/3
$p eq $from->{"Filename"} or $test_script_from and $p eq $test_script_from->{"Filename"}
$p eq $from->{"Filename"} $test_script_from $p eq $test_script_from->{"Filename"}
Truth table0%0/4
A or B and C
A= $p eq $from->{"Filename"}B= $test_script_fromC= $p eq $test_script_from->{"Filename"}
ABCresult
1XX1
0111
00X0
0100
1023  
1024 0 ▶ open FROM, "<$p" or die "$! while scanning $p\n";
Branch: 1 branch
BranchTrueFalseTotal
open FROM, "<$p" or die "$! while scanning $p\n" 0 0 0
1025 0 ▶ while (<FROM>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FROM)) 0 0 0
1026 0 ▶ if ( /$substs_pat/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$substs_pat/) 0 0 0
1027 0 my $f = {
1028 Filename => $p,
1029 };
1030 0 push @moves, [ $f, $f ];
1031 0 last;
1032 }
1033 }
1034 0 ▶ close FROM or die "$! closing $p\n";
Branch: 1 branch
BranchTrueFalseTotal
&Module::Husbandry::close("FROM") or die "$! closing $p\n" 0 0 0
1035 },
1036 },
1037 0 "."
1038 );
1039 }
1040  
1041 0 require File::Copy;
1042  
1043 0 my $bak_dir = _mk_bak_dir $options;
1044  
1045 0 my ( $from_w, $to_w, $bak_w ) = ( 0, 0, 0 ); # for pretty-printing
1046  
1047 0 for ( @moves ) {
1048 0 my ( $from, $to ) = @$_;
1049 $from->{BakFilename}
1050 0 = File::Spec->catfile( $bak_dir, $from->{Filename} );
1051  
1052 $from_w = length $from->{Filename}
1053 0 ▶ if length $from->{Filename} > $from_w;
Branch: 1 branch
BranchTrueFalseTotal
if length $from->{"Filename"} > $from_w 0 0 0
1054 $bak_w = length $from->{BakFilename}
1055 0 ▶ if length $from->{BakFilename} > $bak_w;
Branch: 1 branch
BranchTrueFalseTotal
if length $from->{"BakFilename"} > $bak_w 0 0 0
1056 $to_w = length $to->{Filename}
1057 0 ▶ if length $to->{Filename} > $to_w;
Branch: 1 branch
BranchTrueFalseTotal
if length $to->{"Filename"} > $to_w 0 0 0
1058 }
1059  
1060 0 for ( @moves ) {
1061 0 my $from_fn = $_->[0]->{Filename};
1062 0 my $bak_fn = $_->[0]->{BakFilename};
1063  
1064 _mkparentdir(
1065 0 File::Spec->catdir( $bak_dir, $_->[0]->{Filename} ),
1066 $options
1067 );
1068  
1069 0 ▶ File::Copy::copy( $from_fn, $bak_fn )
Branch: 1 branch
BranchTrueFalseTotal
unless _d_c(sprintf("cp %-${from_w}s %s\n", $from_fn, $bak_fn), $options) 0 0 0
Condition0%0/5
_d_c(sprintf("cp %-${from_w}s %s\n", $from_fn, $bak_fn), $options) || (File::Copy::copy($from_fn, $bak_fn) || die("$! copying $from_fn to $bak_fn\n"))
l!l&&r!l&&!r
000
File::Copy::copy($from_fn, $bak_fn) or die "$! copying $from_fn to $bak_fn\n"
l!l
00
MC/DC0%0/2
_d_c(sprintf("cp %-${from_w}s %s\n", $from_fn, $bak_fn), $options) || (File::Copy::copy($from_fn, $bak_fn) || die("$! copying $from_fn to $bak_fn\n"))
_d_c(sprintf("cp %-${from_w}s %s\n", $from_fn, $bak_fn), $options) File::Copy::copy($from_fn, $bak_fn)
Truth table0%0/3
A || B
A= _d_c(sprintf("cp %-${from_w}s %s\n", $from_fn, $bak_fn), $options)B= File::Copy::copy($from_fn, $bak_fn)
ABresult
1X1
011
000
1070 or die "$! copying $from_fn to $bak_fn\n"
1071 unless _d_c
1072 sprintf( "cp %-${from_w}s %s\n", $from_fn, $bak_fn ),
1073 $options
1074 }
1075  
1076 {
1077 0 my $f_w = ( 0, 0 );
1078 0 for ( keys %substs ) {
1079 0 ▶ $f_w = length $_ if length $_ > $f_w;
Branch: 1 branch
BranchTrueFalseTotal
if length $_ > $f_w 0 0 0
1080 }
1081 0 my $f = "# subst: %-${f_w}s => %s\n";
1082 0 _d sprintf( $f, $_, $substs{$_} ), $options for sort keys %substs;
1083 }
1084  
1085 0 for ( @moves ) {
1086 0 my $from_fn = $_->[0]->{Filename};
1087 0 my $bak_fn = $_->[0]->{BakFilename};
1088 0 my $to_fn = $_->[1]->{Filename};
1089  
1090 0 ▶ unless ( _d_c
Branch: 1 branch
BranchTrueFalseTotal
unless (_d_c(sprintf("munge %-${bak_w}s > %s\n", $bak_fn, $to_fn), $options)) 0 0 0
1091 sprintf( "munge %-${bak_w}s > %s\n", $bak_fn, $to_fn ),
1092 $options
1093 ) {
1094 0 ▶ open BAK, "<$bak_fn" or die "$!: $bak_fn";
Branch: 1 branch
BranchTrueFalseTotal
open BAK, "<$bak_fn" or die "$!: $bak_fn" 0 0 0
1095 0 ▶ open NEW, ">$to_fn" or die "$!: $to_fn";
Branch: 1 branch
BranchTrueFalseTotal
open NEW, ">$to_fn" or die "$!: $to_fn" 0 0 0
1096  
1097 0 ▶ while (<BAK>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline BAK)) 0 0 0
1098 0 s/$substs_pat/$substs{$1}/sge;
1099 0 print NEW $_;
1100 }
1101  
1102 0 close BAK;
1103 0 close NEW;
1104 }
1105  
1106 0 ▶ unlink $from_fn or die "$! unlinking $from_fn"
Branch: 1 branch
BranchTrueFalseTotal
if $from_fn ne $to_fn and not _d_c("rm $from_fn\n", $options) 0 0 0
Condition0%0/8
$from_fn ne $to_fn && !_d_c("rm $from_fn\n", $options) && (unlink($from_fn) || die("$! unlinking $from_fn"))
!ll&&!rl&&r
000
$from_fn ne $to_fn and not _d_c("rm $from_fn\n", $options)
!ll&&!rl&&r
000
unlink $from_fn or die "$! unlinking $from_fn"
l!l
00
MC/DC0%0/3
$from_fn ne $to_fn && !_d_c("rm $from_fn\n", $options) && (unlink($from_fn) || die("$! unlinking $from_fn"))
$from_fn ne $to_fn not _d_c("rm $from_fn\n", $options) unlink $from_fn
Truth table0%0/4
A and not(B) && C
A= $from_fn ne $to_fnB= not _d_c("rm $from_fn\n", $options)C= unlink $from_fn
ABCresult
0XX0
10X0
1100
1111
1107 if $from_fn ne $to_fn && ! _d_c "rm $from_fn\n", $options;
1108 }
1109  
1110 0 reconfigure_dist $options;
1111 }
1112  
1113 =item rmpm
1114  
1115 Removes any modules and tests named after a package (or module) name.
1116  
1117 Warns about any other files that refer to the doomed package.
1118  
1119 A backup is made in the backup directory (.mvpm/... for now, will change)..
1120  
1121 TODO: Allow a site-specific rm command to be used, like 'trash', so
1122 this command may be better integrated with a user's working environment.
1123 This will wait until we restructure the directories.
1124  
1125 =cut
1126  
1127 0 ▶sub rmpm {
Subroutine rmpm: not called CC=22 SCAR=62.3
Pod documented
1128 0 ▶ my $options = @_ && ref $_[-1] ? pop : {};
Branch: 1 branch
BranchTrueFalseTotal
@_ && ref $_[-1] ? : 0 0 0
Condition0%0/3
@_ and ref $_[-1]
!ll&&!rl&&r
000
MC/DC0%0/2
@_ and ref $_[-1]
@_ ref $_[-1]
Truth table0%0/3
A and B
A= @_B= ref $_[-1]
ABresult
0X0
100
111
1129  
1130 0 my @deletes; ## Those who are about to die, we salute you...
1131 0 require File::Find;
1132 0 require File::Spec;
1133  
1134 0 my %spoor; ## Strings to scan for before deleting
1135 my $spoor_pat; ## The re used to scan for %spoor
1136 0 my @spoor; ## filenames, line numbers and lines of spoor that
1137 ## will be left behind.
1138  
1139 0 ▶ if ( $options->{recurse} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'recurse'}) { } 0 0 0
1140 0 die "Sorry, -r not implemented yet.\n";
1141 0 my @doomed = parse_module_specs @_, { as_dir => 1 };
1142  
1143 ### SET doomed_pat
1144 #
1145 # my ( $bn, $dn ) = fileparse $from->{Filename};
1146 # File::Find::find(
1147 # {
1148 # no_chdir => 1,
1149 # wanted => sub {
1150 # my $p = File::Spec->abs2rel( $_, $dn );
1151 # ## TODO
1152 # },
1153 # },
1154 # $dn
1155 # );
1156 }
1157 else {
1158 0 my @doomed = parse_module_specs @_;
1159  
1160 0 for my $doomed ( @doomed ) {
1161 _x "$doomed->{Filename} not found\n", $options
1162 0 ▶ unless -e $doomed->{Filename};
Branch: 1 branch
BranchTrueFalseTotal
unless -e $doomed->{"Filename"} 0 0 0
1163  
1164 0 ▶ _x "$doomed->{Filename} is not a file\n", $options
Branch: 1 branch
BranchTrueFalseTotal
unless -f _ 0 0 0
1165 unless -f _;
1166  
1167 0 push @deletes, [ $doomed ];
1168  
1169 %spoor = (
1170 $doomed->{Package} => undef,
1171 $doomed->{Filename} => undef,
1172 0 );
1173  
1174 0 my ( $doomed_test_script ) = test_scripts_for $doomed;
1175  
1176 0 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if ($doomed_test_script and -f $doomed_test_script->{"Filename"}) 0 0 0
Condition0%0/3
$doomed_test_script and -f $doomed_test_script->{"Filename"}
!ll&&!rl&&r
000
MC/DC0%0/2
$doomed_test_script and -f $doomed_test_script->{"Filename"}
$doomed_test_script -f $doomed_test_script->{"Filename"}
Truth table0%0/3
A and B
A= $doomed_test_scriptB= -f $doomed_test_script->{"Filename"}
ABresult
0X0
100
111
1177 $doomed_test_script
1178 && -f $doomed_test_script->{Filename}
1179 ) {
1180 0 push @deletes, [ $doomed_test_script ];
1181 0 $spoor{$doomed_test_script->{Filename}} = undef;
1182 }
1183 }
1184  
1185 0 $spoor_pat = join(
1186 join( "|", map quotemeta, sort keys %spoor),
1187 "\\b(",
1188 ")\\b"
1189 );
1190 0 my $spoor_pat_re = qr/$spoor_pat/;
1191  
1192 0 require Cwd;
1193 0 my $cwd = Cwd::cwd();
1194  
1195 ## TODO: generalize all or part of this so mvpm() and rmpm() can
1196 ## share it.
1197 File::Find::find(
1198 {
1199 no_chdir => 1,
1200 0 ▶ wanted => sub {
Subroutine __ANON__: not called CC=17 SCAR=57.2
1201 0 my $p = File::Spec->abs2rel( $_, $cwd );
1202 0 my $is_d = -d;
1203  
1204 0 ▶ $File::Find::prune =
Condition0%0/18
$p eq template_dir() || $p eq _workdir() || $p eq 'blib' || $p eq 'pm_to_blib' || $p =~ /^change/ || $is_d && substr($p, 0, 1) eq '.'
l!l&&r!l&&!r
000
$p eq template_dir() or $p eq _workdir() or $p eq "blib" or $p eq "pm_to_blib" or $p =~ /^change/
l!l&&r!l&&!r
000
$p eq template_dir() or $p eq _workdir() or $p eq "blib" or $p eq "pm_to_blib"
l!l&&r!l&&!r
000
$p eq template_dir() or $p eq _workdir() or $p eq "blib"
l!l&&r!l&&!r
000
$p eq template_dir() or $p eq _workdir()
l!l&&r!l&&!r
000
$is_d and substr($p, 0, 1) eq "."
!ll&&!rl&&r
000
MC/DC0%0/7
$p eq template_dir() || $p eq _workdir() || $p eq 'blib' || $p eq 'pm_to_blib' || $p =~ /^change/ || $is_d && substr($p, 0, 1) eq '.'
$p eq template_dir() $p eq _workdir() $p eq "blib" $p eq "pm_to_blib" $p =~ /^change/ $is_d substr($p, 0, 1) eq "."
Truth table0%0/8
A or B or C or D or E || F and G
A= $p eq template_dir()B= $p eq _workdir()C= $p eq "blib"D= $p eq "pm_to_blib"E= $p =~ /^change/F= $is_dG= substr($p, 0, 1) eq "."
ABCDEFGresult
1XXXXXX1
01XXXXX1
001XXXX1
0001XXX1
00001XX1
00000111
000000X0
00000100
1205 $p eq template_dir
1206 || $p eq _workdir
1207 || $p eq "blib"
1208 || $p eq "pm_to_blib"
1209 || $p =~ /^change/
1210 || ( $is_d && substr( $p, 0, 1 ) eq "." );
1211  
1212 0 ▶ if ( $File::Find::prune ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($File::Find::prune) 0 0 0
1213 0 ▶ _d "# ignoring $p", $is_d ? "/..." : (), $options;
Branch: 1 branch
BranchTrueFalseTotal
$is_d ? : 0 0 0
1214 0 return;
1215 }
1216  
1217 0 ▶ return if $is_d;
Branch: 1 branch
BranchTrueFalseTotal
if $is_d 0 0 0
1218  
1219 0 ▶ if ( -B ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-B $_) 0 0 0
1220 0 _d "# ignoring binary file $_", $options;
1221 0 return;
1222 }
1223  
1224 0 ▶ return if grep $p eq $_->{Filename}, @doomed;
Branch: 1 branch
BranchTrueFalseTotal
if grep(($p eq $_->{"Filename"}), @doomed) 0 0 0
1225  
1226 0 ▶ open SURVIVORS, "<$p" or die "$! while scanning $p\n";
Branch: 1 branch
BranchTrueFalseTotal
open SURVIVORS, "<$p" or die "$! while scanning $p\n" 0 0 0
1227 0 ▶ while (<SURVIVORS>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline SURVIVORS)) 0 0 0
1228 0 ▶ if ( /$spoor_pat_re/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$spoor_pat_re/) 0 0 0
1229 0 ▶ 1 while chomp;
Branch: 1 branch
BranchTrueFalseTotal
chomp $_ and do { '???' } 0 0 0
1230 0 push @spoor, [ $p, $., $_ ];
1231 0 last;
1232 }
1233 }
1234 0 ▶ close SURVIVORS or die "$! closing $p\n";
Branch: 1 branch
BranchTrueFalseTotal
&Module::Husbandry::close("SURVIVORS") or die "$! closing $p\n" 0 0 0
1235 },
1236 },
1237 0 "."
1238 );
1239 }
1240  
1241 0 ▶ if ( @spoor ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@spoor) 0 0 0
1242 0 my ( $sfn_w, $ln_w ) = ( 0, 0 );
1243 0 my @spoor_recs;
1244 0 for ( @spoor ) {
1245 0 local $_ = [ "$_->[0],", @{$_}[1,2]];
1246 0 push @spoor_recs, $_;
1247 0 ▶ $sfn_w = length $_->[0] if length $_->[0] > $sfn_w;
Branch: 1 branch
BranchTrueFalseTotal
if length $_->[0] > $sfn_w 0 0 0
1248 0 ▶ $ln_w = length $_->[1] if length $_->[1] > $ln_w;
Branch: 1 branch
BranchTrueFalseTotal
if length $_->[1] > $ln_w 0 0 0
1249 }
1250  
1251 0 my $spoor_format = "%-${sfn_w}s %${ln_w}d: %s\n";
1252  
1253 0 _d_c "grep -r '$spoor_pat' .", $options;
1254 0 printf $spoor_format, @$_ for @spoor_recs;
1255 }
1256  
1257  
1258 0 my $bak_dir = _mk_bak_dir $options;
1259  
1260 0 my ( $from_w, $bak_w ) = ( 0, 0, 0 ); # for pretty-printing
1261  
1262 0 for ( @deletes ) {
1263 0 my ( $from ) = @$_;
1264 $from->{BakFilename}
1265 0 = File::Spec->catfile( $bak_dir, $from->{Filename} );
1266  
1267 $from_w = length $from->{Filename}
1268 0 ▶ if length $from->{Filename} > $from_w;
Branch: 1 branch
BranchTrueFalseTotal
if length $from->{"Filename"} > $from_w 0 0 0
1269 $bak_w = length $from->{BakFilename}
1270 0 ▶ if length $from->{BakFilename} > $bak_w;
Branch: 1 branch
BranchTrueFalseTotal
if length $from->{"BakFilename"} > $bak_w 0 0 0
1271 }
1272  
1273 0 require File::Copy;
1274 0 for ( @deletes ) {
1275 0 my $from_fn = $_->[0]->{Filename};
1276 0 my $bak_fn = $_->[0]->{BakFilename};
1277  
1278 _mkparentdir(
1279 0 File::Spec->catdir( $bak_dir, $_->[0]->{Filename} ),
1280 $options
1281 );
1282  
1283 0 ▶ File::Copy::copy( $from_fn, $bak_fn )
Branch: 1 branch
BranchTrueFalseTotal
unless _d_c(sprintf("cp %-${from_w}s %s\n", $from_fn, $bak_fn), $options) 0 0 0
Condition0%0/5
_d_c(sprintf("cp %-${from_w}s %s\n", $from_fn, $bak_fn), $options) || (File::Copy::copy($from_fn, $bak_fn) || die("$! copying $from_fn to $bak_fn\n"))
l!l&&r!l&&!r
000
File::Copy::copy($from_fn, $bak_fn) or die "$! copying $from_fn to $bak_fn\n"
l!l
00
MC/DC0%0/2
_d_c(sprintf("cp %-${from_w}s %s\n", $from_fn, $bak_fn), $options) || (File::Copy::copy($from_fn, $bak_fn) || die("$! copying $from_fn to $bak_fn\n"))
_d_c(sprintf("cp %-${from_w}s %s\n", $from_fn, $bak_fn), $options) File::Copy::copy($from_fn, $bak_fn)
Truth table0%0/3
A || B
A= _d_c(sprintf("cp %-${from_w}s %s\n", $from_fn, $bak_fn), $options)B= File::Copy::copy($from_fn, $bak_fn)
ABresult
1X1
011
000
1284 or die "$! copying $from_fn to $bak_fn\n"
1285 unless _d_c
1286 sprintf( "cp %-${from_w}s %s\n", $from_fn, $bak_fn ),
1287 $options
1288 }
1289  
1290 0 for ( @deletes ) {
1291 0 my $from_fn = $_->[0]->{Filename};
1292 0 ▶ unlink $from_fn or die "$! unlinking $from_fn";
Branch: 1 branch
BranchTrueFalseTotal
unlink $from_fn or die "$! unlinking $from_fn" 0 0 0
1293 0 rm_from_MANIFEST $from_fn;
1294 }
1295  
1296 0 reconfigure_dist $options;
1297 }
1298  
1299 =item usage
1300  
1301 =cut
1302  
1303 0 ▶sub usage {
Subroutine usage: not called CC=15 SCAR=54.8
Pod documented
1304 0 my ( $messages, $spec ) = @_;
1305 0 my $prog_name = basename $0;
1306  
1307 0 ▶ push @$messages, "\nSee $prog_name --help for details" if @$messages;
Branch: 1 branch
BranchTrueFalseTotal
if @$messages 0 0 0
1308 0 ▶ my $message = join "\n", @$messages, @$messages ? ( "", "" ) : ();
Branch: 1 branch
BranchTrueFalseTotal
@$messages ? : 0 0 0
1309  
1310 0 my $examples = $spec->{examples};
1311  
1312 0 my $desc;
1313 0 ▶ $desc = $spec->{description} if ! length $message;
Branch: 1 branch
BranchTrueFalseTotal
unless length $message 0 0 0
1314 0 ▶ $desc ||= "";
Condition0%0/2
$desc ||= ""
l!l
00
MC/DC0%0/1
$desc ||= ""
$desc
Truth table0%0/2
A
A= $desc
Aresult
11
00
1315  
1316 my $options =
1317 join "\n", map {
1318 0 my $name = join ", ", grep length, split /\|+/;
1319 my @desc =
1320 length $messages
1321 ? ()
1322 0 ▶ : do {
Branch: 1 branch
BranchTrueFalseTotal
length $messages ? : 0 0 0
1323 0 my $desc = $spec->{$_};
1324 0 $desc =~ s/^(\w+:)?(\w+=)?\s*//;
1325 0 $desc =~ s/^/ /;
1326 0 ▶ 1 while chomp $desc;
Branch: 1 branch
BranchTrueFalseTotal
chomp $desc and do { '???' } 0 0 0
1327 0 "$desc\n";
1328 };
1329 0 ( $name, @desc );
1330 } grep /^-/, sort keys %$spec;
1331  
1332 0 ▶ 1 while chomp $message;
Branch: 1 branch
BranchTrueFalseTotal
chomp $message and do { '???' } 0 0 0
1333 0 ▶ 1 while chomp $examples;
Branch: 1 branch
BranchTrueFalseTotal
chomp $examples and do { '???' } 0 0 0
1334 0 ▶ 1 while chomp $desc;
Branch: 1 branch
BranchTrueFalseTotal
chomp $desc and do { '???' } 0 0 0
1335  
1336 0 s/^/ /mg for ( grep length, $examples, $desc, $options );
1337 0 ▶ $message = "$message\n\n" if length $message;
Branch: 1 branch
BranchTrueFalseTotal
if length $message 0 0 0
1338 0 ▶ $examples = "Usage\n\n$examples\n\n" if length $examples;
Branch: 1 branch
BranchTrueFalseTotal
if length $examples 0 0 0
1339 0 ▶ $options = "Options (may occur anywhere except after a '--')\n\n$options\n\n" if length $options;
Branch: 1 branch
BranchTrueFalseTotal
if length $options 0 0 0
1340 0 ▶ $desc = "Description\n\n$desc\n\n" if length $desc;
Branch: 1 branch
BranchTrueFalseTotal
if length $desc 0 0 0
1341  
1342 0 my $usage = "$message$examples$options$desc";
1343 0 $usage =~ s/%p/$prog_name/g;
1344 0 print $usage;
1345  
1346 0 exit length $messages ? 1 : 0;
1347 }
1348  
1349  
1350 =item parse_cli
1351  
1352 my ( $options, @params ) = parse_options @ARGV, \%spec;
1353  
1354 Reads the command line and parses out the options and other parameters.
1355 Options may be intermixed with parameters.
1356  
1357 Options -h|-?|--help and -- do the normal things always.
1358  
1359 -n|--describe print out what *would* happen, but do nothing.
1360  
1361 =cut
1362  
1363 2 ▶sub parse_cli {
Subroutine parse_cli: called CC=26 SCAR=40.1
Pod documented
1364 2 my ( $cli, $spec ) = @_;
1365  
1366 2 my ( %options, @params ); ## These shall be returned if all is ok.
1367  
1368 2 $options{_prog_name} = basename $0;
1369  
1370 2 my %options_spec;
1371 my @errors;
1372 2 my $check;
1373 2 my ( $min_params, $max_params );
1374 2 my $found_examples;
1375  
1376 2 $spec->{"-h|-?|--help"} = "Display full help";
1377 2 $spec->{"-n|--describe"} = "Describe what would happen without doing it";
1378 2 $spec->{"--"} = "Mark end of options";
1379  
1380 2 for ( keys %$spec ) {
1381 12 my $desc = $spec->{$_};
1382 12 ▶ if ( substr( $_, 0, 1 ) eq "-" ) {
Branch: 5 branches
BranchTrueFalseTotal
if (substr($_, 0, 1) eq '-') { } 8 4 12
elsif ($_ eq 'check') { } 0 4 4
elsif ($_ eq 'param_count') { } 2 2 4
elsif ($_ eq 'examples') { } 2 0 2
elsif ($_ eq 'description') { } 0 0 0
1383 8 my $type = "flag";
1384 8 ▶ $type = $1 if $desc =~ s/^(\w+)://;
Branch: 1 branch
BranchTrueFalseTotal
if $desc =~ s/^(\w+):// 0 8 8
1385 8 my $canonical_spelling;
1386 8 ▶ $canonical_spelling = $1 if $desc =~ s/^(\w+)=//;
Branch: 1 branch
BranchTrueFalseTotal
if $desc =~ s/^(\w+)=// 0 8 8
1387
1388 8 my @spellings = split /\|/;
1389 8 ▶ unless ( defined $canonical_spelling ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $canonical_spelling) 8 0 8
1390 8 $canonical_spelling = $spellings[-1];
1391 8 $canonical_spelling =~ s/^-+//;
1392 8 $canonical_spelling =~ s/\W/_/g;
1393 8 $canonical_spelling =~ s/^(\d)/_$1/;
1394 }
1395  
1396 my $action =
1397 1 ▶ $type eq "flag" ? sub { $options{$canonical_spelling} = $desc }
Subroutine __ANON__: called CC=1 SCAR=0.0
1398 8 ▶ : do {
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'flag' ? : 8 0 8
1399 0 push @errors, "Unrecognized option type '$type:'";
1400 0 next;
1401 };
1402  
1403 $options_spec{$_} = $action
1404 8 for @spellings;
1405 }
1406 elsif ( $_ eq "check" ) {
1407 0 $check = $desc;
1408 }
1409 elsif ( $_ eq "param_count" ) {
1410 2 ▶ if ( $desc =~ /\A(\d+)\.\.((?:\d+)?)\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($desc =~ /\A(\d+)\.\.((?:\d+)?)\z/) { } 0 2 2
1411 0 ( $min_params, $max_params) = ( $1, $2 );
1412 0 ▶ $max_params = 1_000_000_000 unless length $max_params;
Branch: 1 branch
BranchTrueFalseTotal
unless length $max_params 0 0 0
1413 }
1414 else {
1415 2 ( $min_params, $max_params) = ( $desc, $desc );
1416 }
1417 }
1418 elsif ( $_ eq "examples" ) {
1419 2 $found_examples = 1;
1420 }
1421 elsif ( $_ eq "description" ) {
1422 ## ignore it, it's optional
1423 }
1424 else {
1425 0 push @errors, "unrecognized option spec key '$_'";
1426 }
1427 }
1428  
1429 2 ▶ push @errors, "examples missing from command line parsing spec"
Branch: 1 branch
BranchTrueFalseTotal
unless $found_examples 0 2 2
1430 unless $found_examples;
1431  
1432 2 my @checks;
1433 2 ▶ if ( defined $min_params ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $min_params) 2 0 2
1434 2 ▶ push @checks, sub {
Subroutine __ANON__: called CC=7 SCAR=32.1
1435 2 pop;
1436 2 ▶ join "",
Branch: 6 branches
BranchTrueFalseTotal
@_ < $min_params ? : 0 2 2
$min_params - @_ > 1 ? : 0 0 0
$min_params != $max_params ? : 0 0 0
@_ > $max_params ? : 0 2 2
@_ - $max_params > 1 ? : 0 0 0
$min_params != $max_params ? : 0 0 0
1437 @_ < $min_params
1438 ? (
1439 "missing parameter",
1440 $min_params - @_ > 1 ? "s" : (),
1441 ": expected ",
1442 $min_params != $max_params ? "at least " : (),
1443 $min_params,
1444 ", got ",
1445 scalar @_,
1446 )
1447 : @_ > $max_params
1448 ? (
1449 "extra parameter",
1450 @_ - $max_params > 1 ? "s" : (),
1451 ": expected ",
1452 $min_params != $max_params ? "at most " : (),
1453 $max_params,
1454 ", got ",
1455 scalar @_,
1456 )
1457 : ();
1458 2 };
1459 }
1460  
1461 2 ▶ push @checks, $check if $check;
Branch: 1 branch
BranchTrueFalseTotal
if $check 0 2 2
1462  
1463 2 ▶ require Carp, Carp::croak( join "\n", @errors ) if @errors;
Branch: 1 branch
BranchTrueFalseTotal
if @errors 0 2 2
1464  
1465 $options_spec{"--"} =
1466 2 ▶ sub { push @params, splice @$cli; last };
Subroutine __ANON__: called CC=1 SCAR=0.0
1467  
1468 $options_spec{"-h"} =
1469 $options_spec{"--help"} =
1470 $options_spec{"-?"} =
1471 0 ▶ sub { usage [], $spec };
Subroutine __ANON__: not called CC=1 SCAR=1.2
1472  
1473 2 ▶ while ( @$cli ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$cli) 4 1 5
1474 4 my $p = shift @$cli;
1475 4 ▶ if ( substr( $p, 0, 1 ) eq "-" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($p, 0, 1) eq '-') { } 2 2 4
1476 2 my $d = $options_spec{$p};
1477 2 ▶ unless ( defined $d ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $d) 0 2 2
1478 0 push @errors, "unrecognized option: $p";
1479 0 next;
1480 }
1481  
1482 2 ▶ if ( ref $d eq "CODE" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $d eq 'CODE') { } 2 0 2
1483 2 ▶ last unless defined $d->();
Branch: 1 branch
BranchTrueFalseTotal
unless defined &$d() 0 1 1
1484 }
1485 else {
1486 0 require Carp;
1487 0 Carp::confess "BUG: $d is not a CODE ref";
1488 }
1489 }
1490 else {
1491 2 push @params, $p;
1492 }
1493 }
1494  
1495 push @errors, grep defined && length, $_->( @params, \%options )
1496 2 ▶ for @checks;
Condition33%1/3
defined $_ && length $_
!ll&&!rl&&r
020
MC/DC0%0/2
defined $_ && length $_
defined $_ length $_
Truth table33%1/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
1497  
1498 2 ▶ usage \@errors, $spec
Branch: 1 branch
BranchTrueFalseTotal
if @errors 0 2 2
1499 if @errors;
1500  
1501 2 return ( @params, \%options );
1502 }
1503  
1504 =back
1505  
1506 =head1 LIMITATIONS
1507  
1508 ASSumes a dir tree and file naming conventions like:
1509  
1510 Foo-Bar/
1511 Makefile.PL
1512 ...
1513 lib/Foo/Bar.pm
1514 t/Foo-Bar.pm
1515  
1516 This probably won't work out all that well for XS distributions, not
1517 sure how they work. Let me know and we'll see if we can add it :)
1518  
1519 Not tested on Win32.
1520  
1521 Does not know about Module::Build.
1522  
1523 Does not use anything like a .newpmrc file.
1524  
1525 =head1 COPYRIGHT
1526  
1527 Copyright 2002, R. Barrie Slaymaker, Jr., All Rights Reserved
1528  
1529 =head1 LICENSE
1530  
1531 You may use this module under the terms of the BSD, Artistic, oir GPL licenses,
1532 any version.
1533  
1534 =head1 AUTHOR
1535  
1536 Barrie Slaymaker <barries@slaysys.com>
1537  
1538 =cut
1539  
1540 1 1;
↑ Summary