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',
227 sub default : Private {
228 my ( \$self, \$c ) = \@_;
229 \$c->res->output('Congratulations, $name is on Catalyst!');
234 $name - A very nice application
242 Very nice application.
250 This library is free software . You can redistribute it and/or modify it under
251 the same terms as perl itself.
261 my $name = $self->{name};
262 my $dir = $self->{dir};
263 my $class = $self->{class};
264 $self->mk_file( "$dir\/Makefile.PL", <<"EOF");
265 use ExtUtils::MakeMaker;
269 VERSION_FROM => 'lib/$class.pm',
270 PREREQ_PM => { Catalyst => 0 },
271 test => { TESTS => join ' ', ( glob('t/*.t'), glob('t/*/*.t') ) }
278 my $dir = $self->{dir};
279 $self->mk_file( "$dir\/README", <<"EOF");
280 Run script/server.pl to test the application.
286 my $name = $self->{name};
287 my $dir = $self->{dir};
288 my $time = localtime time;
289 $self->mk_file( "$dir\/Changes", <<"EOF");
290 This file documents the revision history for Perl extension $name.
293 - initial revision, generated by Catalyst
300 my $name = $self->{name};
301 $self->mk_file( "$t\/01app.t", <<"EOF");
302 use Test::More tests => 2;
303 use_ok( Catalyst::Test, '$name' );
305 ok( request('/')->is_success );
307 $self->mk_file( "$t\/02pod.t", <<"EOF");
310 eval "use Test::Pod 1.14";
311 plan skip_all => 'Test::Pod 1.14 required' if $@;
312 plan skip_all => 'set TEST_POD to enable this test' unless $ENV{TEST_POD};
316 $self->mk_file( "$t\/03podcoverage.t", <<"EOF");
319 eval "use Test::Pod::Coverage 1.04";
320 plan skip_all => 'Test::Pod::Coverage 1.04 required' if \$@;
321 plan skip_all => 'set TEST_POD to enable this test' unless \$ENV{TEST_POD};
323 all_pod_coverage_ok();
329 my $name = $self->{name};
330 my $script = $self->{script};
331 $self->mk_file( "$script\/cgi.pl", <<"EOF");
332 $Config{startperl} -w
334 BEGIN { \$ENV{CATALYST_ENGINE} = 'CGI' }
338 use lib "\$FindBin::Bin/../lib";
352 See L<Catalyst::Manual>
356 Run a Catalyst application as cgi.
360 Sebastian Riedel, C<sri\@oook.de>
364 Copyright 2004 Sebastian Riedel. All rights reserved.
366 This library is free software. You can redistribute it and/or modify it under
367 the same terms as perl itself.
371 chmod 0700, "$script/cgi.pl";
376 my $name = $self->{name};
377 my $script = $self->{script};
378 $self->mk_file( "$script\/nph-cgi.pl", <<"EOF");
379 $Config{startperl} -w
381 BEGIN { \$ENV{CATALYST_ENGINE} = 'CGI::NPH' }
385 use lib "\$FindBin::Bin/../lib";
395 nph-cgi - Catalyst CGI
399 See L<Catalyst::Manual>
403 Run a Catalyst application as nph cgi.
407 Sebastian Riedel, C<sri\@oook.de>
411 Copyright 2004 Sebastian Riedel. All rights reserved.
413 This library is free software. You can redistribute it and/or modify it under
414 the same terms as perl itself.
418 chmod 0700, "$script/nph-cgi.pl";
423 my $name = $self->{name};
424 my $script = $self->{script};
425 $self->mk_file( "$script\/fcgi.pl", <<"EOF");
426 $Config{startperl} -w
428 BEGIN { \$ENV{CATALYST_ENGINE} = 'FCGI' }
432 use lib "\$FindBin::Bin/../lib";
446 See L<Catalyst::Manual>
450 Run a Catalyst application as fcgi.
454 Sebastian Riedel, C<sri\@oook.de>
458 Copyright 2004 Sebastian Riedel. All rights reserved.
460 This library is free software. You can redistribute it and/or modify it under
461 the same terms as perl itself.
465 chmod 0700, "$script/fcgi.pl";
470 my $name = $self->{name};
471 my $script = $self->{script};
472 $self->mk_file( "$script\/server.pl", <<"EOF");
473 $Config{startperl} -w
475 BEGIN { \$ENV{CATALYST_ENGINE} = 'HTTP::Daemon' }
481 use lib "\$FindBin::Bin/../lib";
487 GetOptions( 'help|?' => \\\$help, 'port=s' => \\\$port );
489 pod2usage(1) if \$help;
498 server - Catalyst Testserver
505 -? -help display this help and exits
506 -p -port port (defaults to 3000)
509 perldoc Catalyst::Manual
510 perldoc Catalyst::Manual::Intro
514 Run a Catalyst Testserver for this application.
518 Sebastian Riedel, C<sri\@oook.de>
522 Copyright 2004 Sebastian Riedel. All rights reserved.
524 This library is free software. You can redistribute it and/or modify it under
525 the same terms as perl itself.
529 chmod 0700, "$script/server.pl";
534 my $name = $self->{name};
535 my $script = $self->{script};
536 $self->mk_file( "$script/test.pl", <<"EOF");
537 $Config{startperl} -w
539 BEGIN { \$ENV{CATALYST_ENGINE} = 'Test' }
545 use lib "\$FindBin::Bin/../lib";
550 GetOptions( 'help|?' => \\\$help );
552 pod2usage(1) if ( \$help || !\$ARGV[0] );
554 print $name->run(\$ARGV[0])->content . "\n";
565 test.pl [options] uri
568 -help display this help and exits
571 test.pl http://localhost/some_action
575 perldoc Catalyst::Manual
576 perldoc Catalyst::Manual::Intro
580 Run a Catalyst action from the comand line.
584 Sebastian Riedel, C<sri\@oook.de>
588 Copyright 2004 Sebastian Riedel. All rights reserved.
590 This library is free software. You can redistribute it and/or modify it under
591 the same terms as perl itself.
595 chmod 0700, "$script/test.pl";
600 my $name = $self->{name};
601 my $script = $self->{script};
602 $self->mk_file( "$script\/create.pl", <<"EOF");
603 $Config{startperl} -w
608 use Catalyst::Helper;
612 GetOptions( 'help|?' => \$help );
614 pod2usage(1) if ( \$help || !\$ARGV[0] );
616 my \$helper = Catalyst::Helper->new;
617 pod2usage(1) unless \$helper->mk_component( '$name', \@ARGV );
624 create - Create a new Catalyst Component
628 create.pl [options] model|view|controller name [helper] [options]
631 -help display this help and exits
634 create.pl controller My::Controller
635 create.pl view My::View
636 create.pl view MyView TT
638 create.pl model My::Model
639 create.pl model SomeDB CDBI dbi:SQLite:/tmp/my.db
640 create.pl model AnotherDB CDBI dbi:Pg:dbname=foo root 4321
644 perldoc Catalyst::Manual
645 perldoc Catalyst::Manual::Intro
649 Create a new Catalyst Component.
653 Sebastian Riedel, C<sri\@oook.de>
657 Copyright 2004 Sebastian Riedel. All rights reserved.
659 This library is free software. You can redistribute it and/or modify it under
660 the same terms as perl itself.
664 chmod 0700, "$script/create.pl";
669 my $app = $self->{app};
670 my $class = $self->{class};
671 my $type = $self->{type};
673 $action = <<"EOF" if $type eq 'C';
675 sub default : Private {
676 my ( \$self, \$c ) = \@_;
677 \$c->res->output('Congratulations, $class is on Catalyst!');
681 my $file = $self->{file};
682 return $self->mk_file( "$file", <<"EOF");
686 use base 'Catalyst::Base';
706 This library is free software . You can redistribute it and/or modify it under
707 the same terms as perl itself.
717 my $prefix = $self->{prefix};
718 my $type = $self->{type};
719 my $class = $self->{class};
720 my $app = $self->{app};
721 my $test = $self->{test};
722 if ( $self->{type} eq 'C' ) {
723 $self->mk_file( "$test", <<"EOF");
724 use Test::More tests => 3;
725 use_ok( Catalyst::Test, '$app' );
728 ok( request('$prefix')->is_success );
732 $self->mk_file( "$test", <<"EOF");
733 use Test::More tests => 1;
741 Helpers are classes that provide two methods.
743 * mk_compclass - creates the Component class
744 * mk_comptest - creates the Component test
746 So when you call C<bin/create view MyView TT>, create would try to execute
747 Catalyst::Helper::View::TT->mk_compclass and
748 Catalyst::Helper::View::TT->mk_comptest.
750 See L<Catalyst::Helper::View::TT> and L<Catalyst::Helper::Model::CDBI> for
753 All helper classes should be under one of the following namespaces.
755 Catalyst::Helper::Model::
756 Catalyst::Helper::View::
757 Catalyst::Helper::Controller::
761 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
762 L<Catalyst::Response>, L<Catalyst>
766 Sebastian Riedel, C<sri@oook.de>
770 This library is free software . You can redistribute it and/or modify it under
771 the same terms as perl itself.