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. Autogenerates scripts
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;
53 Create the main application skeleton.
58 my ( $self, $name ) = @_;
59 return 0 if $name =~ /[^\w\:]/;
60 $self->{name} = $name;
62 $self->{dir} =~ s/\:\:/-/g;
63 $self->{startperl} = $Config{startperl};
64 $self->{scriptgen}=$CATALYST_SCRIPT_GEN;
81 This method is called by create.pl to make new components
90 $self->{base} = File::Spec->catdir( $FindBin::Bin, '..' );
91 unless ( $_[0] =~ /^model|m|view|v|controller|c\$/i ) {
94 my $class = "Catalyst::Helper::$helper";
95 eval "require $class";
96 die qq/Couldn't load helper "$class", "$@"/ if $@;
97 if ( $class->can('mk_stuff') ) {
98 return 1 unless $class->mk_stuff( $self, @args );
106 return 0 if $name =~ /[^\w\:]/;
107 $type = 'M' if $type =~ /model|m/i;
108 $type = 'V' if $type =~ /view|v/i;
109 $type = 'C' if $type =~ /controller|c/i;
110 $self->{type} = $type;
111 $self->{name} = $name;
112 $self->{class} = "$app\::$type\::$name";
115 my $appdir = File::Spec->catdir( split /\:\:/, $app );
117 File::Spec->catdir( $FindBin::Bin, '..', 'lib', $appdir, $type );
119 if ( $name =~ /\:/ ) {
120 my @path = split /\:\:/, $name;
122 $path = File::Spec->catdir( $path, @path );
125 $file = File::Spec->catfile( $path, "$file.pm" );
126 $self->{file} = $file;
129 $self->{test_dir} = File::Spec->catdir( $FindBin::Bin, '..', 't' );
130 $self->{test} = $self->next_test;
135 $comp = 'View' if $type eq 'V';
136 $comp = 'Controller' if $type eq 'C';
137 my $class = "Catalyst::Helper::$comp\::$helper";
138 eval "require $class";
139 die qq/Couldn't load helper "$class", "$@"/ if $@;
140 if ( $class->can('mk_compclass') ) {
141 return 1 unless $class->mk_compclass( $self, @args );
143 else { return 1 unless $self->_mk_compclass }
145 if ( $class->can('mk_comptest') ) {
146 $class->mk_comptest( $self, @args );
148 else { $self->_mk_comptest }
153 return 1 unless $self->_mk_compclass;
162 Surprisingly, this function makes a directory.
167 my ( $self, $dir ) = @_;
169 print qq/ exists "$dir"\n/;
173 print qq/created "$dir"\n/;
176 die qq/Couldn't create "$dir", "$!"/;
181 writes content to a file.
186 my ( $self, $file, $content ) = @_;
188 print qq/ exists "$file"\n/;
191 if ( my $f = IO::File->new("> $file") ) {
193 print qq/created "$file"\n/;
196 die qq/Couldn't create "$file", "$!"/;
204 my ( $self, $tname ) = @_;
205 if ($tname) { $tname = "$tname.t" }
207 my $name = $self->{name};
210 $prefix = lc $prefix;
211 $tname = $prefix . '.t';
212 $self->{prefix} = $prefix;
214 my $dir = $self->{test_dir};
215 my $type = lc $self->{type};
216 return File::Spec->catfile( $dir, $type, $tname );
221 Render and create a file from a template in DATA using
227 my ( $self, $file, $path, $vars ) = @_;
229 my $t = Template->new;
230 my $template = $self->get_file( ( caller(0) )[0], $file );
231 return 0 unless $template;
233 $t->process( \$template, { %{$self}, %$vars }, \$output );
234 $self->mk_file( $path, $output );
239 $self->mk_dir( $self->{dir} );
240 $self->{script} = File::Spec->catdir( $self->{dir}, 'script' );
241 $self->mk_dir( $self->{script} );
242 $self->{lib} = File::Spec->catdir( $self->{dir}, 'lib' );
243 $self->mk_dir( $self->{lib} );
244 $self->{root} = File::Spec->catdir( $self->{dir}, 'root' );
245 $self->mk_dir( $self->{root} );
246 $self->{t} = File::Spec->catdir( $self->{dir}, 't' );
247 $self->mk_dir( $self->{t} );
248 $self->mk_dir( File::Spec->catdir( $self->{t}, 'm' ) );
249 $self->mk_dir( File::Spec->catdir( $self->{t}, 'v' ) );
250 $self->mk_dir( File::Spec->catdir( $self->{t}, 'c' ) );
251 $self->{class} = File::Spec->catdir( split( /\:\:/, $self->{name} ) );
252 $self->{mod} = File::Spec->catdir( $self->{lib}, $self->{class} );
253 $self->mk_dir( $self->{mod} );
254 $self->{m} = File::Spec->catdir( $self->{mod}, 'M' );
255 $self->mk_dir( $self->{m} );
256 $self->{v} = File::Spec->catdir( $self->{mod}, 'V' );
257 $self->mk_dir( $self->{v} );
258 $self->{c} = File::Spec->catdir( $self->{mod}, 'C' );
259 $self->mk_dir( $self->{c} );
260 $self->{base} = File::Spec->rel2abs( $self->{dir} );
265 my $mod = $self->{mod};
266 $self->render_file( 'appclass', "$mod.pm" );
271 my $dir = $self->{dir};
272 $self->render_file( 'makefile', "$dir\/Makefile.PL" );
277 my $dir = $self->{dir};
278 $self->render_file( 'readme', "$dir\/README" );
283 my $dir = $self->{dir};
284 my $time = localtime time;
285 $self->render_file( 'changes', "$dir\/Changes", { time => $time } );
291 $self->render_file( 'apptest', "$t\/01app.t" );
292 $self->render_file( 'podtest', "$t\/02pod.t" );
293 $self->render_file( 'podcoveragetest', "$t\/03podcoverage.t" );
298 my $script = $self->{script};
299 $self->render_file( 'cgi', "$script\/cgi.pl" );
300 chmod 0700, "$script/cgi.pl";
305 my $script = $self->{script};
306 $self->render_file( 'fcgi', "$script\/fcgi.pl" );
307 chmod 0700, "$script/fcgi.pl";
312 my $script = $self->{script};
313 $self->render_file( 'server', "$script\/server.pl" );
314 chmod 0700, "$script/server.pl";
319 my $script = $self->{script};
320 $self->render_file( 'test', "$script/test.pl" );
321 chmod 0700, "$script/test.pl";
326 my $script = $self->{script};
327 $self->render_file( 'create', "$script\/create.pl" );
328 chmod 0700, "$script/create.pl";
333 my $file = $self->{file};
334 return $self->render_file( 'compclass', "$file" );
339 my $test = $self->{test};
340 $self->render_file( 'comptest', "$test" );
345 Helpers are classes that provide two methods.
347 * mk_compclass - creates the Component class
348 * mk_comptest - creates the Component test
350 So when you call C<bin/create view MyView TT>, create would try to execute
351 Catalyst::Helper::View::TT->mk_compclass and
352 Catalyst::Helper::View::TT->mk_comptest.
354 See L<Catalyst::Helper::View::TT> and L<Catalyst::Helper::Model::CDBI> for
357 All helper classes should be under one of the following namespaces.
359 Catalyst::Helper::Model::
360 Catalyst::Helper::View::
361 Catalyst::Helper::Controller::
365 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
366 L<Catalyst::Response>, L<Catalyst>
370 Sebastian Riedel, C<sri@oook.de>
374 This library is free software . You can redistribute it and/or modify
375 it under the same terms as perl itself.
386 use Catalyst qw/-Debug/;
388 our $VERSION = '0.01';
391 name => '[% name %]',
392 root => '[% base %]/root',
397 sub default : Private {
398 my ( $self, $c ) = @_;
399 $c->res->output('Congratulations, [% name %] is on Catalyst!');
404 [% name %] - A very nice application
412 Very nice application.
420 This library is free software . You can redistribute it and/or modify
421 it under the same terms as perl itself.
428 use ExtUtils::MakeMaker;
431 NAME => '[% name %]',
432 VERSION_FROM => 'lib/[% class %].pm',
433 PREREQ_PM => { Catalyst => 5 },
434 test => { TESTS => join ' ', ( glob('t/*.t'), glob('t/*/*.t') ) }
438 Run script/server.pl to test the application.
441 This file documents the revision history for Perl extension $name.
444 - initial revision, generated by Catalyst
447 use Test::More tests => 2;
448 use_ok( Catalyst::Test, '[% name %]' );
450 ok( request('/')->is_success );
455 eval "use Test::Pod 1.14";
456 plan skip_all => 'Test::Pod 1.14 required' if $@;
457 plan skip_all => 'set TEST_POD to enable this test' unless $ENV{TEST_POD};
464 eval "use Test::Pod::Coverage 1.04";
465 plan skip_all => 'Test::Pod::Coverage 1.04 required' if $@;
466 plan skip_all => 'set TEST_POD to enable this test' unless $ENV{TEST_POD};
468 all_pod_coverage_ok();
472 BEGIN { $ENV{CATALYST_ENGINE} = 'CGI' }
476 use lib "$FindBin::Bin/../lib";
489 See L<Catalyst::Manual>
493 Run a Catalyst application as cgi.
497 Sebastian Riedel, C<sri@oook.de>
501 Copyright 2004 Sebastian Riedel. All rights reserved.
503 This library is free software. You can redistribute it and/or modify
504 it under the same terms as perl itself.
511 BEGIN { $ENV{CATALYST_ENGINE} = 'FCGI' }
515 use lib "$FindBin::Bin/../lib";
528 See L<Catalyst::Manual>
532 Run a Catalyst application as fcgi.
536 Sebastian Riedel, C<sri@oook.de>
540 Copyright 2004 Sebastian Riedel. All rights reserved.
542 This library is free software. You can redistribute it and/or modify
543 it under the same terms as perl itself.
551 $ENV{CATALYST_ENGINE} = 'HTTP';
552 $ENV{CATALYST_SCRIPT_GEN} = [% scriptgen %];
559 use lib "$FindBin::Bin/../lib";
565 GetOptions( 'help|?' => \$help, 'port=s' => \$port );
567 pod2usage(1) if $help;
569 [% name %]->run($port);
575 server - Catalyst Testserver
582 -? -help display this help and exits
583 -p -port port (defaults to 3000)
586 perldoc Catalyst::Manual
587 perldoc Catalyst::Manual::Intro
591 Run a Catalyst Testserver for this application.
595 Sebastian Riedel, C<sri@oook.de>
599 Copyright 2004 Sebastian Riedel. All rights reserved.
601 This library is free software. You can redistribute it and/or modify
602 it under the same terms as perl itself.
609 BEGIN { $ENV{CATALYST_ENGINE} = 'Test' }
615 use lib "$FindBin::Bin/../lib";
620 GetOptions( 'help|?' => \$help );
622 pod2usage(1) if ( $help || !$ARGV[0] );
624 print [% name %]->run($ARGV[0])->content . "\n";
634 test.pl [options] uri
637 -help display this help and exits
640 test.pl http://localhost/some_action
644 perldoc Catalyst::Manual
645 perldoc Catalyst::Manual::Intro
649 Run a Catalyst action from the comand line.
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
660 it under the same terms as perl itself.
670 use Catalyst::Helper;
674 GetOptions( 'help|?' => \$help );
676 pod2usage(1) if ( $help || !$ARGV[0] );
678 my $helper = Catalyst::Helper->new;
679 pod2usage(1) unless $helper->mk_component( '[% name %]', @ARGV );
685 create - Create a new Catalyst Component
689 create.pl [options] model|view|controller name [helper] [options]
692 -help display this help and exits
695 create.pl controller My::Controller
696 create.pl view My::View
697 create.pl view MyView TT
699 create.pl model My::Model
700 create.pl model SomeDB CDBI dbi:SQLite:/tmp/my.db
701 create.pl model AnotherDB CDBI dbi:Pg:dbname=foo root 4321
705 perldoc Catalyst::Manual
706 perldoc Catalyst::Manual::Intro
710 Create a new Catalyst Component.
714 Sebastian Riedel, C<sri\@oook.de>
718 Copyright 2004 Sebastian Riedel. All rights reserved.
720 This library is free software. You can redistribute it and/or modify
721 it under the same terms as perl itself.
729 use base 'Catalyst::Base';
732 sub default : Private {
733 my ( $self, $c ) = @_;
734 $c->res->output('Congratulations, [% class %] is on Catalyst!');
740 [% class %] - A Component
756 This library is free software . You can redistribute it and/or modify
757 it under the same terms as perl itself.
765 use Test::More tests => 3;
766 use_ok( Catalyst::Test, '[% app %]' );
767 use_ok('[% class %]');
769 ok( request('[% prefix %]')->is_success );
771 use Test::More tests => 1;
772 use_ok('[% class %]');