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