File Coverage

blib/lib/AWS/Lambda/Quick/CreateZip.pm
Criterion Covered Total %
statement 40 42 95.2
branch 5 8 62.5
condition n/a
subroutine 8 8 100.0
pod 0 1 0.0
total 53 59 89.8


line stmt bran cond sub pod time code
1             package AWS::Lambda::Quick::CreateZip;
2 2     2   315735 use Mo qw( default required );
  2         511  
  2         11  
3              
4             our $VERSION = '1.0002';
5              
6 2     2   2743 use Archive::Zip qw( :ERROR_CODES :CONSTANTS );
  2         65056  
  2         315  
7 2     2   16 use Path::Tiny qw( path );
  2         4  
  2         1314  
8              
9             has src_filename => required => 1;
10             has zip_filename => required => 1;
11              
12             has extra_files => default => [];
13              
14             has _src_path => sub { path( shift->src_filename ) };
15             has _src_dir => sub { shift->_src_path->parent };
16             has _zip_class => default => 'Archive::Zip';
17             has _zip => sub { shift->_zip_class->new };
18             has _script_src => sub { shift->_src_path->slurp_raw };
19              
20             # this is the same src as in script src but the first occurance of
21             # "use AWS::Lambda::Quick" is prepended with
22             # "$INC{'AWS/Lambda/Quick.pm'}=1" to prevent it actually being loaded
23             # from disk. Note this happens on just one line to avoid screwing
24             # with line numebrs that could mess with error messages
25             has _converted_src => sub {
26             my $self = shift;
27             my $src = $self->_script_src;
28             $src =~ s{(?=use AWS::Lambda::Quick(?:\s|[;(]))}
29             {BEGIN{\$INC{'AWS/Lambda/Quick.pm'}=1} };
30             return $src;
31             };
32              
33             ### methods for interfacing with Archive::Zip
34             ### no code outside this section should directly interact with the
35             ### zip file
36              
37 1     1   4 sub _add_string {
38 1         2 my $self = shift;
39 1         3 my $string = shift;
40             my $filename = shift;
41 1         5  
42 1         63 my $zip = $self->_zip;
43 1         739 my $string_member = $zip->addString( $string, $filename );
44 1         22 $string_member->desiredCompressionMethod(COMPRESSION_DEFLATED);
45             return ();
46             }
47              
48 2     2   15 sub _add_path {
49 2         6 my $self = shift;
50             my $path = path(shift);
51 2 50       66  
52 0         0 if ( $path->is_absolute ) {
53             die "Cannot add absolute path! $path";
54 2         67 }
55             my $abs_path = path( $self->_src_dir, $path );
56              
57             # silently ignore files that don't exist. This allows you
58             # to say put extra_files => [qw( lib )] in your file and not
59 2 50       156 # worry if that file exists or not
60             return unless -e $abs_path;
61 2         53  
62             $self->_zip->addFileOrDirectory(
63             {
64             name => $abs_path->stringify,
65             zipName => $path->stringify,
66             compressionLevel => COMPRESSION_DEFLATED,
67             }
68             );
69              
70 2 100       821 # was that a directory? Add the contents recursively
71 1         31 return () unless -d $abs_path;
72 1         35 my $iter = $abs_path->iterator;
73 1         168 while ( my $next = $iter->() ) {
74 1         64 my $child = $path->child( $next->basename );
75             $self->_add_path($child);
76             }
77 1         76  
78             return ();
79             }
80              
81 1     1   6 sub _write_zip {
82 1 50       5 my $self = shift;
83             unless ( $self->_zip->writeToFileNamed( $self->zip_filename->stringify )
84 0         0 == AZ_OK ) {
85             die 'write error';
86 1         5170 }
87             return ();
88             }
89              
90             ### logic for building the zip file contents ###
91              
92 1     1   2 sub _build_zip {
93 1         7 my $self = shift;
94 1         3 $self->_add_string( $self->_converted_src, 'handler.pl' );
  1         5  
95 1         3 $self->_add_path($_) for @{ $self->extra_files };
96             return ();
97             }
98              
99 1     1 0 2970 sub create_zip {
100 1         5 my $self = shift;
101 1         5 $self->_build_zip;
102 1         3 $self->_write_zip;
103             return ();
104             }
105              
106             1;
107              
108             __END__
109              
110             =head1 NAME
111              
112             AWS::Lambda::Quick::CreateZip - lambda function zipping for AWS::Lambda::Quick
113              
114             =head1 DESCRIPTION
115              
116             No user servicable parts. See L<AWS::Lambda::Quick> for usage.
117              
118             =head1 AUTHOR
119              
120             Written by Mark Fowler B<mark@twoshortplanks.com>
121              
122             Copyright Mark Fowler 2019.
123              
124             This program is free software; you can redistribute it and/or modify
125             it under the same terms as Perl itself.
126              
127             =head1 SEE ALSO
128              
129             L<AWS::Lambda::Quick>
130              
131             =cut
132