Merge branch 'miyagawa-packer-command'
Karen Etheridge [Sun, 7 Apr 2013 17:45:27 +0000 (10:45 -0700)]
New 'pack' command

Changes
bin/fatpack
lib/App/FatPacker.pm

diff --git a/Changes b/Changes
index 12508af..a81e640 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,5 +1,8 @@
 Revision history for App-FatPacker
 
+  - new 'pack' command, to do everything in one command (thanks, miyagawa!)
+    (RT#84415)
+
 0.009014 - 2013-04-03
   - avoid fatal error when lib/ doesn't exist (now it is simply skipped)
     (RT#84413, miyagawa)
index 1ad9cb2..82f888f 100755 (executable)
@@ -8,6 +8,15 @@ fatpack - Command line frontend for App::FatPacker
 
 =head1 COMMANDS
 
+=head2 pack
+
+  $ fatpack pack myscript.pl > myscript.packed.pl
+
+A shortcut to do all the work of tracing, collecting packlists,
+extracting modules in fatlib, then concatenating into a packed script
+- in one shot. If you need more detailed controls for additional
+modules, use the following commands separately (see L</RECIPES>).
+
 =head2 trace
 
   $ fatpack trace [--to=trace-file|--to-stderr] [--use=MODULE]
index 8cf5462..935584b 100644 (file)
@@ -32,6 +32,17 @@ sub lines_of {
   map +(chomp,$_)[1], do { local @ARGV = ($_[0]); <> };
 }
 
+sub maybe_shebang {
+  my ($file) = @_;
+  open my $in, "<", $file or die "$file: $!";
+  my $head = <$in>;
+  if ($head =~ m/^#\!/) {
+    ($head, do { local $/; <$in> });
+  } else {
+    ('', do { local $/; $head . <$in> });
+  }
+}
+
 sub stripspace {
   my ($text) = @_;
   $text =~ /^(\s+)/ && $text =~ s/^$1//mg;
@@ -67,6 +78,20 @@ sub script_command_help {
   print "Try `perldoc fatpack` for how to use me\n";
 }
 
+sub script_command_pack {
+  my ($self, $args) = @_;
+
+  my @modules = split /\r?\n/, $self->trace(args => $args);
+  my @packlists = $self->packlists_containing(\@modules);
+
+  my $base = catdir(cwd, 'fatlib');
+  $self->packlists_to_tree($base, \@packlists);
+
+  my $file = shift @$args;
+  my($head, $body) = maybe_shebang($file);
+  print $head, $self->fatpack_file($file), $body;
+}
+
 sub script_command_trace {
   my ($self, $args) = @_;
 
@@ -101,12 +126,7 @@ sub script_command_trace {
 sub trace {
   my ($self, %opts) = @_;
 
-  my $capture;
-
-  my $output = $opts{output} || do {
-    $capture++; '>&STDOUT'
-  };
-
+  my $output = $opts{output};
   my $trace_opts = join ',', $output||'>&STDOUT', @{$opts{use}||[]};
 
   local $ENV{PERL5OPT} = '-MApp::FatPacker::Trace='.$trace_opts;
@@ -186,6 +206,11 @@ sub packlists_to_tree {
 sub script_command_file {
   my ($self, $args) = @_;
   my $file = shift @$args;
+  print $self->fatpack_file($file);
+}
+
+sub fatpack_file {
+  my ($self, $file) = @_;
   my $cwd = cwd;
   my @dirs = grep -d, map rel2abs($_, $cwd), ('lib','fatlib');
   my %files;
@@ -234,7 +259,7 @@ sub script_command_file {
     '$fatpacked{'.perlstring($_).qq!} = <<'${name}';\n!
     .qq!${data}${name}\n!;
   } sort keys %files;
-  print join "\n", $start, @segments, $end;
+  return join "\n", $start, @segments, $end;
 }
 
 =encoding UTF-8
@@ -245,6 +270,10 @@ App::FatPacker - pack your dependencies onto your script file
 
 =head1 SYNOPSIS
 
+  $ fatpack pack myscript.pl >myscript.packed.pl
+
+Or, with more step-by-step control:
+
   $ fatpack trace myscript.pl
   $ fatpack packlists-for `cat fatpacker.trace` >packlists
   $ fatpack tree `cat packlists`