1 package Catalyst::Helper;
4 use base 'Class::Accessor::Fast';
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->{appprefix} = lc $self->{dir};
64 $self->{appprefix} =~ s/-/_/g;
65 $self->{startperl} = $Config{startperl};
66 $self->{scriptgen} = $Catalyst::CATALYST_SCRIPT_GEN;
67 $self->{author} = $self->{author} = $ENV{'AUTHOR'}
68 || eval { @{ [ getpwuid($<) ] }[6] }
69 || 'Catalyst developer';
87 This method is called by create.pl to make new components
96 $self->{author} = $self->{author} = $ENV{'AUTHOR'}
97 || eval { @{ [ getpwuid($<) ] }[6] }
99 $self->{base} = File::Spec->catdir( $FindBin::Bin, '..' );
100 unless ( $_[0] =~ /^(?:model|m|view|v|controller|c)$/i ) {
103 my $class = "Catalyst::Helper::$helper";
104 eval "require $class";
105 die qq/Couldn't load helper "$class", "$@"/ if $@;
106 if ( $class->can('mk_stuff') ) {
107 return 1 unless $class->mk_stuff( $self, @args );
112 my $name = shift || "Missing name for model/view/controller";
115 return 0 if $name =~ /[^\w\:]/;
116 $type = 'M' if $type =~ /model|m/i;
117 $type = 'V' if $type =~ /view|v/i;
118 $type = 'C' if $type =~ /controller|c/i;
119 $self->{type} = $type;
120 $self->{name} = $name;
121 $self->{class} = "$app\::$type\::$name";
124 my $appdir = File::Spec->catdir( split /\:\:/, $app );
126 File::Spec->catdir( $FindBin::Bin, '..', 'lib', $appdir, $type );
128 if ( $name =~ /\:/ ) {
129 my @path = split /\:\:/, $name;
131 $path = File::Spec->catdir( $path, @path );
134 $file = File::Spec->catfile( $path, "$file.pm" );
135 $self->{file} = $file;
138 $self->{test_dir} = File::Spec->catdir( $FindBin::Bin, '..', 't' );
139 $self->{test} = $self->next_test;
144 $comp = 'View' if $type eq 'V';
145 $comp = 'Controller' if $type eq 'C';
146 my $class = "Catalyst::Helper::$comp\::$helper";
147 eval "require $class";
148 die qq/Couldn't load helper "$class", "$@"/ if $@;
149 if ( $class->can('mk_compclass') ) {
150 return 1 unless $class->mk_compclass( $self, @args );
152 else { return 1 unless $self->_mk_compclass }
154 if ( $class->can('mk_comptest') ) {
155 $class->mk_comptest( $self, @args );
157 else { $self->_mk_comptest }
162 return 1 unless $self->_mk_compclass;
171 Surprisingly, this function makes a directory.
176 my ( $self, $dir ) = @_;
178 print qq/ exists "$dir"\n/;
182 print qq/created "$dir"\n/;
185 die qq/Couldn't create "$dir", "$!"/;
190 writes content to a file.
195 my ( $self, $file, $content ) = @_;
197 print qq/ exists "$file"\n/;
200 if ( my $f = IO::File->new("> $file") ) {
202 print qq/created "$file"\n/;
205 die qq/Couldn't create "$file", "$!"/;
213 my ( $self, $tname ) = @_;
214 if ($tname) { $tname = "$tname.t" }
216 my $name = $self->{name};
220 $tname = $prefix . '.t';
221 $self->{prefix} = $prefix;
222 $prefix = lc $prefix;
224 $self->{uri} = $prefix;
226 my $dir = $self->{test_dir};
227 my $type = $self->{type};
228 return File::Spec->catfile( $dir, $type, $tname );
233 Render and create a file from a template in DATA using
239 my ( $self, $file, $path, $vars ) = @_;
241 my $t = Template->new;
242 my $template = $self->get_file( ( caller(0) )[0], $file );
243 return 0 unless $template;
245 $t->process( \$template, { %{$self}, %$vars }, \$output );
246 $self->mk_file( $path, $output );
251 $self->mk_dir( $self->{dir} );
252 $self->{script} = File::Spec->catdir( $self->{dir}, 'script' );
253 $self->mk_dir( $self->{script} );
254 $self->{lib} = File::Spec->catdir( $self->{dir}, 'lib' );
255 $self->mk_dir( $self->{lib} );
256 $self->{root} = File::Spec->catdir( $self->{dir}, 'root' );
257 $self->mk_dir( $self->{root} );
258 $self->{t} = File::Spec->catdir( $self->{dir}, 't' );
259 $self->mk_dir( $self->{t} );
260 $self->mk_dir( File::Spec->catdir( $self->{t}, 'M' ) );
261 $self->mk_dir( File::Spec->catdir( $self->{t}, 'V' ) );
262 $self->mk_dir( File::Spec->catdir( $self->{t}, 'C' ) );
263 $self->{class} = File::Spec->catdir( split( /\:\:/, $self->{name} ) );
264 $self->{mod} = File::Spec->catdir( $self->{lib}, $self->{class} );
265 $self->mk_dir( $self->{mod} );
266 $self->{m} = File::Spec->catdir( $self->{mod}, 'M' );
267 $self->mk_dir( $self->{m} );
268 $self->{v} = File::Spec->catdir( $self->{mod}, 'V' );
269 $self->mk_dir( $self->{v} );
270 $self->{c} = File::Spec->catdir( $self->{mod}, 'C' );
271 $self->mk_dir( $self->{c} );
272 $self->{base} = File::Spec->rel2abs( $self->{dir} );
277 my $mod = $self->{mod};
278 $self->render_file( 'appclass', "$mod.pm" );
283 my $dir = $self->{dir};
284 $self->render_file( 'build', "$dir\/Build.PL" );
289 my $dir = $self->{dir};
290 $self->render_file( 'makefile', "$dir\/Makefile.PL" );
295 my $dir = $self->{dir};
296 $self->render_file( 'readme', "$dir\/README" );
301 my $dir = $self->{dir};
302 my $time = localtime time;
303 $self->render_file( 'changes', "$dir\/Changes", { time => $time } );
309 $self->render_file( 'apptest', "$t\/01app.t" );
310 $self->render_file( 'podtest', "$t\/02pod.t" );
311 $self->render_file( 'podcoveragetest', "$t\/03podcoverage.t" );
316 my $script = $self->{script};
317 my $appprefix = $self->{appprefix};
318 $self->render_file( 'cgi', "$script\/$appprefix\_cgi.pl" );
319 chmod 0700, "$script/$appprefix\_cgi.pl";
324 my $script = $self->{script};
325 my $appprefix = $self->{appprefix};
326 $self->render_file( 'fastcgi', "$script\/$appprefix\_fastcgi.pl" );
327 chmod 0700, "$script/$appprefix\_fastcgi.pl";
332 my $script = $self->{script};
333 my $appprefix = $self->{appprefix};
334 $self->render_file( 'server', "$script\/$appprefix\_server.pl" );
335 chmod 0700, "$script/$appprefix\_server.pl";
340 my $script = $self->{script};
341 my $appprefix = $self->{appprefix};
342 $self->render_file( 'test', "$script/$appprefix\_test.pl" );
343 chmod 0700, "$script/$appprefix\_test.pl";
348 my $script = $self->{script};
349 my $appprefix = $self->{appprefix};
350 $self->render_file( 'create', "$script\/$appprefix\_create.pl" );
351 chmod 0700, "$script/$appprefix\_create.pl";
356 my $file = $self->{file};
357 return $self->render_file( 'compclass', "$file" );
362 my $test = $self->{test};
363 $self->render_file( 'comptest', "$test" );
368 Helpers are classes that provide two methods.
370 * mk_compclass - creates the Component class
371 * mk_comptest - creates the Component test
373 So when you call C<bin/create view MyView TT>, create would try to execute
374 Catalyst::Helper::View::TT->mk_compclass and
375 Catalyst::Helper::View::TT->mk_comptest.
377 See L<Catalyst::Helper::View::TT> and L<Catalyst::Helper::Model::CDBI> for
380 All helper classes should be under one of the following namespaces.
382 Catalyst::Helper::Model::
383 Catalyst::Helper::View::
384 Catalyst::Helper::Controller::
388 The helpers will read author name from /etc/passwd by default.
389 To override, please export the AUTHOR variable.
393 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
394 L<Catalyst::Response>, L<Catalyst>
398 Sebastian Riedel, C<sri@oook.de>
402 This library is free software . You can redistribute it and/or modify
403 it under the same terms as perl itself.
414 use Catalyst qw/-Debug/;
416 our $VERSION = '0.01';
418 [% name %]->config( name => '[% name %]' );
424 [% name %] - Catalyst based application
428 script/[% appprefix %]_server.pl
432 Catalyst based application.
442 sub default : Private {
443 my ( $self, $c ) = @_;
444 $c->res->output('Congratulations, [% name %] is on Catalyst!');
455 This library is free software . You can redistribute it and/or modify
456 it under the same terms as perl itself.
462 unless ( eval "use Module::Build::Compat 0.02; 1" ) {
463 print "This module requires Module::Build to install itself.\n";
465 require ExtUtils::MakeMaker;
467 ExtUtils::MakeMaker::prompt( ' Install Module::Build now from CPAN?', 'y' );
469 unless ( $yn =~ /^y/i ) {
470 die " *** Cannot install without Module::Build. Exiting ...\n";
477 # Save this 'cause CPAN will chdir all over the place.
478 my $cwd = Cwd::cwd();
479 my $makefile = File::Spec->rel2abs($0);
481 CPAN::Shell->install('Module::Build::Compat')
482 or die " *** Cannot install without Module::Build. Exiting ...\n";
484 chdir $cwd or die "Cannot chdir() back to $cwd: $!";
486 eval "use Module::Build::Compat 0.02; 1" or die $@;
487 use lib '_build/lib';
488 Module::Build::Compat->run_build_pl( args => \@ARGV );
489 require Module::Build;
490 Module::Build::Compat->write_makefile( build_class => 'Module::Build' );
495 my $build = Catalyst::Build->new(
496 create_makefile_pl => 'passthrough',
498 module_name => '[% name %]',
499 requires => { Catalyst => '5.10' },
500 create_makefile_pl => 'passthrough',
501 script_files => [ glob('script/*') ],
502 test_files => [ glob('t/*.t'), glob('t/*/*.t') ]
504 $build->create_build_script;
506 Run script/[% appprefix %]_server.pl to test the application.
508 This file documents the revision history for Perl extension [% name %].
510 - initial revision, generated by Catalyst
512 use Test::More tests => 2;
513 use_ok( Catalyst::Test, '[% name %]' );
515 ok( request('/')->is_success );
519 eval "use Test::Pod 1.14";
520 plan skip_all => 'Test::Pod 1.14 required' if $@;
521 plan skip_all => 'set TEST_POD to enable this test' unless $ENV{TEST_POD};
527 eval "use Test::Pod::Coverage 1.04";
528 plan skip_all => 'Test::Pod::Coverage 1.04 required' if $@;
529 plan skip_all => 'set TEST_POD to enable this test' unless $ENV{TEST_POD};
531 all_pod_coverage_ok();
535 BEGIN { $ENV{CATALYST_ENGINE} ||= 'CGI' }
539 use lib "$FindBin::Bin/../lib";
548 [% appprefix %]_cgi.pl - Catalyst CGI
552 See L<Catalyst::Manual>
556 Run a Catalyst application as cgi.
560 Sebastian Riedel, C<sri@oook.de>
564 Copyright 2004 Sebastian Riedel. All rights reserved.
566 This library is free software. You can redistribute it and/or modify
567 it under the same terms as perl itself.
573 BEGIN { $ENV{CATALYST_ENGINE} ||= 'FastCGI' }
577 use lib "$FindBin::Bin/../lib";
586 [% appprefix %]_fastcgi.pl - Catalyst FastCGI
590 See L<Catalyst::Manual>
594 Run a Catalyst application as fastcgi.
598 Sebastian Riedel, C<sri@oook.de>
602 Copyright 2004 Sebastian Riedel. All rights reserved.
604 This library is free software. You can redistribute it and/or modify
605 it under the same terms as perl itself.
612 $ENV{CATALYST_ENGINE} ||= 'HTTP';
613 $ENV{CATALYST_SCRIPT_GEN} = [% scriptgen %];
620 use lib "$FindBin::Bin/../lib";
626 GetOptions( 'help|?' => \$help, 'port=s' => \$port );
628 pod2usage(1) if $help;
630 [% name %]->run($port);
636 [% appprefix %]_server.pl - Catalyst Testserver
640 [% appprefix %]_server.pl [options]
643 -? -help display this help and exits
644 -p -port port (defaults to 3000)
647 perldoc Catalyst::Manual
648 perldoc Catalyst::Manual::Intro
652 Run a Catalyst Testserver for this application.
656 Sebastian Riedel, C<sri@oook.de>
660 Copyright 2004 Sebastian Riedel. All rights reserved.
662 This library is free software. You can redistribute it and/or modify
663 it under the same terms as perl itself.
669 BEGIN { $ENV{CATALYST_ENGINE} ||= 'Test' }
675 use lib "$FindBin::Bin/../lib";
680 GetOptions( 'help|?' => \$help );
682 pod2usage(1) if ( $help || !$ARGV[0] );
684 print [% name %]->run($ARGV[0])->content . "\n";
690 [% appprefix %]_test.pl - Catalyst Test
694 [% appprefix %]_test.pl [options] uri
697 -help display this help and exits
700 [% appprefix %]_test.pl http://localhost/some_action
701 [% appprefix %]_test.pl /some_action
704 perldoc Catalyst::Manual
705 perldoc Catalyst::Manual::Intro
709 Run a Catalyst action from the comand line.
713 Sebastian Riedel, C<sri@oook.de>
717 Copyright 2004 Sebastian Riedel. All rights reserved.
719 This library is free software. You can redistribute it and/or modify
720 it under the same terms as perl itself.
729 use Catalyst::Helper;
733 GetOptions( 'help|?' => \$help );
735 pod2usage(1) if ( $help || !$ARGV[0] );
737 my $helper = Catalyst::Helper->new;
738 pod2usage(1) unless $helper->mk_component( '[% name %]', @ARGV );
744 [% appprefix %]_create.pl - Create a new Catalyst Component
748 [% appprefix %]_create.pl [options] model|view|controller name [helper] [options]
751 -help display this help and exits
754 [% appprefix %]_create.pl controller My::Controller
755 [% appprefix %]_create.pl view My::View
756 [% appprefix %]_create.pl view MyView TT
757 [% appprefix %]_create.pl view TT TT
758 [% appprefix %]_create.pl model My::Model
759 [% appprefix %]_create.pl model SomeDB CDBI dbi:SQLite:/tmp/my.db
760 [% appprefix %]_create.pl model AnotherDB CDBI dbi:Pg:dbname=foo root 4321
763 perldoc Catalyst::Manual
764 perldoc Catalyst::Manual::Intro
768 Create a new Catalyst Component.
772 Sebastian Riedel, C<sri\@oook.de>
776 Copyright 2004 Sebastian Riedel. All rights reserved.
778 This library is free software. You can redistribute it and/or modify
779 it under the same terms as perl itself.
786 use base 'Catalyst::Base';
790 [% class %] - Catalyst component
808 sub default : Private {
809 my ( $self, $c ) = @_;
810 $c->res->output('Congratulations, [% class %] is on Catalyst!');
822 This library is free software . You can redistribute it and/or modify
823 it under the same terms as perl itself.
830 use Test::More tests => 3;
831 use_ok( Catalyst::Test, '[% app %]' );
832 use_ok('[% class %]');
834 ok( request('[% uri %]')->is_success );
836 use Test::More tests => 1;
837 use_ok('[% class %]');