Hopen/Gen/Make.pm untested

filter: Statementstmt 0.0% 0 / 71 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 110 CC 5 SCAR 23.7
CC
5
Cov
0%
finalize
30.0
visit_goal
2.0
default_toolset
2.0
CRAP 10.8
1 # Build::Hopen::Gen::Make - generator for a generic make(1).
2 package Build::Hopen::Gen::Make;
3 use Build::Hopen qw(:default $QUIET);
4 use Build::Hopen::Base;
5 use parent 'Exporter';
6  
7 our $VERSION = '0.000008'; # TRIAL
8  
9 use Hash::Ordered;
10  
11 use parent 'Build::Hopen::Gen';
12 use Class::Tiny {
13 targets => sub { Hash::Ordered->new() }
14 };
15  
16 use Build::Hopen::Phases qw(is_last_phase);
17 use Build::Hopen::Arrrgs;
18  
19 # Docs {{{1
20  
21 =head1 NAME
22  
23 Build::Hopen::Gen::Make - hopen generator for simple Makefiles
24  
25 =head1 SYNOPSIS
26  
27 This generator makes a Makefile that does its best to run on cmd.exe or sh(1).
28  
29 =head1 ATTRIBUTES
30  
31 =head2 targets
32  
33 A L<Hash::Ordered> of the targets, in the order encountered.
34  
35 =head1 FUNCTIONS
36  
37 =cut
38  
39 # }}}1
40  
41 =head2 visit_goal
42  
43 Add a target corresponding to the name of the goal.
44  
45 =cut
46  
47 sub visit_goal {
48 my $self = shift or croak 'Need an instance';
49 my $goal = shift or croak 'Need a goal';
50 $self->targets->set($goal->name, $goal);
51 } #visit_goal()
52  
53 #=head2 visit_node
54 #
55 #TODO
56 #
57 #=cut
58 #
59 #sub visit_node {
60 # my $self = shift or croak 'Need an instance';
61 # ...
62 #} #visit_node()
63  
64 =head2 finalize
65  
66 Write out the Makefile.
67  
68 =cut
69  
70 sub finalize {
71 my ($self, %args) = parameters('self', [qw(phase dag data)], @_);
72 hlog { Finalizing => __PACKAGE__ , '- phase', $args{phase} };
73 return unless is_last_phase $args{phase};
74  
75 # During the Gen phase, create the Makefile
76 open my $fh, '>', $self->dest_dir->file('Makefile') or die "Couldn't create Makefile";
77 print $fh <<EOT;
78 # Makefile generated by hopen (https://github.com/cxw42/hopen)
79 # at @{[scalar gmtime]} GMT
80 # From ``@{[$self->proj_dir->absolute]}'' into ``@{[$self->dest_dir->absolute]}''
81  
82 EOT
83  
84 my $iter = $self->targets->iterator;
85 # TODO make this more robust and flexible
86 while( my ($name, $goal) = $iter->() ) {
87 hlog { __PACKAGE__, 'goal', $name } 2;
88 say $fh "### Goal $name ###";
89 unless(eval { scalar @{$goal->outputs->{work}} }) {
90 warn "No work for goal $name" unless $QUIET;
91 next;
92 }
93  
94 my @work = @{$goal->outputs->{work}};
95 unshift @work, { to => [$name], from => $work[0]->{to}, how => undef };
96 # Make a fake record for the goal. TODO move this to visit_goal?
97  
98 hlog { 'Work to do', Dumper(\@work) } 3;
99 foreach my $item (@work) {
100 next unless @{$item->{from}}; # no prerequisites => assume it's a file
101 say $fh $item->{to}->[0], ': ', join(' ', @{$item->{from}});
102 say $fh (_expand($item) =~ s/^/\t/gmr);
103 say $fh '';
104 }
105  
106 } #foreach goal
107 close $fh;
108 } #finalize()
109  
110 =head2 default_toolset
111  
112 Returns the package name of the default toolset for this generator,
113 which is C<Gnu> (i.e., L<Build::Hopen::T::Gnu>).
114  
115 =cut
116  
117 sub default_toolset { 'Gnu' }
118  
119 =head1 INTERNALS
120  
121 =head2 _expand
122  
123 Produce the command line or lines associated with a work item. Used by
124 L</finalize>.
125  
126 =cut
127  
128 sub _expand {
129 my $item = shift or croak 'Need a work item';
130 hlog { __PACKAGE__ . '::expand()', Dumper($item) } 2;
131 my $out = $item->{how} or return ''; # no `how` => no output; not an error
132  
133 $out =~ s{#first\b}{$item->{from}->[0] // ''}ge; # first input
134 $out =~ s{#all\b}{join(' ', @{$item->{from}})}ge; # all inputs
135 $out =~ s{#out\b}{$item->{to}->[0] // ''}ge;
136  
137 return $out;
138 } #_expand()
139  
140 1;
141 __END__