usual dist crap
[p5sagit/App-FatPacker.git] / lib / App / FatPacker.pm
CommitLineData
48af1939 1package App::FatPacker;
2
3use strict;
4use warnings FATAL => 'all';
5use Getopt::Long;
6use Cwd qw(cwd);
7use File::Find qw(find);
8use File::Spec::Functions qw(
9 catdir splitpath splitdir catpath rel2abs abs2rel
10);
11use File::Copy qw(copy);
12use File::Path qw(make_path remove_tree);
13use B qw(perlstring);
14
15my $option_parser = Getopt::Long::Parser->new(
16 config => [ qw(require_order pass_through bundling no_auto_abbrev) ]
17);
18
19sub call_parser {
20 local *ARGV = [ @{$_[0]} ];
21 $option_parser->getoptions(@{$_[1]});
22 [ @ARGV ];
23}
24
25sub lines_of {
26 map +(chomp,$_)[1], do { local @ARGV = ($_[0]); <> };
27}
28
29sub stripspace {
30 my ($text) = @_;
31 $text =~ /^(\s+)/ && $text =~ s/^$1//mg;
32 $text;
33}
34
35our $VERSION = '0.009001'; # 0.9.1
36
37$VERSION = eval $VERSION;
38
39sub import {
40 $_[1] eq '-run_script'
41 and return shift->new->run_script;
42}
43
44sub new { bless({}, $_[0]) }
45
46sub run_script {
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}")) {
51 $self->$meth(\@args);
52 } else {
53 die "No such command ${cmd}";
54 }
55}
56
57sub script_command_trace {
58 my ($self, $args) = @_;
59
60 $args = call_parser $args => [
61 'to=s' => \my $file,
62 'to-stderr' => \my $to_stderr,
63 ];
64
65 die "Can't use to and to-stderr on same call" if $file && $to_stderr;
66
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: $!";
70 }
71 my $arg = do {
72 if ($file) {
73 "=>>${file}"
74 } elsif ($to_stderr) {
75 "=>&STDERR"
76 } else {
77 ""
78 }
79 };
80 {
81 local $ENV{PERL5OPT} = '-MApp::FatPacker::Trace'.$arg;
82 system $^X, @$args;
83 }
84}
85
86sub script_command_packlists_for {
87 my ($self, $args) = @_;
88 foreach my $pl ($self->packlists_containing($args)) {
89 print "${pl}\n";
90 }
91}
92
93sub packlists_containing {
94 my ($self, $targets) = @_;
95 my @targets = @$targets;
96 require $_ for @targets;
97 my @search = grep -d $_, map catdir($_, 'auto'), @INC;
98 my %pack_rev;
99 my $cwd = cwd;
100 find(sub {
101 return unless $_ eq '.packlist' && -f $_;
102 $pack_rev{$_} = $File::Find::name for lines_of $File::Find::name;
103 }, @search);
104 chdir($cwd) or die "Couldn't chdir back to ${cwd} after find: $!";
105 my %found; @found{map $pack_rev{$INC{$_}}, @targets} = ();
106 sort keys %found;
107}
108
109sub script_command_tree {
110 my ($self, $args) = @_;
111 my $base = catdir(cwd,'fatlib');
112 $self->packlists_to_tree($base, $args);
113}
114
115sub packlists_to_tree {
116 my ($self, $where, $packlists) = @_;
117 remove_tree $where;
118 make_path $where;
119 foreach my $pl (@$packlists) {
120 my ($vol, $dirs, $file) = splitpath $pl;
121 my @dir_parts = splitdir $dirs;
122 my $pack_base;
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];
127 last PART;
128 }
129 }
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;
139 }
140 }
141}
142
143sub script_command_file {
144 my ($self, $args) = @_;
145 my $file = shift @$args;
146 my $cwd = cwd;
147 my @dirs = map rel2abs($_, $cwd), ('lib','fatlib');
148 my %files;
149 foreach my $dir (@dirs) {
150 find(sub {
151 return unless -f $_;
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); <>
155 };
156 }, $dir);
157 }
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'
161 BEGIN {
162 my %fatpacked;
163 END_START
164 my $end = stripspace <<' END_END';
165 s/^ //mg for values %fatpacked;
166
167 unshift @INC, sub {
168 if (my $fat = $fatpacked{$_[1]}) {
169 open my $fh, '<', \$fat;
170 return $fh;
171 }
172 return
173 };
174
175 } # END OF FATPACK CODE
176 END_END
177 my @segments = map {
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!;
183 } sort keys %files;
184 print join "\n", $start, @segments, $end;
185}
1861;