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