don't pollute M::I plugin detector with GetOptions symbol from GetOpt::Long
[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;
224 {
225 local @ARGV = $optstring;
226 $p->getoptions(\%o, PAR::Packer->options);
227 }
84a68fcf 228 %PAROPTS = ( %PAROPTS, %o);
7235d8b3 229 }
134481a2 230}
231
232=head2 catalyst_par_script($script)
233
234=cut
235
236sub catalyst_par_script {
237 my ( $self, $script ) = @_;
238 $SCRIPT = $script;
239}
240
241=head2 catalyst_par_usage($usage)
242
243=cut
244
245sub catalyst_par_usage {
246 my ( $self, $usage ) = @_;
247 $USAGE = $usage;
248}
249
250package Catalyst::Module::Install;
251
252use strict;
253use FindBin;
254use File::Copy::Recursive 'rmove';
255use File::Spec ();
256
257sub _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};
84a68fcf 264 my $PAROPTS = $opts->{PAROPTS};
134481a2 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";
305Usage:
306 [parl] $name\[.par] [script] [arguments]
307
308 Examples:
309 parl $name.par $name\_server.pl -r
310 myapp $name\_cgi.pl
311EOF
312
313 my $script = $SCRIPT;
314 my $tmp_file = IO::File->new("> $par_pl ");
315 print $tmp_file <<"EOF";
316if ( \$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
335END
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}
344else {
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}
362EOF
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 = (
4578b8ae 370 %{$PAROPTS},
371 # take user defined options first and override them with harcoded defaults
134481a2 372 'x' => 1,
373 'n' => 0,
374 'o' => $par,
134481a2 375 'p' => 1,
134481a2 376 );
84a68fcf 377 # do not replace the whole $opt{'a'} array; just push required default value
e7c01705 378 push @{$opt{'a'}}, grep( !/par.pl/, glob '.' );
84a68fcf 379
134481a2 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
cb536e7b 396=head1 AUTHORS
134481a2 397
cb536e7b 398Catalyst Contributors, see Catalyst.pm
134481a2 399
400=head1 LICENSE
401
7cd3b67e 402This library is free software. You can redistribute it and/or modify it under
134481a2 403the same terms as Perl itself.
404
405=cut
406
4071;