blib/lib/App/hopen/Phases.pm

filter: Statementstmt 90.1% 73 / 81 Branchbran 54.1% 13 / 24 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 86.3% 19 / 22 Podpod 100% 6 / 6 totaltotal 77.0% 114 / 148 CC 16 SCAR 12.3
CC
16
Cov
78%
_clean
8.5
is_phase
6.0
next_phase
4.1
CRAP 3.4
« blib/lib/App/hopen/Gen/MSBuild/AssetGraphNode.pm ↑ Summary blib/lib/App/hopen/MYhopen.pm »
1 # App::hopen::Phases - definitions of phases
2 package App::hopen::Phases;
3 2 ▶use Data::Hopen;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Data::Hopen::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '0.000013'; # TRIAL
8  
9 2 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 our (@EXPORT, @EXPORT_OK, %EXPORT_TAGS);
11 BEGIN {
12 2 ▶ my @normal_export_ok = qw(is_phase is_last_phase phase_idx
Subroutine BEGIN: called CC=1 SCAR=0.0
13 curr_phase_idx next_phase);
14 2 my @hopenfile_export = qw(on);
15  
16 2 @EXPORT = qw(@PHASES);
17 2 @EXPORT_OK = (@normal_export_ok, @hopenfile_export);
18 2 %EXPORT_TAGS = (
19 default => [@EXPORT],
20 all => [@EXPORT, @normal_export_ok],
21 hopenfile => [@hopenfile_export], # Not included in :all!
22 );
23 }
24  
25 2 ▶use App::hopen::BuildSystemGlobals;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use Getargs::Mixed;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use List::MoreUtils qw(first_index);
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 # Docs {{{1
30  
31 =head1 NAME
32  
33 App::hopen::Phases - Definitions and routines for hopen phases
34  
35 =head1 SYNOPSIS
36  
37 Definition of hopen phases. Phase names are case-insensitive. The canonical
38 form has only the first letter capitalized.
39  
40 Phase names may only contain ASCII letters, digits, or underscore. The first
41 character of a phase may not be a digit. This is so they can be used as
42 identifiers if necessary.
43  
44 This package also defines a special export tag, C<:hopenfile>, for use when
45 running hopen files. The wrapper code in L<App::hopen> uses this
46 tag. Hopen files themselves do not need to use this tag.
47  
48 The names C<first>, C<start>, C<last>, and C<end> are reserved.
49  
50 =head1 VARIABLES
51  
52 =head2 @PHASES
53  
54 The phases we know about, in order.
55  
56 =head1 FUNCTIONS
57  
58 =cut
59  
60 # }}}1
61  
62 # Phases are case-insensitive.
63 2 ▶our @PHASES; BEGIN { @PHASES = ('Check', 'Gen'); }
Subroutine BEGIN: called CC=1 SCAR=0.0
64 # *** This is where the default phase ($PHASES[0]) is set ***
65 # TODO? be more sophisticated about this :)
66  
67 # Internal function to regularize phase names.
68 sub _clean {
69 45 ▶ my $test_phase = shift or croak 'Need a phase name';
Subroutine _clean: called CC=6 SCAR=21.4
Branch: 1 branch
BranchTrueFalseTotal
my $test_phase = shift() or croak("Need a phase name") 0 45 45
70 45 $test_phase = lc $test_phase;
71 45 ▶ $test_phase = $PHASES[0]
Branch: 1 branch
BranchTrueFalseTotal
if $test_phase eq "first" or $test_phase eq "start" 0 45 45
Condition33%1/3
$test_phase eq "first" or $test_phase eq "start"
l!l&&r!l&&!r
0045
MC/DC0%0/2
$test_phase eq "first" or $test_phase eq "start"
$test_phase eq "first" $test_phase eq "start"
Truth table33%1/3
A or B
A= $test_phase eq "first"B= $test_phase eq "start"
ABresult
1X1
011
000
72 if $test_phase eq 'first' or $test_phase eq 'start';
73 45 ▶ $test_phase = $PHASES[$#PHASES]
Branch: 1 branch
BranchTrueFalseTotal
if $test_phase eq "last" or $test_phase eq "end" 0 45 45
Condition33%1/3
$test_phase eq "last" or $test_phase eq "end"
l!l&&r!l&&!r
0045
MC/DC0%0/2
$test_phase eq "last" or $test_phase eq "end"
$test_phase eq "last" $test_phase eq "end"
Truth table33%1/3
A or B
A= $test_phase eq "last"B= $test_phase eq "end"
ABresult
1X1
011
000
74 if $test_phase eq 'last' or $test_phase eq 'end';
75 45 return lc($test_phase);
76 } #_clean()
77  
78 =head2 is_phase
79  
80 Return truthy if the given argument is the name of a phase we know about.
81  
82 =cut
83  
84 sub is_phase {
85 0 ▶ my $test_phase = shift or croak 'Need a phase name';
Subroutine is_phase: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $test_phase = shift() or croak("Need a phase name") 0 0 0
86 0 $test_phase = _clean($test_phase);
87 0 ▶ my $curr_idx = first_index { lc($_) eq $test_phase } @PHASES;
Subroutine __ANON__: not called CC=1 SCAR=6.9
88 0 return $curr_idx+1; # -1 => falsy; all others => truthy
89 } #is_phase()
90  
91 =head2 is_last_phase
92  
93 Return truthy if the argument is the name of the last phase.
94 If no argument is given, checks the current phase
95 (L<App::hopen::BuildSystemGlobals/$Phase>).
96  
97 =cut
98  
99 6 ▶sub is_last_phase { _clean(shift // $Phase) eq lc($PHASES[$#PHASES]) }
Subroutine is_last_phase: called CC=2 SCAR=9.2
Pod documented
Condition33%1/3
shift() // $App::hopen::Phases::Phase
l!l&&r!l&&!r
600
MC/DC0%0/2
shift() // $App::hopen::Phases::Phase
shift() $App::hopen::Phases::Phase
Truth table33%1/3
A // B
A= shift()B= $App::hopen::Phases::Phase
ABresult
1X1
011
000
100  
101 =head2 phase_idx
102  
103 Get the index of the phase given as a parameter.
104 Returns undef if none. Phases are case-insensitive.
105  
106 =cut
107  
108 sub phase_idx {
109 27 ▶ my $test_phase = shift or croak 'Need a phase name';
Subroutine phase_idx: called CC=3 SCAR=11.3
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $test_phase = shift() or croak("Need a phase name") 0 27 27
110 27 $test_phase = _clean($test_phase);
111 27 ▶ my $curr_idx = first_index { lc($_) eq $test_phase } @PHASES;
Subroutine __ANON__: called CC=1 SCAR=0.0
112 27 ▶ return $curr_idx<0 ? undef : $curr_idx;
Branch: 1 branch
BranchTrueFalseTotal
$curr_idx < 0 ? : 0 27 27
113 } #phase_idx()
114  
115 =head2 curr_phase_idx
116  
117 Get the index of the current phase.
118  
119 =cut
120  
121 6 ▶sub curr_phase_idx { phase_idx $Phase }
Subroutine curr_phase_idx: called CC=1 SCAR=0.0
Pod documented
122  
123 =head2 next_phase
124  
125 Get the phase after the given on. Returns undef if the argument
126 is the last phase. Dies if the argument is not a phase.
127  
128 =cut
129  
130 sub next_phase {
131 6 ▶ my $test_phase = shift or croak 'Need a phase name';
Subroutine next_phase: called CC=4 SCAR=14.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
my $test_phase = shift() or croak("Need a phase name") 0 6 6
132 6 $test_phase = _clean($test_phase);
133 6 my $curr_idx = phase_idx $test_phase;
134 6 ▶ die "$test_phase is not a phase I know about" unless defined($curr_idx);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $curr_idx 0 6 6
135 6 ▶ return undef if $curr_idx == $#PHASES; # Last one
Branch: 1 branch
BranchTrueFalseTotal
if $curr_idx == $#App::hopen::Phases::PHASES 3 3 6
136  
137 3 return $PHASES[$curr_idx+1];
138 } #next_phase()
139  
140 =head1 ROUTINES FOR USE IN HOPEN FILES
141  
142 These are exported if the tag C<:hopenfile> is given on the C<use> line.
143  
144 =head2 on
145  
146 Take a given action only in a specified phase. Usage examples:
147  
148 on check => { foo => 42 }; # Just return the given hashref
149 on gen => 1337; # Returns { Gen => 1337 }
150 on check => sub { return { foo => 1337 } };
151 # Call the given sub and return its return value.
152  
153 This is designed for use within a hopen file.
154 See L<App::hopen/_run_phase> for the execution environment C<on()> is
155 designed to run in.
156  
157 When run as part of a hopen file, C<on()> will skip the rest of the file if it
158 runs. For example:
159  
160 say "Hello, world!"; # This always runs
161 on check => { answer => $answer }; # This runs during the Check phase
162 on gen => { done => true }; # This runs during the Gen phase
163 say "Phase was neither Check nor Gen"; # Doesn't run in Check or Gen
164  
165 TODO support C<< on '!last' => ... >> or similar to take action when not in
166 the given phase.
167  
168 =cut
169  
170 sub on {
171 6 ▶ my $caller = caller;
Subroutine on: called CC=4 SCAR=14.0
Pod documented
172 6 my (%args) = parameters([qw(phase value)], @_);
173  
174 6 my $which_phase = _clean($args{phase});
175 6 my $val = $args{value};
176  
177 6 my $which_idx = phase_idx($which_phase);
178 6 ▶ return if $which_idx != curr_phase_idx;
Branch: 1 branch
BranchTrueFalseTotal
if $which_idx != curr_phase_idx() 3 3 6
179  
180 # We are in the correct phase. Take appropriate action.
181 # However, don't change our own return value.
182 3 my $result;
183 3 ▶ if(ref $val eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $val eq 'CODE') { } 0 3 3
elsif (ref $val eq 'HASH') { } 3 0 3
184 0 $result = &$val;
185 } elsif(ref $val eq 'HASH') {
186 3 $result = $val; # TODO? clone?
187 } else {
188 0 $result = {$PHASES[$which_idx] => $val};
189 }
190  
191 # Stash the value for the caller.
192 {
193 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
194 3 ${ $caller . "::__R_on_result" } = $result;
195 }
196  
197 # Done --- skip the rest of the hopen file if we're in one.
198 3 ▶ hlog { 'Done with script for phase ``' . $args{phase} . "''" } 3;
Subroutine __ANON__: not called CC=1 SCAR=1.2
199 3 eval {
200 2 ▶ no warnings 'exiting';
Subroutine BEGIN: called CC=1 SCAR=0.0
201 3 last __R_DO;
202 };
203 } #on()
204  
205 2 1;
206 __END__
207 # vi: set fdm=marker: #
« blib/lib/App/hopen/Gen/MSBuild/AssetGraphNode.pm ↑ Summary blib/lib/App/hopen/MYhopen.pm »