File Coverage

blib/lib/Beam/Make/File.pm
Criterion Covered Total %
statement 38 39 97.4
branch 5 8 62.5
condition n/a
subroutine 10 10 100.0
pod 2 2 100.0
total 55 59 93.2


line stmt bran cond sub pod time code
1             package Beam::Make::File;
2             our $VERSION = '0.002';
3             # ABSTRACT: A Beam::Make recipe to build a file from shell scripts
4              
5             #pod =head1 SYNOPSIS
6             #pod
7             #pod ### Beamfile
8             #pod a.out:
9             #pod requires:
10             #pod - main.c
11             #pod commands:
12             #pod - cc -Wall main.c
13             #pod
14             #pod =head1 DESCRIPTION
15             #pod
16             #pod This L<Beam::Make> recipe class creates a file by running one or more
17             #pod shell scripts. The recipe's name should be the file that will be created
18             #pod by the recipe.
19             #pod
20             #pod =head1 SEE ALSO
21             #pod
22             #pod L<Beam::Make>, L<Beam::Wire>, L<DBI>
23             #pod
24             #pod =cut
25              
26 1     1   14 use v5.20;
  1         4  
27 1     1   8 use warnings;
  1         2  
  1         29  
28 1     1   6 use Moo;
  1         3  
  1         8  
29 1     1   538 use File::stat;
  1         3  
  1         9  
30 1     1   58 use Time::Piece;
  1         2  
  1         9  
31 1     1   632 use Digest::SHA;
  1         2602  
  1         52  
32 1     1   8 use experimental qw( signatures postderef );
  1         3  
  1         7  
33              
34             extends 'Beam::Make::Recipe';
35              
36             #pod =attr commands
37             #pod
38             #pod An array of commands to run. Commands can be strings, which will be interpreted by
39             #pod the shell, or arrays, which will be invoked directly by the system.
40             #pod
41             #pod # Interpreted as a shell script. Pipes, environment variables, redirects,
42             #pod # etc... allowed
43             #pod - cc -Wall main.c
44             #pod
45             #pod # `cc` invoked directly. Shell functions will not work.
46             #pod - [ cc, -Wall, main.c ]
47             #pod
48             #pod # A single, multi-line shell script
49             #pod - |
50             #pod if [ $( date ) -gt $DATE ]; then
51             #pod echo Another day $( date ) >> /var/log/calendar.log
52             #pod fi
53             #pod
54             #pod =cut
55              
56             has commands => ( is => 'ro', required => 1 );
57              
58 5     5 1 17 sub make( $self, %vars ) {
  5         8  
  5         12  
  5         9  
59 5         22 for my $cmd ( $self->commands->@* ) {
60 5 50       28 my @cmd = ref $cmd eq 'ARRAY' ? @$cmd : ( $cmd );
61 5         27579 system @cmd;
62 5 50       354 if ( $? != 0 ) {
63 0         0 die sprintf 'Error running external command "%s": %s', "@cmd", $?;
64             }
65             }
66             # XXX: If the recipe does not create the file, throw an error
67 5         186 $self->cache->set( $self->name, $self->_cache_hash );
68 5         87 return 0;
69             }
70              
71 19     19   51 sub _cache_hash( $self ) {
  19         46  
  19         42  
72 19 50       543 return -e $self->name ? Digest::SHA->new( 1 )->addfile( $self->name )->b64digest : '';
73             }
74              
75 19     19 1 45 sub last_modified( $self ) {
  19         29  
  19         28  
76 19 100       433 return -e $self->name ? $self->cache->last_modified( $self->name, $self->_cache_hash ) : 0;
77             }
78              
79             1;
80              
81             __END__
82              
83             =pod
84              
85             =head1 NAME
86              
87             Beam::Make::File - A Beam::Make recipe to build a file from shell scripts
88              
89             =head1 VERSION
90              
91             version 0.002
92              
93             =head1 SYNOPSIS
94              
95             ### Beamfile
96             a.out:
97             requires:
98             - main.c
99             commands:
100             - cc -Wall main.c
101              
102             =head1 DESCRIPTION
103              
104             This L<Beam::Make> recipe class creates a file by running one or more
105             shell scripts. The recipe's name should be the file that will be created
106             by the recipe.
107              
108             =head1 ATTRIBUTES
109              
110             =head2 commands
111              
112             An array of commands to run. Commands can be strings, which will be interpreted by
113             the shell, or arrays, which will be invoked directly by the system.
114              
115             # Interpreted as a shell script. Pipes, environment variables, redirects,
116             # etc... allowed
117             - cc -Wall main.c
118              
119             # `cc` invoked directly. Shell functions will not work.
120             - [ cc, -Wall, main.c ]
121              
122             # A single, multi-line shell script
123             - |
124             if [ $( date ) -gt $DATE ]; then
125             echo Another day $( date ) >> /var/log/calendar.log
126             fi
127              
128             =head1 SEE ALSO
129              
130             L<Beam::Make>, L<Beam::Wire>, L<DBI>
131              
132             =head1 AUTHOR
133              
134             Doug Bell <preaction@cpan.org>
135              
136             =head1 COPYRIGHT AND LICENSE
137              
138             This software is copyright (c) 2020 by Doug Bell.
139              
140             This is free software; you can redistribute it and/or modify it under
141             the same terms as the Perl 5 programming language system itself.
142              
143             =cut