lib/ChordPro/Paths.pm

filter: Statementstmt 70.4% 169 / 240 Branchbran 40.9% 36 / 88 Conditioncond 29.1% 7 / 24 MC/DCmcdc 0.0% 0 / 15 Subroutinesub 67.6% 23 / 34 Podpod 0.0% 0 / 25 totaltotal 55.1% 235 / 426 CC 62 SCAR 29.1
CC
62
Cov
60%
findexe
110.0
path
20.0
setup_resdirs
17.8
CRAP 18.3
« blib/lib/ChordPro/Wx/EditorPanel_wxg.pm ↑ Summary lib/ChordPro/Testing.pm »
1 #! perl
2  
3 92 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 92 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 92 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 92 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 class ChordPro::Paths;
9  
10 92 my $instance;
11  
12 # Work around Object::Pad 0.817 breakage.
13 #method get :common ( $reset = 0 ) {
14 # undef $instance if $reset;
15 # $instance //= $class->new;
16 #}
17  
18 493 ▶sub get( $class, $reset = 0 ) {
Subroutine get: called CC=4 SCAR=14.5
Pod undocumented
19 493 ▶ undef $instance if $reset;
Branch: 1 branch
BranchTrueFalseTotal
if $reset 0 493 493
20 493 ▶ $instance //= $class->new;
Condition66%2/3
$instance //= $class->new
l!l&&r!l&&!r
460330
MC/DC0%0/2
$instance //= $class->new
$instance $class->new
Truth table66%2/3
A //= B
A= $instanceB= $class->new
ABresult
1X1
011
000
21 }
22  
23 92 ▶use Cwd qw(realpath);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 92 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 92 ▶use ChordPro::Files;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 0 ▶field $home :reader; # dir
Subroutine home: not called CC=1 SCAR=2.6
28 0 ▶field $configdir :reader; # dir
Subroutine configdir: not called CC=1 SCAR=2.6
Pod undocumented
29 0 ▶field $privlib :reader; # dir
Subroutine privlib: not called CC=1 SCAR=2.6
Pod undocumented
30 14 ▶field $resdirs :reader; # [ dir, ... ]
Subroutine resdirs: called CC=1 SCAR=0.0
Pod undocumented
31 96 ▶field $configs :reader; # { config => dir, ... }
Subroutine configs: called CC=1 SCAR=0.0
Pod undocumented
32 0 ▶field $pathsep :reader; # : or ;
Subroutine pathsep: not called CC=1 SCAR=2.6
Pod undocumented
33  
34 94 ▶field $packager :reader;
Subroutine packager: called CC=1 SCAR=0.0
Pod undocumented
35  
36 # Cwd::realpath always returns forward slashes.
37 # On Windows, Cwd::realpath always returns a volume.
38  
39 BUILD {
40 my $app = "ChordPro";
41 my $app_lc = lc($app);
42  
43 $pathsep = is_msw ? ';' : ':';
44  
45 $home = realpath( $ENV{HOME} = File::HomeDir->my_home );
46  
47 # $desktop = File::HomeDir->my_desktop;
48 # $docs = File::HomeDir->my_documents;
49 # $music = File::HomeDir->my_music;
50 # $pics = File::HomeDir->my_pictures;
51 # $videos = File::HomeDir->my_videos;
52 # $data = File::HomeDir->my_data;
53 # $dist = File::HomeDir->my_dist_data('ChordPro');
54 # $dist = File::HomeDir->my_dist_config('ChordPro');
55  
56 # Establish config files. Global config is easy.
57 for ( $self->normalize("/etc/$app_lc.json") ) {
58 next unless $_ && -f;
59 $configs->{sysconfig} = $_;
60 }
61  
62 $configs = {};
63 # The user specific config requires some alternatives.
64 # -d $XDG_CONFIG_HOME/$app_lc
65 # -d ~/.config/$app_lc
66 # -d ~/.$app_lc
67 # -d my_dist_config
68 my @try;
69 if ( defined( $ENV{XDG_CONFIG_HOME} ) && $ENV{XDG_CONFIG_HOME} ne "" ) {
70 push( @try,
71 # See https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
72 # fn_catdir( $ENV{XDG_CONFIG_HOME}, ".config", $app_lc ),
73 # fn_catdir( $ENV{XDG_CONFIG_HOME}, ".config" ),
74 # fn_catdir( $ENV{XDG_CONFIG_HOME}, ".$app_lc" ) );
75 fn_catdir( $ENV{XDG_CONFIG_HOME}, "$app_lc" ) );
76 }
77 else {
78 push( @try,
79 fn_catdir( $home, ".config", $app_lc ),
80 fn_catdir( $home, ".$app_lc" ),
81 File::HomeDir->my_dist_config($app) );
82 }
83  
84 for ( @try ) {
85 next unless $_ && fs_test( d => $_);
86 my $path = $self->normalize($_);
87 warn("Paths: configdir try $_ => $path\n") if $self->debug > 1;
88 next unless $path && fs_test( d => $path);
89 $configdir = $path;
90 for ( $self->normalize( fn_catfile( $path, "$app_lc.prp" ) ),
91 $self->normalize( fn_catfile( $path, "$app_lc.json" ) ) ) {
92 next unless $_ && fs_test( f => $_ );
93 $configs->{userconfig} = $_;
94 last;
95 }
96 last if $configdir;
97 }
98 warn("Paths: configdir = ", $configdir // "<undef>", "\n") if $self->debug;
99  
100 for ( $self->normalize(".$app_lc.json"),
101 $self->normalize("$app_lc.json") ) {
102 next unless $_ && fs_test( f => $_ );
103 $configs->{config} = $_;
104 last;
105 }
106 if ( $self->debug ) {
107 for ( qw( sysconfig userconfig config ) ) {
108 warn(sprintf("Paths: %-10s = %s\n",
109 $_, $configs->{$_} // "<undef>" ) );
110 }
111 }
112  
113 # Private lib.
114 $privlib = $INC{'ChordPro.pm'} =~ s/\.pm$/\/lib/r;
115  
116 # Now for the resources.
117 $self->setup_resdirs;
118  
119 # Check for packaged image.
120 for ( qw( OCI Docker AppImage PPL ) ) {
121 next unless exists $ENV{uc($_)."_PACKAGED"}
122 && $ENV{uc($_)."_PACKAGED"};
123 $packager = $_;
124 last;
125 }
126  
127 92 };
128  
129 # We need this to be able to re-establish the resdirs, e.g. after a change
130 # of CHORDPRO_LIB.
131 33 ▶method setup_resdirs {
Subroutine setup_resdirs: called CC=11 SCAR=28.8
Pod undocumented
132 33 $resdirs = [];
133 33 my @try = ();
134 push( @try, $self->path($ENV{CHORDPRO_LIB}) )
135 33 ▶ if defined($ENV{CHORDPRO_LIB});
Branch: 1 branch
BranchTrueFalseTotal
if defined $ENV{"CHORDPRO_LIB"} 0 33 33
136 33 ▶ push( @try, $configdir ) if $configdir;
Branch: 1 branch
BranchTrueFalseTotal
if $configdir 0 33 33
137 33 push( @try, $INC{'ChordPro.pm'} =~ s/\.pm$/\/res/r );
138  
139 33 for ( @try ) {
140 33 ▶ next unless $_;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ 0 33 33
141 33 my $path = $self->normalize($_);
142 33 ▶ warn("Paths: resdirs try $_ => $path\n") if $self->debug > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug > 1 0 33 33
143 33 ▶ next unless $path && fs_test( d => $path );
Branch: 1 branch
BranchTrueFalseTotal
unless $path and fs_test("d", $path) 0 33 33
Condition33%1/3
$path and fs_test("d", $path)
!ll&&!rl&&r
0033
MC/DC0%0/2
$path and fs_test("d", $path)
$path fs_test("d", $path)
Truth table33%1/3
A and B
A= $pathB= fs_test("d", $path)
ABresult
0X0
100
111
144 33 push( @$resdirs, $path );
145 }
146  
147 33 ▶ if ( $self->debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 33 33
148 0 for ( 0..$#{$resdirs} ) {
149 0 warn("Paths: resdirs[$_] = $resdirs->[$_]\n");
150 }
151 }
152  
153 33 ▶ unless ( @$resdirs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$resdirs) 0 33 33
154 0 warn("Paths: Cannot find resources, prepare for disaster\n");
155 }
156 92 }
157  
158 406 ▶method debug {
Subroutine debug: called CC=3 SCAR=14.2
Pod undocumented
159 # We need to take an env var into account, since the Paths
160 # singleton is created far before any config processing.
161 406 ▶ $ENV{CHORDPRO_DEBUG_PATHS} || $::config->{debug}->{paths} || 0;
Branch: 1 branch
BranchTrueFalseTotal
$ENV{"CHORDPRO_DEBUG_PATHS"} or $main::config->{"debug"}{"paths"} or 0 0 406 406
Condition33%1/3
$ENV{"CHORDPRO_DEBUG_PATHS"} or $main::config->{"debug"}{"paths"}
l!l&&r!l&&!r
00406
MC/DC0%0/2
$ENV{"CHORDPRO_DEBUG_PATHS"} or $main::config->{"debug"}{"paths"}
$ENV{"CHORDPRO_DEBUG_PATHS"} $main::config->{"debug"}{"paths"}
Truth table33%1/3
A or B
A= $ENV{"CHORDPRO_DEBUG_PATHS"}B= $main::config->{"debug"}{"paths"}
ABresult
1X1
011
000
162 92 }
163  
164 # Is absolute.
165  
166 0 ▶method is_absolute ( $p ) {
Subroutine is_absolute: not called CC=1 SCAR=6.9
Pod undocumented
167 0 fn_is_absolute( $p );
168 92 }
169  
170 # Is bare (no volume/dir).
171  
172 2 ▶method is_here ( $p ) {
Subroutine is_here: called CC=3 SCAR=12.3
Pod undocumented
173 2 my ( $v, $d, $f ) = fn_splitpath($p);
174 2 ▶ $v eq '' && ( $d eq '' || $d =~ /^\.[\\\/]/ );
Branch: 1 branch
BranchTrueFalseTotal
$v eq "" and $d eq "" || $d =~ m[^\.[\\/]]u 2 0 2
Condition33%2/6
$v eq '' && ($d eq '' || $d =~ m[^\.[\\/]]u)
!ll&&!rl&&r
002
$d eq "" or $d =~ m[^\.[\\/]]u
l!l&&r!l&&!r
200
MC/DC0%0/3
$v eq '' && ($d eq '' || $d =~ m[^\.[\\/]]u)
$v eq '' $d eq "" $d =~ m[^\.[\\/]]u
Truth table25%1/4
A && B or C
A= $v eq ''B= $d eq ""C= $d =~ m[^\.[\\/]]u
ABCresult
0XX0
1000
11X1
1011
175 92 }
176  
177 # Normalize - full path, forward slashes, ~ expanded.
178  
179 314 ▶method normalize ( $p, %opts ) {
Subroutine normalize: called CC=2 SCAR=7.0
Pod undocumented
180 314 ▶ $p = $home . "/$1" if $p =~ /~[\\\/](.*)/;
Branch: 1 branch
BranchTrueFalseTotal
if $p =~ m[~[\\/](.*)]u 0 314 314
181 314 realpath($p)
182 92 }
183  
184 # This is only used in ::runtimeinfo for display purposes.
185  
186 182 ▶method display ( $p ) {
Subroutine display: called CC=3 SCAR=11.0
Pod undocumented
187 182 ▶ return "<undef>" unless defined $p;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $p 0 182 182
188 182 $p = $self->normalize($p);
189 182 ▶ if ( index( $p, $home ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($p, $home) == 0) 42 140 182
190 42 substr( $p, 0, length($home), '~' );
191 }
192 182 return $p;
193 92 }
194  
195 0 ▶method path ( $p = undef ) {
Subroutine path: not called CC=4 SCAR=30.0
Pod undocumented
196 0 ▶ if ( defined($p) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p) 0 0 0
197 0 local $ENV{PATH} = $p;
198 0 my @p = File::Spec->path();
199 # On MSWindows, '.' is always prepended.
200 0 ▶ shift(@p) if is_msw;
Branch: 1 branch
BranchTrueFalseTotal
if is_msw() 0 0 0
201 0 return @p;
202 }
203 0 return File::Spec->path();
204 92 }
205  
206 # Prepend/append dirs to path.
207  
208 0 ▶method pathprepend( @d ) {
Subroutine pathprepend: not called CC=1 SCAR=6.9
Pod undocumented
209 0 $ENV{PATH} = $self->pathcombine( @d, $ENV{PATH} );
210 92 }
211  
212 0 ▶method pathappend( @d ) {
Subroutine pathappend: not called CC=1 SCAR=6.9
Pod undocumented
213 0 $ENV{PATH} = $self->pathcombine( $ENV{PATH}, @d );
214 92 }
215  
216 0 ▶method pathcombine( @d ) {
Subroutine pathcombine: not called CC=1 SCAR=6.9
Pod undocumented
217 0 join( $pathsep, @d );
218 92 }
219  
220 # Locate an executable file (program) using PATH.
221  
222 0 ▶method findexe ( $p, %opts ) {
Subroutine findexe: not called CC=10 SCAR=47.0
Pod undocumented
223 0 my $try = $p;
224 0 my $found;
225 0 ▶ if ( is_msw ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_msw()) 0 0 0
226 0 $try .= ".exe";
227 }
228  
229 0 ▶ if ( fn_is_absolute($p)
Branch: 1 branch
BranchTrueFalseTotal
if (fn_is_absolute($p) and ChordPro::Files::fs_test("fx", $p)) 0 0 0
Condition0%0/3
fn_is_absolute($p) and ChordPro::Files::fs_test("fx", $p)
!ll&&!rl&&r
000
MC/DC0%0/2
fn_is_absolute($p) and ChordPro::Files::fs_test("fx", $p)
fn_is_absolute($p) ChordPro::Files::fs_test("fx", $p)
Truth table0%0/3
A and B
A= fn_is_absolute($p)B= ChordPro::Files::fs_test("fx", $p)
ABresult
0X0
100
111
230 && ChordPro::Files::fs_test( fx => $p ) ) {
231 0 ▶ warn("Paths: findexe $p => ", $self->display($p), "\n")
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
232 if $self->debug;
233 0 return $p;
234 }
235  
236 0 for ( $self->path ) {
237 0 my $e = fn_catfile( $_, $try );
238 0 ▶ $found = realpath($e), last if fs_test( fx => $e );
Branch: 1 branch
BranchTrueFalseTotal
if fs_test("fx", $e) 0 0 0
239 }
240 0 ▶ if ( $self->debug ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->debug) { } 0 0 0
elsif (not $found || $opts{'silent'}) { } 0 0 0
241 0 warn("Paths: findexe $p => ", $self->display($found), "\n");
242 }
243 ▶ elsif ( !$found && !$opts{silent} ) {
Condition0%0/3
$found || $opts{'silent'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$found || $opts{'silent'}
$found $opts{'silent'}
Truth table0%0/3
A || B
A= $foundB= $opts{'silent'}
ABresult
1X1
011
000
244 warn("Could not find $p in ",
245 0 join( " ", map { qq{"$_"} } $self->path ), "\n");
246 }
247 0 return $found;
248 92 }
249  
250 # Locate a config file (prp or json) using respath.
251  
252 124 ▶method findcfg ( $p ) {
Subroutine findcfg: called CC=8 SCAR=22.1
Pod undocumented
253 124 my $found;
254 my @p;
255 124 ▶ if ( $p =~ /\.\w+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p =~ /\.\w+$/u) { } 0 124 124
256 0 ▶ $found = realpath($p) if fs_test( fs => $p );
Branch: 1 branch
BranchTrueFalseTotal
if fs_test("fs", $p) 0 0 0
257 0 @p = ( $p );
258 }
259 else {
260 124 $p =~ s/:+/\//g;
261 124 @p = ( "$p.prp", "$p.json" );
262 }
263 124 ▶ unless ( $found ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found) 124 0 124
264 124 for ( @$resdirs ) {
265 124 for my $cfg ( @p ) {
266 248 my $f = fn_catfile( $_, "config", $cfg );
267 248 ▶ $found = realpath($f), last if fs_test( fs => $f );
Branch: 1 branch
BranchTrueFalseTotal
if fs_test("fs", $f) 124 124 248
268 }
269 }
270 }
271 124 ▶ warn("Paths: findcfg $p => ", $self->display($found), "\n")
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 124 124
272 if $self->debug;
273 124 return $found;
274 92 }
275  
276 # Locate a resource file (optionally classified) using respath.
277  
278 44 ▶method findres ( $p, %opts ) {
Subroutine findres: called CC=7 SCAR=19.9
Pod undocumented
279 44 my $try = $p;
280 44 my $found;
281 44 ▶ if ( fn_is_absolute($p) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (fn_is_absolute($p)) { } 0 44 44
282 0 $found = realpath($p);
283 }
284 else {
285 44 ▶ if ( defined $opts{class} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"class"}) 31 13 44
286 31 $try = fn_catfile( $opts{class}, $try );
287 }
288 44 for ( @$resdirs ) {
289 44 my $f = fn_catfile( $_, $try );
290 44 ▶ $found = realpath($f), last if fs_test( fs => $f );
Branch: 1 branch
BranchTrueFalseTotal
if fs_test("fs", $f) 26 18 44
291 }
292 }
293 44 ▶ warn("Paths: findres", $opts{class} ? " [$opts{class}]" : "",
Branch: 2 branches
BranchTrueFalseTotal
if $self->debug 0 44 44
$opts{'class'} ? : 0 0 0
294 " $p => ", $self->display($found), "\n")
295 if $self->debug;
296 44 return $found;
297 92 }
298  
299 # Locate resource directories (optionally classified) using respath.
300  
301 91 ▶method findresdirs ( $p, %opts ) {
Subroutine findresdirs: called CC=8 SCAR=25.1
Pod undocumented
302 91 my $try = $p;
303 91 my @found;
304 91 ▶ if ( defined $opts{class} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"class"}) 0 91 91
305 0 $p = fn_catdir( $opts{class}, $p );
306 }
307 91 for ( @$resdirs ) {
308 91 my $d = fn_catdir( $_, $p );
309 91 ▶ push( @found, realpath($d) ) if fs_test( d => $d );
Branch: 1 branch
BranchTrueFalseTotal
if fs_test("d", $d) 91 0 91
310 }
311 91 ▶ if ( $self->debug ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) 0 91 91
312 0 my $i = 0;
313 0 ▶ @found = ( "<none>" ) unless @found;
Branch: 1 branch
BranchTrueFalseTotal
unless @found 0 0 0
314 warn("Paths: findresdirs[",
315 $opts{class} ? "$opts{class}:" : "",
316 $i++, "]",
317 0 ▶ " $p => ", $self->display($_), "\n") for @found;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'class'} ? : 0 0 0
318 }
319 91 return \@found;
320 92 }
321  
322 # Return the name of a sibling (i.e., same place, different name
323 # and/or extension).
324  
325 15 ▶method sibling ( $orig, %opts ) {
Subroutine sibling: called CC=6 SCAR=18.1
Pod undocumented
326 # Split.
327 15 my ( $v, $d, $f ) = fn_splitpath($orig);
328 15 my $res;
329 15 ▶ if ( $opts{name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'name'}) { } 2 13 15
330 2 $res = fn_catpath( $v, $d, $opts{name} );
331 }
332 else {
333 # Get base and extension.
334 13 my ( $b, $e ) = $f =~ /^(.*)(?:\.(\w+))$/;
335 # Adjust.
336 13 ▶ $b = $opts{base} if defined $opts{base};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"base"} 0 13 13
337 13 ▶ $e = $opts{ext} if defined $opts{ext};
Branch: 1 branch
BranchTrueFalseTotal
if defined $opts{"ext"} 13 0 13
338 # New file name.
339 13 $f = $b;
340 13 ▶ $f .= $e if defined $e;
Branch: 1 branch
BranchTrueFalseTotal
if defined $e 13 0 13
341 # Join with path.
342 13 $res = fn_catpath( $v, $d, $f );
343 }
344 15 ▶ warn("Paths: sibling $orig => ", $self->display($res), "\n")
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 15 15
345 if $self->debug;
346 15 return $res;
347 92 }
348  
349 # Given a file and a name, try name as a sibling, otherwise look it up.
350  
351 2 ▶method siblingres ( $orig, $name, %opts ) {
Subroutine siblingres: called CC=4 SCAR=14.4
Pod undocumented
352 2 ▶ return unless defined $orig;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $orig 0 2 2
353 2 my $try = $self->sibling( $orig, name => $name );
354 my $found = ( $try && fs_test( s => $try ) )
355 ? $try
356 2 ▶ : $self->findres( $name, class => $opts{class} );
Branch: 1 branch
BranchTrueFalseTotal
$try && fs_test('s', $try) ? : 2 0 2
Condition33%1/3
$try and fs_test("s", $try)
!ll&&!rl&&r
002
MC/DC0%0/2
$try and fs_test("s", $try)
$try fs_test("s", $try)
Truth table33%1/3
A and B
A= $tryB= fs_test("s", $try)
ABresult
0X0
100
111
357 2 return $found;
358 92 }
359  
360 0 ▶method packager_version {
Subroutine packager_version: not called CC=2 SCAR=17.9
Pod undocumented
361 0 ▶ return unless $packager;
Branch: 1 branch
BranchTrueFalseTotal
unless $packager 0 0 0
362 0 $ENV{uc($packager)."_PACKAGED"};
363 92 }
364  
365 ################ Export ################
366  
367 # For convenience.
368  
369 92 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
370 92 our @EXPORT;
371  
372 466 ▶sub CP() { __PACKAGE__->get }
Subroutine CP: called CC=1 SCAR=0.0
Pod undocumented
373  
374 92 push( @EXPORT, 'CP' );
375  
376 92 1;
« blib/lib/ChordPro/Wx/EditorPanel_wxg.pm ↑ Summary lib/ChordPro/Testing.pm »