1 package Catalyst::Helper;
4 use base 'Class::Accessor::Fast';
13 Catalyst::Helper - Bootstrap a Catalyst application
17 See L<Catalyst::Manual::Intro>
21 Bootstrap a Catalyst application.
30 my ( $self, $name ) = @_;
31 return 0 if $name =~ /[^\w\:]/;
32 $self->{name} = $name;
34 $self->{dir} =~ s/\:\:/-/g;
57 $self->{base} = File::Spec->catdir( $FindBin::Bin, '..' );
58 unless ( $_[0] =~ /^model|m|view|v|controller|c\$/i ) {
61 my $class = "Catalyst::Helper::$helper";
62 eval "require $class";
63 die qq/Couldn't load helper "$class", "$@"/ if $@;
64 if ( $class->can('mk_stuff') ) {
65 return 1 unless $class->mk_stuff( $self, @args );
73 return 0 if $name =~ /[^\w\:]/;
74 $type = 'M' if $type =~ /model|m/i;
75 $type = 'V' if $type =~ /view|v/i;
76 $type = 'C' if $type =~ /controller|c/i;
77 $self->{type} = $type;
78 $self->{name} = $name;
79 $self->{class} = "$app\::$type\::$name";
82 my $appdir = File::Spec->catdir( split /\:\:/, $app );
84 File::Spec->catdir( $FindBin::Bin, '..', 'lib', $appdir, $type );
86 if ( $name =~ /\:/ ) {
87 my @path = split /\:\:/, $name;
89 $path = File::Spec->catdir( $path, @path );
92 $file = File::Spec->catfile( $path, "$file.pm" );
93 $self->{file} = $file;
96 $self->{test_dir} = File::Spec->catdir( $FindBin::Bin, '..', 't' );
97 $self->{test} = $self->next_test;
102 $comp = 'View' if $type eq 'V';
103 $comp = 'Controller' if $type eq 'C';
104 my $class = "Catalyst::Helper::$comp\::$helper";
105 eval "require $class";
106 die qq/Couldn't load helper "$class", "$@"/ if $@;
107 if ( $class->can('mk_compclass') ) {
108 return 1 unless $class->mk_compclass( $self, @args );
110 else { return 1 unless $self->_mk_compclass }
112 if ( $class->can('mk_comptest') ) {
113 $class->mk_comptest( $self, @args );
115 else { $self->_mk_comptest }
120 return 1 unless $self->_mk_compclass;
132 my ( $self, $dir ) = @_;
134 print qq/ exists "$dir"\n/;
138 print qq/created "$dir"\n/;
141 die qq/Couldn't create "$dir", "$!"/;
149 my ( $self, $file, $content ) = @_;
151 print qq/ exists "$file"\n/;
154 if ( my $f = IO::File->new("> $file") ) {
156 print qq/created "$file"\n/;
159 die qq/Couldn't create "$file", "$!"/;
167 my ( $self, $tname ) = @_;
168 if ($tname) { $tname = "$tname.t" }
170 my $name = $self->{name};
173 $prefix = lc $prefix;
174 $tname = $prefix . '.t';
175 $self->{prefix} = $prefix;
177 my $dir = $self->{test_dir};
178 my $type = lc $self->{type};
179 return File::Spec->catfile( $dir, $type, $tname );
184 $self->mk_dir( $self->{dir} );
185 $self->{script} = File::Spec->catdir( $self->{dir}, 'script' );
186 $self->mk_dir( $self->{script} );
187 $self->{lib} = File::Spec->catdir( $self->{dir}, 'lib' );
188 $self->mk_dir( $self->{lib} );
189 $self->{root} = File::Spec->catdir( $self->{dir}, 'root' );
190 $self->mk_dir( $self->{root} );
191 $self->{t} = File::Spec->catdir( $self->{dir}, 't' );
192 $self->mk_dir( $self->{t} );
193 $self->mk_dir( File::Spec->catdir( $self->{t}, 'm' ) );
194 $self->mk_dir( File::Spec->catdir( $self->{t}, 'v' ) );
195 $self->mk_dir( File::Spec->catdir( $self->{t}, 'c' ) );
196 $self->{class} = File::Spec->catdir( split( /\:\:/, $self->{name} ) );
197 $self->{mod} = File::Spec->catdir( $self->{lib}, $self->{class} );
198 $self->mk_dir( $self->{mod} );
199 $self->{m} = File::Spec->catdir( $self->{mod}, 'M' );
200 $self->mk_dir( $self->{m} );
201 $self->{v} = File::Spec->catdir( $self->{mod}, 'V' );
202 $self->mk_dir( $self->{v} );
203 $self->{c} = File::Spec->catdir( $self->{mod}, 'C' );
204 $self->mk_dir( $self->{c} );
205 $self->{base} = File::Spec->rel2abs( $self->{dir} );
210 my $mod = $self->{mod};
211 my $name = $self->{name};
212 my $base = $self->{base};
213 $self->mk_file( "$mod.pm", <<"EOF");
217 use Catalyst qw/-Debug/;
219 our \$VERSION = '0.01';
223 root => '$base/root',
228 sub default : Private {
229 my ( \$self, \$c ) = \@_;
230 \$c->res->output('Congratulations, $name is on Catalyst!');
235 $name - A very nice application
243 Very nice application.
251 This library is free software . You can redistribute it and/or modify it under
252 the same terms as perl itself.
262 my $name = $self->{name};
263 my $dir = $self->{dir};
264 my $class = $self->{class};
265 $self->mk_file( "$dir\/Makefile.PL", <<"EOF");
266 use ExtUtils::MakeMaker;
270 VERSION_FROM => 'lib/$class.pm',
271 PREREQ_PM => { Catalyst => 5 },
272 test => { TESTS => join ' ', ( glob('t/*.t'), glob('t/*/*.t') ) }
279 my $dir = $self->{dir};
280 $self->mk_file( "$dir\/README", <<"EOF");
281 Run script/server.pl to test the application.
287 my $name = $self->{name};
288 my $dir = $self->{dir};
289 my $time = localtime time;
290 $self->mk_file( "$dir\/Changes", <<"EOF");
291 This file documents the revision history for Perl extension $name.
294 - initial revision, generated by Catalyst
301 my $name = $self->{name};
302 $self->mk_file( "$t\/01app.t", <<"EOF");
303 use Test::More tests => 2;
304 use_ok( Catalyst::Test, '$name' );
306 ok( request('/')->is_success );
308 $self->mk_file( "$t\/02pod.t", <<"EOF");
311 eval "use Test::Pod 1.14";
312 plan skip_all => 'Test::Pod 1.14 required' if \$@;
313 plan skip_all => 'set TEST_POD to enable this test' unless \$ENV{TEST_POD};
317 $self->mk_file( "$t\/03podcoverage.t", <<"EOF");
320 eval "use Test::Pod::Coverage 1.04";
321 plan skip_all => 'Test::Pod::Coverage 1.04 required' if \$@;
322 plan skip_all => 'set TEST_POD to enable this test' unless \$ENV{TEST_POD};
324 all_pod_coverage_ok();
330 my $name = $self->{name};
331 my $script = $self->{script};
332 $self->mk_file( "$script\/cgi.pl", <<"EOF");
333 $Config{startperl} -w
335 BEGIN { \$ENV{CATALYST_ENGINE} = 'CGI' }
339 use lib "\$FindBin::Bin/../lib";
353 See L<Catalyst::Manual>
357 Run a Catalyst application as cgi.
361 Sebastian Riedel, C<sri\@oook.de>
365 Copyright 2004 Sebastian Riedel. All rights reserved.
367 This library is free software. You can redistribute it and/or modify it under
368 the same terms as perl itself.
372 chmod 0700, "$script/cgi.pl";
377 my $name = $self->{name};
378 my $script = $self->{script};
379 $self->mk_file( "$script\/fcgi.pl", <<"EOF");
380 $Config{startperl} -w
382 BEGIN { \$ENV{CATALYST_ENGINE} = 'FCGI' }
386 use lib "\$FindBin::Bin/../lib";
400 See L<Catalyst::Manual>
404 Run a Catalyst application as fcgi.
408 Sebastian Riedel, C<sri\@oook.de>
412 Copyright 2004 Sebastian Riedel. All rights reserved.
414 This library is free software. You can redistribute it and/or modify it under
415 the same terms as perl itself.
419 chmod 0700, "$script/fcgi.pl";
424 my $name = $self->{name};
425 my $script = $self->{script};
426 $self->mk_file( "$script\/server.pl", <<"EOF");
427 $Config{startperl} -w
429 BEGIN { \$ENV{CATALYST_ENGINE} = 'HTTP' }
435 use lib "\$FindBin::Bin/../lib";
441 GetOptions( 'help|?' => \\\$help, 'port=s' => \\\$port );
443 pod2usage(1) if \$help;
452 server - Catalyst Testserver
459 -? -help display this help and exits
460 -p -port port (defaults to 3000)
463 perldoc Catalyst::Manual
464 perldoc Catalyst::Manual::Intro
468 Run a Catalyst Testserver for this application.
472 Sebastian Riedel, C<sri\@oook.de>
476 Copyright 2004 Sebastian Riedel. All rights reserved.
478 This library is free software. You can redistribute it and/or modify it under
479 the same terms as perl itself.
483 chmod 0700, "$script/server.pl";
488 my $name = $self->{name};
489 my $script = $self->{script};
490 $self->mk_file( "$script/test.pl", <<"EOF");
491 $Config{startperl} -w
493 BEGIN { \$ENV{CATALYST_ENGINE} = 'Test' }
499 use lib "\$FindBin::Bin/../lib";
504 GetOptions( 'help|?' => \\\$help );
506 pod2usage(1) if ( \$help || !\$ARGV[0] );
508 print $name->run(\$ARGV[0])->content . "\n";
519 test.pl [options] uri
522 -help display this help and exits
525 test.pl http://localhost/some_action
529 perldoc Catalyst::Manual
530 perldoc Catalyst::Manual::Intro
534 Run a Catalyst action from the comand line.
538 Sebastian Riedel, C<sri\@oook.de>
542 Copyright 2004 Sebastian Riedel. All rights reserved.
544 This library is free software. You can redistribute it and/or modify it under
545 the same terms as perl itself.
549 chmod 0700, "$script/test.pl";
554 my $name = $self->{name};
555 my $script = $self->{script};
556 $self->mk_file( "$script\/create.pl", <<"EOF");
557 $Config{startperl} -w
562 use Catalyst::Helper;
566 GetOptions( 'help|?' => \$help );
568 pod2usage(1) if ( \$help || !\$ARGV[0] );
570 my \$helper = Catalyst::Helper->new;
571 pod2usage(1) unless \$helper->mk_component( '$name', \@ARGV );
578 create - Create a new Catalyst Component
582 create.pl [options] model|view|controller name [helper] [options]
585 -help display this help and exits
588 create.pl controller My::Controller
589 create.pl view My::View
590 create.pl view MyView TT
592 create.pl model My::Model
593 create.pl model SomeDB CDBI dbi:SQLite:/tmp/my.db
594 create.pl model AnotherDB CDBI dbi:Pg:dbname=foo root 4321
598 perldoc Catalyst::Manual
599 perldoc Catalyst::Manual::Intro
603 Create a new Catalyst Component.
607 Sebastian Riedel, C<sri\@oook.de>
611 Copyright 2004 Sebastian Riedel. All rights reserved.
613 This library is free software. You can redistribute it and/or modify it under
614 the same terms as perl itself.
618 chmod 0700, "$script/create.pl";
623 my $app = $self->{app};
624 my $class = $self->{class};
625 my $type = $self->{type};
627 $action = <<"EOF" if $type eq 'C';
629 sub default : Private {
630 my ( \$self, \$c ) = \@_;
631 \$c->res->output('Congratulations, $class is on Catalyst!');
634 my $file = $self->{file};
635 return $self->mk_file( "$file", <<"EOF");
639 use base 'Catalyst::Base';
659 This library is free software . You can redistribute it and/or modify it under
660 the same terms as perl itself.
670 my $prefix = $self->{prefix};
671 my $type = $self->{type};
672 my $class = $self->{class};
673 my $app = $self->{app};
674 my $test = $self->{test};
675 if ( $self->{type} eq 'C' ) {
676 $self->mk_file( "$test", <<"EOF");
677 use Test::More tests => 3;
678 use_ok( Catalyst::Test, '$app' );
681 ok( request('$prefix')->is_success );
685 $self->mk_file( "$test", <<"EOF");
686 use Test::More tests => 1;
694 Helpers are classes that provide two methods.
696 * mk_compclass - creates the Component class
697 * mk_comptest - creates the Component test
699 So when you call C<bin/create view MyView TT>, create would try to execute
700 Catalyst::Helper::View::TT->mk_compclass and
701 Catalyst::Helper::View::TT->mk_comptest.
703 See L<Catalyst::Helper::View::TT> and L<Catalyst::Helper::Model::CDBI> for
706 All helper classes should be under one of the following namespaces.
708 Catalyst::Helper::Model::
709 Catalyst::Helper::View::
710 Catalyst::Helper::Controller::
714 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
715 L<Catalyst::Response>, L<Catalyst>
719 Sebastian Riedel, C<sri@oook.de>
723 This library is free software . You can redistribute it and/or modify it under
724 the same terms as perl itself.