1 package Catalyst::Helper;
4 use base 'Class::Accessor::Fast';
11 our $CATALYST_SCRIPT_GEN = 2;
17 Catalyst::Helper - Bootstrap a Catalyst application
21 See L<Catalyst::Manual::Intro>
25 Bootstrap a Catalyst application.
36 my ( $self, $class, $file ) = @_;
37 unless ( $cache{$class} ) {
39 $cache{$class} = eval "package $class; <DATA>";
41 my $data = $cache{$class};
42 my @files = split /^__(.+)__\n/m, $data;
45 my ( $name, $content ) = splice @files, 0, 2;
46 return $content if $name eq $file;
56 my ( $self, $name ) = @_;
57 return 0 if $name =~ /[^\w\:]/;
58 $self->{name} = $name;
60 $self->{dir} =~ s/\:\:/-/g;
61 $self->{startperl} = $Config{startperl};
62 $self->{scriptgen}=$CATALYST_SCRIPT_GEN;
85 $self->{base} = File::Spec->catdir( $FindBin::Bin, '..' );
86 unless ( $_[0] =~ /^model|m|view|v|controller|c\$/i ) {
89 my $class = "Catalyst::Helper::$helper";
90 eval "require $class";
91 die qq/Couldn't load helper "$class", "$@"/ if $@;
92 if ( $class->can('mk_stuff') ) {
93 return 1 unless $class->mk_stuff( $self, @args );
101 return 0 if $name =~ /[^\w\:]/;
102 $type = 'M' if $type =~ /model|m/i;
103 $type = 'V' if $type =~ /view|v/i;
104 $type = 'C' if $type =~ /controller|c/i;
105 $self->{type} = $type;
106 $self->{name} = $name;
107 $self->{class} = "$app\::$type\::$name";
110 my $appdir = File::Spec->catdir( split /\:\:/, $app );
112 File::Spec->catdir( $FindBin::Bin, '..', 'lib', $appdir, $type );
114 if ( $name =~ /\:/ ) {
115 my @path = split /\:\:/, $name;
117 $path = File::Spec->catdir( $path, @path );
120 $file = File::Spec->catfile( $path, "$file.pm" );
121 $self->{file} = $file;
124 $self->{test_dir} = File::Spec->catdir( $FindBin::Bin, '..', 't' );
125 $self->{test} = $self->next_test;
130 $comp = 'View' if $type eq 'V';
131 $comp = 'Controller' if $type eq 'C';
132 my $class = "Catalyst::Helper::$comp\::$helper";
133 eval "require $class";
134 die qq/Couldn't load helper "$class", "$@"/ if $@;
135 if ( $class->can('mk_compclass') ) {
136 return 1 unless $class->mk_compclass( $self, @args );
138 else { return 1 unless $self->_mk_compclass }
140 if ( $class->can('mk_comptest') ) {
141 $class->mk_comptest( $self, @args );
143 else { $self->_mk_comptest }
148 return 1 unless $self->_mk_compclass;
160 my ( $self, $dir ) = @_;
162 print qq/ exists "$dir"\n/;
166 print qq/created "$dir"\n/;
169 die qq/Couldn't create "$dir", "$!"/;
177 my ( $self, $file, $content ) = @_;
179 print qq/ exists "$file"\n/;
182 if ( my $f = IO::File->new("> $file") ) {
184 print qq/created "$file"\n/;
187 die qq/Couldn't create "$file", "$!"/;
195 my ( $self, $tname ) = @_;
196 if ($tname) { $tname = "$tname.t" }
198 my $name = $self->{name};
201 $prefix = lc $prefix;
202 $tname = $prefix . '.t';
203 $self->{prefix} = $prefix;
205 my $dir = $self->{test_dir};
206 my $type = lc $self->{type};
207 return File::Spec->catfile( $dir, $type, $tname );
212 Render and create a file from a template in DATA.
217 my ( $self, $file, $path, $vars ) = @_;
219 my $t = Template->new;
220 my $template = $self->get_file( ( caller(0) )[0], $file );
221 return 0 unless $template;
223 $t->process( \$template, { %{$self}, %$vars }, \$output );
224 $self->mk_file( $path, $output );
229 $self->mk_dir( $self->{dir} );
230 $self->{script} = File::Spec->catdir( $self->{dir}, 'script' );
231 $self->mk_dir( $self->{script} );
232 $self->{lib} = File::Spec->catdir( $self->{dir}, 'lib' );
233 $self->mk_dir( $self->{lib} );
234 $self->{root} = File::Spec->catdir( $self->{dir}, 'root' );
235 $self->mk_dir( $self->{root} );
236 $self->{t} = File::Spec->catdir( $self->{dir}, 't' );
237 $self->mk_dir( $self->{t} );
238 $self->mk_dir( File::Spec->catdir( $self->{t}, 'm' ) );
239 $self->mk_dir( File::Spec->catdir( $self->{t}, 'v' ) );
240 $self->mk_dir( File::Spec->catdir( $self->{t}, 'c' ) );
241 $self->{class} = File::Spec->catdir( split( /\:\:/, $self->{name} ) );
242 $self->{mod} = File::Spec->catdir( $self->{lib}, $self->{class} );
243 $self->mk_dir( $self->{mod} );
244 $self->{m} = File::Spec->catdir( $self->{mod}, 'M' );
245 $self->mk_dir( $self->{m} );
246 $self->{v} = File::Spec->catdir( $self->{mod}, 'V' );
247 $self->mk_dir( $self->{v} );
248 $self->{c} = File::Spec->catdir( $self->{mod}, 'C' );
249 $self->mk_dir( $self->{c} );
250 $self->{base} = File::Spec->rel2abs( $self->{dir} );
255 my $mod = $self->{mod};
256 $self->render_file( 'appclass', "$mod.pm" );
261 my $dir = $self->{dir};
262 $self->render_file( 'makefile', "$dir\/Makefile.PL" );
267 my $dir = $self->{dir};
268 $self->render_file( 'readme', "$dir\/README" );
273 my $dir = $self->{dir};
274 my $time = localtime time;
275 $self->render_file( 'changes', "$dir\/Changes", { time => $time } );
281 $self->render_file( 'apptest', "$t\/01app.t" );
282 $self->render_file( 'podtest', "$t\/02pod.t" );
283 $self->render_file( 'podcoveragetest', "$t\/03podcoverage.t" );
288 my $script = $self->{script};
289 $self->render_file( 'cgi', "$script\/cgi.pl" );
290 chmod 0700, "$script/cgi.pl";
295 my $script = $self->{script};
296 $self->render_file( 'fcgi', "$script\/fcgi.pl" );
297 chmod 0700, "$script/fcgi.pl";
302 my $script = $self->{script};
303 $self->render_file( 'server', "$script\/server.pl" );
304 chmod 0700, "$script/server.pl";
309 my $script = $self->{script};
310 $self->render_file( 'test', "$script/test.pl" );
311 chmod 0700, "$script/test.pl";
316 my $script = $self->{script};
317 $self->render_file( 'create', "$script\/create.pl" );
318 chmod 0700, "$script/create.pl";
323 my $file = $self->{file};
324 return $self->render_file( 'compclass', "$file" );
329 my $test = $self->{test};
330 $self->render_file( 'comptest', "$test" );
335 Helpers are classes that provide two methods.
337 * mk_compclass - creates the Component class
338 * mk_comptest - creates the Component test
340 So when you call C<bin/create view MyView TT>, create would try to execute
341 Catalyst::Helper::View::TT->mk_compclass and
342 Catalyst::Helper::View::TT->mk_comptest.
344 See L<Catalyst::Helper::View::TT> and L<Catalyst::Helper::Model::CDBI> for
347 All helper classes should be under one of the following namespaces.
349 Catalyst::Helper::Model::
350 Catalyst::Helper::View::
351 Catalyst::Helper::Controller::
355 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
356 L<Catalyst::Response>, L<Catalyst>
360 Sebastian Riedel, C<sri@oook.de>
364 This library is free software . You can redistribute it and/or modify it under
365 the same terms as perl itself.
376 use Catalyst qw/-Debug/;
378 our $VERSION = '0.01';
381 name => '[% name %]',
382 root => '[% base %]/root',
387 sub default : Private {
388 my ( $self, $c ) = @_;
389 $c->res->output('Congratulations, [% name %] is on Catalyst!');
394 [% name %] - A very nice application
402 Very nice application.
410 This library is free software . You can redistribute it and/or modify it under
411 the same terms as perl itself.
418 use ExtUtils::MakeMaker;
421 NAME => '[% name %]',
422 VERSION_FROM => 'lib/[% class %].pm',
423 PREREQ_PM => { Catalyst => 5 },
424 test => { TESTS => join ' ', ( glob('t/*.t'), glob('t/*/*.t') ) }
428 Run script/server.pl to test the application.
431 This file documents the revision history for Perl extension $name.
434 - initial revision, generated by Catalyst
437 use Test::More tests => 2;
438 use_ok( Catalyst::Test, '[% name %]' );
440 ok( request('/')->is_success );
445 eval "use Test::Pod 1.14";
446 plan skip_all => 'Test::Pod 1.14 required' if $@;
447 plan skip_all => 'set TEST_POD to enable this test' unless $ENV{TEST_POD};
454 eval "use Test::Pod::Coverage 1.04";
455 plan skip_all => 'Test::Pod::Coverage 1.04 required' if $@;
456 plan skip_all => 'set TEST_POD to enable this test' unless $ENV{TEST_POD};
458 all_pod_coverage_ok();
462 BEGIN { $ENV{CATALYST_ENGINE} = 'CGI' }
466 use lib "$FindBin::Bin/../lib";
479 See L<Catalyst::Manual>
483 Run a Catalyst application as cgi.
487 Sebastian Riedel, C<sri@oook.de>
491 Copyright 2004 Sebastian Riedel. All rights reserved.
493 This library is free software. You can redistribute it and/or modify it under
494 the same terms as perl itself.
501 BEGIN { $ENV{CATALYST_ENGINE} = 'FCGI' }
505 use lib "$FindBin::Bin/../lib";
518 See L<Catalyst::Manual>
522 Run a Catalyst application as fcgi.
526 Sebastian Riedel, C<sri@oook.de>
530 Copyright 2004 Sebastian Riedel. All rights reserved.
532 This library is free software. You can redistribute it and/or modify it under
533 the same terms as perl itself.
541 $ENV{CATALYST_ENGINE} = 'HTTP';
542 $ENV{CATALYST_SCRIPT_GEN} = [% scriptgen %];
549 use lib "$FindBin::Bin/../lib";
555 GetOptions( 'help|?' => \$help, 'port=s' => \$port );
557 pod2usage(1) if $help;
559 [% name %]->run($port);
565 server - Catalyst Testserver
572 -? -help display this help and exits
573 -p -port port (defaults to 3000)
576 perldoc Catalyst::Manual
577 perldoc Catalyst::Manual::Intro
581 Run a Catalyst Testserver for this application.
585 Sebastian Riedel, C<sri@oook.de>
589 Copyright 2004 Sebastian Riedel. All rights reserved.
591 This library is free software. You can redistribute it and/or modify it under
592 the same terms as perl itself.
599 BEGIN { $ENV{CATALYST_ENGINE} = 'Test' }
605 use lib "$FindBin::Bin/../lib";
610 GetOptions( 'help|?' => \$help );
612 pod2usage(1) if ( $help || !$ARGV[0] );
614 print [% name %]->run($ARGV[0])->content . "\n";
624 test.pl [options] uri
627 -help display this help and exits
630 test.pl http://localhost/some_action
634 perldoc Catalyst::Manual
635 perldoc Catalyst::Manual::Intro
639 Run a Catalyst action from the comand line.
643 Sebastian Riedel, C<sri@oook.de>
647 Copyright 2004 Sebastian Riedel. All rights reserved.
649 This library is free software. You can redistribute it and/or modify it under
650 the same terms as perl itself.
660 use Catalyst::Helper;
664 GetOptions( 'help|?' => \$help );
666 pod2usage(1) if ( $help || !$ARGV[0] );
668 my $helper = Catalyst::Helper->new;
669 pod2usage(1) unless $helper->mk_component( '[% name %]', @ARGV );
675 create - Create a new Catalyst Component
679 create.pl [options] model|view|controller name [helper] [options]
682 -help display this help and exits
685 create.pl controller My::Controller
686 create.pl view My::View
687 create.pl view MyView TT
689 create.pl model My::Model
690 create.pl model SomeDB CDBI dbi:SQLite:/tmp/my.db
691 create.pl model AnotherDB CDBI dbi:Pg:dbname=foo root 4321
695 perldoc Catalyst::Manual
696 perldoc Catalyst::Manual::Intro
700 Create a new Catalyst Component.
704 Sebastian Riedel, C<sri\@oook.de>
708 Copyright 2004 Sebastian Riedel. All rights reserved.
710 This library is free software. You can redistribute it and/or modify it under
711 the same terms as perl itself.
719 use base 'Catalyst::Base';
722 sub default : Private {
723 my ( $self, $c ) = @_;
724 $c->res->output('Congratulations, [% class %] is on Catalyst!');
730 [% class %] - A Component
746 This library is free software . You can redistribute it and/or modify it under
747 the same terms as perl itself.
755 use Test::More tests => 3;
756 use_ok( Catalyst::Test, '[% app %]' );
757 use_ok('[% class %]');
759 ok( request('[% prefix %]')->is_success );
761 use Test::More tests => 1;
762 use_ok('[% class %]');