don't pollute M::I plugin detector with GetOptions symbol from GetOpt::Long
[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         {
225             local @ARGV = $optstring;
226             $p->getoptions(\%o, PAR::Packer->options);
227         }
228         %PAROPTS = ( %PAROPTS, %o);
229     }
230 }
231
232 =head2 catalyst_par_script($script)
233
234 =cut
235
236 sub catalyst_par_script {
237     my ( $self, $script ) = @_;
238     $SCRIPT = $script;
239 }
240
241 =head2 catalyst_par_usage($usage)
242
243 =cut
244
245 sub catalyst_par_usage {
246     my ( $self, $usage ) = @_;
247     $USAGE = $usage;
248 }
249
250 package Catalyst::Module::Install;
251
252 use strict;
253 use FindBin;
254 use File::Copy::Recursive 'rmove';
255 use File::Spec ();
256
257 sub _catalyst_par {
258     my ( $par, $class_name, $opts ) = @_;
259
260     my $ENGINE    = $opts->{ENGINE};
261     my $CLASSES   = $opts->{CLASSES} || [];
262     my $USAGE     = $opts->{USAGE};
263     my $SCRIPT    = $opts->{SCRIPT};
264     my $PAROPTS   = $opts->{PAROPTS};
265
266     my $name = $class_name;
267     $name =~ s/::/_/g;
268     $name = lc $name;
269     $par ||= "$name.par";
270     my $engine = $ENGINE || 'CGI';
271
272     # Check for PAR
273     eval "use PAR ()";
274     die "Please install PAR\n" if $@;
275     eval "use PAR::Packer ()";
276     die "Please install PAR::Packer\n" if $@;
277     eval "use App::Packer::PAR ()";
278     die "Please install App::Packer::PAR\n" if $@;
279     eval "use Module::ScanDeps ()";
280     die "Please install Module::ScanDeps\n" if $@;
281
282     my $root = $FindBin::Bin;
283     $class_name =~ s/-/::/g;
284     my $path = File::Spec->catfile( 'blib', 'lib', split( '::', $class_name ) );
285     $path .= '.pm';
286     unless ( -f $path ) {
287         print qq/Not writing PAR, "$path" doesn't exist\n/;
288         return 0;
289     }
290     print qq/Writing PAR "$par"\n/;
291     chdir File::Spec->catdir( $root, 'blib' );
292
293     my $par_pl = 'par.pl';
294     unlink $par_pl;
295
296     my $version = $Catalyst::VERSION;
297     my $class   = $class_name;
298
299     my $classes = '';
300     $classes .= "    require $_;\n" for @$CLASSES;
301
302     unlink $par_pl;
303
304     my $usage = $USAGE || <<"EOF";
305 Usage:
306     [parl] $name\[.par] [script] [arguments]
307
308   Examples:
309     parl $name.par $name\_server.pl -r
310     myapp $name\_cgi.pl
311 EOF
312
313     my $script   = $SCRIPT;
314     my $tmp_file = IO::File->new("> $par_pl ");
315     print $tmp_file <<"EOF";
316 if ( \$ENV{PAR_PROGNAME} ) {
317     my \$zip = \$PAR::LibCache{\$ENV{PAR_PROGNAME}}
318         || Archive::Zip->new(__FILE__);
319     my \$script = '$script';
320     \$ARGV[0] ||= \$script if \$script;
321     if ( ( \@ARGV == 0 ) || ( \$ARGV[0] eq '-h' ) || ( \$ARGV[0] eq '-help' )) {
322         my \@members = \$zip->membersMatching('.*script/.*\.pl');
323         my \$list = "  Available scripts:\\n";
324         for my \$member ( \@members ) {
325             my \$name = \$member->fileName;
326             \$name =~ /(\\w+\\.pl)\$/;
327             \$name = \$1;
328             next if \$name =~ /^main\.pl\$/;
329             next if \$name =~ /^par\.pl\$/;
330             \$list .= "    \$name\\n";
331         }
332         die <<"END";
333 $usage
334 \$list
335 END
336     }
337     my \$file = shift \@ARGV;
338     \$file =~ s/^.*[\\/\\\\]//;
339     \$file =~ s/\\.[^.]*\$//i;
340     my \$member = eval { \$zip->memberNamed("./script/\$file.pl") };
341     die qq/Can't open perl script "\$file"\n/ unless \$member;
342     PAR::_run_member( \$member, 1 );
343 }
344 else {
345     require lib;
346     import lib 'lib';
347     \$ENV{CATALYST_ENGINE} = '$engine';
348     require $class;
349     import $class;
350     require Catalyst::Helper;
351     require Catalyst::Test;
352     require Catalyst::Engine::HTTP;
353     require Catalyst::Engine::CGI;
354     require Catalyst::Controller;
355     require Catalyst::Model;
356     require Catalyst::View;
357     require Getopt::Long;
358     require Pod::Usage;
359     require Pod::Text;
360     $classes
361 }
362 EOF
363     $tmp_file->close;
364
365     # Create package
366     local $SIG{__WARN__} = sub { };
367     open my $olderr, '>&STDERR';
368     open STDERR, '>', File::Spec->devnull;
369     my %opt = (
370         %{$PAROPTS},
371         # take user defined options first and override them with harcoded defaults
372         'x' => 1,
373         'n' => 0,
374         'o' => $par,
375         'p' => 1,
376     );
377     # do not replace the whole $opt{'a'} array; just push required default value
378     push @{$opt{'a'}}, grep( !/par.pl/, glob '.' );
379
380     App::Packer::PAR->new(
381         frontend  => 'Module::ScanDeps',
382         backend   => 'PAR::Packer',
383         frontopts => \%opt,
384         backopts  => \%opt,
385         args      => ['par.pl'],
386     )->go;
387
388     open STDERR, '>&', $olderr;
389
390     unlink $par_pl;
391     chdir $root;
392     rmove( File::Spec->catfile( 'blib', $par ), $par );
393     return 1;
394 }
395
396 =head1 AUTHORS
397
398 Catalyst Contributors, see Catalyst.pm
399
400 =head1 LICENSE
401
402 This library is free software. You can redistribute it and/or modify it under
403 the same terms as Perl itself.
404
405 =cut
406
407 1;