blib/lib/File/ShareDir/ProjectDistDir.pm

filter: Statementstmt 77.4% 127 / 164 Branchbran 59.3% 38 / 64 Conditioncond 66.6% 4 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 84.0% 21 / 25 Podpod 100% 2 / 2 totaltotal 73.2% 194 / 265 CC 37 SCAR 25.0
CC
37
Cov
72%
_wrap_return
52.4
import
12.9
_get_cached_dist_dir_result
5.2
CRAP 12.2
↑ Summary
1 15 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package File::ShareDir::ProjectDistDir;
6  
7 15 our $VERSION = '1.000009';
8  
9 # ABSTRACT: Simple set-and-forget using of a '/share' directory in your projects root
10  
11 15 our $AUTHORITY = 'cpan:KENTNL'; # AUTHORITY
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23 15 ▶use Path::IsDev qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 15 ▶use Path::FindDev qw(find_dev);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 15 ▶use Sub::Exporter qw(build_exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 15 ▶use File::ShareDir qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 15 my ($exporter) = build_exporter(
29 {
30 exports => [ dist_dir => \&build_dist_dir, dist_file => \&build_dist_file ],
31 groups => {
32 all => [qw( dist_dir dist_file )],
33 'default' => [qw( dist_dir dist_file )],
34 },
35 collectors => [ 'defaults', ],
36 },
37 );
38 15 my $env_key = 'FILE_SHAREDIR_PROJECTDISTDIR_DEBUG';
39  
40 ## no critic (Subroutines::ProhibitSubroutinePrototypes)
41 34 ▶sub _debug($) { }
Subroutine _debug: called CC=1 SCAR=0.0
42 ## use critic
43  
44 15 ▶if ( $ENV{$env_key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{$env_key}) 0 15 15
45 ## no critic (ProtectPrivateVars,TestingAndDebugging::ProhibitNoWarnings)
46 15 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
47 0 ▶ *File::ShareDir::ProjectDistDir::_debug = sub ($) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
48 0 *STDERR->printf( qq{[ProjectDistDir] %s\n}, $_[0] );
49 0 };
50 0 $Path::IsDev::DEBUG = 1;
51 0 $Path::FindDev::DEBUG = 1;
52 }
53  
54 ## no critic (RequireArgUnpacking)
55 0 ▶sub _croak { require Carp; goto &Carp::croak }
Subroutine _croak: not called CC=1 SCAR=6.9
56 1 ▶sub _carp { require Carp; goto &Carp::carp }
Subroutine _carp: called CC=1 SCAR=0.0
57  
58 34 ▶sub _path { require Path::Tiny; goto &Path::Tiny::path }
Subroutine _path: called CC=1 SCAR=0.0
59  
60 1 ▶sub _need_pathclass {
Subroutine _need_pathclass: called CC=3 SCAR=12.7
61 1 for my $package ( q[], q[::File], q[::Dir] ) {
62 ## no critic (Variables::RequireInitializationForLocalVars)
63 3 local $@ = undef;
64 3 my $code = sprintf 'require %s%s;1', 'Path::Class', $package;
65 ## no critic (BuiltinFunctions::ProhibitStringyEval,Lax::ProhibitStringyEval::ExceptForRequire)
66 3 ▶ next if eval $code;
Branch: 1 branch
BranchTrueFalseTotal
if eval $code 3 0 3
67 0 my $err = $@;
68 0 _carp('Path::Class is not installed.');
69 ## no critic (RequireCarping, ErrorHandling::RequireUseOfExceptions)
70 0 die $err;
71 }
72 1 return 1;
73 }
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202 18 ▶sub import {
Subroutine import: called CC=12 SCAR=25.6
203 18 my ( $class, @args ) = @_;
204  
205 18 my ( undef, $xfilename, undef ) = caller;
206  
207 18 my $defaults = {
208 filename => $xfilename,
209 projectdir => 'share',
210 pathclass => undef,
211 strict => undef,
212 };
213  
214 18 ▶ if ( not @args ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@args) 6 12 18
215 6 @_ = ( $class, ':all', defaults => $defaults );
216 6 goto $exporter;
217 }
218  
219 12 for ( 0 .. $#args - 1 ) {
220 29 my ( $key, $value );
221 29 ▶ next unless $key = $args[$_] and $value = $args[ $_ + 1 ];
Branch: 1 branch
BranchTrueFalseTotal
unless $key = $args[$_] and $value = $args[$_ + 1] 3 26 29
Condition66%2/3
$key = $args[$_] and $value = $args[$_ + 1]
!ll&&!rl&&r
3026
MC/DC50%1/2
$key = $args[$_] and $value = $args[$_ + 1]
$key = $args[$_] $value = $args[$_ + 1]
Truth table66%2/3
A and B
A= $key = $args[$_]B= $value = $args[$_ + 1]
ABresult
0X0
100
111
222  
223 26 ▶ if ( 'defaults' eq $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("defaults" eq $key) 0 26 26
224 0 $defaults = $value;
225 0 undef $args[$_];
226 0 undef $args[ $_ + 1 ];
227 0 next;
228 }
229 26 for my $setting (qw( projectdir filename distname pathclass pathtiny strict )) {
230 132 ▶ if ( $key eq $setting and not ref $value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq $setting and not ref $value) 14 118 132
Condition66%2/3
$key eq $setting and not ref $value
!ll&&!rl&&r
118014
MC/DC50%1/2
$key eq $setting and not ref $value
$key eq $setting not ref $value
Truth table66%2/3
A and not(B)
A= $key eq $settingB= not ref $value
ABresult
0X0
100
111
231 14 $defaults->{$setting} = $value;
232 14 undef $args[$_];
233 14 undef $args[ $_ + 1 ];
234 14 last;
235 }
236 }
237 }
238  
239 12 ▶ $defaults->{filename} = $xfilename if not defined $defaults->{filename};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $defaults->{"filename"} 0 12 12
240 12 ▶ $defaults->{projectdir} = 'share' if not defined $defaults->{projectdir};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $defaults->{"projectdir"} 0 12 12
241  
242 12 ▶ if ( defined $defaults->{pathclass} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $defaults->{"pathclass"}) 1 11 12
243 1 _carp( 'Path::Class support depecated and will be removed from a future release.' . ' see Documentation for details' );
244 1 _need_pathclass();
245 }
246  
247 12 @_ = ( $class, ( grep { defined } @args ), 'defaults' => $defaults );
248  
249 12 goto $exporter;
250 }
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276  
277  
278  
279  
280  
281  
282  
283  
284  
285  
286  
287  
288  
289  
290  
291  
292  
293  
294  
295  
296  
297  
298  
299 216 ▶sub _get_defaults {
Subroutine _get_defaults: called CC=3 SCAR=11.0
300 216 my ( $field, $arg, $col ) = @_;
301 216 my $result;
302 216 ▶ $result = $col->{defaults}->{$field} if $col->{defaults}->{$field};
Branch: 1 branch
BranchTrueFalseTotal
if $col->{"defaults"}{$field} 96 120 216
303 216 ▶ $result = $arg->{$field} if $arg->{$field};
Branch: 1 branch
BranchTrueFalseTotal
if $arg->{$field} 0 216 216
304 216 return $result;
305 }
306  
307 16 ▶sub _wrap_return {
Subroutine _wrap_return: called CC=9 SCAR=39.6
308 16 my ( $type, $value ) = @_;
309 16 ▶ if ( not $type ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($type) 16 0 16
310 16 ▶ return $value unless ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 0 16 16
311 16 return "$value";
312 }
313 0 ▶ if ( 'pathtiny' eq $type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("pathtiny" eq $type) 0 0 0
314 0 ▶ return $value if 'Path::Tiny' eq ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if "Path::Tiny" eq ref $value 0 0 0
315 0 return _path($value);
316 }
317 0 ▶ if ( 'pathclassdir' eq $type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("pathclassdir" eq $type) 0 0 0
318 0 ▶ return $value if 'Path::Class::Dir' eq ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if "Path::Class::Dir" eq ref $value 0 0 0
319 0 _need_pathclass;
320 0 return Path::Class::Dir->new("$value");
321 }
322 0 ▶ if ( 'pathclassfile' eq $type ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("pathclassfile" eq $type) 0 0 0
323 0 ▶ return $value if 'Path::Class::File' eq ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if "Path::Class::File" eq ref $value 0 0 0
324 0 _need_pathclass;
325 0 return Path::Class::File->new("$value");
326 }
327 0 return _croak("Unknown return type $type");
328 }
329  
330 15 our %DIST_DIR_CACHE;
331  
332 17 ▶sub _get_cached_dist_dir_result {
Subroutine _get_cached_dist_dir_result: called CC=5 SCAR=16.5
333 17 my ( undef, $filename, $projectdir, $distname, $strict ) = @_;
334 17 ▶ if ( defined $DIST_DIR_CACHE{$distname} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $File::ShareDir::ProjectDistDir::DIST_DIR_CACHE{$distname}) 0 17 17
335 0 return $DIST_DIR_CACHE{$distname};
336 }
337 17 _debug( 'Working on: ' . $filename );
338 17 my $dev = find_dev( _path($filename)->parent );
339  
340 17 ▶ if ( not defined $dev ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dev) 0 17 17
341 ## no critic (Variables::ProhibitPackageVars)
342 0 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
343 0 return File::ShareDir::dist_dir($distname);
344 }
345  
346 17 my $devel_share_dir = $dev->child($projectdir);
347  
348 17 ▶ if ($strict) {
Branch: 1 branch
BranchTrueFalseTotal
if ($strict) 7 10 17
349 7 $devel_share_dir = $devel_share_dir->child( 'dist', $distname );
350 }
351 17 ▶ if ( -d $devel_share_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $devel_share_dir) 11 6 17
352 11 _debug( 'ISDEV : exists : <devroot>/' . $devel_share_dir->relative($dev) );
353 11 return ( $DIST_DIR_CACHE{$distname} = $devel_share_dir );
354 }
355 6 _debug( 'ISPROD: does not exist : <devroot>/' . $devel_share_dir->relative($dev) );
356 ## no critic (Variables::ProhibitPackageVars)
357 6 local $Carp::CarpLevel = $Carp::CarpLevel + 1;
358 6 return File::ShareDir::dist_dir($distname);
359 }
360  
361 18 ▶sub build_dist_dir {
Subroutine build_dist_dir: called CC=4 SCAR=14.6
Pod documented
362 18 my ( $class, undef, $arg, $col ) = @_;
363  
364 18 my $projectdir = _get_defaults( projectdir => $arg, $col );
365 18 my $pathclass = _get_defaults( pathclass => $arg, $col );
366 18 my $pathtiny = _get_defaults( pathtiny => $arg, $col );
367 18 my $strict = _get_defaults( strict => $arg, $col );
368 18 my $filename = _get_defaults( filename => $arg, $col );
369  
370 18 my $wrap_return_type;
371  
372 18 ▶ if ($pathclass) { $wrap_return_type = 'pathclassdir' }
Branch: 1 branch
BranchTrueFalseTotal
if ($pathclass) 1 17 18
373 0 ▶ if ($pathtiny) { $wrap_return_type = 'pathtiny' }
Branch: 1 branch
BranchTrueFalseTotal
if ($pathtiny) 0 18 18
374  
375 18 my $distname = _get_defaults( distname => $arg, $col );
376  
377 18 ▶ if ( not $distname ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($distname) 14 4 18
378 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
379 0 my ($udistname) = @_;
380 0 my $distdir = $class->_get_cached_dist_dir_result( $filename, $projectdir, $udistname, $strict );
381 0 return _wrap_return( $wrap_return_type, $distdir );
382 14 };
383 }
384 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
385 0 my $distdir = $class->_get_cached_dist_dir_result( $filename, $projectdir, $distname, $strict );
386 0 return _wrap_return( $wrap_return_type, $distdir );
387 4 };
388 }
389  
390  
391  
392  
393  
394  
395  
396  
397  
398  
399  
400  
401  
402  
403  
404  
405  
406  
407  
408  
409  
410  
411  
412  
413  
414  
415  
416  
417  
418  
419  
420  
421  
422  
423  
424  
425  
426  
427  
428  
429  
430  
431  
432  
433  
434  
435  
436  
437 18 ▶sub build_dist_file {
Subroutine build_dist_file: called CC=4 SCAR=14.0
Pod documented
438 18 my ( $class, undef, $arg, $col ) = @_;
439  
440 18 my $projectdir = _get_defaults( projectdir => $arg, $col );
441 18 my $pathclass = _get_defaults( pathclass => $arg, $col );
442 18 my $pathtiny = _get_defaults( pathtiny => $arg, $col );
443  
444 18 my $strict = _get_defaults( strict => $arg, $col );
445 18 my $filename = _get_defaults( filename => $arg, $col );
446  
447 18 my $distname = _get_defaults( distname => $arg, $col );
448  
449 18 my $wrap_return_type;
450  
451 18 ▶ if ($pathclass) { $wrap_return_type = 'pathclassfile' }
Branch: 1 branch
BranchTrueFalseTotal
if ($pathclass) 1 17 18
452 0 ▶ if ($pathtiny) { $wrap_return_type = 'pathtiny' }
Branch: 1 branch
BranchTrueFalseTotal
if ($pathtiny) 0 18 18
453  
454 17 ▶ my $check_file = sub {
Subroutine __ANON__: called CC=4 SCAR=14.8
455 17 my ( $distdir, $wanted_file ) = @_;
456 17 my $child = _path($distdir)->child($wanted_file);
457 17 ▶ return unless -e $child;
Branch: 1 branch
BranchTrueFalseTotal
unless -e $child 1 16 17
458 16 ▶ if ( -d $child ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $child) 0 16 16
459 0 return _croak("Found dist_file '$child', but is a dir");
460 }
461 16 ▶ if ( not -r $child ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-r $child) 0 16 16
462 0 return _croak("File '$child', no read permissions");
463 }
464 16 return _wrap_return( $wrap_return_type, $child );
465 18 };
466 18 ▶ if ( not $distname ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($distname) 14 4 18
467 13 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
468 13 my ( $udistname, $wanted_file ) = @_;
469 13 my $distdir = $class->_get_cached_dist_dir_result( $filename, $projectdir, $udistname, $strict );
470 13 return $check_file->( $distdir, $wanted_file );
471 14 };
472 }
473 4 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
474 4 my ($wanted_file) = @_;
475 4 my $distdir = $class->_get_cached_dist_dir_result( $filename, $projectdir, $distname, $strict );
476 4 return $check_file->( $distdir, $wanted_file );
477 4 };
478 }
479  
480 15 1;
481  
482 __END__
↑ Summary