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;
58 $self->{base} = File::Spec->catdir( $FindBin::Bin, '..' );
59 unless ( $_[0] =~ /^model|m|view|v|controller|c\$/i ) {
62 my $class = "Catalyst::Helper::$helper";
63 eval "require $class";
64 die qq/Couldn't load helper "$class", "$@"/ if $@;
65 if ( $class->can('mk_stuff') ) {
66 return 1 unless $class->mk_stuff( $self, @args );
74 return 0 if $name =~ /[^\w\:]/;
75 $type = 'M' if $type =~ /model|m/i;
76 $type = 'V' if $type =~ /view|v/i;
77 $type = 'C' if $type =~ /controller|c/i;
78 $self->{type} = $type;
79 $self->{name} = $name;
80 $self->{class} = "$app\::$type\::$name";
83 my $appdir = File::Spec->catdir( split /\:\:/, $app );
85 File::Spec->catdir( $FindBin::Bin, '..', 'lib', $appdir, $type );
87 if ( $name =~ /\:/ ) {
88 my @path = split /\:\:/, $name;
90 $path = File::Spec->catdir( $path, @path );
93 $file = File::Spec->catfile( $path, "$file.pm" );
94 $self->{file} = $file;
97 $self->{test_dir} = File::Spec->catdir( $FindBin::Bin, '..', 't' );
98 $self->{test} = $self->next_test;
103 $comp = 'View' if $type eq 'V';
104 $comp = 'Controller' if $type eq 'C';
105 my $class = "Catalyst::Helper::$comp\::$helper";
106 eval "require $class";
107 die qq/Couldn't load helper "$class", "$@"/ if $@;
108 if ( $class->can('mk_compclass') ) {
109 return 1 unless $class->mk_compclass( $self, @args );
111 else { return 1 unless $self->_mk_compclass }
113 if ( $class->can('mk_comptest') ) {
114 $class->mk_comptest( $self, @args );
116 else { $self->_mk_comptest }
121 return 1 unless $self->_mk_compclass;
133 my ( $self, $dir ) = @_;
135 print qq/ exists "$dir"\n/;
139 print qq/created "$dir"\n/;
142 die qq/Couldn't create "$dir", "$!"/;
150 my ( $self, $file, $content ) = @_;
152 print qq/ exists "$file"\n/;
155 if ( my $f = IO::File->new("> $file") ) {
157 print qq/created "$file"\n/;
160 die qq/Couldn't create "$file", "$!"/;
168 my ( $self, $tname ) = @_;
169 if ($tname) { $tname = "$tname.t" }
171 my $name = $self->{name};
174 $prefix = lc $prefix;
175 $tname = $prefix . '.t';
176 $self->{prefix} = $prefix;
178 my $dir = $self->{test_dir};
179 my $type = lc $self->{type};
180 return File::Spec->catfile( $dir, $type, $tname );
185 $self->mk_dir( $self->{dir} );
186 $self->{script} = File::Spec->catdir( $self->{dir}, 'script' );
187 $self->mk_dir( $self->{script} );
188 $self->{lib} = File::Spec->catdir( $self->{dir}, 'lib' );
189 $self->mk_dir( $self->{lib} );
190 $self->{root} = File::Spec->catdir( $self->{dir}, 'root' );
191 $self->mk_dir( $self->{root} );
192 $self->{t} = File::Spec->catdir( $self->{dir}, 't' );
193 $self->mk_dir( $self->{t} );
194 $self->mk_dir( File::Spec->catdir( $self->{t}, 'm' ) );
195 $self->mk_dir( File::Spec->catdir( $self->{t}, 'v' ) );
196 $self->mk_dir( File::Spec->catdir( $self->{t}, 'c' ) );
197 $self->{class} = File::Spec->catdir( split( /\:\:/, $self->{name} ) );
198 $self->{mod} = File::Spec->catdir( $self->{lib}, $self->{class} );
199 $self->mk_dir( $self->{mod} );
200 $self->{m} = File::Spec->catdir( $self->{mod}, 'M' );
201 $self->mk_dir( $self->{m} );
202 $self->{v} = File::Spec->catdir( $self->{mod}, 'V' );
203 $self->mk_dir( $self->{v} );
204 $self->{c} = File::Spec->catdir( $self->{mod}, 'C' );
205 $self->mk_dir( $self->{c} );
206 $self->{base} = File::Spec->rel2abs( $self->{dir} );
211 my $mod = $self->{mod};
212 my $name = $self->{name};
213 my $base = $self->{base};
214 $self->mk_file( "$mod.pm", <<"EOF");
218 use Catalyst qw/-Debug/;
220 our \$VERSION = '0.01';
224 root => '$base/root',
230 my ( \$self, \$c ) = \@_;
231 \$c->res->output('Congratulations, $name is on Catalyst!');
238 $name - A very nice application
246 Very nice application.
254 This library is free software . You can redistribute it and/or modify it under
255 the same terms as perl itself.
265 my $name = $self->{name};
266 my $dir = $self->{dir};
267 my $class = $self->{class};
268 $self->mk_file( "$dir\/Makefile.PL", <<"EOF");
269 use ExtUtils::MakeMaker;
273 VERSION_FROM => 'lib/$class.pm',
274 PREREQ_PM => { Catalyst => 0 },
275 test => { TESTS => join ' ', ( glob('t/*.t'), glob('t/*/*.t') ) }
282 my $dir = $self->{dir};
283 $self->mk_file( "$dir\/README", <<"EOF");
284 Run script/server.pl to test the application.
290 my $name = $self->{name};
291 my $dir = $self->{dir};
292 my $time = localtime time;
293 $self->mk_file( "$dir\/Changes", <<"EOF");
294 This file documents the revision history for Perl extension $name.
297 - initial revision, generated by Catalyst
304 my $name = $self->{name};
305 $self->mk_file( "$t\/01app.t", <<"EOF");
306 use Test::More tests => 2;
307 use_ok( Catalyst::Test, '$name' );
309 ok( request('/')->is_success );
311 $self->mk_file( "$t\/02podcoverage.t", <<"EOF");
314 eval "use Test::Pod::Coverage 1.04";
315 plan skip_all => 'Test::Pod::Coverage 1.04 required' if \$@;
316 plan skip_all => 'set TEST_POD to enable this test' unless \$ENV{TEST_POD};
318 all_pod_coverage_ok();
324 my $name = $self->{name};
325 my $script = $self->{script};
326 $self->mk_file( "$script\/cgi.pl", <<"EOF");
327 $Config{startperl} -w
329 BEGIN { \$ENV{CATALYST_ENGINE} = 'CGI' }
333 use lib "\$FindBin::Bin/../lib";
347 See L<Catalyst::Manual>
351 Run a Catalyst application as cgi.
355 Sebastian Riedel, C<sri\@oook.de>
359 Copyright 2004 Sebastian Riedel. All rights reserved.
361 This library is free software. You can redistribute it and/or modify it under
362 the same terms as perl itself.
366 chmod 0700, "$script/cgi.pl";
371 my $name = $self->{name};
372 my $script = $self->{script};
373 $self->mk_file( "$script\/nph-cgi.pl", <<"EOF");
374 $Config{startperl} -w
376 BEGIN { \$ENV{CATALYST_ENGINE} = 'CGI::NPH' }
380 use lib "\$FindBin::Bin/../lib";
390 nph-cgi - Catalyst CGI
394 See L<Catalyst::Manual>
398 Run a Catalyst application as nph cgi.
402 Sebastian Riedel, C<sri\@oook.de>
406 Copyright 2004 Sebastian Riedel. All rights reserved.
408 This library is free software. You can redistribute it and/or modify it under
409 the same terms as perl itself.
413 chmod 0700, "$script/nph-cgi.pl";
418 my $name = $self->{name};
419 my $script = $self->{script};
420 $self->mk_file( "$script\/fcgi.pl", <<"EOF");
421 $Config{startperl} -w
423 BEGIN { \$ENV{CATALYST_ENGINE} = 'FCGI' }
427 use lib "\$FindBin::Bin/../lib";
441 See L<Catalyst::Manual>
445 Run a Catalyst application as fcgi.
449 Sebastian Riedel, C<sri\@oook.de>
453 Copyright 2004 Sebastian Riedel. All rights reserved.
455 This library is free software. You can redistribute it and/or modify it under
456 the same terms as perl itself.
460 chmod 0700, "$script/fcgi.pl";
465 my $name = $self->{name};
466 my $script = $self->{script};
467 $self->mk_file( "$script\/server.pl", <<"EOF");
468 $Config{startperl} -w
470 BEGIN { \$ENV{CATALYST_ENGINE} = 'Server' }
476 use lib "\$FindBin::Bin/../lib";
482 GetOptions( 'help|?' => \\\$help, 'port=s' => \\\$port );
484 pod2usage(1) if \$help;
493 server - Catalyst Testserver
500 -? -help display this help and exits
501 -p -port port (defaults to 3000)
504 perldoc Catalyst::Manual
505 perldoc Catalyst::Manual::Intro
509 Run a Catalyst Testserver for this application.
513 Sebastian Riedel, C<sri\@oook.de>
517 Copyright 2004 Sebastian Riedel. All rights reserved.
519 This library is free software. You can redistribute it and/or modify it under
520 the same terms as perl itself.
524 chmod 0700, "$script/server.pl";
529 my $name = $self->{name};
530 my $script = $self->{script};
531 $self->mk_file( "$script/test.pl", <<"EOF");
532 $Config{startperl} -w
534 BEGIN { \$ENV{CATALYST_ENGINE} = 'Test' }
540 use lib "\$FindBin::Bin/../lib";
545 GetOptions( 'help|?' => \\\$help );
547 pod2usage(1) if ( \$help || !\$ARGV[0] );
549 print $name->run(\$ARGV[0])->content . "\n";
560 test.pl [options] uri
563 -help display this help and exits
566 test.pl http://localhost/some_action
570 perldoc Catalyst::Manual
571 perldoc Catalyst::Manual::Intro
575 Run a Catalyst action from the comand line.
579 Sebastian Riedel, C<sri\@oook.de>
583 Copyright 2004 Sebastian Riedel. All rights reserved.
585 This library is free software. You can redistribute it and/or modify it under
586 the same terms as perl itself.
590 chmod 0700, "$script/test.pl";
595 my $name = $self->{name};
596 my $script = $self->{script};
597 $self->mk_file( "$script\/create.pl", <<"EOF");
598 $Config{startperl} -w
603 use Catalyst::Helper;
607 GetOptions( 'help|?' => \$help );
609 pod2usage(1) if ( \$help || !\$ARGV[0] );
611 my \$helper = Catalyst::Helper->new;
612 pod2usage(1) unless \$helper->mk_component( '$name', \@ARGV );
619 create - Create a new Catalyst Component
623 create.pl [options] model|view|controller name [helper] [options]
626 -help display this help and exits
629 create.pl controller My::Controller
630 create.pl view My::View
631 create.pl view MyView TT
633 create.pl model My::Model
634 create.pl model SomeDB CDBI dbi:SQLite:/tmp/my.db
635 create.pl model AnotherDB CDBI dbi:Pg:dbname=foo root 4321
639 perldoc Catalyst::Manual
640 perldoc Catalyst::Manual::Intro
644 Create a new Catalyst Component.
648 Sebastian Riedel, C<sri\@oook.de>
652 Copyright 2004 Sebastian Riedel. All rights reserved.
654 This library is free software. You can redistribute it and/or modify it under
655 the same terms as perl itself.
659 chmod 0700, "$script/create.pl";
664 my $app = $self->{app};
665 my $class = $self->{class};
666 my $type = $self->{type};
668 $action = <<"EOF" if $type eq 'C';
673 my ( \$self, \$c ) = \@_;
674 \$c->res->output('Congratulations, $class is on Catalyst!');
679 my $file = $self->{file};
680 return $self->mk_file( "$file", <<"EOF");
684 use base 'Catalyst::Base';
704 This library is free software . You can redistribute it and/or modify it under
705 the same terms as perl itself.
715 my $prefix = $self->{prefix};
716 my $type = $self->{type};
717 my $class = $self->{class};
718 my $app = $self->{app};
719 my $test = $self->{test};
720 if ( $self->{type} eq 'C' ) {
721 $self->mk_file( "$test", <<"EOF");
722 use Test::More tests => 3;
723 use_ok( Catalyst::Test, '$app' );
726 ok( request('$prefix')->is_success );
730 $self->mk_file( "$test", <<"EOF");
731 use Test::More tests => 1;
739 Helpers are classes that provide two methods.
741 * mk_compclass - creates the Component class
742 * mk_comptest - creates the Component test
744 So when you call C<bin/create view MyView TT>, create would try to execute
745 Catalyst::Helper::View::TT->mk_compclass and
746 Catalyst::Helper::View::TT->mk_comptest.
748 See L<Catalyst::Helper::View::TT> and L<Catalyst::Helper::Model::CDBI> for
751 All helper classes should be under one of the following namespaces.
753 Catalyst::Helper::Model::
754 Catalyst::Helper::View::
755 Catalyst::Helper::Controller::
759 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
760 L<Catalyst::Response>, L<Catalyst>
764 Sebastian Riedel, C<sri@oook.de>
768 This library is free software . You can redistribute it and/or modify it under
769 the same terms as perl itself.