File Coverage

blib/lib/Dist/Zilla/Plugin/FatPacker.pm
Criterion Covered Total %
statement 26 64 40.6
branch 0 18 0.0
condition n/a
subroutine 9 13 69.2
pod 4 4 100.0
total 39 99 39.3


line stmt bran cond sub pod time code
1 1     1   13104 use 5.008;
  1         2  
2 1     1   4 use strict;
  1         1  
  1         14  
3 1     1   2 use warnings;
  1         3  
  1         43  
4              
5             package Dist::Zilla::Plugin::FatPacker;
6             $Dist::Zilla::Plugin::FatPacker::VERSION = '1.161630';
7             # ABSTRACT: Pack your dependencies onto your script file
8 1     1   592 use File::Temp 'tempfile';
  1         14491  
  1         47  
9 1     1   5 use File::Path 'remove_tree';
  1         0  
  1         34  
10 1     1   352 use File::pushd 'tempd';
  1         1578  
  1         42  
11 1     1   364 use Path::Class 'file';
  1         12209  
  1         42  
12 1     1   453 use Moose;
  1         280858  
  1         4  
13             with 'Dist::Zilla::Role::FileMunger';
14             has script => (is => 'ro');
15              
16             around munge_files => sub {
17             my ($orig, $self, @args) = @_;
18             my $tmpdir = tempd();
19              
20             for my $file (@{ $self->zilla->files }) {
21             my $path = file($file->name);
22             $path->dir->mkpath();
23              
24             my $fh = $path->open('>:bytes')
25             or die "Can't create $path in fatpacking work dir: $!\n";
26             $fh->print($file->encoded_content);
27             }
28              
29             return $self->$orig(@args);
30             };
31              
32             sub safe_pipe_command {
33 0     0 1   my ($binmode, @cmd) = @_;
34              
35 0 0         open(my($pipe), '-|', @cmd) or die "can't run command @cmd: $!";
36 0           binmode($pipe, $binmode);
37 0           my $output = join('', <$pipe>);
38 0           close($pipe);
39              
40 0           return $output;
41             }
42              
43             sub safe_system {
44 0     0 1   my $cmd = shift;
45 0 0         system($cmd) == 0 or die "can't $cmd: $?";
46             }
47              
48             sub safe_remove_tree {
49 0     0 1   my $errors;
50 0           remove_tree(@_, { error => \$errors });
51 0 0         return unless @$errors;
52 0           for my $diag (@$errors) {
53 0           my ($file, $message) = %$diag;
54 0 0         if ($file eq '') {
55 0           warn "general error: $message\n";
56             } else {
57 0           warn "problem unlinking $file: $message\n";
58             }
59             }
60 0           die "remove_tree had errors, aborting\n";
61             }
62              
63             sub munge_file {
64 0     0 1   my ($self, $file) = @_;
65 0 0         unless (defined $self->script) {
66 0           our $did_warn;
67 0 0         $did_warn++ || warn "[FatPacker] requires a 'script' configuration\n";
68 0           return;
69             }
70 0 0         return unless $file->name eq $self->script;
71 0           my $content = $file->encoded_content;
72 0           my ($fh, $temp_script) = tempfile();
73 0           binmode($fh, ':bytes');
74 0           warn "temp script [$temp_script]\n";
75 0           print $fh $content;
76 0 0         close $fh or die "can't close temp file $temp_script: $!\n";
77              
78 0           $ENV{PERL5LIB} = join ':', grep defined, 'lib', $ENV{PERL5LIB};
79 0           safe_system("fatpack trace $temp_script");
80 0           safe_system("fatpack packlists-for `cat fatpacker.trace` >packlists");
81 0           safe_system("fatpack tree `cat packlists`");
82 0           my $fatpack = safe_pipe_command(':bytes', 'fatpack', 'file', $temp_script);
83              
84 0           for ($temp_script, 'fatpacker.trace', 'packlists') {
85 0 0         unlink $_ or die "can't unlink $_: $!\n";
86             }
87 0           safe_remove_tree('fatlib');
88 0           $file->encoded_content($fatpack);
89             }
90             __PACKAGE__->meta->make_immutable;
91 1     1   4375 no Moose;
  1         1  
  1         4  
92             1;
93              
94             __END__
95              
96             =pod
97              
98             =for test_synopsis 1;
99             __END__
100              
101             =head1 NAME
102              
103             Dist::Zilla::Plugin::FatPacker - Pack your dependencies onto your script file
104              
105             =head1 VERSION
106              
107             version 1.161630
108              
109             =head1 SYNOPSIS
110              
111             In C<dist.ini>:
112              
113             [FatPacker]
114             script = bin/my_script
115              
116             =head1 DESCRIPTION
117              
118             This plugin uses L<App::FatPacker> to pack your dependencies onto your script
119             file.
120              
121             =head1 METHODS
122              
123             =head2 munge_file
124              
125             When processing the script file indicated by the C<script> configuration parameter,
126             it prepends its packed dependencies to the script.
127              
128             This process creates temporary files outside the build directory, but if there
129             are no errors, they will be removed again.
130              
131             =head1 FUNCTIONS
132              
133             =head2 safe_pipe_command
134              
135             This runs a command in a pipe, and returns the stdout.
136              
137             =head2 safe_remove_tree
138              
139             This is a wrapper around C<remove_tree()> from C<File::Path> that adds some
140             error checks.
141              
142             =head2 safe_system
143              
144             This is a wrapper around C<system()> that adds some error checks.
145              
146             =head1 INSTALLATION
147              
148             See perlmodinstall for information and options on installing Perl modules.
149              
150             =head1 BUGS AND LIMITATIONS
151              
152             You can make new bug reports, and view existing ones, through the
153             web interface at L<http://rt.cpan.org/Public/Dist/Display.html?Name=Dist-Zilla-Plugin-FatPacker>.
154              
155             =head1 AVAILABILITY
156              
157             The project homepage is L<http://search.cpan.org/dist/Dist-Zilla-Plugin-FatPacker/>.
158              
159             The latest version of this module is available from the Comprehensive Perl
160             Archive Network (CPAN). Visit L<http://www.perl.com/CPAN/> to find a CPAN
161             site near you, or see L<https://metacpan.org/module/Dist::Zilla::Plugin::FatPacker/>.
162              
163             =head1 AUTHOR
164              
165             Mike Doherty <doherty@cpan.org>
166              
167             =head1 COPYRIGHT AND LICENSE
168              
169             This software is copyright (c) 2010 by Mike Doherty.
170              
171             This is free software; you can redistribute it and/or modify it under
172             the same terms as the Perl 5 programming language system itself.
173              
174             =cut