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