use shellwords to split up pp option string
[catagits/Catalyst-Devel.git] / lib / Module / Install / Catalyst.pm
1 package Module::Install::Catalyst;
2
3 use strict;
4
5 our @ISA;
6 require Module::Install::Base;
7 @ISA = qw/Module::Install::Base/;
8
9 use File::Find;
10 use FindBin;
11 use File::Copy::Recursive 'rcopy';
12 use File::Spec ();
13 use Getopt::Long ();
14 use Data::Dumper;
15
16 my $SAFETY = 0;
17
18 our @IGNORE =
19   qw/Build Build.PL Changes MANIFEST META.yml Makefile.PL Makefile README
20   _build blib lib script t inc .*\.svn \.git _darcs \.bzr \.hg
21   debian build-stamp install-stamp configure-stamp/;
22 our @CLASSES   = ();
23 our $ENGINE    = 'CGI';
24 our $SCRIPT    = '';
25 our $USAGE     = '';
26 our %PAROPTS   = ();
27
28 =head1 NAME
29
30   Module::Install::Catalyst - Module::Install extension for Catalyst
31
32 =head1 SYNOPSIS
33
34   use inc::Module::Install;
35
36   name 'MyApp';
37   all_from 'lib/MyApp.pm';
38
39   requires 'Catalyst::Runtime' => '5.7014';
40
41   catalyst_ignore('.*temp');
42   catalyst_ignore('.*tmp');
43   catalyst;
44   WriteAll;
45
46 =head1 DESCRIPTION
47
48 L<Module::Install> extension for Catalyst.
49
50 =head1 METHODS
51
52 =head2 catalyst
53
54 Calls L<catalyst_files> and L<catalyst_par>. Should be the last catalyst*
55 command called in C<Makefile.PL>.
56
57 =cut
58
59 sub catalyst {
60     my $self = shift;
61     print <<EOF;
62 *** Module::Install::Catalyst
63 EOF
64     $self->catalyst_files;
65     $self->catalyst_par;
66     print <<EOF;
67 *** Module::Install::Catalyst finished.
68 EOF
69 }
70
71 =head2 catalyst_files
72
73 Collect a list of all files a Catalyst application consists of and copy it
74 inside the blib/lib/ directory. Files and directories that match the modules
75 ignore list are excluded (see L<catalyst_ignore> and L<catalyst_ignore_all>).
76
77 =cut
78
79 sub 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
103 This function replaces the built-in default ignore list with the given list.
104
105 =cut
106
107 sub catalyst_ignore_all {
108     my ( $self, $ignore ) = @_;
109     @IGNORE = @$ignore;
110 }
111
112 =head2 catalyst_ignore(\@ignore)
113
114 Add a regexp to the list of ignored patterns. Can be called multiple times.
115
116 =cut
117
118 sub 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
128 sub catalyst_par {
129     my ( $self, $par ) = @_;
130     $par ||= '';
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;
138     local $Data::Dumper::Indent = 0;
139     local $Data::Dumper::Terse = 1;
140     local $Data::Dumper::Pad = ' ';
141     my $paropts_string = Dumper(\%PAROPTS) || "{ }";
142     $self->postamble(<<EOF);
143 catalyst_par :: all
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# } )"
145 EOF
146     print <<EOF;
147 Please run "make catalyst_par" to create the PAR package!
148 EOF
149 }
150
151 =head2 catalyst_par_core($core)
152
153 =cut
154
155 sub catalyst_par_core {
156     my ( $self, $core ) = @_;
157     $core ? ( $PAROPTS{'B'} = $core ) : $PAROPTS{'B'}++;
158 }
159
160 =head2 catalyst_par_classes(@clases)
161
162 =cut
163
164 sub catalyst_par_classes {
165     my ( $self, @classes ) = @_;
166     push @CLASSES, @classes;
167 }
168
169 =head2 catalyst_par_engine($engine)
170
171 =cut
172
173 sub catalyst_par_engine {
174     my ( $self, $engine ) = @_;
175     $ENGINE = $engine;
176 }
177
178 =head2 catalyst_par_multiarch($multiarch)
179
180 =cut
181
182 sub catalyst_par_multiarch {
183     my ( $self, $multiarch ) = @_;
184     $multiarch ? ( $PAROPTS{'m'} = $multiarch ) : $PAROPTS{'m'}++;
185 }
186
187 =head2 catalyst_par_options($optstring)
188
189 This command can be used in Makefile.PL to customise the PAR creation process.
190 The parameter "$optstring" contains a string with arguments in identical syntax
191 as arguments of B<pp> command from L<PAR::Packer> package.
192
193 Example:
194
195     # part of your Makefile.PL
196
197     catalyst_par_options("--verbose=2 -f Bleach -z 9");
198     # verbose mode; use filter 'Bleach'; zip with compression level 9
199     catalyst;
200
201 Note1: There is no reason to use catalyst_par_options() command multiple times
202 as you can spacify in "$optstring" as many options as you want. Still, it
203 is supported to call catalyst_par_options() more than once. In that case the
204 specified options are merged (collisions are handled on principle "later wins").
205 BEWARE: you are discouraged from using parameters -a -A -X -f -F -I -l -M in
206 multiple catalyst_par_options() as they are not merged but replaced as you would
207 expected.
208
209 Note2: By default the options "-x -p -o=<appname>.par" are set and option "-n"
210 is unset. This default always overrides whatever you specify by
211 catalyst_par_options().
212
213 =cut
214
215 sub catalyst_par_options {
216     my ( $self, $optstring ) = @_;
217     eval "use PAR::Packer ()";
218     if ($@) {
219         warn "WARNING: catalyst_par_options ignored - you need PAR::Packer\n"
220     }
221     else {
222         my $p = Getopt::Long::Parser->new(config => ['no_ignore_case']);
223         my %o;
224         require Text::ParseWords;
225         {
226             local @ARGV = Text::ParseWords::shellwords($optstring);
227             $p->getoptions(\%o, PAR::Packer->options);
228         }
229         %PAROPTS = ( %PAROPTS, %o);
230     }
231 }
232
233 =head2 catalyst_par_script($script)
234
235 =cut
236
237 sub catalyst_par_script {
238     my ( $self, $script ) = @_;
239     $SCRIPT = $script;
240 }
241
242 =head2 catalyst_par_usage($usage)
243
244 =cut
245
246 sub catalyst_par_usage {
247     my ( $self, $usage ) = @_;
248     $USAGE = $usage;
249 }
250
251 package Catalyst::Module::Install;
252
253 use strict;
254 use FindBin;
255 use File::Copy::Recursive 'rmove';
256 use File::Spec ();
257
258 sub _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};
265     my $PAROPTS   = $opts->{PAROPTS};
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";
306 Usage:
307     [parl] $name\[.par] [script] [arguments]
308
309   Examples:
310     parl $name.par $name\_server.pl -r
311     myapp $name\_cgi.pl
312 EOF
313
314     my $script   = $SCRIPT;
315     my $tmp_file = IO::File->new("> $par_pl ");
316     print $tmp_file <<"EOF";
317 if ( \$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
336 END
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 }
345 else {
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 }
363 EOF
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 = (
371         %{$PAROPTS},
372         # take user defined options first and override them with harcoded defaults
373         'x' => 1,
374         'n' => 0,
375         'o' => $par,
376         'p' => 1,
377     );
378     # do not replace the whole $opt{'a'} array; just push required default value
379     push @{$opt{'a'}}, grep( !/par.pl/, glob '.' );
380
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
397 =head1 AUTHORS
398
399 Catalyst Contributors, see Catalyst.pm
400
401 =head1 LICENSE
402
403 This library is free software. You can redistribute it and/or modify it under
404 the same terms as Perl itself.
405
406 =cut
407
408 1;