blib/lib/FindApp/Object/Behavior.pm untested

filter: Statementstmt 0.0% 0 / 186 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 24 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 39 Podpod 0.0% 0 / 28 totaltotal 0.0% 0 / 315 CC 20 SCAR 26.5
CC
20
Cov
0%
generate_helper_methods
42.0
findapp_root
30.0
show_shell_var
20.0
CRAP 14.2
1 package FindApp::Object::Behavior;
2  
3 use v5.10;
4 use strict;
5 use warnings;
6 use mro "c3";
7  
8 require FindBin;
9  
10 use FindApp::Vars qw(:all);
11 use FindApp::Utils qw(:all);
12  
13 sub apply ;
14 sub constraint_failure ;
15 sub generate_helper_methods ;
16  
17 use namespace::clean;
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 sub findapp { &ENTER_TRACE;
38 my $self = &myself;
39 $self->findapp_root(@_) || constraint_failure($self);
40 $self->copy_founds_to_globals;
41 return $self->app_root;
42 }
43  
44 sub findapp_and_export { &ENTER_TRACE;
45 my $self = &myself;
46 $self->findapp;
47 () = $self->export_to_env;
48 }
49  
50 sub export_to_env { &ENTER_TRACE;
51 my $self = &myself;
52 apply $self => alldir_map { "export_${_}_to_env" };
53 }
54  
55 sub apply {
56 my $self = &myself;
57 function method_map => sub { map { $self->$_ } @_ };
58 if (my $context = wantarray) { return &method_map }
59 elsif (!defined $context) { &method_map; return }
60 else {
61 panic "called in scalar context not void context or list context";
62 }
63 }
64  
65 sub copy_founds_to_globals { &ENTER_TRACE;
66 my $self = &myself;
67 for my $dir ($self->group_names) {
68 my $var = ucfirst lc $dir;
69 my $dirmeth = $dir . "dirs";
70 my @dirs = $self->$dirmeth;
71 no strict "refs";
72 $$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 sub constraint_text {
84 my $self = &myself;
85 my $pair = function allow_want_constraint_text_pair => sub {
86 my $self = shift; # this is a group object
87 bad_args(@_ > 1);
88 my $allowed = @_ ? [shift] : [$self->allowed];
89 return $self->wanted->count ? [ $allowed => [$self->wanted] ] : ();
90 };
91 return commify_and map { commify_and ( @{ $$_[1] } )
92 . " in "
93 . commify_or ( @{ $$_[0] } )
94 } ( $self->rootdir->$pair("root"),
95 map { $_->$pair() } $self->subgroups,
96 );
97 }
98  
99 sub findapp_root { &ENTER_TRACE;
100 my $self = &myself;
101 bad_args(@_ > 1);
102 my($path) = @_;
103  
104 if (@_) {
105 # if it's the same as before, return cached version
106 # XXX: ignores constraint changes
107 if ($self->has_app_root && $path eq $self->app_root) {
108 debug "findapp_root returning cached $path";
109 return $path;
110 }
111 else {
112 return $self->findapp_root_from_path($path);
113 }
114 }
115  
116 if ($self->has_origin) {
117 return $self->findapp_root_from_path($self->origin);
118 }
119  
120 # no path, so try both ways
121 my($first, $second) = ($FindBin::Bin, getcwd());
122 ($first, $second) = ($second, $first) if $self->default_origin eq "cwd";
123  
124 return $self->findapp_root_from_path($first)
125 || $self->findapp_root_from_path($second);
126 }
127  
128 sub findapp_root_from_path { &ENTER_TRACE;
129 my $self = &myself;
130 good_args(@_ == 1);
131 my($path) = @_;
132  
133 $self->origin($path);
134 for (my $dir = abs_path($path) || $path;
135 $dir ne "/";
136 $dir = dirname($dir))
137 {
138 return $dir if $self->path_passes_constraints($dir);
139 }
140 $self->reset_origin;
141 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 sub path_passes_constraints { &ENTER_TRACE;
149 my $self = &myself;
150 my($candidate_dir) = @_;
151  
152 $self->reset_all_groups;
153  
154 my %groups = %{ $self->group };
155 my @groups = delete $groups{root};
156 push @groups, values %groups;
157  
158 for my $group (@groups) {
159 next if $group->base_has_wanteds($candidate_dir);
160 $self->reset_all_groups;
161 return;
162 }
163  
164 return 1;
165 }
166  
167 sub reset_all_groups {
168 my $self = &myself;
169 $_->found->reset for values %{ $self->group };
170 }
171  
172 ################################################################
173 # Misc methods.
174  
175 sub shell_settings {
176 my $self = &myself;
177 return q() unless $self->has_app_root;
178 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 sub show_shell_var {
187 &all_args_defined;
188 my $self = &myself;
189 state $poly = { map { $_ => 1 } qw(PERL5LIB PATH MANPATH) };
190 my($varname, @dirs) = @_;
191 $varname =~ /^\w+$/ || croak "$varname doesn't look like a good shell variable name";
192 my $retstr = q();
193 if (@dirs) {
194 $retstr = is_csh()
195 ? "setenv $varname "
196 : "export $varname=" ;
197 push(@dirs, '$' . $varname) if $$poly{$varname};
198 $retstr .= sprintf qq{"%s";\n}, colonize @dirs;
199 }
200 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 no namespace::clean;
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 no namespace::clean;
323  
324  
325 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