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;
65 $self->{author}=$self->{author} = $ENV{'AUTHOR'} ||
83 This method is called by create.pl to make new components
92 $self->{author}=$self->{author} = $ENV{'AUTHOR'} ||
94 $self->{base} = File::Spec->catdir( $FindBin::Bin, '..' );
95 unless ( $_[0] =~ /^model|m|view|v|controller|c\$/i ) {
98 my $class = "Catalyst::Helper::$helper";
99 eval "require $class";
100 die qq/Couldn't load helper "$class", "$@"/ if $@;
101 if ( $class->can('mk_stuff') ) {
102 return 1 unless $class->mk_stuff( $self, @args );
110 return 0 if $name =~ /[^\w\:]/;
111 $type = 'M' if $type =~ /model|m/i;
112 $type = 'V' if $type =~ /view|v/i;
113 $type = 'C' if $type =~ /controller|c/i;
114 $self->{type} = $type;
115 $self->{name} = $name;
116 $self->{class} = "$app\::$type\::$name";
119 my $appdir = File::Spec->catdir( split /\:\:/, $app );
121 File::Spec->catdir( $FindBin::Bin, '..', 'lib', $appdir, $type );
123 if ( $name =~ /\:/ ) {
124 my @path = split /\:\:/, $name;
126 $path = File::Spec->catdir( $path, @path );
129 $file = File::Spec->catfile( $path, "$file.pm" );
130 $self->{file} = $file;
133 $self->{test_dir} = File::Spec->catdir( $FindBin::Bin, '..', 't' );
134 $self->{test} = $self->next_test;
139 $comp = 'View' if $type eq 'V';
140 $comp = 'Controller' if $type eq 'C';
141 my $class = "Catalyst::Helper::$comp\::$helper";
142 eval "require $class";
143 die qq/Couldn't load helper "$class", "$@"/ if $@;
144 if ( $class->can('mk_compclass') ) {
145 return 1 unless $class->mk_compclass( $self, @args );
147 else { return 1 unless $self->_mk_compclass }
149 if ( $class->can('mk_comptest') ) {
150 $class->mk_comptest( $self, @args );
152 else { $self->_mk_comptest }
157 return 1 unless $self->_mk_compclass;
166 Surprisingly, this function makes a directory.
171 my ( $self, $dir ) = @_;
173 print qq/ exists "$dir"\n/;
177 print qq/created "$dir"\n/;
180 die qq/Couldn't create "$dir", "$!"/;
185 writes content to a file.
190 my ( $self, $file, $content ) = @_;
192 print qq/ exists "$file"\n/;
195 if ( my $f = IO::File->new("> $file") ) {
197 print qq/created "$file"\n/;
200 die qq/Couldn't create "$file", "$!"/;
208 my ( $self, $tname ) = @_;
209 if ($tname) { $tname = "$tname.t" }
211 my $name = $self->{name};
214 $prefix = lc $prefix;
215 $tname = $prefix . '.t';
216 $self->{prefix} = $prefix;
218 my $dir = $self->{test_dir};
219 my $type = lc $self->{type};
220 return File::Spec->catfile( $dir, $type, $tname );
225 Render and create a file from a template in DATA using
231 my ( $self, $file, $path, $vars ) = @_;
233 my $t = Template->new;
234 my $template = $self->get_file( ( caller(0) )[0], $file );
235 return 0 unless $template;
237 $t->process( \$template, { %{$self}, %$vars }, \$output );
238 $self->mk_file( $path, $output );
243 $self->mk_dir( $self->{dir} );
244 $self->{script} = File::Spec->catdir( $self->{dir}, 'script' );
245 $self->mk_dir( $self->{script} );
246 $self->{lib} = File::Spec->catdir( $self->{dir}, 'lib' );
247 $self->mk_dir( $self->{lib} );
248 $self->{root} = File::Spec->catdir( $self->{dir}, 'root' );
249 $self->mk_dir( $self->{root} );
250 $self->{t} = File::Spec->catdir( $self->{dir}, 't' );
251 $self->mk_dir( $self->{t} );
252 $self->mk_dir( File::Spec->catdir( $self->{t}, 'm' ) );
253 $self->mk_dir( File::Spec->catdir( $self->{t}, 'v' ) );
254 $self->mk_dir( File::Spec->catdir( $self->{t}, 'c' ) );
255 $self->{class} = File::Spec->catdir( split( /\:\:/, $self->{name} ) );
256 $self->{mod} = File::Spec->catdir( $self->{lib}, $self->{class} );
257 $self->mk_dir( $self->{mod} );
258 $self->{m} = File::Spec->catdir( $self->{mod}, 'M' );
259 $self->mk_dir( $self->{m} );
260 $self->{v} = File::Spec->catdir( $self->{mod}, 'V' );
261 $self->mk_dir( $self->{v} );
262 $self->{c} = File::Spec->catdir( $self->{mod}, 'C' );
263 $self->mk_dir( $self->{c} );
264 $self->{base} = File::Spec->rel2abs( $self->{dir} );
269 my $mod = $self->{mod};
270 $self->render_file( 'appclass', "$mod.pm" );
275 my $dir = $self->{dir};
276 $self->render_file( 'makefile', "$dir\/Makefile.PL" );
281 my $dir = $self->{dir};
282 $self->render_file( 'readme', "$dir\/README" );
287 my $dir = $self->{dir};
288 my $time = localtime time;
289 $self->render_file( 'changes', "$dir\/Changes", { time => $time } );
295 $self->render_file( 'apptest', "$t\/01app.t" );
296 $self->render_file( 'podtest', "$t\/02pod.t" );
297 $self->render_file( 'podcoveragetest', "$t\/03podcoverage.t" );
302 my $script = $self->{script};
303 $self->render_file( 'cgi', "$script\/cgi.pl" );
304 chmod 0700, "$script/cgi.pl";
309 my $script = $self->{script};
310 $self->render_file( 'fcgi', "$script\/fcgi.pl" );
311 chmod 0700, "$script/fcgi.pl";
316 my $script = $self->{script};
317 $self->render_file( 'server', "$script\/server.pl" );
318 chmod 0700, "$script/server.pl";
323 my $script = $self->{script};
324 $self->render_file( 'test', "$script/test.pl" );
325 chmod 0700, "$script/test.pl";
330 my $script = $self->{script};
331 $self->render_file( 'create', "$script\/create.pl" );
332 chmod 0700, "$script/create.pl";
337 my $file = $self->{file};
338 return $self->render_file( 'compclass', "$file" );
343 my $test = $self->{test};
344 $self->render_file( 'comptest', "$test" );
349 Helpers are classes that provide two methods.
351 * mk_compclass - creates the Component class
352 * mk_comptest - creates the Component test
354 So when you call C<bin/create view MyView TT>, create would try to execute
355 Catalyst::Helper::View::TT->mk_compclass and
356 Catalyst::Helper::View::TT->mk_comptest.
358 See L<Catalyst::Helper::View::TT> and L<Catalyst::Helper::Model::CDBI> for
361 All helper classes should be under one of the following namespaces.
363 Catalyst::Helper::Model::
364 Catalyst::Helper::View::
365 Catalyst::Helper::Controller::
369 The helpers will read author name from /etc/passwd by default.
370 To override, please export the AUTHOR variable.
374 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
375 L<Catalyst::Response>, L<Catalyst>
379 Sebastian Riedel, C<sri@oook.de>
383 This library is free software . You can redistribute it and/or modify
384 it under the same terms as perl itself.
395 use Catalyst qw/-Debug/;
397 our $VERSION = '0.01';
400 name => '[% name %]',
401 root => '[% base %]/root',
406 sub default : Private {
407 my ( $self, $c ) = @_;
408 $c->res->output('Congratulations, [% name %] is on Catalyst!');
413 [% name %] - A very nice application
421 Very nice application.
429 This library is free software . You can redistribute it and/or modify
430 it under the same terms as perl itself.
437 use ExtUtils::MakeMaker;
440 NAME => '[% name %]',
441 VERSION_FROM => 'lib/[% class %].pm',
442 PREREQ_PM => { Catalyst => 5 },
443 test => { TESTS => join ' ', ( glob('t/*.t'), glob('t/*/*.t') ) }
447 Run script/server.pl to test the application.
450 This file documents the revision history for Perl extension [% name %].
453 - initial revision, generated by Catalyst
456 use Test::More tests => 2;
457 use_ok( Catalyst::Test, '[% name %]' );
459 ok( request('/')->is_success );
464 eval "use Test::Pod 1.14";
465 plan skip_all => 'Test::Pod 1.14 required' if $@;
466 plan skip_all => 'set TEST_POD to enable this test' unless $ENV{TEST_POD};
473 eval "use Test::Pod::Coverage 1.04";
474 plan skip_all => 'Test::Pod::Coverage 1.04 required' if $@;
475 plan skip_all => 'set TEST_POD to enable this test' unless $ENV{TEST_POD};
477 all_pod_coverage_ok();
481 BEGIN { $ENV{CATALYST_ENGINE} = 'CGI' }
485 use lib "$FindBin::Bin/../lib";
498 See L<Catalyst::Manual>
502 Run a Catalyst application as cgi.
506 Sebastian Riedel, C<sri@oook.de>
510 Copyright 2004 Sebastian Riedel. All rights reserved.
512 This library is free software. You can redistribute it and/or modify
513 it under the same terms as perl itself.
520 BEGIN { $ENV{CATALYST_ENGINE} = 'FCGI' }
524 use lib "$FindBin::Bin/../lib";
537 See L<Catalyst::Manual>
541 Run a Catalyst application as fcgi.
545 Sebastian Riedel, C<sri@oook.de>
549 Copyright 2004 Sebastian Riedel. All rights reserved.
551 This library is free software. You can redistribute it and/or modify
552 it under the same terms as perl itself.
560 $ENV{CATALYST_ENGINE} = 'HTTP';
561 $ENV{CATALYST_SCRIPT_GEN} = [% scriptgen %];
568 use lib "$FindBin::Bin/../lib";
574 GetOptions( 'help|?' => \$help, 'port=s' => \$port );
576 pod2usage(1) if $help;
578 [% name %]->run($port);
584 server - Catalyst Testserver
591 -? -help display this help and exits
592 -p -port port (defaults to 3000)
595 perldoc Catalyst::Manual
596 perldoc Catalyst::Manual::Intro
600 Run a Catalyst Testserver for this application.
604 Sebastian Riedel, C<sri@oook.de>
608 Copyright 2004 Sebastian Riedel. All rights reserved.
610 This library is free software. You can redistribute it and/or modify
611 it under the same terms as perl itself.
618 BEGIN { $ENV{CATALYST_ENGINE} = 'Test' }
624 use lib "$FindBin::Bin/../lib";
629 GetOptions( 'help|?' => \$help );
631 pod2usage(1) if ( $help || !$ARGV[0] );
633 print [% name %]->run($ARGV[0])->content . "\n";
643 test.pl [options] uri
646 -help display this help and exits
649 test.pl http://localhost/some_action
653 perldoc Catalyst::Manual
654 perldoc Catalyst::Manual::Intro
658 Run a Catalyst action from the comand line.
662 Sebastian Riedel, C<sri@oook.de>
666 Copyright 2004 Sebastian Riedel. All rights reserved.
668 This library is free software. You can redistribute it and/or modify
669 it under the same terms as perl itself.
679 use Catalyst::Helper;
683 GetOptions( 'help|?' => \$help );
685 pod2usage(1) if ( $help || !$ARGV[0] );
687 my $helper = Catalyst::Helper->new;
688 pod2usage(1) unless $helper->mk_component( '[% name %]', @ARGV );
694 create - Create a new Catalyst Component
698 create.pl [options] model|view|controller name [helper] [options]
701 -help display this help and exits
704 create.pl controller My::Controller
705 create.pl view My::View
706 create.pl view MyView TT
708 create.pl model My::Model
709 create.pl model SomeDB CDBI dbi:SQLite:/tmp/my.db
710 create.pl model AnotherDB CDBI dbi:Pg:dbname=foo root 4321
714 perldoc Catalyst::Manual
715 perldoc Catalyst::Manual::Intro
719 Create a new Catalyst Component.
723 Sebastian Riedel, C<sri\@oook.de>
727 Copyright 2004 Sebastian Riedel. All rights reserved.
729 This library is free software. You can redistribute it and/or modify
730 it under the same terms as perl itself.
738 use base 'Catalyst::Base';
741 sub default : Private {
742 my ( $self, $c ) = @_;
743 $c->res->output('Congratulations, [% class %] is on Catalyst!');
749 [% class %] - A Component
765 This library is free software . You can redistribute it and/or modify
766 it under the same terms as perl itself.
774 use Test::More tests => 3;
775 use_ok( Catalyst::Test, '[% app %]' );
776 use_ok('[% class %]');
778 ok( request('[% prefix %]')->is_success );
780 use Test::More tests => 1;
781 use_ok('[% class %]');