1 package Catalyst::Helper;
4 use base 'Class::Accessor::Fast';
12 use Catalyst::Exception;
18 Catalyst::Helper - Bootstrap a Catalyst application
22 See L<Catalyst::Manual::Intro>
26 Bootstrap a Catalyst application. Autogenerates scripts
37 my ( $self, $class, $file ) = @_;
38 unless ( $cache{$class} ) {
40 $cache{$class} = eval "package $class; <DATA>";
42 my $data = $cache{$class};
43 my @files = split /^__(.+)__\r?\n/m, $data;
46 my ( $name, $content ) = splice @files, 0, 2;
47 return $content if $name eq $file;
54 Create the main application skeleton.
59 my ( $self, $name ) = @_;
60 return 0 if $name =~ /[^\w\:]/;
61 $self->{name} = $name;
63 $self->{dir} =~ s/\:\:/-/g;
64 $self->{appprefix} = lc $self->{dir};
65 $self->{appprefix} =~ s/-/_/g;
66 $self->{startperl} = $Config{startperl};
67 $self->{scriptgen} = $Catalyst::CATALYST_SCRIPT_GEN || 4;
68 $self->{author} = $self->{author} = $ENV{'AUTHOR'}
69 || eval { @{ [ getpwuid($<) ] }[6] }
70 || 'Catalyst developer';
88 This method is called by create.pl to make new components
97 $self->{author} = $self->{author} = $ENV{'AUTHOR'}
98 || eval { @{ [ getpwuid($<) ] }[6] }
100 $self->{base} = File::Spec->catdir( $FindBin::Bin, '..' );
101 unless ( $_[0] =~ /^(?:model|m|view|v|controller|c)$/i ) {
104 my $class = "Catalyst::Helper::$helper";
105 eval "require $class";
108 Catalyst::Exception->throw(
109 message => qq/Couldn't load helper "$class", "$@"/ );
112 if ( $class->can('mk_stuff') ) {
113 return 1 unless $class->mk_stuff( $self, @args );
118 my $name = shift || "Missing name for model/view/controller";
121 return 0 if $name =~ /[^\w\:]/;
122 $type = 'M' if $type =~ /model|m/i;
123 $type = 'V' if $type =~ /view|v/i;
124 $type = 'C' if $type =~ /controller|c/i;
125 $self->{type} = $type;
126 $self->{name} = $name;
127 $self->{class} = "$app\::$type\::$name";
130 my $appdir = File::Spec->catdir( split /\:\:/, $app );
132 File::Spec->catdir( $FindBin::Bin, '..', 'lib', $appdir, $type );
134 if ( $name =~ /\:/ ) {
135 my @path = split /\:\:/, $name;
137 $path = File::Spec->catdir( $path, @path );
140 $file = File::Spec->catfile( $path, "$file.pm" );
141 $self->{file} = $file;
144 $self->{test_dir} = File::Spec->catdir( $FindBin::Bin, '..', 't' );
145 $self->{test} = $self->next_test;
150 $comp = 'View' if $type eq 'V';
151 $comp = 'Controller' if $type eq 'C';
152 my $class = "Catalyst::Helper::$comp\::$helper";
153 eval "require $class";
156 Catalyst::Exception->throw(
157 message => qq/Couldn't load helper "$class", "$@"/ );
160 if ( $class->can('mk_compclass') ) {
161 return 1 unless $class->mk_compclass( $self, @args );
163 else { return 1 unless $self->_mk_compclass }
165 if ( $class->can('mk_comptest') ) {
166 $class->mk_comptest( $self, @args );
168 else { $self->_mk_comptest }
173 return 1 unless $self->_mk_compclass;
182 Surprisingly, this function makes a directory.
187 my ( $self, $dir ) = @_;
189 print qq/ exists "$dir"\n/;
192 if ( mkpath [$dir] ) {
193 print qq/created "$dir"\n/;
197 Catalyst::Exception->throw( message => qq/Couldn't create "$dir", "$!"/ );
202 writes content to a file.
207 my ( $self, $file, $content ) = @_;
209 print qq/ exists "$file"\n/;
210 return 0 unless $self->{'.newfiles'};
211 if ( my $f = IO::File->new("< $file") ) {
212 my $oldcontent = join( '', (<$f>) );
213 return 0 if $content eq $oldcontent;
217 if ( my $f = IO::File->new("> $file") ) {
219 print qq/created "$file"\n/;
223 Catalyst::Exception->throw( message => qq/Couldn't create "$file", "$!"/ );
231 my ( $self, $tname ) = @_;
232 if ($tname) { $tname = "$tname.t" }
234 my $name = $self->{name};
238 $tname = $prefix . '.t';
239 $self->{prefix} = $prefix;
240 $prefix = lc $prefix;
242 $self->{uri} = $prefix;
244 my $dir = $self->{test_dir};
245 my $type = $self->{type};
246 return File::Spec->catfile( $dir, $type, $tname );
251 Render and create a file from a template in DATA using
257 my ( $self, $file, $path, $vars ) = @_;
259 my $t = Template->new;
260 my $template = $self->get_file( ( caller(0) )[0], $file );
261 return 0 unless $template;
263 $t->process( \$template, { %{$self}, %$vars }, \$output )
264 || Catalyst::Exception->throw(
265 message => qq/Couldn't process "$file", / . $t->error() );
266 $self->mk_file( $path, $output );
271 $self->mk_dir( $self->{dir} );
272 $self->{script} = File::Spec->catdir( $self->{dir}, 'script' );
273 $self->mk_dir( $self->{script} );
274 $self->{lib} = File::Spec->catdir( $self->{dir}, 'lib' );
275 $self->mk_dir( $self->{lib} );
276 $self->{root} = File::Spec->catdir( $self->{dir}, 'root' );
277 $self->mk_dir( $self->{root} );
278 $self->{t} = File::Spec->catdir( $self->{dir}, 't' );
279 $self->mk_dir( $self->{t} );
280 $self->mk_dir( File::Spec->catdir( $self->{t}, 'M' ) );
281 $self->mk_dir( File::Spec->catdir( $self->{t}, 'V' ) );
282 $self->mk_dir( File::Spec->catdir( $self->{t}, 'C' ) );
283 $self->{class} = File::Spec->catdir( split( /\:\:/, $self->{name} ) );
284 $self->{mod} = File::Spec->catdir( $self->{lib}, $self->{class} );
285 $self->mk_dir( $self->{mod} );
286 $self->{m} = File::Spec->catdir( $self->{mod}, 'M' );
287 $self->mk_dir( $self->{m} );
288 $self->{v} = File::Spec->catdir( $self->{mod}, 'V' );
289 $self->mk_dir( $self->{v} );
290 $self->{c} = File::Spec->catdir( $self->{mod}, 'C' );
291 $self->mk_dir( $self->{c} );
292 $self->{base} = File::Spec->rel2abs( $self->{dir} );
297 my $mod = $self->{mod};
298 $self->render_file( 'appclass', "$mod.pm" );
303 my $dir = $self->{dir};
304 $self->render_file( 'build', "$dir\/Build.PL" );
309 my $dir = $self->{dir};
310 $self->render_file( 'makefile', "$dir\/Makefile.PL" );
315 my $dir = $self->{dir};
316 $self->render_file( 'readme', "$dir\/README" );
321 my $dir = $self->{dir};
322 my $time = localtime time;
323 $self->render_file( 'changes', "$dir\/Changes", { time => $time } );
329 $self->render_file( 'apptest', "$t\/01app.t" );
330 $self->render_file( 'podtest', "$t\/02pod.t" );
331 $self->render_file( 'podcoveragetest', "$t\/03podcoverage.t" );
336 my $script = $self->{script};
337 my $appprefix = $self->{appprefix};
338 $self->render_file( 'cgi', "$script\/$appprefix\_cgi.pl" );
339 chmod 0700, "$script/$appprefix\_cgi.pl";
344 my $script = $self->{script};
345 my $appprefix = $self->{appprefix};
346 $self->render_file( 'fastcgi', "$script\/$appprefix\_fastcgi.pl" );
347 chmod 0700, "$script/$appprefix\_fastcgi.pl";
352 my $script = $self->{script};
353 my $appprefix = $self->{appprefix};
354 $self->render_file( 'server', "$script\/$appprefix\_server.pl" );
355 chmod 0700, "$script/$appprefix\_server.pl";
360 my $script = $self->{script};
361 my $appprefix = $self->{appprefix};
362 $self->render_file( 'test', "$script/$appprefix\_test.pl" );
363 chmod 0700, "$script/$appprefix\_test.pl";
368 my $script = $self->{script};
369 my $appprefix = $self->{appprefix};
370 $self->render_file( 'create', "$script\/$appprefix\_create.pl" );
371 chmod 0700, "$script/$appprefix\_create.pl";
376 my $file = $self->{file};
377 return $self->render_file( 'compclass', "$file" );
382 my $test = $self->{test};
383 $self->render_file( 'comptest', "$test" );
388 Helpers are classes that provide two methods.
390 * mk_compclass - creates the Component class
391 * mk_comptest - creates the Component test
393 So when you call C<bin/create view MyView TT>, create would try to execute
394 Catalyst::Helper::View::TT->mk_compclass and
395 Catalyst::Helper::View::TT->mk_comptest.
397 See L<Catalyst::Helper::View::TT> and L<Catalyst::Helper::Model::CDBI> for
400 All helper classes should be under one of the following namespaces.
402 Catalyst::Helper::Model::
403 Catalyst::Helper::View::
404 Catalyst::Helper::Controller::
408 The helpers will read author name from /etc/passwd by default.
409 To override, please export the AUTHOR variable.
413 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
414 L<Catalyst::Response>, L<Catalyst>
418 Sebastian Riedel, C<sri@oook.de>
422 This library is free software . You can redistribute it and/or modify
423 it under the same terms as perl itself.
434 use Catalyst qw/-Debug/;
436 our $VERSION = '0.01';
438 [% name %]->config( name => '[% name %]' );
444 [% name %] - Catalyst based application
448 script/[% appprefix %]_server.pl
452 Catalyst based application.
462 sub default : Private {
463 my ( $self, $c ) = @_;
466 $c->response->output('Congratulations, [% name %] is on Catalyst!');
474 # my ( $self, $c ) = @_;
476 # # Forward to View unless response body is already defined
477 # $c->forward('MyApp::V::') unless $c->response->body;
488 This library is free software . You can redistribute it and/or modify
489 it under the same terms as perl itself.
495 unless ( eval "use Module::Build::Compat 0.02; 1" ) {
496 print "This module requires Module::Build to install itself.\n";
498 require ExtUtils::MakeMaker;
500 ExtUtils::MakeMaker::prompt( ' Install Module::Build now from CPAN?', 'y' );
502 unless ( $yn =~ /^y/i ) {
503 die " *** Cannot install without Module::Build. Exiting ...\n";
510 # Save this 'cause CPAN will chdir all over the place.
511 my $cwd = Cwd::cwd();
512 my $makefile = File::Spec->rel2abs($0);
514 CPAN::Shell->install('Module::Build::Compat')
515 or die " *** Cannot install without Module::Build. Exiting ...\n";
517 chdir $cwd or die "Cannot chdir() back to $cwd: $!";
519 eval "use Module::Build::Compat 0.02; 1" or die $@;
520 use lib '_build/lib';
521 Module::Build::Compat->run_build_pl( args => \@ARGV );
522 require Module::Build;
523 Module::Build::Compat->write_makefile( build_class => 'Module::Build' );
528 my $build = Catalyst::Build->new(
529 create_makefile_pl => 'passthrough',
531 module_name => '[% name %]',
532 requires => { Catalyst => '5.10' },
533 create_makefile_pl => 'passthrough',
534 script_files => [ glob('script/*') ],
535 test_files => [ glob('t/*.t'), glob('t/*/*.t') ]
537 $build->create_build_script;
539 Run script/[% appprefix %]_server.pl to test the application.
541 This file documents the revision history for Perl extension [% name %].
543 - initial revision, generated by Catalyst
545 use Test::More tests => 2;
546 use_ok( Catalyst::Test, '[% name %]' );
548 ok( request('/')->is_success );
552 eval "use Test::Pod 1.14";
553 plan skip_all => 'Test::Pod 1.14 required' if $@;
554 plan skip_all => 'set TEST_POD to enable this test' unless $ENV{TEST_POD};
560 eval "use Test::Pod::Coverage 1.04";
561 plan skip_all => 'Test::Pod::Coverage 1.04 required' if $@;
562 plan skip_all => 'set TEST_POD to enable this test' unless $ENV{TEST_POD};
564 all_pod_coverage_ok();
568 BEGIN { $ENV{CATALYST_ENGINE} ||= 'CGI' }
572 use lib "$FindBin::Bin/../lib";
581 [% appprefix %]_cgi.pl - Catalyst CGI
585 See L<Catalyst::Manual>
589 Run a Catalyst application as cgi.
593 Sebastian Riedel, C<sri@oook.de>
597 Copyright 2004 Sebastian Riedel. All rights reserved.
599 This library is free software. You can redistribute it and/or modify
600 it under the same terms as perl itself.
606 BEGIN { $ENV{CATALYST_ENGINE} ||= 'FastCGI' }
610 use lib "$FindBin::Bin/../lib";
619 [% appprefix %]_fastcgi.pl - Catalyst FastCGI
623 See L<Catalyst::Manual>
627 Run a Catalyst application as fastcgi.
631 Sebastian Riedel, C<sri@oook.de>
635 Copyright 2004 Sebastian Riedel. All rights reserved.
637 This library is free software. You can redistribute it and/or modify
638 it under the same terms as perl itself.
645 $ENV{CATALYST_ENGINE} ||= 'HTTP';
646 $ENV{CATALYST_SCRIPT_GEN} = [% scriptgen %];
653 use lib "$FindBin::Bin/../lib";
661 my $restart_delay = 1;
662 my $restart_regex = '\.yml$|\.yaml$|\.pm$';
671 'restart|r' => \$restart,
672 'restartdelay|rd=s' => \$restart_delay,
673 'restartregex|rr=s' => \$restart_regex
676 pod2usage(1) if $help;
678 [% name %]->run( $port, $host, {
682 restart_delay => $restart_delay,
683 restart_regex => qr/$restart_regex/
690 [% appprefix %]_server.pl - Catalyst Testserver
694 [% appprefix %]_server.pl [options]
697 -f -fork handle each request in a new process
699 -? -help display this help and exits
700 -host host (defaults to all)
701 -p -port port (defaults to 3000)
702 -r -restart restart when files got modified
704 -rd -restartdelay delay between file checks
705 -rr -restartregex regex match files that trigger
706 a restart when modified
707 (defaults to '\.yml$|\.yaml$|\.pm$')
710 perldoc Catalyst::Manual
711 perldoc Catalyst::Manual::Intro
715 Run a Catalyst Testserver for this application.
719 Sebastian Riedel, C<sri@oook.de>
723 Copyright 2004 Sebastian Riedel. All rights reserved.
725 This library is free software. You can redistribute it and/or modify
726 it under the same terms as perl itself.
732 BEGIN { $ENV{CATALYST_ENGINE} ||= 'Test' }
738 use lib "$FindBin::Bin/../lib";
743 GetOptions( 'help|?' => \$help );
745 pod2usage(1) if ( $help || !$ARGV[0] );
747 print [% name %]->run($ARGV[0])->content . "\n";
753 [% appprefix %]_test.pl - Catalyst Test
757 [% appprefix %]_test.pl [options] uri
760 -help display this help and exits
763 [% appprefix %]_test.pl http://localhost/some_action
764 [% appprefix %]_test.pl /some_action
767 perldoc Catalyst::Manual
768 perldoc Catalyst::Manual::Intro
772 Run a Catalyst action from the command line.
776 Sebastian Riedel, C<sri@oook.de>
780 Copyright 2004 Sebastian Riedel. All rights reserved.
782 This library is free software. You can redistribute it and/or modify
783 it under the same terms as perl itself.
792 use Catalyst::Helper;
797 GetOptions( 'help|?' => \$help,
798 'nonew' => \$nonew );
800 pod2usage(1) if ( $help || !$ARGV[0] );
802 my $helper = Catalyst::Helper->new({'.newfiles' => !$nonew});
803 pod2usage(1) unless $helper->mk_component( '[% name %]', @ARGV );
809 [% appprefix %]_create.pl - Create a new Catalyst Component
813 [% appprefix %]_create.pl [options] model|view|controller name [helper] [options]
816 -help display this help and exits
817 -nonew don't create a .new file where a file to be created exists
820 [% appprefix %]_create.pl controller My::Controller
821 [% appprefix %]_create.pl view My::View
822 [% appprefix %]_create.pl view MyView TT
823 [% appprefix %]_create.pl view TT TT
824 [% appprefix %]_create.pl model My::Model
825 [% appprefix %]_create.pl model SomeDB CDBI dbi:SQLite:/tmp/my.db
826 [% appprefix %]_create.pl model AnotherDB CDBI dbi:Pg:dbname=foo root 4321
829 perldoc Catalyst::Manual
830 perldoc Catalyst::Manual::Intro
834 Create a new Catalyst Component.
836 Existing component files are not overwritten. If any of the component files
837 to be created already exist the file will be written with a '.new' suffix.
838 This behavior can be suppressed with the C<-nonew> option.
842 Sebastian Riedel, C<sri\@oook.de>
846 Copyright 2004 Sebastian Riedel. All rights reserved.
848 This library is free software. You can redistribute it and/or modify
849 it under the same terms as perl itself.
856 use base 'Catalyst::Base';
860 [% class %] - Catalyst component
878 sub default : Private {
879 my ( $self, $c ) = @_;
882 $c->response->output('Congratulations, [% class %] is on Catalyst!');
894 This library is free software . You can redistribute it and/or modify
895 it under the same terms as perl itself.
902 use Test::More tests => 3;
903 use_ok( Catalyst::Test, '[% app %]' );
904 use_ok('[% class %]');
906 ok( request('[% uri %]')->is_success );
908 use Test::More tests => 1;
909 use_ok('[% class %]');