blib/lib/App/hopen/Gen.pm

filter: Statementstmt 85.5% 83 / 97 Branchbran 37.5% 9 / 24 Conditioncond 25.0% 1 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 75.0% 21 / 28 Podpod 100% 9 / 9 totaltotal 75.0% 123 / 164 CC 17 SCAR 13.9
CC
17
Cov
74%
run_build
12.0
asset
10.5
connect
5.0
CRAP 4.0
« blib/lib/App/hopen/T/MSCL/C/LinkCmd.pm ↑ Summary blib/lib/App/hopen/Gen/Ninja/AssetGraphNode.pm »
1 # App::hopen::Gen - base class for hopen generators
2 package App::hopen::Gen;
3 1 ▶use Data::Hopen qw(:default *QUIET);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Data::Hopen::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.000015'; # TRIAL
8  
9 1 ▶use parent 'Data::Hopen::Visitor';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Class::Tiny qw(proj_dir dest_dir), {
Subroutine BEGIN: called CC=1 SCAR=0.0
11 architecture => '',
12  
13 # private
14 _assets => undef, # A Data::Hopen::G::DAG of the assets
15 3 ▶ _assetop_by_asset => sub { +{} }, # Indexed by refaddr($asset)
Subroutine __ANON__: called CC=1 SCAR=0.0
16 1 };
17  
18 1 ▶use App::hopen::Asset;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use App::hopen::BuildSystemGlobals;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use App::hopen::Util::String qw(eval_here);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use Data::Hopen::G::DAG;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 1 ▶use Data::Hopen::Util::Data qw(forward_opts);
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use File::pushd qw(pushd);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use Path::Class ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 1 ▶use Scalar::Util qw(refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 # Docs {{{1
28  
29 =head1 NAME
30  
31 App::hopen::Gen - Base class for hopen generators
32  
33 =head1 SYNOPSIS
34  
35 The code that generates blueprints for specific build systems
36 lives under C<App::hopen::Gen>. L<App::hopen> calls modules
37 under C<App::hopen::Gen> to create the blueprints. Those modules must
38 implement the interface defined here.
39  
40 =head1 ATTRIBUTES
41  
42 =head2 architecture
43  
44 The architecture. The use of this is defined by the specific
45 generator or toolset.
46  
47 =head2 proj_dir
48  
49 (Required) A L<Path::Class::Dir> instance specifying the root directory of
50 the project.
51  
52 =head2 dest_dir
53  
54 (Required) A L<Path::Class::Dir> instance specifying where the generated output
55 (e.g., blueprint or other files) should be written.
56  
57 =head2 _assets (Internal)
58  
59 A L<Data::Hopen::G::DAG> of L<App::hopen::G::AssetOp> instances representing
60 the L<App::Hopen::Asset>s to be created when a build is run.
61  
62 =head1 FUNCTIONS
63  
64 A generator (C<App::hopen::Gen> subclass) is a Visitor plus some.
65  
66 B<Note>:
67 The generator does not have access to L<Data::Hopen::G::Link> instances.
68 That lack of access is the primary distinction between Ops and Links.
69  
70 =cut
71  
72 # }}}1
73  
74 =head2 asset
75  
76 Called by an Op (L<App::hopen::G::Op> subclass) to add an asset
77 (L<App::hopen::G::AssetOp> instance) to the build. Usage:
78  
79 $Generator->asset([-asset=>]$asset, [-from=>]$from[, [-how=>]$how]);
80  
81 If C<$how> is specified, it will be saved in the C<AssetOp> for use later.
82 Later calls with the same asset and a defined C<$how> will overwrite the
83 C<how> value in the C<AssetOp>. Specify 'UNDEF' as the C<$how> to
84 expressly undefine a C<how>.
85  
86 Returns the C<AssetOp>.
87  
88 =cut
89  
90 12 ▶sub asset {
Subroutine asset: called CC=6 SCAR=23.5
Pod documented
91 12 my ($self, %args) = getparameters('self', [qw(asset; how)], @_);
92 0 ▶ hlog { 'Generator adding asset at',refaddr($args{asset}),$args{asset} } 3;
Subroutine __ANON__: not called CC=1 SCAR=1.2
93  
94 12 my $existing_op = $self->_assetop_by_asset->{refaddr($args{asset})};
95  
96 # Update an existing op
97 12 ▶ if(defined $existing_op) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $existing_op) 0 12 12
98 0 ▶ if( ($args{how}//'') eq 'UNDEF') {
Branch: 2 branches
BranchTrueFalseTotal
if (($args{'how'} // '') eq 'UNDEF') { } 0 0 0
elsif (defined $args{'how'}) { } 0 0 0
Condition0%0/2
$args{"how"} // ""
l!l
00
MC/DC0%0/1
$args{"how"} // ""
$args{"how"}
Truth table0%0/2
A
A= $args{"how"}
Aresult
11
00
99 0 $existing_op->how(undef);
100 } elsif(defined $args{how}) {
101 0 $existing_op->how($args{how});
102 }
103 0 return $existing_op;
104 }
105  
106 # Need to create an op. First, load its class.
107 12 my $class = $self->_assetop_class;
108  
109 12 eval_here <<EOT;
110 require $class;
111 EOT
112 12 ▶ die "$@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 12 12
113  
114 # Create a new op
115 12 my $op = $class->new(name => 'Op:<<' . $args{asset}->target . '>>',
116 forward_opts(\%args, qw(asset how)));
117 12 $self->_assetop_by_asset->{refaddr($args{asset})} = $op;
118 12 $self->_assets->add($op);
119 12 return $op;
120 } #asset()
121  
122 =head2 connect
123  
124 Add a dependency edge between two assets or goals. Any assets must have already
125 been added using L</asset>. Usage:
126  
127 $Generator->connect([-from=>]$from, [-to=>$to]);
128  
129 TODO add missing assets automatically?
130  
131 TODO rename the asset-graph public interface so it's more clear that it's
132 the asset graph and not the command graph.
133  
134 =cut
135  
136 12 ▶sub connect {
Subroutine connect: called CC=5 SCAR=16.2
Pod documented
137 12 my ($self, %args) = getparameters('self', [qw(from to)], @_);
138 12 my %nodes;
139  
140 # Get the nodes if we were passed assets.
141 12 foreach my $field (qw(from to)) {
142 24 ▶ if(eval { $args{$field}->DOES('App::hopen::Asset') }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $args{$field}->DOES('App::hopen::Asset') } }) { } 15 9 24
143 15 $nodes{$field} = $self->_assetop_by_asset->{refaddr($args{$field})};
144 } else {
145 9 $nodes{$field} = $args{$field};
146 }
147 }
148  
149 # TODO better error messages
150 12 ▶ croak "No From node for asset " . refaddr($args{from}) unless $nodes{from};
Branch: 1 branch
BranchTrueFalseTotal
unless $nodes{"from"} 0 12 12
151 12 ▶ croak "No To node for asset " . refaddr($args{to}) unless $nodes{to};
Branch: 1 branch
BranchTrueFalseTotal
unless $nodes{"to"} 0 12 12
152 12 $self->_assets->connect($nodes{from}, $nodes{to});
153 } #connect()
154  
155 =head2 asset_default_goal
156  
157 Read-only accessor for the default goal of the asset graph
158  
159 =cut
160  
161 4 ▶sub asset_default_goal () { shift->_assets->default_goal }
Subroutine asset_default_goal: called CC=1 SCAR=0.0
Pod documented
162  
163 =head2 run_build
164  
165 Runs the build tool for which this generator has created blueprint files.
166 Runs the tool with the destination directory as the current dir.
167  
168 =cut
169  
170 0 ▶sub run_build {
Subroutine run_build: not called CC=3 SCAR=24.8
Pod documented
171 0 ▶ my $self = shift or croak 'Need an instance';
Branch: 1 branch
BranchTrueFalseTotal
my $self = shift() or croak("Need an instance") 0 0 0
172 0 my $abs_dir = $DestDir->absolute;
173 # NOTE: You have to call this *before* pushd() or chdir(), because
174 # it may be a relative path, and absolute() converts with respect
175 # to cwd at the time of the call.
176 0 my $dir = pushd($abs_dir);
177 0 ▶ say "Building in ${abs_dir}..." unless $QUIET;
Branch: 1 branch
BranchTrueFalseTotal
unless $App::hopen::Gen::QUIET 0 0 0
178 0 $self->_run_build();
179 } #run_build()
180  
181 =head2 BUILD
182  
183 Constructor.
184  
185 =cut
186  
187 6 ▶sub BUILD {
Subroutine BUILD: called CC=3 SCAR=11.2
Pod documented
188 6 my ($self, $args) = @_;
189  
190 # Enforce the required argument types
191 croak "Need a project directory (Path::Class::Dir)"
192 6 ▶ unless eval { $self->proj_dir->DOES('Path::Class::Dir') };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->proj_dir->DOES("Path::Class::Dir") } } 0 6 6
193 croak "Need a destination directory (Path::Class::Dir)"
194 6 ▶ unless eval { $self->dest_dir->DOES('Path::Class::Dir') };
Branch: 1 branch
BranchTrueFalseTotal
unless eval { do { $self->dest_dir->DOES("Path::Class::Dir") } } 0 6 6
195  
196 # Create the asset graph
197 6 $self->_assets(hnew DAG => 'asset graph');
198 6 $self->_assets->goal('__R_asset_default_goal');
199 # Create and set default goal
200 } #BUILD()
201  
202 =head1 FUNCTIONS TO BE IMPLEMENTED BY SUBCLASSES
203  
204 =head2 _assetop_class
205  
206 (Required) Returns the name of the L<App::hopen::G::AssetOp> subclass that
207 should be used to represent assets in the C<_assets> graph.
208  
209 =cut
210  
211 0 ▶sub _assetop_class { ... }
Subroutine _assetop_class: not called CC=1 SCAR=6.9
212  
213 =head2 default_toolset
214  
215 (Required) Returns the package stem of the default toolset for this generator.
216  
217 When a hopen file invokes C<use language "Foo">, hopen will load
218 C<< App::hopen::T::<stem>::Foo >>. C<< <stem> >> is the return
219 value of this function unless the user has specified a different toolset.
220  
221 As a sanity check, hopen will first try to load C<< App::hopen::T::<stem> >>,
222 so make sure that is a valid package.
223  
224 =cut
225  
226 0 ▶sub default_toolset { ... }
Subroutine default_toolset: not called CC=1 SCAR=6.9
Pod documented
227  
228 =head2 finalize
229  
230 (Optional)
231 Do whatever the generator wants to do to finish up. By default, no-op.
232 Is provided the L<Data::Hopen::G::DAG> instance as a parameter. Usage:
233  
234 $generator->finalize(-phase=>$Phase, -graph=>$Build,
235 -data=>$data)
236  
237 C<$dag> is the command graph, and C<$data> is the output from the
238 command graph.
239  
240 C<finalize> is always called with named parameters.
241  
242 =cut
243  
244 0 ▶sub finalize { }
Subroutine finalize: not called CC=1 SCAR=0.0
Pod documented
245  
246 =head2 _run_build
247  
248 (Optional)
249 Implementation of L</run_build>. The default does not die, but does warn().
250  
251 =cut
252  
253 0 ▶sub _run_build {
Subroutine _run_build: not called CC=1 SCAR=6.9
254 0 warn "This generator is not configured to run a build tool. Sorry!";
255 } #_run_build()
256  
257 =head2 visit_goal
258  
259 Add a target corresponding to the name of the goal. Usage:
260  
261 $Generator->visit_goal($node, $node_inputs);
262  
263 This happens while the command graph is being run.
264  
265 This can be overriden by a generator that wants to handle
266 L<Data::Hopen::G::Goal> nodes differently.
267 For example, the generator may want to change the goal's C<outputs>.
268  
269 =cut
270  
271 3 ▶sub visit_goal {
Subroutine visit_goal: called CC=4 SCAR=14.2
Pod documented
272 3 my ($self, %args) = getparameters('self', [qw(goal node_inputs)], @_);
273  
274 # --- Add the goal to the asset graph ---
275  
276 #my $asset_goal = $self->_assets->goal($args{goal}->name);
277 my $phony_asset = App::hopen::Asset->new(
278 target => $args{goal}->name,
279 3 made_by => $self,
280 );
281 3 my $phony_node = $self->asset(-asset => $phony_asset, -how => '');
282 # \p how defined but falsy => it's a goal
283 3 $self->connect($phony_node, $self->asset_default_goal);
284  
285 # Pull the inputs. TODO refactor out the code in common with
286 # AhG::Cmd::input_assets().
287 my $hrSourceFiles =
288 3 ▶ $args{node_inputs}->find(-name => 'made',
Condition50%1/2
$args{"node_inputs"}->find("-name", "made", "-set", "*", "-levels", "local") // {}
l!l
30
MC/DC0%0/1
$args{"node_inputs"}->find("-name", "made", "-set", "*", "-levels", "local") // {}
$args{"node_inputs"}->find("-name", "made", "-set", "*", "-levels", "local")
Truth table50%1/2
A
A= $args{"node_inputs"}->find("-name", "made", "-set", "*", "-levels", "local")
Aresult
11
00
289 -set => '*', -levels => 'local') // {};
290 die 'No input files to goal ' . $args{goal}->name
291 3 ▶ unless scalar keys %$hrSourceFiles;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %$hrSourceFiles 0 3 3
292  
293 3 my $lrSourceFiles = $hrSourceFiles->{(keys %$hrSourceFiles)[0]};
294 0 ▶ hlog { 'found inputs to goal', $args{goal}->name, Dumper($lrSourceFiles) } 2;
Subroutine __ANON__: not called CC=1 SCAR=1.2
295  
296 # TODO? verify that all the assets are actually in the graph first?
297 3 $self->connect($_, $phony_node) foreach @$lrSourceFiles;
298  
299 } #visit_goal()
300  
301 =head2 visit_node
302  
303 (Optional)
304 Do whatever the generator wants to do with a L<Data::Hopen::G::Node> that
305 is not a Goal (see L</visit_goal>). By default, no-op. Usage:
306  
307 $generator->visit_node($node)
308  
309 =cut
310  
311 9 ▶sub visit_node { }
Subroutine visit_node: called CC=1 SCAR=0.0
Pod documented
312  
313 1 1;
314 __END__
« blib/lib/App/hopen/T/MSCL/C/LinkCmd.pm ↑ Summary blib/lib/App/hopen/Gen/Ninja/AssetGraphNode.pm »