debug message for params
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Helper.pm
CommitLineData
fc7ec1d9 1package Catalyst::Helper;
2
3use strict;
4use base 'Class::Accessor::Fast';
f1d0b7ea 5use Config;
fc7ec1d9 6use File::Spec;
7use File::Path;
8use IO::File;
9use FindBin;
10
11=head1 NAME
12
13Catalyst::Helper - Bootstrap a Catalyst application
14
15=head1 SYNOPSIS
16
17See L<Catalyst::Manual::Intro>
18
19=head1 DESCRIPTION
20
21Bootstrap a Catalyst application.
22
23=head2 METHODS
24
25=head3 mk_app
26
27=cut
28
29sub mk_app {
30 my ( $self, $name ) = @_;
31 return 0 if $name =~ /[^\w\:]/;
32 $self->{name} = $name;
33 $self->{dir} = $name;
34 $self->{dir} =~ s/\:\:/-/g;
35 $self->_mk_dirs;
36 $self->_mk_appclass;
37 $self->_mk_makefile;
91864987 38 $self->_mk_readme;
39 $self->_mk_changes;
fc7ec1d9 40 $self->_mk_apptest;
41 $self->_mk_server;
42 $self->_mk_test;
43 $self->_mk_create;
44 return 1;
45}
46
ad41e777 47=head3 mk_component
48
49=cut
50
51sub mk_component {
52 my ( $self, $app, $type, $name, $helper, @args ) = @_;
53 return 0
54 if ( $name =~ /[^\w\:]/ || !\$type =~ /^model|m|view|v|controller|c\$/i );
55 return 0 if $name =~ /[^\w\:]/;
56 $type = 'M' if $type =~ /model|m/i;
57 $type = 'V' if $type =~ /view|v/i;
58 $type = 'C' if $type =~ /controller|c/i;
59 $self->{type} = $type;
60 $self->{name} = $name;
61 $self->{class} = "$app\::$type\::$name";
62 $self->{app} = $app;
63
64 # Class
65 my $appdir = File::Spec->catdir( split /\:\:/, $app );
66 my $path = File::Spec->catdir( $FindBin::Bin, '..', 'lib', $appdir, $type );
67 my $file = $name;
68 if ( $name =~ /\:/ ) {
69 my @path = split /\:\:/, $name;
70 $file = pop @path;
71 $path = File::Spec->catdir( $path, @path );
72 mkpath $path;
73 }
74 $file = File::Spec->catfile( $path, "$file.pm" );
75 $self->{file} = $file;
76
77 # Test
78 $self->{test_dir} = File::Spec->catdir( $FindBin::Bin, '..', 't' );
79 $self->{test} = $self->next_test;
80
81 # Helper
82 if ($helper) {
83 my $comp = 'Model';
84 $comp = 'View' if $type eq 'V';
85 $comp = 'Controller' if $type eq 'C';
86 my $class = "Catalyst::Helper::$comp\::$helper";
87 eval "require $class";
88 die qq/Couldn't load helper "$class", "$@"/ if $@;
89 if ( $class->can('mk_compclass') ) {
90 return 1 unless $class->mk_compclass( $self, @args );
91 }
92 else { return 1 unless $self->_mk_compclass }
93
94 if ( $class->can('mk_comptest') ) {
95 $class->mk_comptest( $self, @args );
96 }
97 else { $self->_mk_comptest }
98 }
99
100 # Fallback
101 else {
102 return 1 unless $self->_mk_compclass;
103 $self->_mk_comptest;
104 }
105 return 1;
106}
107
108=head3 mk_dir
109
110=cut
111
112sub mk_dir {
113 my ( $self, $dir ) = @_;
114 if ( -d $dir ) {
115 print qq/ exists "$dir"\n/;
116 return 0;
117 }
118 if ( mkpath $dir) {
119 print qq/created "$dir"\n/;
120 return 1;
121 }
122 die qq/Couldn't create "$dir", "$!"/;
123}
124
125=head3 mk_file
126
127=cut
128
129sub mk_file {
130 my ( $self, $file, $content ) = @_;
131 if ( -e $file ) {
132 print qq/ exists "$file"\n/;
133 return 0;
134 }
135 if ( my $f = IO::File->new("> $file") ) {
136 print $f $content;
137 print qq/created "$file"\n/;
138 return 1;
139 }
140 die qq/Couldn't create "$file", "$!"/;
141}
142
143=head3 next_test
144
145=cut
146
147sub next_test {
148 my ( $self, $tname ) = @_;
3cb1db8c 149 if ($tname) { $tname = "$tname.t" }
ad41e777 150 else {
151 my $name = $self->{name};
ad41e777 152 my $prefix = $name;
153 $prefix =~ s/::/_/g;
154 $prefix = lc $prefix;
3cb1db8c 155 $tname = $prefix . '.t';
ad41e777 156 $self->{prefix} = $prefix;
157 }
3cb1db8c 158 my $dir = $self->{test_dir};
159 my $type = lc $self->{type};
160 return File::Spec->catfile( $dir, $type, $tname );
ad41e777 161}
162
fc7ec1d9 163sub _mk_dirs {
164 my $self = shift;
ad41e777 165 $self->mk_dir( $self->{dir} );
91864987 166 $self->{script} = File::Spec->catdir( $self->{dir}, 'script' );
167 $self->mk_dir( $self->{script} );
fc7ec1d9 168 $self->{lib} = File::Spec->catdir( $self->{dir}, 'lib' );
ad41e777 169 $self->mk_dir( $self->{lib} );
fc7ec1d9 170 $self->{root} = File::Spec->catdir( $self->{dir}, 'root' );
ad41e777 171 $self->mk_dir( $self->{root} );
fc7ec1d9 172 $self->{t} = File::Spec->catdir( $self->{dir}, 't' );
ad41e777 173 $self->mk_dir( $self->{t} );
3cb1db8c 174 $self->mk_dir( File::Spec->catdir( $self->{t}, 'm' ) );
175 $self->mk_dir( File::Spec->catdir( $self->{t}, 'v' ) );
176 $self->mk_dir( File::Spec->catdir( $self->{t}, 'c' ) );
fc7ec1d9 177 $self->{class} = File::Spec->catdir( split( /\:\:/, $self->{name} ) );
178 $self->{mod} = File::Spec->catdir( $self->{lib}, $self->{class} );
ad41e777 179 $self->mk_dir( $self->{mod} );
fc7ec1d9 180 $self->{m} = File::Spec->catdir( $self->{mod}, 'M' );
ad41e777 181 $self->mk_dir( $self->{m} );
fc7ec1d9 182 $self->{v} = File::Spec->catdir( $self->{mod}, 'V' );
ad41e777 183 $self->mk_dir( $self->{v} );
fc7ec1d9 184 $self->{c} = File::Spec->catdir( $self->{mod}, 'C' );
ad41e777 185 $self->mk_dir( $self->{c} );
fc7ec1d9 186 $self->{base} = File::Spec->rel2abs( $self->{dir} );
187}
188
189sub _mk_appclass {
ad41e777 190 my $self = shift;
191 my $mod = $self->{mod};
192 my $name = $self->{name};
193 my $base = $self->{base};
194 $self->mk_file( "$mod.pm", <<"EOF");
fc7ec1d9 195package $name;
196
197use strict;
198use Catalyst qw/-Debug/;
199
200our \$VERSION = '0.01';
201
202$name->config(
203 name => '$name',
204 root => '$base/root',
205);
206
207$name->action(
208
209 '!default' => sub {
210 my ( \$self, \$c ) = \@_;
211 \$c->res->output('Congratulations, $name is on Catalyst!');
212 },
213
214);
215
216=head1 NAME
217
218$name - A very nice application
219
220=head1 SYNOPSIS
221
222 Very simple to use
223
224=head1 DESCRIPTION
225
226Very nice application.
227
228=head1 AUTHOR
229
230Clever guy
231
232=head1 LICENSE
233
234This library is free software . You can redistribute it and/or modify it under
235the same terms as perl itself.
236
237=cut
238
2391;
240EOF
241}
242
243sub _mk_makefile {
ad41e777 244 my $self = shift;
245 my $name = $self->{name};
246 my $dir = $self->{dir};
247 my $class = $self->{class};
248 $self->mk_file( "$dir\/Makefile.PL", <<"EOF");
fc7ec1d9 249use ExtUtils::MakeMaker;
250
251WriteMakefile(
252 NAME => '$name',
253 VERSION_FROM => 'lib/$class.pm',
3cb1db8c 254 PREREQ_PM => { Catalyst => 0 },
255 test => { TESTS => join ' ', ( glob('t/*.t'), glob('t/*/*.t') ) }
fc7ec1d9 256);
257EOF
258}
259
91864987 260sub _mk_readme {
261 my $self = shift;
262 my $dir = $self->{dir};
263 $self->mk_file( "$dir\/README", <<"EOF");
264Run script/server.pl to test the application.
265EOF
266}
267
268sub _mk_changes {
269 my $self = shift;
270 my $name = $self->{name};
271 my $dir = $self->{dir};
272 my $time = localtime time;
273 $self->mk_file( "$dir\/Changes", <<"EOF");
274This file documents the revision history for Perl extension $name.
275
2760.01 $time
277 - initial revision, generated by Catalyst
278EOF
279}
280
fc7ec1d9 281sub _mk_apptest {
282 my $self = shift;
283 my $t = $self->{t};
284 my $name = $self->{name};
ad41e777 285 $self->mk_file( "$t\/01app.t", <<"EOF");
fc7ec1d9 286use Test::More tests => 2;
287use_ok( Catalyst::Test, '$name' );
288
289ok( request('/')->is_success );
290EOF
ad41e777 291 $self->mk_file( "$t\/02podcoverage.t", <<"EOF");
1df125c9 292use Test::More;
293
294eval "use Test::Pod::Coverage 1.04";
295plan skip_all => 'Test::Pod::Coverage 1.04 required' if \$@;
296plan skip_all => 'set TEST_POD to enable this test' unless \$ENV{TEST_POD};
297
298all_pod_coverage_ok();
299EOF
fc7ec1d9 300}
301
302sub _mk_server {
91864987 303 my $self = shift;
304 my $name = $self->{name};
305 my $script = $self->{script};
306 $self->mk_file( "$script\/server.pl", <<"EOF");
f1d0b7ea 307$Config{startperl} -w
fc7ec1d9 308
309use strict;
310use Getopt::Long;
311use Pod::Usage;
312use FindBin;
313use lib "\$FindBin::Bin/../lib";
314use Catalyst::Test '$name';
315
316my \$help = 0;
317my \$port = 3000;
318
319GetOptions( 'help|?' => \\\$help, 'port=s' => \\\$port );
320
321pod2usage(1) if \$help;
322
323Catalyst::Test::server(\$port);
324
3251;
326__END__
327
328=head1 NAME
329
330server - Catalyst Testserver
331
332=head1 SYNOPSIS
333
91864987 334server.pl [options]
fc7ec1d9 335
336 Options:
ad41e777 337 -? -help display this help and exits
338 -p -port port (defaults to 3000)
fc7ec1d9 339
03a53815 340 See also:
341 perldoc Catalyst::Manual
342 perldoc Catalyst::Manual::Intro
343
fc7ec1d9 344=head1 DESCRIPTION
345
346Run a Catalyst Testserver for this application.
347
348=head1 AUTHOR
349
350Sebastian Riedel, C<sri\@oook.de>
351
352=head1 COPYRIGHT
353
354Copyright 2004 Sebastian Riedel. All rights reserved.
355
356This library is free software. You can redistribute it and/or modify it under
357the same terms as perl itself.
358
359=cut
360EOF
91864987 361 chmod 0700, "$script/server.pl";
fc7ec1d9 362}
363
364sub _mk_test {
91864987 365 my $self = shift;
366 my $name = $self->{name};
367 my $script = $self->{script};
368 $self->mk_file( "$script/test.pl", <<"EOF");
f1d0b7ea 369$Config{startperl} -w
fc7ec1d9 370
371use strict;
372use Getopt::Long;
373use Pod::Usage;
374use FindBin;
375use lib "\$FindBin::Bin/../lib";
376
377my \$help = 0;
378
379GetOptions( 'help|?' => \\\$help );
380
381pod2usage(1) if ( \$help || !\$ARGV[0] );
382
383require Catalyst::Test;
384import Catalyst::Test '$name';
385
386print get(\$ARGV[0]) . "\n";
387
3881;
389__END__
390
391=head1 NAME
392
393test - Catalyst Test
394
395=head1 SYNOPSIS
396
91864987 397test.pl [options] uri
fc7ec1d9 398
399 Options:
400 -help display this help and exits
401
402 Examples:
91864987 403 test.pl http://localhost/some_action
404 test.pl /some_action
fc7ec1d9 405
03a53815 406 See also:
407 perldoc Catalyst::Manual
408 perldoc Catalyst::Manual::Intro
409
fc7ec1d9 410=head1 DESCRIPTION
411
412Run a Catalyst action from the comand line.
413
414=head1 AUTHOR
415
416Sebastian Riedel, C<sri\@oook.de>
417
418=head1 COPYRIGHT
419
420Copyright 2004 Sebastian Riedel. All rights reserved.
421
422This library is free software. You can redistribute it and/or modify it under
423the same terms as perl itself.
424
425=cut
426EOF
91864987 427 chmod 0700, "$script/test.pl";
fc7ec1d9 428}
429
430sub _mk_create {
91864987 431 my $self = shift;
432 my $name = $self->{name};
433 my $script = $self->{script};
434 $self->mk_file( "$script\/create.pl", <<"EOF");
f1d0b7ea 435$Config{startperl} -w
fc7ec1d9 436
437use strict;
438use Getopt::Long;
439use Pod::Usage;
440use Catalyst::Helper;
441
442my \$help = 0;
443
444GetOptions( 'help|?' => \$help );
445
446pod2usage(1) if ( \$help || !\$ARGV[1] );
447
448my \$helper = Catalyst::Helper->new;
449pod2usage(1) unless \$helper->mk_component( '$name', \@ARGV );
450
4511;
452__END__
453
454=head1 NAME
455
456create - Create a new Catalyst Component
457
458=head1 SYNOPSIS
459
91864987 460create.pl [options] model|view|controller name [helper] [options]
fc7ec1d9 461
462 Options:
463 -help display this help and exits
464
465 Examples:
91864987 466 create.pl controller My::Controller
467 create.pl view My::View
468 create.pl view MyView TT
469 create.pl view TT TT
470 create.pl model My::Model
471 create.pl model SomeDB CDBI dbi:SQLite:/tmp/my.db
472 create.pl model AnotherDB CDBI dbi:Pg:dbname=foo root 4321
03a53815 473
474 See also:
475 perldoc Catalyst::Manual
476 perldoc Catalyst::Manual::Intro
fc7ec1d9 477
478=head1 DESCRIPTION
479
480Create a new Catalyst Component.
481
482=head1 AUTHOR
483
484Sebastian Riedel, C<sri\@oook.de>
485
486=head1 COPYRIGHT
487
488Copyright 2004 Sebastian Riedel. All rights reserved.
489
490This library is free software. You can redistribute it and/or modify it under
491the same terms as perl itself.
492
493=cut
494EOF
91864987 495 chmod 0700, "$script/create.pl";
fc7ec1d9 496}
497
fc7ec1d9 498sub _mk_compclass {
499 my $self = shift;
500 my $app = $self->{app};
501 my $class = $self->{class};
502 my $type = $self->{type};
503 my $action = '';
504 $action = <<"EOF" if $type eq 'C';
505
506$app->action(
507
508 '!?default' => sub {
509 my ( \$self, \$c ) = \@_;
510 \$c->res->output('Congratulations, $class is on Catalyst!');
511 },
512
513);
514EOF
515 my $file = $self->{file};
ad41e777 516 return $self->mk_file( "$file", <<"EOF");
fc7ec1d9 517package $class;
518
519use strict;
520use base 'Catalyst::Base';
521$action
522=head1 NAME
523
524$class - A Component
525
526=head1 SYNOPSIS
527
528 Very simple to use
529
530=head1 DESCRIPTION
531
532Very nice component.
533
534=head1 AUTHOR
535
536Clever guy
537
538=head1 LICENSE
539
540This library is free software . You can redistribute it and/or modify it under
541the same terms as perl itself.
542
543=cut
544
5451;
546EOF
547}
548
549sub _mk_comptest {
550 my $self = shift;
551 my $prefix = $self->{prefix};
552 my $type = $self->{type};
553 my $class = $self->{class};
554 my $app = $self->{app};
555 my $test = $self->{test};
fc7ec1d9 556 if ( $self->{type} eq 'C' ) {
ad41e777 557 $self->mk_file( "$test", <<"EOF");
fc7ec1d9 558use Test::More tests => 3;
559use_ok( Catalyst::Test, '$app' );
560use_ok('$class');
561
562ok( request('$prefix')->is_success );
563EOF
564 }
565 else {
ad41e777 566 $self->mk_file( "$test", <<"EOF");
fc7ec1d9 567use Test::More tests => 1;
568use_ok('$class');
569EOF
570 }
571}
572
7833fdfc 573=head1 HELPERS
574
575Helpers are classes that provide two methods.
576
577 * mk_compclass - creates the Component class
578 * mk_comptest - creates the Component test
579
580So when you call C<bin/create view MyView TT>, create would try to execute
581Catalyst::Helper::View::TT->mk_compclass and
582Catalyst::Helper::View::TT->mk_comptest.
583
584See L<Catalyst::Helper::View::TT> and L<Catalyst::Helper::Model::CDBI> for
585examples.
586
587All helper classes should be under one of the following namespaces.
588
589 Catalyst::Helper::Model::
590 Catalyst::Helper::View::
591 Catalyst::Helper::Controller::
592
fc7ec1d9 593=head1 SEE ALSO
594
595L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
596L<Catalyst::Response>, L<Catalyst>
597
598=head1 AUTHOR
599
600Sebastian Riedel, C<sri@oook.de>
601
602=head1 LICENSE
603
604This library is free software . You can redistribute it and/or modify it under
605the same terms as perl itself.
606
607=cut
608
6091;