1 package App::FatPacker;
4 use warnings FATAL => 'all';
7 use File::Find qw(find);
8 use File::Spec::Functions qw(
9 catdir splitpath splitdir catpath rel2abs abs2rel
11 use File::Copy qw(copy);
12 use File::Path qw(make_path remove_tree);
15 my $option_parser = Getopt::Long::Parser->new(
16 config => [ qw(require_order pass_through bundling no_auto_abbrev) ]
20 local *ARGV = [ @{$_[0]} ];
21 $option_parser->getoptions(@{$_[1]});
26 map +(chomp,$_)[1], do { local @ARGV = ($_[0]); <> };
31 $text =~ /^(\s+)/ && $text =~ s/^$1//mg;
35 our $VERSION = '0.009001'; # 0.9.1
37 $VERSION = eval $VERSION;
40 $_[1] eq '-run_script'
41 and return shift->new->run_script;
44 sub new { bless({}, $_[0]) }
47 my ($self, $args) = @_;
48 my @args = $args ? @$args : @ARGV;
49 (my $cmd = shift @args) =~ s/-/_/g;
50 if (my $meth = $self->can("script_command_${cmd}")) {
53 die "No such command ${cmd}";
57 sub script_command_trace {
58 my ($self, $args) = @_;
60 $args = call_parser $args => [
62 'to-stderr' => \my $to_stderr,
65 die "Can't use to and to-stderr on same call" if $file && $to_stderr;
67 (my $use_file = $file) ||= 'fatpacker.trace';
68 if (!$to_stderr and -e $use_file) {
69 unlink $use_file or die "Couldn't remove old trace file: $!";
74 } elsif ($to_stderr) {
81 local $ENV{PERL5OPT} = '-MApp::FatPacker::Trace'.$arg;
86 sub script_command_packlists_for {
87 my ($self, $args) = @_;
88 foreach my $pl ($self->packlists_containing($args)) {
93 sub packlists_containing {
94 my ($self, $targets) = @_;
95 my @targets = @$targets;
96 require $_ for @targets;
97 my @search = grep -d $_, map catdir($_, 'auto'), @INC;
101 return unless $_ eq '.packlist' && -f $_;
102 $pack_rev{$_} = $File::Find::name for lines_of $File::Find::name;
104 chdir($cwd) or die "Couldn't chdir back to ${cwd} after find: $!";
105 my %found; @found{map $pack_rev{$INC{$_}}, @targets} = ();
109 sub script_command_tree {
110 my ($self, $args) = @_;
111 my $base = catdir(cwd,'fatlib');
112 $self->packlists_to_tree($base, $args);
115 sub packlists_to_tree {
116 my ($self, $where, $packlists) = @_;
119 foreach my $pl (@$packlists) {
120 my ($vol, $dirs, $file) = splitpath $pl;
121 my @dir_parts = splitdir $dirs;
123 PART: foreach my $p (0 .. $#dir_parts) {
124 if ($dir_parts[$p] eq 'auto') {
125 # $p-2 since it's <wanted path>/$Config{archname}/auto
126 $pack_base = catpath $vol, catdir @dir_parts[0..$p-2];
130 die "Couldn't figure out base path of packlist ${pl}" unless $pack_base;
131 foreach my $source (lines_of $pl) {
132 # there is presumably a better way to do "is this under this base?"
133 # but if so, it's not obvious to me in File::Spec
134 next unless substr($source,0,length $pack_base) eq $pack_base;
135 my $target = rel2abs( abs2rel($source, $pack_base), $where );
136 my $target_dir = catpath((splitpath $target)[0,1]);
137 make_path $target_dir;
138 copy $source => $target;
143 sub script_command_file {
144 my ($self, $args) = @_;
145 my $file = shift @$args;
147 my @dirs = map rel2abs($_, $cwd), ('lib','fatlib');
149 foreach my $dir (@dirs) {
152 !/\.pm$/ and warn "File ${File::Find::name} isn't a .pm file - can't pack this and if you hoped we were going to things may not be what you expected later\n" and return;
153 $files{abs2rel($File::Find::name,$dir)} = do {
154 local (@ARGV, $/) = ($File::Find::name); <>
158 my $start = stripspace <<' END_START';
159 # This chunk of stuff was generated by App::FatPacker. To find the original
160 # file's code, look for the end of this BEGIN block or the string 'FATPACK'
164 my $end = stripspace <<' END_END';
165 s/^ //mg for values %fatpacked;
168 if (my $fat = $fatpacked{$_[1]}) {
169 open my $fh, '<', \$fat;
175 } # END OF FATPACK CODE
178 (my $stub = $_) =~ s/\.pm$//;
179 my $name = uc join '_', split '/', $stub;
180 my $data = $files{$_}; $data =~ s/^/ /mg;
181 '$fatpacked{'.perlstring($_).qq!} = <<'${name}';\n!
182 .qq!${data}${name}\n!;
184 print join "\n", $start, @segments, $end;