FindApp/Object/Behavior.pm

filter: Statementstmt 70.0% 89 / 127 Branchbran 29.4% 10 / 34 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 84.0% 21 / 25 Podpod 100% 12 / 12 totaltotal 62.9% 134 / 213 CC 32 SCAR 22.4
CC
32
Cov
59%
show_shell_var
30.0
show_shell_var
30.0
findapp_root
14.5
CRAP 9.4
« FindApp/Utils/List.pm ↑ Summary FindApp/Object/State/Group/Behavior.pm »
1 package FindApp::Object::Behavior;
2  
3 6 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use mro "c3";
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 6 require FindBin;
9  
10 6 ▶use FindApp::Vars qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 6 ▶use FindApp::Utils qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 sub apply ;
14 sub constraint_failure ;
15 sub generate_helper_methods ;
16  
17 6 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 sub constraint_text ;
20 sub copy_founds_to_globals ;
21 sub export_to_env ;
22 sub findapp ;
23 sub findapp_and_export ;
24 sub findapp_root ;
25 sub findapp_root_from_path ;
26 sub path_passes_constraints ;
27 sub reset_all_groups ;
28 sub shell_settings ;
29 sub show_shell_var ;
30  
31 ################################################################
32 ################################################################
33 # This is where everything happens.
34 ################################################################
35 ################################################################
36  
37 4 ▶sub findapp { &ENTER_TRACE;
Subroutine findapp: called CC=2 SCAR=7.0
Pod documented
38 4 my $self = &myself;
39 4 ▶ $self->findapp_root(@_) || constraint_failure($self);
Branch: 1 branch
BranchTrueFalseTotal
$self->findapp_root(@_) or constraint_failure($self) 4 0 4
40 4 $self->copy_founds_to_globals;
41 4 return $self->app_root;
42 }
43  
44 4 ▶sub findapp_and_export { &ENTER_TRACE;
Subroutine findapp_and_export: called CC=1 SCAR=0.0
Pod documented
45 4 my $self = &myself;
46 4 $self->findapp;
47 4 () = $self->export_to_env;
48 }
49  
50 4 ▶sub export_to_env { &ENTER_TRACE;
Subroutine export_to_env: called CC=1 SCAR=0.0
Pod documented
51 4 my $self = &myself;
52 4 ▶ apply $self => alldir_map { "export_${_}_to_env" };
Subroutine __ANON__: called CC=1 SCAR=0.0
53 }
54  
55 4 ▶sub apply {
Subroutine apply: called CC=3 SCAR=13.6
56 4 my $self = &myself;
57 4 ▶ function method_map => sub { map { $self->$_ } @_ };
Subroutine method_map: called CC=1 SCAR=0.0
58 4 ▶ if (my $context = wantarray) { return &method_map }
Branch: 2 branches
BranchTrueFalseTotal
if (my $context = wantarray) { } 4 0 4
elsif (not defined $context) { } 0 0 0
59 0 elsif (!defined $context) { &method_map; return }
60 else {
61 0 panic "called in scalar context not void context or list context";
62 }
63 }
64  
65 4 ▶sub copy_founds_to_globals { &ENTER_TRACE;
Subroutine copy_founds_to_globals: called CC=2 SCAR=6.9
Pod documented
66 4 my $self = &myself;
67 4 for my $dir ($self->group_names) {
68 16 my $var = ucfirst lc $dir;
69 16 my $dirmeth = $dir . "dirs";
70 16 my @dirs = $self->$dirmeth;
71 6 ▶ no strict "refs";
Subroutine BEGIN: called CC=1 SCAR=0.0
72 16 $$var = (@$var = @dirs)[0];
73 }
74 }
75  
76 sub constraint_failure {
77 my $self = shift;
78 croak "no app root above ", $self->origin,
79 " matching constraint ", $self->constraint_text;
80 }
81  
82 # Generate pretty text corresponding to all the constraints.
83 0 ▶sub constraint_text {
Subroutine constraint_text: not called CC=1 SCAR=6.9
Pod documented
84 0 ▶ my $self = &myself;
Pod documented
85 0 ▶ my $pair = function allow_want_constraint_text_pair => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
86 0 my $self = shift; # this is a group object
87 0 bad_args(@_ > 1);
88 0 ▶ my $allowed = @_ ? [shift] : [$self->allowed];
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
89 0 ▶ return $self->wanted->count ? [ $allowed => [$self->wanted] ] : ();
Branch: 1 branch
BranchTrueFalseTotal
$self->wanted->count ? : 0 0 0
90 0 };
91 0 return commify_and map { commify_and ( @{ $$_[1] } )
92 . " in "
93 0 . commify_or ( @{ $$_[0] } )
94 } ( $self->rootdir->$pair("root"),
95 0 map { $_->$pair() } $self->subgroups,
96 );
97 }
98  
99 4 ▶sub findapp_root { &ENTER_TRACE;
Subroutine findapp_root: called CC=7 SCAR=26.8
Pod documented
100 4 my $self = &myself;
101 4 bad_args(@_ > 1);
102 4 my($path) = @_;
103  
104 4 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 4 4
105 # if it's the same as before, return cached version
106 # XXX: ignores constraint changes
107 0 ▶ if ($self->has_app_root && $path eq $self->app_root) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_app_root and $path eq $self->app_root) { } 0 0 0
Condition0%0/3
$self->has_app_root and $path eq $self->app_root
!ll&&!rl&&r
000
MC/DC0%0/2
$self->has_app_root and $path eq $self->app_root
$self->has_app_root $path eq $self->app_root
Truth table0%0/3
A and B
A= $self->has_app_rootB= $path eq $self->app_root
ABresult
0X0
100
111
108 0 debug "findapp_root returning cached $path";
109 0 return $path;
110 }
111 else {
112 0 return $self->findapp_root_from_path($path);
113 }
114 }
115  
116 4 ▶ if ($self->has_origin) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_origin) 0 4 4
117 0 return $self->findapp_root_from_path($self->origin);
118 }
119  
120 # no path, so try both ways
121 4 my($first, $second) = ($FindBin::Bin, getcwd());
122 4 ▶ ($first, $second) = ($second, $first) if $self->default_origin eq "cwd";
Branch: 1 branch
BranchTrueFalseTotal
if $self->default_origin eq "cwd" 0 4 4
123  
124 4 ▶ return $self->findapp_root_from_path($first)
Condition33%1/3
$self->findapp_root_from_path($first) || $self->findapp_root_from_path($second)
l!l&&r!l&&!r
400
MC/DC0%0/2
$self->findapp_root_from_path($first) || $self->findapp_root_from_path($second)
$self->findapp_root_from_path($first) $self->findapp_root_from_path($second)
Truth table33%1/3
A || B
A= $self->findapp_root_from_path($first)B= $self->findapp_root_from_path($second)
ABresult
1X1
011
000
125 || $self->findapp_root_from_path($second);
126 }
127  
128 4 ▶sub findapp_root_from_path { &ENTER_TRACE;
Subroutine findapp_root_from_path: called CC=4 SCAR=15.0
Pod documented
129 4 my $self = &myself;
130 4 good_args(@_ == 1);
131 4 my($path) = @_;
132  
133 4 $self->origin($path);
134 4 ▶ for (my $dir = abs_path($path) || $path;
Branch: 1 branch
BranchTrueFalseTotal
if ($dir ne "/") 6 0 6
Condition33%1/3
&abs_path($path) || $path
l!l&&r!l&&!r
400
MC/DC0%0/2
&abs_path($path) || $path
&abs_path($path) $path
Truth table33%1/3
A || B
A= &abs_path($path)B= $path
ABresult
1X1
011
000
135 $dir ne "/";
136 $dir = dirname($dir))
137 {
138 6 ▶ return $dir if $self->path_passes_constraints($dir);
Branch: 1 branch
BranchTrueFalseTotal
if $self->path_passes_constraints($dir) 4 2 6
139 }
140 0 $self->reset_origin;
141 0 return;
142  
143 }
144  
145 # Takes a directory argument and makes sure that everything
146 # wanted can be found in any of the allowed (sub)directories.
147 # Returns a boolean success value.
148 6 ▶sub path_passes_constraints { &ENTER_TRACE;
Subroutine path_passes_constraints: called CC=3 SCAR=11.0
Pod documented
149 6 my $self = &myself;
150 6 my($candidate_dir) = @_;
151  
152 6 $self->reset_all_groups;
153  
154 6 my %groups = %{ $self->group };
155 6 my @groups = delete $groups{root};
156 6 push @groups, values %groups;
157  
158 6 for my $group (@groups) {
159 18 ▶ next if $group->base_has_wanteds($candidate_dir);
Branch: 1 branch
BranchTrueFalseTotal
if $group->base_has_wanteds($candidate_dir) 16 2 18
160 2 $self->reset_all_groups;
161 2 return;
162 }
163  
164 4 return 1;
165 }
166  
167 8 ▶sub reset_all_groups {
Subroutine reset_all_groups: called CC=2 SCAR=6.9
Pod documented
168 8 my $self = &myself;
169 8 $_->found->reset for values %{ $self->group };
170 }
171  
172 ################################################################
173 # Misc methods.
174  
175 0 ▶sub shell_settings {
Subroutine shell_settings: not called CC=2 SCAR=17.9
Pod documented
176 0 my $self = &myself;
177 0 ▶ return q() unless $self->has_app_root;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_app_root 0 0 0
178 0 return join q() => (
179 $self->show_shell_var(APP_ROOT => $self->app_root),
180 $self->show_shell_var(PERL5LIB => $self->libdirs),
181 $self->show_shell_var(PATH => $self->bindirs),
182 $self->show_shell_var(MANPATH => $self->mandirs),
183 );
184 }
185  
186 0 ▶sub show_shell_var {
Subroutine show_shell_var: not called CC=5 SCAR=34.0
Pod documented
187 0 &all_args_defined;
188 0 my $self = &myself;
189 0 state $poly = { map { $_ => 1 } qw(PERL5LIB PATH MANPATH) };
190 0 my($varname, @dirs) = @_;
191 0 ▶ $varname =~ /^\w+$/ || croak "$varname doesn't look like a good shell variable name";
Branch: 1 branch
BranchTrueFalseTotal
$varname =~ /^\w+$/ or croak("$varname doesn't look like a good shell variable name") 0 0 0
192 0 my $retstr = q();
193 0 ▶ if (@dirs) {
Branch: 1 branch
BranchTrueFalseTotal
if (@dirs) 0 0 0
194 0 ▶ $retstr = is_csh()
Branch: 1 branch
BranchTrueFalseTotal
&is_csh() ? : 0 0 0
195 ? "setenv $varname "
196 : "export $varname=" ;
197 0 ▶ push(@dirs, '$' . $varname) if $$poly{$varname};
Branch: 1 branch
BranchTrueFalseTotal
if $poly->{$varname} 0 0 0
198 0 $retstr .= sprintf qq{"%s";\n}, colonize @dirs;
199 }
200 0 return $retstr;
201 }
202  
203 #################################################################
204 # BUILDERS
205 #################################################################
206  
207 # All generated functions are given proper names, thus allowing for
208 # not merely "not-from-hell" stack traces, but even better, letting you
209 # breakpoint and even list them in the debugger. The builders are
210 # all written in a way to make it utterly clear which lexicals are
211 # being accessed from outside the closure's scope. These are really
212 # paramerized function-building templates, like macros, and so $ALL_CAPS
213 # is used for the template "arguments"; that is, the stuff outside
214 # our scope that we are relying inside the closure.
215  
216 6 ▶no namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
217  
218 sub generate_helper_methods {
219 my $class = shift || __PACKAGE__;
220 my @_SUBDIRS = +BLM;
221 my @_ALLDIRS = (root => @_SUBDIRS);
222  
223 for my $DIR (@_ALLDIRS) {
224 # BUILD: export_bin_to_env
225 # BUIOD: export_lib_to_env
226 # BUIOD: export_man_to_env
227 # BUIOD: export_root_to_env
228 function "export_${DIR}_to_env" => sub { &ENTER_TRACE;
229 my $self = &myself;
230 good_args(@_ == 0);
231 $self->group($DIR)->export_to_env;
232 };
233 # BUILD: bindirs libdirs mandirs rootdirs
234 function "${DIR}dirs" => sub {
235 my $self = &myself;
236 good_args(@_ == 0);
237 my $group = $self->group($DIR);
238 return wantarray ? $group->found : $group;
239 };
240 *rootdir = \&rootdirs;
241  
242 my $is_pl = $DIR ne "root";
243 my $NAMEDIR = $DIR . "dir" . ($is_pl && "s");
244 my $has_have = $is_pl ? "have" : "has";
245 my $is_are = $is_pl ? "are" : "is";
246  
247 my %access_verb = (
248 wanted => $has_have,
249 allowed => $is_are,
250 );
251  
252 while (my($ACCESSOR, $VERB) = each %access_verb) {
253 print "making ${NAMEDIR}_${VERB}\n";
254 function $NAMEDIR . "_" . $VERB => sub {
255 my $self = &myself;
256 my $action = join("_" => (@_ ? "set" : "get"), $NAMEDIR, $ACCESSOR);
257 $self->$action(@_);
258 };
259 }
260  
261 } ## for my @DIR (@_ALLDIRS)
262  
263 my @ACCESSORS = qw(allowed found wanted);
264  
265 for my $TYPE (@ACCESSORS) {
266 # BUILD: allowed found wanted
267 function $TYPE => sub {
268 my $self = &myself;
269 good_args(@_ == 1);
270 my $dir = shift;
271 return $self->group($dir)->$TYPE;
272 };
273  
274 for my $DIR (@_ALLDIRS) {
275 my $is_pl = $DIR ne "root";
276 my $NAMEDIR = $DIR . "dir" . ($is_pl && "s");
277  
278 my $ACTION = $NAMEDIR . "_" . $TYPE;
279  
280 # BUILD: <{rootdir,{bin,lib,man}dirs}_{allowed,found,wanted}>
281 # BUILD: rootdir_allowed rootdir_found rootdir_wanted
282 # BUILD: bindirs_allowed bindirs_found bindirs_wanted
283 # BUILD: libdirs_allowed libdirs_found libdirs_wanted
284 # BUILD: mandirs_allowed mandirs_found mandirs_wanted
285 function $ACTION => sub {
286 my $self = &myself;
287 good_args(@_ == 0);
288 return $self->group($DIR)->$TYPE;
289 };
290  
291 for my $OP (qw(add get set)) {
292 my $FUNC_NAME = "${OP}_${ACTION}";
293 # BUILD: <{add,get,set}_{rootdir,{bin,lib,man}dirs}_{allowed,found,wanted}>
294 # BUILD: add_rootdir_allowed add_rootdir_found add_rootdir_wanted
295 # BUILD: add_bindirs_allowed add_bindirs_found add_bindirs_wanted
296 # BUILD: add_libdirs_allowed add_libdirs_found add_libdirs_wanted
297 # BUILD: add_mandirs_allowed add_mandirs_found add_mandirs_wanted
298 # BUILD: get_rootdir_allowed get_rootdir_found get_rootdir_wanted
299 # BUILD: get_bindirs_allowed get_bindirs_found get_bindirs_wanted
300 # BUILD: get_libdirs_allowed get_libdirs_found get_libdirs_wanted
301 # BUILD: get_mandirs_allowed get_mandirs_found get_mandirs_wanted
302 # BUILD: set_rootdir_allowed set_rootdir_found set_rootdir_wanted
303 # BUILD: set_bindirs_allowed set_bindirs_found set_bindirs_wanted
304 # BUILD: set_libdirs_allowed set_libdirs_found set_libdirs_wanted
305 # BUILD: set_mandirs_allowed set_mandirs_found set_mandirs_wanted
306 function $FUNC_NAME => sub {
307 my $self = &myself;
308 croak("$FUNC_NAME: no args allowed") if @_ && $OP eq "get";
309 return $self->group($DIR)->$TYPE->$OP(@_);
310 };
311 } ## for my $OP (qw(add get set))
312  
313 } ## for my @DIR (@_ALLDIRS)
314 } ## for my $TYPE (@ACCESSORS)
315  
316 } ## sub generate_helper_methods
317  
318 #BEGIN { generate_helper_methods() }
319  
320 #use namespace::clean;
321  
322 6 ▶no namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
323  
324  
325 6 1;
326  
327 =encoding utf8
328  
329 =head1 NAME
330  
331 FindApp::Object::Behavior - FIXME
332  
333 =head1 SYNOPSIS
334  
335 use FindApp::Object::Behavior;
336  
337 =head1 DESCRIPTION
338  
339 =head2 Public Methods
340  
341 =over
342  
343 =item FIXME
344  
345 =back
346  
347 =head2 Exports
348  
349 =over
350  
351 =item add_allowed_bindirs
352  
353 =item add_allowed_libdirs
354  
355 =item add_allowed_mandirs
356  
357 =item add_allowed_rootdir
358  
359 =item add_bindirs_allowed
360  
361 =item add_bindirs_found
362  
363 =item add_bindirs_wanted
364  
365 =item add_found_bindirs
366  
367 =item add_found_libdirs
368  
369 =item add_found_mandirs
370  
371 =item add_found_rootdir
372  
373 =item add_libdirs_allowed
374  
375 =item add_libdirs_found
376  
377 =item add_libdirs_wanted
378  
379 =item add_mandirs_allowed
380  
381 =item add_mandirs_found
382  
383 =item add_mandirs_wanted
384  
385 =item add_rootdir_allowed
386  
387 =item add_rootdir_found
388  
389 =item add_rootdir_wanted
390  
391 =item add_wanted_bindirs
392  
393 =item add_wanted_libdirs
394  
395 =item add_wanted_mandirs
396  
397 =item add_wanted_rootdir
398  
399 =item allowed
400  
401 =item allowed_bindirs
402  
403 =item allowed_libdirs
404  
405 =item allowed_mandirs
406  
407 =item allowed_rootdir
408  
409 =item bindirs
410  
411 =item bindirs_allowed
412  
413 =item bindirs_found
414  
415 =item bindirs_wanted
416  
417 =item constraint_text
418  
419 =item copy_founds_to_globals
420  
421 =item export_bin_to_env
422  
423 =item export_lib_to_env
424  
425 =item export_man_to_env
426  
427 =item export_root_to_env
428  
429 =item export_to_env
430  
431 =item findapp
432  
433 =item findapp_and_export
434  
435 =item findapp_root
436  
437 =item findapp_root_from_path
438  
439 =item found
440  
441 =item found_bindirs
442  
443 =item found_libdirs
444  
445 =item found_mandirs
446  
447 =item found_rootdir
448  
449 =item get_allowed_bindirs
450  
451 =item get_allowed_libdirs
452  
453 =item get_allowed_mandirs
454  
455 =item get_allowed_rootdir
456  
457 =item get_bindirs_allowed
458  
459 =item get_bindirs_found
460  
461 =item get_bindirs_wanted
462  
463 =item get_found_bindirs
464  
465 =item get_found_libdirs
466  
467 =item get_found_mandirs
468  
469 =item get_found_rootdir
470  
471 =item get_libdirs_allowed
472  
473 =item get_libdirs_found
474  
475 =item get_libdirs_wanted
476  
477 =item get_mandirs_allowed
478  
479 =item get_mandirs_found
480  
481 =item get_mandirs_wanted
482  
483 =item get_rootdir_allowed
484  
485 =item get_rootdir_found
486  
487 =item get_rootdir_wanted
488  
489 =item get_wanted_bindirs
490  
491 =item get_wanted_libdirs
492  
493 =item get_wanted_mandirs
494  
495 =item get_wanted_rootdir
496  
497 =item libdirs
498  
499 =item libdirs_allowed
500  
501 =item libdirs_found
502  
503 =item libdirs_wanted
504  
505 =item mandirs
506  
507 =item mandirs_allowed
508  
509 =item mandirs_found
510  
511 =item mandirs_wanted
512  
513 =item path_passes_constraints
514  
515 =item reset_all_groups
516  
517 =item rootdir
518  
519 =item rootdir_allowed
520  
521 =item rootdir_found
522  
523 =item rootdir_wanted
524  
525 =item rootdirs
526  
527 =item set_allowed_bindirs
528  
529 =item set_allowed_libdirs
530  
531 =item set_allowed_mandirs
532  
533 =item set_allowed_rootdir
534  
535 =item set_bindirs_allowed
536  
537 =item set_bindirs_found
538  
539 =item set_bindirs_wanted
540  
541 =item set_found_bindirs
542  
543 =item set_found_libdirs
544  
545 =item set_found_mandirs
546  
547 =item set_found_rootdir
548  
549 =item set_libdirs_allowed
550  
551 =item set_libdirs_found
552  
553 =item set_libdirs_wanted
554  
555 =item set_mandirs_allowed
556  
557 =item set_mandirs_found
558  
559 =item set_mandirs_wanted
560  
561 =item set_rootdir_allowed
562  
563 =item set_rootdir_found
564  
565 =item set_rootdir_wanted
566  
567 =item set_wanted_bindirs
568  
569 =item set_wanted_libdirs
570  
571 =item set_wanted_mandirs
572  
573 =item set_wanted_rootdir
574  
575 =item shell_settings
576  
577 =item show_shell_var
578  
579 =item wanted
580  
581 =item wanted_bindirs
582  
583 =item wanted_libdirs
584  
585 =item wanted_mandirs
586  
587 =item wanted_rootdir
588  
589 =back
590  
591 =head1 EXAMPLES
592  
593 =head1 ENVIRONMENT
594  
595 =head1 SEE ALSO
596  
597 =over
598  
599 =item L<FindApp>
600  
601 =back
602  
603 =head1 CAVEATS AND PROVISOS
604  
605 =head1 BUGS AND LIMITATIONS
606  
607 =head1 HISTORY
608  
609 =head1 AUTHOR
610  
611 Tom Christiansen << <tchrist@perl.com> >>
612  
613 =head1 LICENCE AND COPYRIGHT
614  
615 Copyright (c) 2016, Tom Christiansen C<< <tchrist@perl.com> >>.
616 All Rights Reserved.
617  
618 This module is free software; you can redistribute it and/or
619 modify it under the same terms as Perl itself. See L<perlartistic>.
620  
621 =cut
622  
« FindApp/Utils/List.pm ↑ Summary FindApp/Object/State/Group/Behavior.pm »