blib/lib/File/AddInc.pm

filter: Statementstmt 82.3% 98 / 119 Branchbran 56.6% 17 / 30 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 88.0% 22 / 25 Podpod 18.1% 2 / 11 totaltotal 73.3% 143 / 195 CC 19 SCAR 14.2
CC
19
Cov
76%
resolve_symlink_1
12.0
dispatch_declare
9.0
resolve_symlink
6.0
CRAP 4.1
↑ Summary Build »
1 package File::AddInc;
2 8 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use mro qw/c3/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 our $VERSION = "0.002";
8  
9 8 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Cwd ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 8 ▶use lib ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 8 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 8 ▶use constant DEBUG => $ENV{DEBUG_MOP4IMPORT};
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 {
18 package
19 File::AddInc::Opts;
20 8 ▶ use fields qw/caller callpack filename line/;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # This Opts->new does not bless the hash.
23 sub new {
24 6 ▶ my ($pack, %opts) = @_;
Subroutine new: called CC=1 SCAR=0.0
25 6 my __PACKAGE__ $opts = \%opts;
26 ($opts->{callpack}, $opts->{filename}, $opts->{line})
27 6 = @{$opts->{caller}};
28 6 $opts;
29 }
30 }
31 8 sub Opts () {'File::AddInc::Opts'}
32  
33 #
34 # Limited version of MOP4Import::Declare#import()
35 #
36 sub import {
37 5 ▶ my ($pack, @pragma) = @_;
Subroutine import: called CC=2 SCAR=6.9
38  
39 5 my Opts $opts = $pack->Opts->new(caller => [caller]);
40  
41 5 ▶ @pragma = (-file_inc) unless @pragma;
Branch: 1 branch
BranchTrueFalseTotal
unless @pragma 1 4 5
42  
43 5 $pack->dispatch_declare($opts, @pragma);
44 }
45  
46 sub dispatch_declare {
47 5 ▶ (my $pack, my Opts $opts, my @pragma) = @_;
Subroutine dispatch_declare: called CC=8 SCAR=22.0
Pod undocumented
48  
49 5 foreach my $pragmaSpec (@pragma) {
50  
51 5 my ($pragma, @args) = do {
52 5 ▶ if (ref $pragmaSpec eq 'ARRAY') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $pragmaSpec eq 'ARRAY') { } 2 3 5
elsif (not ref $pragmaSpec and $pragmaSpec =~ /^-(\w+)$/) { } 2 1 3
elsif (not ref $pragmaSpec and $pragmaSpec =~ /^\$\w+\z/) { } 1 0 1
53 2 @$pragmaSpec
54 }
55 ▶ elsif (not ref $pragmaSpec and $pragmaSpec =~ /^-(\w+)$/) {
Condition66%2/3
not ref $pragmaSpec and $pragmaSpec =~ /^-(\w+)$/
!ll&&!rl&&r
012
MC/DC50%1/2
not ref $pragmaSpec and $pragmaSpec =~ /^-(\w+)$/
not ref $pragmaSpec $pragmaSpec =~ /^-(\w+)$/
Truth table66%2/3
not(A) and B
A= not ref $pragmaSpecB= $pragmaSpec =~ /^-(\w+)$/
ABresult
0X0
100
111
56 2 $1
57 }
58 ▶ elsif (not ref $pragmaSpec and $pragmaSpec =~ /^\$\w+\z/) {
Condition33%1/3
not ref $pragmaSpec and $pragmaSpec =~ /^\$\w+\z/
!ll&&!rl&&r
001
MC/DC0%0/2
not ref $pragmaSpec and $pragmaSpec =~ /^\$\w+\z/
not ref $pragmaSpec $pragmaSpec =~ /^\$\w+\z/
Truth table33%1/3
not(A) and B
A= not ref $pragmaSpecB= $pragmaSpec =~ /^\$\w+\z/
ABresult
0X0
100
111
59 1 (libdir_var => $pragmaSpec)
60 }
61 else {
62 0 Carp::croak "Unsupported pragma: $pragmaSpec";
63 }
64 };
65  
66 5 ▶ my $sub = $pack->can("declare_$pragma")
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $pack->can("declare_$pragma") or Carp::croak("Unknown pragma: $pragma") 0 5 5
67 or Carp::croak "Unknown pragma: $pragma";
68  
69 5 $sub->($pack, $opts, @args);
70 }
71 }
72  
73 sub declare_file_inc {
74 1 ▶ (my $pack, my Opts $opts) = @_;
Subroutine declare_file_inc: called CC=1 SCAR=0.0
Pod undocumented
75  
76 1 my $libdir = libdir($pack, $opts->{callpack}, $opts->{filename});
77  
78 1 $pack->add_inc_if_necessary($libdir);
79 }
80  
81 sub declare_local_lib {
82 1 ▶ (my $pack, my Opts $opts) = @_;
Subroutine declare_local_lib: called CC=1 SCAR=0.0
Pod undocumented
83  
84 1 $pack->declare_these_libdirs($opts, '', [dirname => "local/lib/perl5"]);
85 }
86  
87 sub add_inc_if_necessary {
88 4 ▶ my ($pack, @libdir) = @_;
Subroutine add_inc_if_necessary: called CC=3 SCAR=11.4
Pod documented
89  
90 4 ▶ if (my @necessary = grep {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@necessary) = grep({my $dir = $_; -d $dir and not grep({$dir eq $_;} @INC);} @libdir)) { } 4 0 4
91  
92 9 my $dir = $_;
93 9 ▶ -d $dir and not grep {$dir eq $_} @INC;
Branch: 1 branch
BranchTrueFalseTotal
-d $dir and not grep({$dir eq $_;} @INC) 9 0 9
94  
95 } @libdir) {
96  
97 4 print STDERR "# use lib ", join(", ", map(qq{'$_'}, @necessary))
98 , "\n" if DEBUG;
99  
100 4 lib->import(@necessary);
101  
102 } else {
103  
104 0 print STDERR "# No need to add libs: ", join(", ", map(qq{'$_'}, @libdir))
105 , "\n" if DEBUG;
106 }
107 }
108  
109 sub declare_libdir_var {
110 2 ▶ (my $pack, my Opts $opts, my $varname) = @_;
Subroutine declare_libdir_var: called CC=1 SCAR=0.0
Pod undocumented
111  
112 2 my $libdir = libdir($pack, $opts->{callpack}, $opts->{filename});
113  
114 2 $varname =~ s/^\$//;
115  
116 2 my $fullvarname = join("::", $opts->{callpack}, $varname);
117  
118 8 ▶ my $glob = do {no strict qw/refs/; \*{$fullvarname}};
Subroutine BEGIN: called CC=1 SCAR=0.0
119  
120 2 print STDERR "# set \$$fullvarname = '$libdir'\n" if DEBUG;
121  
122 2 *$glob = \$libdir;
123 }
124  
125 sub declare_these_libdirs {
126 3 ▶ (my $pack, my Opts $opts, my @dirSpec) = @_;
Subroutine declare_these_libdirs: called CC=3 SCAR=11.0
Pod undocumented
127  
128 3 my $libdir = libdir($pack, $opts->{callpack}, $opts->{filename});
129  
130 my @libdir = map {
131 3 ▶ if (ref $_) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 1 7 8
132  
133 1 my ($kind, @rest) = @$_;
134  
135 1 ▶ my $sub = $pack->can("libdir_to_$kind")
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $pack->can("libdir_to_$kind") or Carp::croak("Unknown libdir spec: $kind") 0 1 1
136 or Carp::croak "Unknown libdir spec: $kind";
137  
138 1 my $realPrefix = $sub->($pack, $libdir);
139  
140 1 File::Spec->catfile($realPrefix, map {split(m{/}, $_)} @rest);
141  
142 }
143 # elsif (/\%s/) {
144 # File::Spec->catfile(split m{/}, sprintf($_, $libdir));
145 # }
146 else {
147  
148 7 File::Spec->catfile($libdir, split(m{/}, $_));
149 }
150 } @dirSpec;
151  
152 3 add_inc_if_necessary($pack, @libdir);
153 }
154  
155 sub libdir_to_libdir {
156 0 ▶ my ($pack, $libdir) = @_;
Subroutine libdir_to_libdir: not called CC=1 SCAR=6.9
Pod undocumented
157 0 $libdir;
158 }
159  
160 sub libdir_to_dirname {
161 1 ▶ my ($pack, $libdir) = @_;
Subroutine libdir_to_dirname: called CC=1 SCAR=0.0
Pod undocumented
162 1 dirname($libdir);
163 }
164  
165 sub libdir {
166 9 ▶ my ($pack, @caller) = @_;
Subroutine libdir: called CC=4 SCAR=14.0
Pod documented
167  
168 9 ▶ my ($callpack, $filename) = @caller ? @caller : caller;
Branch: 1 branch
BranchTrueFalseTotal
@caller ? : 8 1 9
169  
170 9 (my $packfn = $callpack) =~ s,::,/,g;
171 9 $packfn .= ".pm";
172  
173 9 ▶ my $realFn = -l $filename
Branch: 1 branch
BranchTrueFalseTotal
-l $filename ? : 0 9 9
174 ? resolve_symlink($pack, $filename)
175 : $filename;
176  
177 9 my $absfn = File::Spec->rel2abs($realFn);
178  
179 9 ▶ $absfn =~ /\Q$packfn\E\z/
Branch: 1 branch
BranchTrueFalseTotal
$absfn =~ /\Q$packfn\E\z/ or Carp::croak("Can't handle this case! absfn=$absfn; packfn=$packfn") 0 9 9
180 or Carp::croak("Can't handle this case! absfn=$absfn; packfn=$packfn");
181  
182 9 substr($absfn, 0, length($absfn) - length($packfn) - 1);
183 }
184  
185 sub resolve_symlink {
186 0 ▶ my ($pack, $filePath) = @_;
Subroutine resolve_symlink: not called CC=2 SCAR=17.9
Pod undocumented
187  
188 0 print STDERR "# resolve_symlink($filePath)...\n" if DEBUG;
189  
190 0 (undef, my ($realDir)) = fileparse($filePath);
191  
192 0 ▶ while (defined (my $linkText = readlink $filePath)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $linkText = readlink $filePath)) 0 0 0
193 0 ($filePath, $realDir) = resolve_symlink_1($pack, $linkText, $realDir);
194 0 print STDERR "# => $filePath (realDir=$realDir)\n" if DEBUG;
195 }
196  
197 0 return $filePath;
198 }
199  
200 sub resolve_symlink_1 {
201 0 ▶ my ($pack, $linkText, $realDir) = @_;
Subroutine resolve_symlink_1: not called CC=3 SCAR=24.8
Pod undocumented
202  
203 0 my $filePath = do {
204 0 ▶ if (File::Spec->file_name_is_absolute($linkText)) {
Branch: 1 branch
BranchTrueFalseTotal
if ('File::Spec'->file_name_is_absolute($linkText)) { } 0 0 0
205 0 $linkText;
206 } else {
207 0 File::Spec->catfile($realDir, $linkText);
208 }
209 };
210  
211 0 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
212 # purify x/../y to y
213 0 my $realPath = Cwd::realpath($filePath);
214 0 (undef, $realDir) = fileparse($realPath);
215 0 ($realPath, $realDir);
216 } else {
217 0 $filePath;
218 }
219 }
220  
221 8 1;
↑ Summary Build »