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\/02pod.t", <<"EOF");
314 eval "use Test::Pod 1.14";
315 plan skip_all => 'Test::Pod 1.14 required' if $@;
316 plan skip_all => 'set TEST_POD to enable this test' unless $ENV{TEST_POD};
320 $self->mk_file( "$t\/03podcoverage.t", <<"EOF");
323 eval "use Test::Pod::Coverage 1.04";
324 plan skip_all => 'Test::Pod::Coverage 1.04 required' if \$@;
325 plan skip_all => 'set TEST_POD to enable this test' unless \$ENV{TEST_POD};
327 all_pod_coverage_ok();
333 my $name = $self->{name};
334 my $script = $self->{script};
335 $self->mk_file( "$script\/cgi.pl", <<"EOF");
336 $Config{startperl} -w
338 BEGIN { \$ENV{CATALYST_ENGINE} = 'CGI' }
342 use lib "\$FindBin::Bin/../lib";
356 See L<Catalyst::Manual>
360 Run a Catalyst application as cgi.
364 Sebastian Riedel, C<sri\@oook.de>
368 Copyright 2004 Sebastian Riedel. All rights reserved.
370 This library is free software. You can redistribute it and/or modify it under
371 the same terms as perl itself.
375 chmod 0700, "$script/cgi.pl";
380 my $name = $self->{name};
381 my $script = $self->{script};
382 $self->mk_file( "$script\/nph-cgi.pl", <<"EOF");
383 $Config{startperl} -w
385 BEGIN { \$ENV{CATALYST_ENGINE} = 'CGI::NPH' }
389 use lib "\$FindBin::Bin/../lib";
399 nph-cgi - Catalyst CGI
403 See L<Catalyst::Manual>
407 Run a Catalyst application as nph cgi.
411 Sebastian Riedel, C<sri\@oook.de>
415 Copyright 2004 Sebastian Riedel. All rights reserved.
417 This library is free software. You can redistribute it and/or modify it under
418 the same terms as perl itself.
422 chmod 0700, "$script/nph-cgi.pl";
427 my $name = $self->{name};
428 my $script = $self->{script};
429 $self->mk_file( "$script\/fcgi.pl", <<"EOF");
430 $Config{startperl} -w
432 BEGIN { \$ENV{CATALYST_ENGINE} = 'FCGI' }
436 use lib "\$FindBin::Bin/../lib";
450 See L<Catalyst::Manual>
454 Run a Catalyst application as fcgi.
458 Sebastian Riedel, C<sri\@oook.de>
462 Copyright 2004 Sebastian Riedel. All rights reserved.
464 This library is free software. You can redistribute it and/or modify it under
465 the same terms as perl itself.
469 chmod 0700, "$script/fcgi.pl";
474 my $name = $self->{name};
475 my $script = $self->{script};
476 $self->mk_file( "$script\/server.pl", <<"EOF");
477 $Config{startperl} -w
479 BEGIN { \$ENV{CATALYST_ENGINE} = 'HTTP::Daemon' }
485 use lib "\$FindBin::Bin/../lib";
491 GetOptions( 'help|?' => \\\$help, 'port=s' => \\\$port );
493 pod2usage(1) if \$help;
502 server - Catalyst Testserver
509 -? -help display this help and exits
510 -p -port port (defaults to 3000)
513 perldoc Catalyst::Manual
514 perldoc Catalyst::Manual::Intro
518 Run a Catalyst Testserver for this application.
522 Sebastian Riedel, C<sri\@oook.de>
526 Copyright 2004 Sebastian Riedel. All rights reserved.
528 This library is free software. You can redistribute it and/or modify it under
529 the same terms as perl itself.
533 chmod 0700, "$script/server.pl";
538 my $name = $self->{name};
539 my $script = $self->{script};
540 $self->mk_file( "$script/test.pl", <<"EOF");
541 $Config{startperl} -w
543 BEGIN { \$ENV{CATALYST_ENGINE} = 'Test' }
549 use lib "\$FindBin::Bin/../lib";
554 GetOptions( 'help|?' => \\\$help );
556 pod2usage(1) if ( \$help || !\$ARGV[0] );
558 print $name->run(\$ARGV[0])->content . "\n";
569 test.pl [options] uri
572 -help display this help and exits
575 test.pl http://localhost/some_action
579 perldoc Catalyst::Manual
580 perldoc Catalyst::Manual::Intro
584 Run a Catalyst action from the comand line.
588 Sebastian Riedel, C<sri\@oook.de>
592 Copyright 2004 Sebastian Riedel. All rights reserved.
594 This library is free software. You can redistribute it and/or modify it under
595 the same terms as perl itself.
599 chmod 0700, "$script/test.pl";
604 my $name = $self->{name};
605 my $script = $self->{script};
606 $self->mk_file( "$script\/create.pl", <<"EOF");
607 $Config{startperl} -w
612 use Catalyst::Helper;
616 GetOptions( 'help|?' => \$help );
618 pod2usage(1) if ( \$help || !\$ARGV[0] );
620 my \$helper = Catalyst::Helper->new;
621 pod2usage(1) unless \$helper->mk_component( '$name', \@ARGV );
628 create - Create a new Catalyst Component
632 create.pl [options] model|view|controller name [helper] [options]
635 -help display this help and exits
638 create.pl controller My::Controller
639 create.pl view My::View
640 create.pl view MyView TT
642 create.pl model My::Model
643 create.pl model SomeDB CDBI dbi:SQLite:/tmp/my.db
644 create.pl model AnotherDB CDBI dbi:Pg:dbname=foo root 4321
648 perldoc Catalyst::Manual
649 perldoc Catalyst::Manual::Intro
653 Create a new Catalyst Component.
657 Sebastian Riedel, C<sri\@oook.de>
661 Copyright 2004 Sebastian Riedel. All rights reserved.
663 This library is free software. You can redistribute it and/or modify it under
664 the same terms as perl itself.
668 chmod 0700, "$script/create.pl";
673 my $app = $self->{app};
674 my $class = $self->{class};
675 my $type = $self->{type};
677 $action = <<"EOF" if $type eq 'C';
682 my ( \$self, \$c ) = \@_;
683 \$c->res->output('Congratulations, $class is on Catalyst!');
688 my $file = $self->{file};
689 return $self->mk_file( "$file", <<"EOF");
693 use base 'Catalyst::Base';
713 This library is free software . You can redistribute it and/or modify it under
714 the same terms as perl itself.
724 my $prefix = $self->{prefix};
725 my $type = $self->{type};
726 my $class = $self->{class};
727 my $app = $self->{app};
728 my $test = $self->{test};
729 if ( $self->{type} eq 'C' ) {
730 $self->mk_file( "$test", <<"EOF");
731 use Test::More tests => 3;
732 use_ok( Catalyst::Test, '$app' );
735 ok( request('$prefix')->is_success );
739 $self->mk_file( "$test", <<"EOF");
740 use Test::More tests => 1;
748 Helpers are classes that provide two methods.
750 * mk_compclass - creates the Component class
751 * mk_comptest - creates the Component test
753 So when you call C<bin/create view MyView TT>, create would try to execute
754 Catalyst::Helper::View::TT->mk_compclass and
755 Catalyst::Helper::View::TT->mk_comptest.
757 See L<Catalyst::Helper::View::TT> and L<Catalyst::Helper::Model::CDBI> for
760 All helper classes should be under one of the following namespaces.
762 Catalyst::Helper::Model::
763 Catalyst::Helper::View::
764 Catalyst::Helper::Controller::
768 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
769 L<Catalyst::Response>, L<Catalyst>
773 Sebastian Riedel, C<sri@oook.de>
777 This library is free software . You can redistribute it and/or modify it under
778 the same terms as perl itself.