use shellwords to split up pp option string
[catagits/Catalyst-Devel.git] / lib / Module / Install / Catalyst.pm
CommitLineData
134481a2 1package Module::Install::Catalyst;
2
3use strict;
4
5our @ISA;
6require Module::Install::Base;
7@ISA = qw/Module::Install::Base/;
8
9use File::Find;
10use FindBin;
11use File::Copy::Recursive 'rcopy';
12use File::Spec ();
09a0d496 13use Getopt::Long ();
84a68fcf 14use Data::Dumper;
134481a2 15
16my $SAFETY = 0;
17
18our @IGNORE =
19 qw/Build Build.PL Changes MANIFEST META.yml Makefile.PL Makefile README
d99c8718 20 _build blib lib script t inc .*\.svn \.git _darcs \.bzr \.hg
21 debian build-stamp install-stamp configure-stamp/;
134481a2 22our @CLASSES = ();
23our $ENGINE = 'CGI';
2f7a957f 24our $SCRIPT = '';
25our $USAGE = '';
84a68fcf 26our %PAROPTS = ();
134481a2 27
28=head1 NAME
29
d99c8718 30 Module::Install::Catalyst - Module::Install extension for Catalyst
e7c01705 31
134481a2 32=head1 SYNOPSIS
e7c01705 33
d99c8718 34 use inc::Module::Install;
e7c01705 35
d99c8718 36 name 'MyApp';
37 all_from 'lib/MyApp.pm';
e7c01705 38
d99c8718 39 requires 'Catalyst::Runtime' => '5.7014';
e7c01705 40
d99c8718 41 catalyst_ignore('.*temp');
42 catalyst_ignore('.*tmp');
43 catalyst;
44 WriteAll;
134481a2 45
46=head1 DESCRIPTION
47
48L<Module::Install> extension for Catalyst.
49
50=head1 METHODS
51
52=head2 catalyst
53
d99c8718 54Calls L<catalyst_files> and L<catalyst_par>. Should be the last catalyst*
55command called in C<Makefile.PL>.
56
134481a2 57=cut
58
59sub catalyst {
60 my $self = shift;
61 print <<EOF;
62*** Module::Install::Catalyst
63EOF
64 $self->catalyst_files;
65 $self->catalyst_par;
66 print <<EOF;
67*** Module::Install::Catalyst finished.
68EOF
69}
70
71=head2 catalyst_files
72
e7c01705 73Collect a list of all files a Catalyst application consists of and copy it
74inside the blib/lib/ directory. Files and directories that match the modules
d99c8718 75ignore list are excluded (see L<catalyst_ignore> and L<catalyst_ignore_all>).
76
134481a2 77=cut
78
79sub catalyst_files {
80 my $self = shift;
81
82 chdir $FindBin::Bin;
83
84 my @files;
85 opendir CATDIR, '.';
86 CATFILES: for my $name ( readdir CATDIR ) {
87 for my $ignore (@IGNORE) {
88 next CATFILES if $name =~ /^$ignore$/;
89 next CATFILES if $name !~ /\w/;
90 }
91 push @files, $name;
92 }
93 closedir CATDIR;
94 my @path = split '-', $self->name;
95 for my $orig (@files) {
96 my $path = File::Spec->catdir( 'blib', 'lib', @path, $orig );
97 rcopy( $orig, $path );
98 }
99}
100
101=head2 catalyst_ignore_all(\@ignore)
102
d99c8718 103This function replaces the built-in default ignore list with the given list.
104
134481a2 105=cut
106
107sub catalyst_ignore_all {
108 my ( $self, $ignore ) = @_;
109 @IGNORE = @$ignore;
110}
111
112=head2 catalyst_ignore(\@ignore)
113
d99c8718 114Add a regexp to the list of ignored patterns. Can be called multiple times.
115
134481a2 116=cut
117
118sub catalyst_ignore {
119 my ( $self, @ignore ) = @_;
120 push @IGNORE, @ignore;
121}
122
123=head2 catalyst_par($name)
124
125=cut
126
127# Workaround for a namespace conflict
128sub catalyst_par {
129 my ( $self, $par ) = @_;
2f7a957f 130 $par ||= '';
134481a2 131 return if $SAFETY;
132 $SAFETY++;
133 my $name = $self->name;
134 my $usage = $USAGE;
135 $usage =~ s/"/\\"/g;
136 my $class_string = join "', '", @CLASSES;
137 $class_string = "'$class_string'" if $class_string;
84a68fcf 138 local $Data::Dumper::Indent = 0;
139 local $Data::Dumper::Terse = 1;
140 local $Data::Dumper::Pad = ' ';
141 my $paropts_string = Dumper(\%PAROPTS) || "{ }";
134481a2 142 $self->postamble(<<EOF);
143catalyst_par :: all
84a68fcf 144\t\$(NOECHO) \$(PERL) -Ilib -Minc::Module::Install -MModule::Install::Catalyst -e"Catalyst::Module::Install::_catalyst_par( '$par', '$name', { CLASSES => [$class_string], PAROPTS => $paropts_string, ENGINE => '$ENGINE', SCRIPT => '$SCRIPT', USAGE => q#$usage# } )"
134481a2 145EOF
146 print <<EOF;
147Please run "make catalyst_par" to create the PAR package!
148EOF
149}
150
151=head2 catalyst_par_core($core)
152
153=cut
154
155sub catalyst_par_core {
156 my ( $self, $core ) = @_;
84a68fcf 157 $core ? ( $PAROPTS{'B'} = $core ) : $PAROPTS{'B'}++;
134481a2 158}
159
160=head2 catalyst_par_classes(@clases)
161
162=cut
163
164sub catalyst_par_classes {
165 my ( $self, @classes ) = @_;
166 push @CLASSES, @classes;
167}
168
169=head2 catalyst_par_engine($engine)
170
171=cut
172
173sub catalyst_par_engine {
174 my ( $self, $engine ) = @_;
175 $ENGINE = $engine;
176}
177
178=head2 catalyst_par_multiarch($multiarch)
179
180=cut
181
182sub catalyst_par_multiarch {
183 my ( $self, $multiarch ) = @_;
84a68fcf 184 $multiarch ? ( $PAROPTS{'m'} = $multiarch ) : $PAROPTS{'m'}++;
185}
186
187=head2 catalyst_par_options($optstring)
188
189This command can be used in Makefile.PL to customise the PAR creation process.
190The parameter "$optstring" contains a string with arguments in identical syntax
191as arguments of B<pp> command from L<PAR::Packer> package.
192
193Example:
194
195 # part of your Makefile.PL
e7c01705 196
4578b8ae 197 catalyst_par_options("--verbose=2 -f Bleach -z 9");
84a68fcf 198 # verbose mode; use filter 'Bleach'; zip with compression level 9
199 catalyst;
200
201Note1: There is no reason to use catalyst_par_options() command multiple times
202as you can spacify in "$optstring" as many options as you want. Still, it
4578b8ae 203is supported to call catalyst_par_options() more than once. In that case the
e7c01705 204specified options are merged (collisions are handled on principle "later wins").
4578b8ae 205BEWARE: you are discouraged from using parameters -a -A -X -f -F -I -l -M in
e7c01705 206multiple catalyst_par_options() as they are not merged but replaced as you would
4578b8ae 207expected.
84a68fcf 208
e7c01705 209Note2: By default the options "-x -p -o=<appname>.par" are set and option "-n"
4578b8ae 210is unset. This default always overrides whatever you specify by
211catalyst_par_options().
84a68fcf 212
213=cut
214
215sub catalyst_par_options {
216 my ( $self, $optstring ) = @_;
84a68fcf 217 eval "use PAR::Packer ()";
218 if ($@) {
219 warn "WARNING: catalyst_par_options ignored - you need PAR::Packer\n"
220 }
221 else {
09a0d496 222 my $p = Getopt::Long::Parser->new(config => ['no_ignore_case']);
223 my %o;
2120406d 224 require Text::ParseWords;
09a0d496 225 {
2120406d 226 local @ARGV = Text::ParseWords::shellwords($optstring);
09a0d496 227 $p->getoptions(\%o, PAR::Packer->options);
228 }
84a68fcf 229 %PAROPTS = ( %PAROPTS, %o);
7235d8b3 230 }
134481a2 231}
232
233=head2 catalyst_par_script($script)
234
235=cut
236
237sub catalyst_par_script {
238 my ( $self, $script ) = @_;
239 $SCRIPT = $script;
240}
241
242=head2 catalyst_par_usage($usage)
243
244=cut
245
246sub catalyst_par_usage {
247 my ( $self, $usage ) = @_;
248 $USAGE = $usage;
249}
250
251package Catalyst::Module::Install;
252
253use strict;
254use FindBin;
255use File::Copy::Recursive 'rmove';
256use File::Spec ();
257
258sub _catalyst_par {
259 my ( $par, $class_name, $opts ) = @_;
260
261 my $ENGINE = $opts->{ENGINE};
262 my $CLASSES = $opts->{CLASSES} || [];
263 my $USAGE = $opts->{USAGE};
264 my $SCRIPT = $opts->{SCRIPT};
84a68fcf 265 my $PAROPTS = $opts->{PAROPTS};
134481a2 266
267 my $name = $class_name;
268 $name =~ s/::/_/g;
269 $name = lc $name;
270 $par ||= "$name.par";
271 my $engine = $ENGINE || 'CGI';
272
273 # Check for PAR
274 eval "use PAR ()";
275 die "Please install PAR\n" if $@;
276 eval "use PAR::Packer ()";
277 die "Please install PAR::Packer\n" if $@;
278 eval "use App::Packer::PAR ()";
279 die "Please install App::Packer::PAR\n" if $@;
280 eval "use Module::ScanDeps ()";
281 die "Please install Module::ScanDeps\n" if $@;
282
283 my $root = $FindBin::Bin;
284 $class_name =~ s/-/::/g;
285 my $path = File::Spec->catfile( 'blib', 'lib', split( '::', $class_name ) );
286 $path .= '.pm';
287 unless ( -f $path ) {
288 print qq/Not writing PAR, "$path" doesn't exist\n/;
289 return 0;
290 }
291 print qq/Writing PAR "$par"\n/;
292 chdir File::Spec->catdir( $root, 'blib' );
293
294 my $par_pl = 'par.pl';
295 unlink $par_pl;
296
297 my $version = $Catalyst::VERSION;
298 my $class = $class_name;
299
300 my $classes = '';
301 $classes .= " require $_;\n" for @$CLASSES;
302
303 unlink $par_pl;
304
305 my $usage = $USAGE || <<"EOF";
306Usage:
307 [parl] $name\[.par] [script] [arguments]
308
309 Examples:
310 parl $name.par $name\_server.pl -r
311 myapp $name\_cgi.pl
312EOF
313
314 my $script = $SCRIPT;
315 my $tmp_file = IO::File->new("> $par_pl ");
316 print $tmp_file <<"EOF";
317if ( \$ENV{PAR_PROGNAME} ) {
318 my \$zip = \$PAR::LibCache{\$ENV{PAR_PROGNAME}}
319 || Archive::Zip->new(__FILE__);
320 my \$script = '$script';
321 \$ARGV[0] ||= \$script if \$script;
322 if ( ( \@ARGV == 0 ) || ( \$ARGV[0] eq '-h' ) || ( \$ARGV[0] eq '-help' )) {
323 my \@members = \$zip->membersMatching('.*script/.*\.pl');
324 my \$list = " Available scripts:\\n";
325 for my \$member ( \@members ) {
326 my \$name = \$member->fileName;
327 \$name =~ /(\\w+\\.pl)\$/;
328 \$name = \$1;
329 next if \$name =~ /^main\.pl\$/;
330 next if \$name =~ /^par\.pl\$/;
331 \$list .= " \$name\\n";
332 }
333 die <<"END";
334$usage
335\$list
336END
337 }
338 my \$file = shift \@ARGV;
339 \$file =~ s/^.*[\\/\\\\]//;
340 \$file =~ s/\\.[^.]*\$//i;
341 my \$member = eval { \$zip->memberNamed("./script/\$file.pl") };
342 die qq/Can't open perl script "\$file"\n/ unless \$member;
343 PAR::_run_member( \$member, 1 );
344}
345else {
346 require lib;
347 import lib 'lib';
348 \$ENV{CATALYST_ENGINE} = '$engine';
349 require $class;
350 import $class;
351 require Catalyst::Helper;
352 require Catalyst::Test;
353 require Catalyst::Engine::HTTP;
354 require Catalyst::Engine::CGI;
355 require Catalyst::Controller;
356 require Catalyst::Model;
357 require Catalyst::View;
358 require Getopt::Long;
359 require Pod::Usage;
360 require Pod::Text;
361 $classes
362}
363EOF
364 $tmp_file->close;
365
366 # Create package
367 local $SIG{__WARN__} = sub { };
368 open my $olderr, '>&STDERR';
369 open STDERR, '>', File::Spec->devnull;
370 my %opt = (
4578b8ae 371 %{$PAROPTS},
372 # take user defined options first and override them with harcoded defaults
134481a2 373 'x' => 1,
374 'n' => 0,
375 'o' => $par,
134481a2 376 'p' => 1,
134481a2 377 );
84a68fcf 378 # do not replace the whole $opt{'a'} array; just push required default value
e7c01705 379 push @{$opt{'a'}}, grep( !/par.pl/, glob '.' );
84a68fcf 380
134481a2 381 App::Packer::PAR->new(
382 frontend => 'Module::ScanDeps',
383 backend => 'PAR::Packer',
384 frontopts => \%opt,
385 backopts => \%opt,
386 args => ['par.pl'],
387 )->go;
388
389 open STDERR, '>&', $olderr;
390
391 unlink $par_pl;
392 chdir $root;
393 rmove( File::Spec->catfile( 'blib', $par ), $par );
394 return 1;
395}
396
cb536e7b 397=head1 AUTHORS
134481a2 398
cb536e7b 399Catalyst Contributors, see Catalyst.pm
134481a2 400
401=head1 LICENSE
402
7cd3b67e 403This library is free software. You can redistribute it and/or modify it under
134481a2 404the same terms as Perl itself.
405
406=cut
407
4081;