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             package Dist::Zilla::Plugin::FatPacker;
2 1     1   14416 use 5.008;
  1         3  
3 1     1   4 use strict;
  1         1  
  1         14  
4 1     1   3 use warnings;
  1         5  
  1         35  
5             # ABSTRACT: Pack your dependencies onto your script file
6             our $VERSION = '1.161631'; # VERSION
7              
8 1     1   698 use File::Temp 'tempfile';
  1         16578  
  1         57  
9 1     1   6 use File::Path 'remove_tree';
  1         2  
  1         34  
10 1     1   357 use File::pushd 'tempd';
  1         1833  
  1         47  
11 1     1   428 use Path::Class 'file';
  1         13577  
  1         52  
12 1     1   605 use Moose;
  1         311424  
  1         6  
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   5401 no Moose;
  1         2  
  1         5  
92             1;
93              
94             __END__
95              
96             =pod
97              
98             =encoding UTF-8
99              
100             =head1 NAME
101              
102             Dist::Zilla::Plugin::FatPacker - Pack your dependencies onto your script file
103              
104             =head1 VERSION
105              
106             version 1.161631
107              
108             =for test_synopsis BEGIN { die "SKIP: synopsis isn't perl code" }
109              
110             =head1 SYNOPSIS
111              
112             In C<dist.ini>:
113              
114             [FatPacker]
115             script = bin/my_script
116              
117             =head1 DESCRIPTION
118              
119             This plugin uses L<App::FatPacker> to pack your dependencies onto your script
120             file.
121              
122             =head2 munge_file
123              
124             When processing the script file indicated by the C<script> configuration parameter,
125             it prepends its packed dependencies to the script.
126              
127             This process creates temporary files outside the build directory, but if there
128             are no errors, they will be removed again.
129              
130             =head2 safe_pipe_command
131              
132             Runs a command in a pipe, and returns the stdout.
133              
134             =head2 safe_remove_tree
135              
136             A wrapper around C<remove_tree()> from C<File::Path> that adds some
137             error checks.
138              
139             =head2 safe_system
140              
141             A wrapper around C<system()> that adds some error checks.
142              
143             =head1 AVAILABILITY
144              
145             The latest version of this module is available from the Comprehensive Perl
146             Archive Network (CPAN). Visit L<http://www.perl.com/CPAN/> to find a CPAN
147             site near you, or see L<https://metacpan.org/module/Dist::Zilla::Plugin::FatPacker/>.
148              
149             =head1 SOURCE
150              
151             The development version is on github at L<http://github.com/doherty/Dist-Zilla-Plugin-FatPacker>
152             and may be cloned from L<git://github.com/doherty/Dist-Zilla-Plugin-FatPacker.git>
153              
154             =head1 BUGS AND LIMITATIONS
155              
156             You can make new bug reports, and view existing ones, through the
157             web interface at L<https://github.com/doherty/Dist-Zilla-Plugin-FatPacker/issues>.
158              
159             =head1 AUTHOR
160              
161             Mike Doherty <doherty@cpan.org>
162              
163             =head1 COPYRIGHT AND LICENSE
164              
165             This software is copyright (c) 2010 by Mike Doherty.
166              
167             This is free software; you can redistribute it and/or modify it under
168             the same terms as the Perl 5 programming language system itself.
169              
170             =cut