Make.pm

filter: Statementstmt 97.1% 103 / 106 Branchbran 81.2% 26 / 32 Conditioncond 80.0% 8 / 10 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 94.1% 16 / 17 Podpod 0.0% 0 / 1 totaltotal 91.2% 157 / 172 CC 27 SCAR 21.0
CC
27
Cov
93%
__ANON__
14.3
_resolve_ref
11.1
run
4.0
CRAP 8.2
« Make/Docker/Container.pm ↑ Summary Make/Docker/Image/Hub.pm »
1 package Beam::Make;
2 5 our $VERSION = '0.003';
3 # ABSTRACT: Recipes to declare and resolve dependencies between things
4  
5 #pod =head1 SYNOPSIS
6 #pod
7 #pod ### container.yml
8 #pod # This Beam::Wire container stores shared objects for our recipes
9 #pod dbh:
10 #pod $class: DBI
11 #pod $method: connect
12 #pod $args:
13 #pod - dbi:SQLite:RECENT.db
14 #pod
15 #pod ### Beamfile
16 #pod # This file contains our recipes
17 #pod # Download a list of recent changes to CPAN
18 #pod RECENT-6h.json:
19 #pod commands:
20 #pod - curl -O https://www.cpan.org/RECENT-6h.json
21 #pod
22 #pod # Parse that JSON file into a CSV using an external program
23 #pod RECENT-6h.csv:
24 #pod requires:
25 #pod - RECENT-6h.json
26 #pod commands:
27 #pod - yfrom json RECENT-6h.json | yq '.recent.[]' | yto csv > RECENT-6h.csv
28 #pod
29 #pod # Build a SQLite database to hold the recent data
30 #pod RECENT.db:
31 #pod $class: Beam::Make::DBI::Schema
32 #pod dbh: { $ref: 'container.yml:dbh' }
33 #pod schema:
34 #pod - table: recent
35 #pod columns:
36 #pod - path: VARCHAR(255)
37 #pod - epoch: DOUBLE
38 #pod - type: VARCHAR(10)
39 #pod
40 #pod # Load the recent data CSV into the SQLite database
41 #pod cpan-recent:
42 #pod $class: Beam::Make::DBI::CSV
43 #pod requires:
44 #pod - RECENT.db
45 #pod - RECENT-6h.csv
46 #pod dbh: { $ref: 'container.yml:dbh' }
47 #pod table: recent
48 #pod file: RECENT-6h.csv
49 #pod
50 #pod ### Load the recent data into our database
51 #pod $ beam make cpan-recent
52 #pod
53 #pod =head1 DESCRIPTION
54 #pod
55 #pod C<Beam::Make> allows an author to describe how to build some thing (a
56 #pod file, some data in a database, an image, a container, etc...) and the
57 #pod relationships between things. This is similar to the classic C<make>
58 #pod program used to build some software packages.
59 #pod
60 #pod Each thing is a C<recipe> and can depend on other recipes. A user runs
61 #pod the C<beam make> command to build the recipes they want, and
62 #pod C<Beam::Make> ensures that the recipe's dependencies are satisfied
63 #pod before building the recipe.
64 #pod
65 #pod This class is a L<Beam::Runnable> object and can be embedded in other
66 #pod L<Beam::Wire> containers.
67 #pod
68 #pod =head2 Recipe Classes
69 #pod
70 #pod Unlike C<make>, C<Beam::Make> recipes can do more than just execute
71 #pod a series of shell scripts. Each recipe is a Perl class that describes
72 #pod how to build the desired thing and how to determine if that thing needs
73 #pod to be rebuilt.
74 #pod
75 #pod These recipe classes come with C<Beam::Make>:
76 #pod
77 #pod =over
78 #pod
79 #pod =item * L<File|Beam::Make::File> - The default recipe class that creates
80 #pod a file using one or more shell commands (a la C<make>)
81 #pod
82 #pod =item * L<DBI|Beam::Make::DBI> - Write data to a database
83 #pod
84 #pod =item * L<DBI::Schema|Beam::Make::DBI::Schema> - Create a database
85 #pod schema
86 #pod
87 #pod =item * L<DBI::CSV|Beam::Make::DBI::CSV> - Load data from a CSV into
88 #pod a database table
89 #pod
90 #pod =item * L<Docker::Image|Beam::Make::Docker::Image> - Build or pull a Docker image
91 #pod
92 #pod =item * L<Docker::Container|Beam::Make::Docker::Container> - Build a Docker container
93 #pod
94 #pod =back
95 #pod
96 #pod Future recipe class ideas are:
97 #pod
98 #pod =over
99 #pod
100 #pod =item *
101 #pod
102 #pod B<Template rendering>: Files could be generated from a configuration
103 #pod file or database and a template.
104 #pod
105 #pod =item *
106 #pod
107 #pod B<Docker compose>: An entire docker-compose network could be rebuilt.
108 #pod
109 #pod =item *
110 #pod
111 #pod B<System services (init daemon, systemd service, etc...)>: Services
112 #pod could depend on their configuration files (built with a template) and be
113 #pod restarted when their configuration file is updated.
114 #pod
115 #pod =back
116 #pod
117 #pod =head2 Beamfile
118 #pod
119 #pod The C<Beamfile> defines the recipes. To avoid the pitfalls of C<Makefile>, this is
120 #pod a YAML file containing a mapping of recipe names to recipe configuration. Each
121 #pod recipe configuration is a mapping containing the attributes for the recipe class.
122 #pod The C<$class> special configuration key declares the recipe class to use. If no
123 #pod C<$class> is specified, the default L<Beam::Wire::File> recipe class is used.
124 #pod All recipe classes inherit from L<Beam::Class::Recipe> and have the L<name|Beam::Class::Recipe/name>
125 #pod and L<requires|Beam::Class::Recipe/requires> attributes.
126 #pod
127 #pod For examples, see the L<Beam::Wire examples directory on
128 #pod Github|https://github.com/preaction/Beam-Make/tree/master/eg>.
129 #pod
130 #pod =head2 Object Containers
131 #pod
132 #pod For additional configuration, create a L<Beam::Wire> container and
133 #pod reference the objects inside using C<< $ref: "<container>:<service>" >>
134 #pod as the value for a recipe attribute.
135 #pod
136 #pod =head1 TODO
137 #pod
138 #pod =over
139 #pod
140 #pod =item Target names in C<Beamfile> should be regular expressions
141 #pod
142 #pod This would work like Make's wildcard recipes, but with Perl regexp. The
143 #pod recipe object's name is the real name, but the recipe chosen is the one
144 #pod the matches the regexp.
145 #pod
146 #pod =item Environment variables should interpolate into all attributes
147 #pod
148 #pod Right now, the C<< NAME=VALUE >> arguments to C<beam make> only work in
149 #pod recipes that use shell scripts (like L<Beam::Make::File>). It would be
150 #pod nice if they were also interpolated into other recipe attributes.
151 #pod
152 #pod =item Recipes should be able to require wildcards and directories
153 #pod
154 #pod Recipe requirements should be able to depend on patterns, like all
155 #pod C<*.conf> files in a directory. It should also be able to depend on
156 #pod a directory, which would be the same as depending on every file,
157 #pod recursively, in that directory.
158 #pod
159 #pod This would allow rebuilding a ZIP file when something changes, or
160 #pod rebuilding a Docker image when needed.
161 #pod
162 #pod =item Beam::Wire should support the <container>:<service> syntax
163 #pod for references
164 #pod
165 #pod The L<Beam::Wire> class should handle the C<BEAM_PATH> environment
166 #pod variable directly and be able to resolve services from other files
167 #pod without building another C<Beam::Wire> object in the container.
168 #pod
169 #pod =item Beam::Wire should support resolving objects in arbitrary data
170 #pod structures
171 #pod
172 #pod L<Beam::Wire> should have a class method that one can pass in a hash and
173 #pod get back a hash with any C<Beam::Wire> object references resolved,
174 #pod including C<$ref> or C<$class> object.
175 #pod
176 #pod =back
177 #pod
178 #pod =head1 SEE ALSO
179 #pod
180 #pod L<Beam::Wire>
181 #pod
182 #pod =cut
183  
184 5 ▶use v5.20;
Subroutine BEGIN: called CC=1 SCAR=0.0
185 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
186 5 ▶use Log::Any qw( $LOG );
Subroutine BEGIN: called CC=1 SCAR=0.0
187 5 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
188 5 ▶use experimental qw( signatures postderef );
Subroutine BEGIN: called CC=1 SCAR=0.0
189 5 ▶use Time::Piece;
Subroutine BEGIN: called CC=1 SCAR=0.0
190 5 ▶use YAML ();
Subroutine BEGIN: called CC=1 SCAR=0.0
191 5 ▶use Beam::Wire;
Subroutine BEGIN: called CC=1 SCAR=0.0
192 5 ▶use Scalar::Util qw( blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
193 5 ▶use List::Util qw( max );
Subroutine BEGIN: called CC=1 SCAR=0.0
194 5 ▶use Beam::Make::Cache;
Subroutine BEGIN: called CC=1 SCAR=0.0
195 5 ▶use File::stat;
Subroutine BEGIN: called CC=1 SCAR=0.0
196 5 with 'Beam::Runnable';
197  
198 0 ▶has conf => ( is => 'ro', default => sub { YAML::LoadFile( 'Beamfile' ) } );
Subroutine __ANON__: not called CC=1 SCAR=1.2
199 # Beam::Wire container objects
200 3 ▶has _wire => ( is => 'ro', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
201  
202 8 ▶sub run( $self, @argv ) {
Subroutine run: called CC=4 SCAR=13.9
Pod undocumented
203 8 my ( @targets, %vars );
204  
205 8 for my $arg ( @argv ) {
206 12 ▶ if ( $arg =~ /^([^=]+)=([^=]+)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg =~ /^([^=]+)=([^=]+)$/u) { } 3 9 12
207 3 $vars{ $1 } = $2;
208 }
209 else {
210 9 push @targets, $arg;
211 }
212 }
213  
214 8 local @ENV{ keys %vars } = values %vars;
215 8 my $conf = $self->conf;
216 8 my $cache = Beam::Make::Cache->new;
217  
218 # Targets must be built in order
219 # Prereqs satisfied by original target remain satisfied
220 8 my %recipes; # Built recipes
221 my @target_stack;
222 # Build a target (if necessary) and return its last modified date.
223 # Each dependent will be checked against their depencencies' last
224 # modified date to see if they need to be updated
225 18 ▶ my $build = sub( $target ) {
Subroutine __ANON__: called CC=14 SCAR=26.6
226 18 $LOG->debug( "Want to build: $target" );
227 18 ▶ if ( grep { $_ eq $target } @target_stack ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$_ eq $target;} @target_stack) 0 18 18
228 0 die "Recursion at @target_stack";
229 }
230 # If we already have the recipe, it must already have been run
231 18 ▶ if ( $recipes{ $target } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($recipes{$target}) 1 17 18
232 1 $LOG->debug( "Nothing to do: $target already built" );
233 1 return $recipes{ $target }->last_modified;
234 }
235  
236 # If there is no recipe for the target, it must be a source
237 # file. Source files cannot be built, but we do want to know
238 # when they were last modified
239 17 ▶ if ( !$conf->{ $target } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($conf->{$target}) 1 16 17
240 1 ▶ $LOG->debug(
Branch: 1 branch
BranchTrueFalseTotal
-e $target ? : 0 1 1
241 "$target has no recipe and "
242 . ( -e $target ? 'exists as a file' : 'does not exist as a file' )
243 );
244 1 ▶ return stat( $target )->mtime if -e $target;
Branch: 1 branch
BranchTrueFalseTotal
if -e $target 0 1 1
245 1 die $LOG->errorf( q{No recipe for target "%s" and file does not exist}."\n", $target );
246 }
247  
248 # Resolve any references in the recipe object via Beam::Wire
249 # containers.
250 16 my $target_conf = $self->_resolve_ref( $conf->{ $target } );
251 16 ▶ my $class = delete( $target_conf->{ '$class' } ) || 'Beam::Make::File';
Condition100%2/2
delete $target_conf->{'$class'} || 'Beam::Make::File'
l!l
106
MC/DC100%1/1
delete $target_conf->{'$class'} || 'Beam::Make::File'
delete $target_conf->{'$class'}
Truth table100%2/2
A
A= delete $target_conf->{'$class'}
Aresult
11
00
252 16 $LOG->debug( "Building recipe object $target ($class)" );
253 16 eval "require $class";
254 16 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 16 16
255 0 die "Could not load $class: $@";
256 }
257 16 my $recipe = $recipes{ $target } = $class->new(
258 $target_conf->%*,
259 name => $target,
260 cache => $cache,
261 );
262  
263 16 my $requires_modified = 0;
264 16 ▶ if ( my @requires = $recipe->requires->@* ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@requires) = @{$recipe->requires}) 9 7 16
265 9 $LOG->debug( "Checking requirements for $target: @requires" );
266 9 push @target_stack, $target;
267 9 for my $require ( @requires ) {
268 9 $requires_modified = max $requires_modified, __SUB__->( $require );
269 }
270 7 pop @target_stack;
271 }
272  
273 # Do we need to build this recipe?
274 14 my $result;
275 14 ▶ if ( $requires_modified > ( $recipe->last_modified || -1 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($requires_modified > ($recipe->last_modified || -1)) { } 10 4 14
Condition100%2/2
$recipe->last_modified || -1
l!l
410
MC/DC100%1/1
$recipe->last_modified || -1
$recipe->last_modified
Truth table100%2/2
A
A= $recipe->last_modified
Aresult
11
00
276 10 $LOG->debug( "Building $target" );
277 10 $recipe->make( %vars );
278 9 $result = $LOG->info( "$target updated (modified: " . $recipe->last_modified . ")" );
279 }
280 else {
281 4 $result = $LOG->info( "$target up-to-date (modified: " . $recipe->last_modified . ")" );
282 }
283 13 ▶ if ( !@target_stack && !$LOG->is_info ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@target_stack or $Beam::Make::LOG->is_info) 7 6 13
Condition66%2/3
@target_stack or $Beam::Make::LOG->is_info
l!l&&r!l&&!r
607
MC/DC50%1/2
@target_stack or $Beam::Make::LOG->is_info
@target_stack $Beam::Make::LOG->is_info
Truth table66%2/3
A or B
A= @target_stackB= $Beam::Make::LOG->is_info
ABresult
1X1
011
000
284 # We were directly asked to build this, so let the user
285 # know about it
286 7 say $result;
287 }
288 13 return $recipe->last_modified;
289 8 };
290 8 $build->( $_ ) for @targets;
291 }
292  
293 # Resolve any references via Beam::Wire container lookups
294 134 ▶sub _resolve_ref( $self, $conf ) {
Subroutine _resolve_ref: called CC=11 SCAR=24.1
295 134 ▶ return $conf if !ref $conf || blessed $conf;
Branch: 1 branch
BranchTrueFalseTotal
if not ref $conf or builtin::blessed($conf) 61 73 134
Condition66%2/3
not ref $conf or builtin::blessed($conf)
l!l&&r!l&&!r
61073
MC/DC50%1/2
not ref $conf or builtin::blessed($conf)
not ref $conf builtin::blessed($conf)
Truth table66%2/3
not(A) or B
A= not ref $confB= builtin::blessed($conf)
ABresult
1X1
011
000
296 73 ▶ if ( ref $conf eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $conf eq 'HASH') { } 44 29 73
elsif (ref $conf eq 'ARRAY') { } 29 0 29
297 44 ▶ if ( grep { $_ !~ /^\$/ } keys %$conf ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {not $_ =~ /^\$/u;} keys %$conf) { } 37 7 44
298 37 my %resolved;
299 37 for my $key ( keys %$conf ) {
300 74 $resolved{ $key } = $self->_resolve_ref( $conf->{ $key } );
301 }
302 37 return \%resolved;
303 }
304 else {
305 # All keys begin with '$', so this must be a reference
306 # XXX: We should add the 'file:path' syntax to
307 # Beam::Wire directly. We could even call it as a class
308 # method! We should also move BEAM_PATH resolution to
309 # Beam::Wire directly...
310 # A single Beam::Wire->resolve( $conf ) should recursively
311 # resolve the refs in a hash (like this entire subroutine
312 # does), but also allow defining inline objects (with
313 # $class)
314 7 my ( $file, $service ) = split /:/, $conf->{ '$ref' }, 2;
315 7 my $wire = $self->_wire->{ $file };
316 7 ▶ if ( !$wire ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($wire) 1 6 7
317 1 for my $path ( split /:/, $ENV{BEAM_PATH} ) {
318 1 ▶ next unless -e join '/', $path, $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -e join("/", $path, $file) 0 1 1
319 1 $wire = $self->_wire->{ $file } = Beam::Wire->new( file => join '/', $path, $file );
320 }
321 }
322 7 return $wire->get( $service );
323 }
324 }
325 elsif ( ref $conf eq 'ARRAY' ) {
326 29 my @resolved;
327 29 for my $i ( 0..$#$conf ) {
328 44 $resolved[$i] = $self->_resolve_ref( $conf->[$i] );
329 }
330 29 return \@resolved;
331 }
332 }
333  
334 5 1;
335  
336 __END__
« Make/Docker/Container.pm ↑ Summary Make/Docker/Image/Hub.pm »