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.003';
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   15 use v5.20;
  1         3  
27 1     1   6 use warnings;
  1         11  
  1         41  
28 1     1   7 use Moo;
  1         1  
  1         13  
29 1     1   495 use File::stat;
  1         2  
  1         11  
30 1     1   71 use Time::Piece;
  1         2  
  1         7  
31 1     1   709 use Digest::SHA;
  1         2907  
  1         58  
32 1     1   11 use experimental qw( signatures postderef );
  1         3  
  1         8  
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 11 sub make( $self, %vars ) {
  5         10  
  5         10  
  5         9  
59 5         21 for my $cmd ( $self->commands->@* ) {
60 5 50       26 my @cmd = ref $cmd eq 'ARRAY' ? @$cmd : ( $cmd );
61 5         36294 system @cmd;
62 5 50       475 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         280 $self->cache->set( $self->name, $self->_cache_hash );
68 5         106 return 0;
69             }
70              
71 19     19   128 sub _cache_hash( $self ) {
  19         51  
  19         29  
72 19 50       685 return -e $self->name ? Digest::SHA->new( 1 )->addfile( $self->name )->b64digest : '';
73             }
74              
75 19     19 1 36 sub last_modified( $self ) {
  19         45  
  19         30  
76 19 100       491 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.003
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