| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | #!/usr/bin/env perl | 
| 2 |  |  |  |  |  |  | package App::implode::cli; | 
| 3 | 2 |  |  | 2 |  | 715 | use strict; | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 58 |  | 
| 4 | 2 |  |  | 2 |  | 7 | use warnings; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 40 |  | 
| 5 | 2 |  |  | 2 |  | 1253 | use Archive::Tar; | 
|  | 2 |  |  |  |  | 173885 |  | 
|  | 2 |  |  |  |  | 108 |  | 
| 6 | 2 |  |  | 2 |  | 13 | use Cwd 'abs_path'; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 64 |  | 
| 7 | 2 |  |  | 2 |  | 882 | use Carton (); | 
|  | 2 |  |  |  |  | 3880 |  | 
|  | 2 |  |  |  |  | 34 |  | 
| 8 | 2 |  |  | 2 |  | 849 | use Carton::Builder; | 
|  | 2 |  |  |  |  | 54818 |  | 
|  | 2 |  |  |  |  | 57 |  | 
| 9 | 2 |  |  | 2 |  | 919 | use Carton::Environment; | 
|  | 2 |  |  |  |  | 193781 |  | 
|  | 2 |  |  |  |  | 63 |  | 
| 10 | 2 |  |  | 2 |  | 830 | use Carton::Mirror; | 
|  | 2 |  |  |  |  | 1486 |  | 
|  | 2 |  |  |  |  | 63 |  | 
| 11 | 2 |  |  | 2 |  | 11 | use File::Basename 'basename'; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 113 |  | 
| 12 | 2 |  |  | 2 |  | 9 | use File::Find (); | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 22 |  | 
| 13 | 2 |  |  | 2 |  | 6 | use File::Path (); | 
|  | 2 |  |  |  |  | 2 |  | 
|  | 2 |  |  |  |  | 27 |  | 
| 14 | 2 |  |  | 2 |  | 920 | use File::Spec::Functions qw( catdir catfile ); | 
|  | 2 |  |  |  |  | 1147 |  | 
|  | 2 |  |  |  |  | 119 |  | 
| 15 | 2 |  |  | 2 |  | 1355 | use File::Temp 'tempdir'; | 
|  | 2 |  |  |  |  | 15353 |  | 
|  | 2 |  |  |  |  | 114 |  | 
| 16 | 2 |  |  | 2 |  | 11 | use IO::Compress::Bzip2; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 2609 |  | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | our $PATH = abs_path(__FILE__); | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | sub DESTROY { | 
| 21 | 4 |  |  | 4 |  | 2380 | my $self = shift; | 
| 22 | 4 | 100 |  |  |  | 55 | return chdir $self->[0] if UNIVERSAL::isa($self, 'ARRAY'); | 
| 23 | 2 | 50 | 33 |  |  | 55 | File::Path::remove_tree($self->{tmpdir}) if $self->{cleanup} and $self->{tmpdir}; | 
| 24 |  |  |  |  |  |  | } | 
| 25 |  |  |  |  |  |  |  | 
| 26 | 0 |  | 0 | 0 |  | 0 | sub mirror { shift->{mirror} ||= Carton::Mirror->new($ENV{PERL_CARTON_MIRROR} || $Carton::Mirror::DefaultMirror) } | 
|  |  |  | 0 |  |  |  |  | 
| 27 | 1 | 0 | 33 | 1 |  | 5 | sub tmpdir { shift->{tmpdir} //= tempdir(CLEANUP => $ENV{IMPLODE_NO_CLEANUP} ? 0 : 1) } | 
| 28 | 3 | 50 | 66 | 3 |  | 18 | sub verbose { shift->{verbose} //= $ENV{APP_IMPLODE_VERBOSE} ? 0 : -t STDOUT } | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | sub bundle { | 
| 31 | 0 |  |  | 0 |  | 0 | my $self     = shift; | 
| 32 | 0 |  |  |  |  | 0 | my $script   = $self->slurp($self->{script}); | 
| 33 | 0 |  |  |  |  | 0 | my $exploder = $self->code('exploder'); | 
| 34 | 0 |  |  |  |  | 0 | my $id       = basename $self->{out}; | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 0 |  |  |  |  | 0 | $exploder =~ s!^sub.*\@_;!BEGIN{my \$id='$id';!s; | 
| 37 | 0 |  |  |  |  | 0 | $exploder =~ s!^\s+!!mg; | 
| 38 | 0 |  |  |  |  | 0 | $exploder =~ s!\n!!g; | 
| 39 |  |  |  |  |  |  |  | 
| 40 | 0 | 0 |  |  |  | 0 | open my $OUT, '>', $self->{out} or die "Could not write $self->{out}: $!\n"; | 
| 41 | 0 | 0 |  |  |  | 0 | warn sprintf "Generating $self->{out} with embedded bzip archive...\n" if $self->verbose; | 
| 42 | 0 | 0 |  |  |  | 0 | print $OUT $script =~ s/^(#!.+?[\r\n]+)//m ? $1 : "#!/usr/bin/perl\n"; | 
| 43 | 0 |  |  |  |  | 0 | print $OUT $exploder, "\n", $script, "\n__END__\n"; | 
| 44 | 0 |  |  |  |  | 0 | $self->tarball->write(IO::Compress::Bzip2->new($OUT), COMPRESS_GZIP); | 
| 45 | 0 |  |  |  |  | 0 | close $OUT; | 
| 46 | 0 |  |  |  |  | 0 | chmod 0755, $self->{out}; | 
| 47 | 0 | 0 |  |  |  | 0 | warn sprintf "$self->{out} is generated.\n" if $self->verbose; | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  |  | 
| 50 |  |  |  |  |  |  | sub chdir { | 
| 51 | 2 |  |  | 2 |  | 3 | my $self = shift; | 
| 52 | 2 |  |  |  |  | 11 | my $guard = bless [abs_path], ref($self); | 
| 53 | 2 | 50 |  |  |  | 24 | chdir $_[0] or die "chdir $_[0]: $!"; | 
| 54 | 2 |  |  |  |  | 4 | $guard; | 
| 55 |  |  |  |  |  |  | } | 
| 56 |  |  |  |  |  |  |  | 
| 57 |  |  |  |  |  |  | sub code { | 
| 58 | 1 |  |  | 1 |  | 2 | my ($self, $name) = @_; | 
| 59 | 1 | 50 |  |  |  | 30 | open my $SELF, '<', $PATH or die "Read $PATH: $!"; | 
| 60 | 1 |  |  |  |  | 84 | return join '', grep { /^sub $name/ .. /^\}/ } <$SELF>; | 
|  | 159 |  |  |  |  | 230 |  | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  |  | 
| 63 |  |  |  |  |  |  | sub deps { | 
| 64 | 0 |  |  | 0 |  | 0 | my $self    = shift; | 
| 65 | 0 |  |  |  |  | 0 | my $env     = Carton::Environment->build('cpanfile', $self->tmpdir); | 
| 66 | 0 |  |  |  |  | 0 | my $builder = Carton::Builder->new(mirror => $self->mirror, cpanfile => $env->cpanfile); | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 0 | 0 |  |  |  | 0 | $self->dir_is_empty($self->tmpdir) or die "Cannot build $self->{script}: @{[$env->install_path]} already exists.\n"; | 
|  | 0 |  |  |  |  | 0 |  | 
| 69 | 0 |  |  |  |  | 0 | $self->{cleanup} = 1; | 
| 70 | 0 |  |  |  |  | 0 | $builder->install($env->install_path); | 
| 71 |  |  |  |  |  |  | } | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | sub dir_is_empty { | 
| 74 | 3 |  |  | 3 |  | 316 | my ($self, $dir) = @_; | 
| 75 | 3 | 100 |  |  |  | 69 | opendir(my $DH, $dir) or return 1; | 
| 76 | 2 |  |  |  |  | 25 | not scalar grep {/\w/} readdir $DH; | 
|  | 9 |  |  |  |  | 35 |  | 
| 77 |  |  |  |  |  |  | } | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | sub exploder { | 
| 80 | 1 |  |  | 1 |  | 705 | my ($self, $id) = @_; | 
| 81 | 1 |  |  |  |  | 6 | require Archive::Tar; | 
| 82 | 1 |  |  |  |  | 2 | require File::Path; | 
| 83 | 1 |  |  |  |  | 3 | require File::Spec; | 
| 84 | 1 |  |  |  |  | 3 | require IO::Uncompress::Bunzip2; | 
| 85 | 1 |  |  | 1 |  | 2004 | sub App::implode::temp::DESTROY { File::Path::remove_tree(${$_[0]}) } | 
|  | 1 |  |  |  |  | 613 |  | 
| 86 | 1 |  | 33 |  |  | 97 | $App::implode::explodedir | 
| 87 |  |  |  |  |  |  | = bless \($ENV{APP_EXPLODE_DIR} || File::Spec->catdir(File::Spec->tmpdir, "app-implode-$id")), 'App::implode::temp'; | 
| 88 | 1 | 50 |  |  |  | 12 | warn "[App::implode] cd $$App::implode::explodedir; tar -xfz $0\n" if $ENV{APP_EXPLODE_VERBOSE}; | 
| 89 | 1 |  |  |  |  | 8 | my $tar = Archive::Tar->new; | 
| 90 |  |  |  |  |  |  | $tar->read( | 
| 91 |  |  |  |  |  |  | IO::Uncompress::Bunzip2->new( | 
| 92 | 1 |  |  |  |  | 14 | do { | 
| 93 | 1 |  |  |  |  | 27 | open my $FH, '<', $0; | 
| 94 | 1 |  |  |  |  | 2 | my $m = 0; | 
| 95 | 1 | 100 | 100 |  |  | 61 | \join '', grep { $m++ if /^__END__\r?\n/ || $m; $m > 1; } <$FH>; | 
|  | 44 |  |  |  |  | 102 |  | 
|  | 44 |  |  |  |  | 65 |  | 
| 96 |  |  |  |  |  |  | } | 
| 97 |  |  |  |  |  |  | ) | 
| 98 |  |  |  |  |  |  | ); | 
| 99 | 1 |  |  |  |  | 2811 | $tar->setcwd($$App::implode::explodedir); | 
| 100 | 1 | 50 |  |  |  | 9 | $tar->extract or die "[App::implode] tar -xfz $0 failed: @{[$tar->error]}"; | 
|  | 0 |  |  |  |  | 0 |  | 
| 101 | 1 |  |  |  |  | 1287 | unshift @INC, File::Spec->catdir($$App::implode::explodedir, 'lib', 'perl5'); | 
| 102 | 1 |  |  |  |  | 4 | $ENV{PATH} = join ':', grep {defined} File::Spec->catdir($$App::implode::explodedir, 'bin'), $ENV{PATH}; | 
|  | 2 |  |  |  |  | 7 |  | 
| 103 | 1 |  |  |  |  | 20 | $ENV{PERL5LIB} = join ':', @INC; | 
| 104 |  |  |  |  |  |  | } | 
| 105 |  |  |  |  |  |  |  | 
| 106 |  |  |  |  |  |  | sub slurp { | 
| 107 | 1 |  |  | 1 |  | 2 | my ($self, $file) = @_; | 
| 108 | 1 | 50 |  |  |  | 23 | open my $FH, '<', $file or die "Could not read $file: $!\n"; | 
| 109 | 1 |  |  |  |  | 3 | local $/; | 
| 110 | 1 |  |  |  |  | 22 | readline $FH; | 
| 111 |  |  |  |  |  |  | } | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | sub tarball { | 
| 114 | 1 |  |  | 1 |  | 1 | my $self  = shift; | 
| 115 | 1 |  |  |  |  | 2 | my $chdir = $self->chdir($self->tmpdir); | 
| 116 | 1 | 100 |  | 8 |  | 3 | my $chmod = sub { -f and chmod 0600 | (0777 & (stat _)[2]), $_ }; | 
|  | 8 |  |  |  |  | 320 |  | 
| 117 | 1 | 50 |  | 2 |  | 2 | my $files = sub { @_ > 1 and File::Find::find({no_chdir => 1, wanted => shift}, @_) }; | 
|  | 2 |  |  |  |  | 104 |  | 
| 118 | 1 |  |  |  |  | 8 | my $tar   = Archive::Tar->new; | 
| 119 |  |  |  |  |  |  |  | 
| 120 |  |  |  |  |  |  | $files->( | 
| 121 |  |  |  |  |  |  | sub { | 
| 122 | 5 | 100 |  | 5 |  | 596 | return unless $chmod->(); | 
| 123 | 2 | 50 |  |  |  | 6 | warn sprintf "Add @{[catfile $self->{tmpdir}, $_]}\n" if $self->verbose; | 
|  | 0 |  |  |  |  | 0 |  | 
| 124 | 2 |  |  |  |  | 8 | $tar->add_files($_); | 
| 125 |  |  |  |  |  |  | }, | 
| 126 | 1 |  |  |  |  | 12 | grep {-d} qw( bin lib ) | 
|  | 2 |  |  |  |  | 16 |  | 
| 127 |  |  |  |  |  |  | ); | 
| 128 |  |  |  |  |  |  |  | 
| 129 | 1 |  |  |  |  | 288 | undef $chdir; | 
| 130 |  |  |  |  |  |  | $files->( | 
| 131 |  |  |  |  |  |  | sub { | 
| 132 | 3 | 100 | 66 | 3 |  | 6 | return unless $chmod->() and s!lib!lib/perl5!; | 
| 133 | 1 | 50 |  |  |  | 3 | warn sprintf "Add $_\n" if $self->verbose; | 
| 134 | 1 |  |  |  |  | 3 | $tar->add_data($_, $self->slurp($File::Find::name)); | 
| 135 |  |  |  |  |  |  | }, | 
| 136 | 1 |  |  |  |  | 2 | grep {-d} qw( lib ) | 
|  | 1 |  |  |  |  | 9 |  | 
| 137 |  |  |  |  |  |  | ); | 
| 138 |  |  |  |  |  |  |  | 
| 139 | 1 |  |  |  |  | 204 | return $tar; | 
| 140 |  |  |  |  |  |  | } | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | sub run { | 
| 143 | 2 |  |  | 2 |  | 724 | my $self = shift; | 
| 144 |  |  |  |  |  |  |  | 
| 145 | 2 | 100 |  |  |  | 12 | $self->{script} = shift or die "Usage: implode myapp.pl [path/to/outfile.pl]\n\n"; | 
| 146 | 1 |  | 33 |  |  | 77 | $self->{out} = shift || basename $self->{script}; | 
| 147 | 1 | 50 |  |  |  | 64 | -r $self->{script} or die "Cannot read '$self->{script}'.\n"; | 
| 148 | 0 | 0 |  |  |  |  | -e $self->{out} and die "Outfile '$self->{out}' already exists.\n"; | 
| 149 |  |  |  |  |  |  |  | 
| 150 | 0 | 0 |  |  |  |  | warn sprintf "Building application in %s\n", $self->tmpdir if $self->verbose; | 
| 151 | 0 |  |  |  |  |  | $self->deps; | 
| 152 | 0 |  |  |  |  |  | $self->bundle; | 
| 153 |  |  |  |  |  |  |  | 
| 154 | 0 |  |  |  |  |  | return 0; | 
| 155 |  |  |  |  |  |  | } | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | exit((bless {})->run(@ARGV)) unless defined wantarray; | 
| 158 | 2 |  |  | 2 |  | 12 | no warnings; | 
|  | 2 |  |  |  |  | 3 |  | 
|  | 2 |  |  |  |  | 101 |  | 
| 159 |  |  |  |  |  |  | 'App::implode::cli'; |