1 package Catalyst::Helper;
4 use base 'Class::Accessor::Fast';
16 Catalyst::Helper - Bootstrap a Catalyst application
20 See L<Catalyst::Manual::Intro>
24 Bootstrap a Catalyst application.
35 my ( $self, $class, $file ) = @_;
36 unless ( $cache{$class} ) {
38 $cache{$class} = eval "package $class; <DATA>";
40 my $data = $cache{$class};
41 my @files = split /^__(.+)__\n/m, $data;
44 my ( $name, $content ) = splice @files, 0, 2;
45 return $content if $name eq $file;
55 my ( $self, $name ) = @_;
56 return 0 if $name =~ /[^\w\:]/;
57 $self->{name} = $name;
59 $self->{dir} =~ s/\:\:/-/g;
60 $self->{startperl} = $Config{startperl};
83 $self->{base} = File::Spec->catdir( $FindBin::Bin, '..' );
84 unless ( $_[0] =~ /^model|m|view|v|controller|c\$/i ) {
87 my $class = "Catalyst::Helper::$helper";
88 eval "require $class";
89 die qq/Couldn't load helper "$class", "$@"/ if $@;
90 if ( $class->can('mk_stuff') ) {
91 return 1 unless $class->mk_stuff( $self, @args );
99 return 0 if $name =~ /[^\w\:]/;
100 $type = 'M' if $type =~ /model|m/i;
101 $type = 'V' if $type =~ /view|v/i;
102 $type = 'C' if $type =~ /controller|c/i;
103 $self->{type} = $type;
104 $self->{name} = $name;
105 $self->{class} = "$app\::$type\::$name";
108 my $appdir = File::Spec->catdir( split /\:\:/, $app );
110 File::Spec->catdir( $FindBin::Bin, '..', 'lib', $appdir, $type );
112 if ( $name =~ /\:/ ) {
113 my @path = split /\:\:/, $name;
115 $path = File::Spec->catdir( $path, @path );
118 $file = File::Spec->catfile( $path, "$file.pm" );
119 $self->{file} = $file;
122 $self->{test_dir} = File::Spec->catdir( $FindBin::Bin, '..', 't' );
123 $self->{test} = $self->next_test;
128 $comp = 'View' if $type eq 'V';
129 $comp = 'Controller' if $type eq 'C';
130 my $class = "Catalyst::Helper::$comp\::$helper";
131 eval "require $class";
132 die qq/Couldn't load helper "$class", "$@"/ if $@;
133 if ( $class->can('mk_compclass') ) {
134 return 1 unless $class->mk_compclass( $self, @args );
136 else { return 1 unless $self->_mk_compclass }
138 if ( $class->can('mk_comptest') ) {
139 $class->mk_comptest( $self, @args );
141 else { $self->_mk_comptest }
146 return 1 unless $self->_mk_compclass;
158 my ( $self, $dir ) = @_;
160 print qq/ exists "$dir"\n/;
164 print qq/created "$dir"\n/;
167 die qq/Couldn't create "$dir", "$!"/;
175 my ( $self, $file, $content ) = @_;
177 print qq/ exists "$file"\n/;
180 if ( my $f = IO::File->new("> $file") ) {
182 print qq/created "$file"\n/;
185 die qq/Couldn't create "$file", "$!"/;
193 my ( $self, $tname ) = @_;
194 if ($tname) { $tname = "$tname.t" }
196 my $name = $self->{name};
199 $prefix = lc $prefix;
200 $tname = $prefix . '.t';
201 $self->{prefix} = $prefix;
203 my $dir = $self->{test_dir};
204 my $type = lc $self->{type};
205 return File::Spec->catfile( $dir, $type, $tname );
210 Render and create a file from a template in DATA.
215 my ( $self, $file, $path, $vars ) = @_;
217 my $t = Template->new;
218 my $template = $self->get_file( ( caller(0) )[0], $file );
219 return 0 unless $template;
221 $t->process( \$template, { %{$self}, %$vars }, \$output );
222 $self->mk_file( $path, $output );
227 $self->mk_dir( $self->{dir} );
228 $self->{script} = File::Spec->catdir( $self->{dir}, 'script' );
229 $self->mk_dir( $self->{script} );
230 $self->{lib} = File::Spec->catdir( $self->{dir}, 'lib' );
231 $self->mk_dir( $self->{lib} );
232 $self->{root} = File::Spec->catdir( $self->{dir}, 'root' );
233 $self->mk_dir( $self->{root} );
234 $self->{t} = File::Spec->catdir( $self->{dir}, 't' );
235 $self->mk_dir( $self->{t} );
236 $self->mk_dir( File::Spec->catdir( $self->{t}, 'm' ) );
237 $self->mk_dir( File::Spec->catdir( $self->{t}, 'v' ) );
238 $self->mk_dir( File::Spec->catdir( $self->{t}, 'c' ) );
239 $self->{class} = File::Spec->catdir( split( /\:\:/, $self->{name} ) );
240 $self->{mod} = File::Spec->catdir( $self->{lib}, $self->{class} );
241 $self->mk_dir( $self->{mod} );
242 $self->{m} = File::Spec->catdir( $self->{mod}, 'M' );
243 $self->mk_dir( $self->{m} );
244 $self->{v} = File::Spec->catdir( $self->{mod}, 'V' );
245 $self->mk_dir( $self->{v} );
246 $self->{c} = File::Spec->catdir( $self->{mod}, 'C' );
247 $self->mk_dir( $self->{c} );
248 $self->{base} = File::Spec->rel2abs( $self->{dir} );
253 my $mod = $self->{mod};
254 $self->render_file( 'appclass', "$mod.pm" );
259 my $dir = $self->{dir};
260 $self->render_file( 'makefile', "$dir\/Makefile.PL" );
265 my $dir = $self->{dir};
266 $self->render_file( 'readme', "$dir\/README" );
271 my $dir = $self->{dir};
272 my $time = localtime time;
273 $self->render_file( 'changes', "$dir\/Changes", { time => $time } );
279 $self->render_file( 'apptest', "$t\/01app.t" );
280 $self->render_file( 'podtest', "$t\/02pod.t" );
281 $self->render_file( 'podcoveragetest', "$t\/03podcoverage.t" );
286 my $script = $self->{script};
287 $self->render_file( 'cgi', "$script\/cgi.pl" );
288 chmod 0700, "$script/cgi.pl";
293 my $script = $self->{script};
294 $self->render_file( 'fcgi', "$script\/fcgi.pl" );
295 chmod 0700, "$script/fcgi.pl";
300 my $script = $self->{script};
301 $self->render_file( 'server', "$script\/server.pl" );
302 chmod 0700, "$script/server.pl";
307 my $script = $self->{script};
308 $self->render_file( 'test', "$script/test.pl" );
309 chmod 0700, "$script/test.pl";
314 my $script = $self->{script};
315 $self->render_file( 'create', "$script\/create.pl" );
316 chmod 0700, "$script/create.pl";
321 my $file = $self->{file};
322 return $self->render_file( 'compclass', "$file" );
327 my $test = $self->{test};
328 $self->render_file( 'comptest', "$test" );
333 Helpers are classes that provide two methods.
335 * mk_compclass - creates the Component class
336 * mk_comptest - creates the Component test
338 So when you call C<bin/create view MyView TT>, create would try to execute
339 Catalyst::Helper::View::TT->mk_compclass and
340 Catalyst::Helper::View::TT->mk_comptest.
342 See L<Catalyst::Helper::View::TT> and L<Catalyst::Helper::Model::CDBI> for
345 All helper classes should be under one of the following namespaces.
347 Catalyst::Helper::Model::
348 Catalyst::Helper::View::
349 Catalyst::Helper::Controller::
353 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
354 L<Catalyst::Response>, L<Catalyst>
358 Sebastian Riedel, C<sri@oook.de>
362 This library is free software . You can redistribute it and/or modify it under
363 the same terms as perl itself.
374 use Catalyst qw/-Debug/;
376 our $VERSION = '0.01';
379 name => '[% name %]',
380 root => '[% base %]/root',
385 sub default : Private {
386 my ( $self, $c ) = @_;
387 $c->res->output('Congratulations, [% name %] is on Catalyst!');
392 [% name %] - A very nice application
400 Very nice application.
408 This library is free software . You can redistribute it and/or modify it under
409 the same terms as perl itself.
416 use ExtUtils::MakeMaker;
419 NAME => '[% name %]',
420 VERSION_FROM => 'lib/[% class %].pm',
421 PREREQ_PM => { Catalyst => 5 },
422 test => { TESTS => join ' ', ( glob('t/*.t'), glob('t/*/*.t') ) }
426 Run script/server.pl to test the application.
429 This file documents the revision history for Perl extension $name.
432 - initial revision, generated by Catalyst
435 use Test::More tests => 2;
436 use_ok( Catalyst::Test, '[% name %]' );
438 ok( request('/')->is_success );
443 eval "use Test::Pod 1.14";
444 plan skip_all => 'Test::Pod 1.14 required' if $@;
445 plan skip_all => 'set TEST_POD to enable this test' unless $ENV{TEST_POD};
452 eval "use Test::Pod::Coverage 1.04";
453 plan skip_all => 'Test::Pod::Coverage 1.04 required' if $@;
454 plan skip_all => 'set TEST_POD to enable this test' unless $ENV{TEST_POD};
456 all_pod_coverage_ok();
460 BEGIN { $ENV{CATALYST_ENGINE} = 'CGI' }
464 use lib "$FindBin::Bin/../lib";
477 See L<Catalyst::Manual>
481 Run a Catalyst application as cgi.
485 Sebastian Riedel, C<sri@oook.de>
489 Copyright 2004 Sebastian Riedel. All rights reserved.
491 This library is free software. You can redistribute it and/or modify it under
492 the same terms as perl itself.
499 BEGIN { $ENV{CATALYST_ENGINE} = 'FCGI' }
503 use lib "$FindBin::Bin/../lib";
516 See L<Catalyst::Manual>
520 Run a Catalyst application as fcgi.
524 Sebastian Riedel, C<sri@oook.de>
528 Copyright 2004 Sebastian Riedel. All rights reserved.
530 This library is free software. You can redistribute it and/or modify it under
531 the same terms as perl itself.
538 BEGIN { $ENV{CATALYST_ENGINE} = 'HTTP' }
544 use lib "$FindBin::Bin/../lib";
550 GetOptions( 'help|?' => \$help, 'port=s' => \$port );
552 pod2usage(1) if $help;
554 [% name %]->run($port);
560 server - Catalyst Testserver
567 -? -help display this help and exits
568 -p -port port (defaults to 3000)
571 perldoc Catalyst::Manual
572 perldoc Catalyst::Manual::Intro
576 Run a Catalyst Testserver for this application.
580 Sebastian Riedel, C<sri@oook.de>
584 Copyright 2004 Sebastian Riedel. All rights reserved.
586 This library is free software. You can redistribute it and/or modify it under
587 the same terms as perl itself.
594 BEGIN { $ENV{CATALYST_ENGINE} = 'Test' }
600 use lib "$FindBin::Bin/../lib";
605 GetOptions( 'help|?' => \$help );
607 pod2usage(1) if ( $help || !$ARGV[0] );
609 print [% name %]->run($ARGV[0])->content . "\n";
619 test.pl [options] uri
622 -help display this help and exits
625 test.pl http://localhost/some_action
629 perldoc Catalyst::Manual
630 perldoc Catalyst::Manual::Intro
634 Run a Catalyst action from the comand line.
638 Sebastian Riedel, C<sri@oook.de>
642 Copyright 2004 Sebastian Riedel. All rights reserved.
644 This library is free software. You can redistribute it and/or modify it under
645 the same terms as perl itself.
655 use Catalyst::Helper;
659 GetOptions( 'help|?' => \$help );
661 pod2usage(1) if ( $help || !$ARGV[0] );
663 my $helper = Catalyst::Helper->new;
664 pod2usage(1) unless $helper->mk_component( '[% name %]', @ARGV );
670 create - Create a new Catalyst Component
674 create.pl [options] model|view|controller name [helper] [options]
677 -help display this help and exits
680 create.pl controller My::Controller
681 create.pl view My::View
682 create.pl view MyView TT
684 create.pl model My::Model
685 create.pl model SomeDB CDBI dbi:SQLite:/tmp/my.db
686 create.pl model AnotherDB CDBI dbi:Pg:dbname=foo root 4321
690 perldoc Catalyst::Manual
691 perldoc Catalyst::Manual::Intro
695 Create a new Catalyst Component.
699 Sebastian Riedel, C<sri\@oook.de>
703 Copyright 2004 Sebastian Riedel. All rights reserved.
705 This library is free software. You can redistribute it and/or modify it under
706 the same terms as perl itself.
714 use base 'Catalyst::Base';
717 sub default : Private {
718 my ( $self, $c ) = @_;
719 $c->res->output('Congratulations, [% class %] is on Catalyst!');
725 [% class %] - A Component
741 This library is free software . You can redistribute it and/or modify it under
742 the same terms as perl itself.
750 use Test::More tests => 3;
751 use_ok( Catalyst::Test, '[% app %]' );
752 use_ok('[% class %]');
754 ok( request('[% prefix %]')->is_success );
756 use Test::More tests => 1;
757 use_ok('[% class %]');