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};
219 $prefix = lc $prefix;
220 $tname = $prefix . '.t';
221 $self->{prefix} = $prefix;
223 my $dir = $self->{test_dir};
224 my $type = lc $self->{type};
225 return File::Spec->catfile( $dir, $type, $tname );
230 Render and create a file from a template in DATA using
236 my ( $self, $file, $path, $vars ) = @_;
238 my $t = Template->new;
239 my $template = $self->get_file( ( caller(0) )[0], $file );
240 return 0 unless $template;
242 $t->process( \$template, { %{$self}, %$vars }, \$output );
243 $self->mk_file( $path, $output );
248 $self->mk_dir( $self->{dir} );
249 $self->{script} = File::Spec->catdir( $self->{dir}, 'script' );
250 $self->mk_dir( $self->{script} );
251 $self->{lib} = File::Spec->catdir( $self->{dir}, 'lib' );
252 $self->mk_dir( $self->{lib} );
253 $self->{root} = File::Spec->catdir( $self->{dir}, 'root' );
254 $self->mk_dir( $self->{root} );
255 $self->{t} = File::Spec->catdir( $self->{dir}, 't' );
256 $self->mk_dir( $self->{t} );
257 $self->mk_dir( File::Spec->catdir( $self->{t}, 'm' ) );
258 $self->mk_dir( File::Spec->catdir( $self->{t}, 'v' ) );
259 $self->mk_dir( File::Spec->catdir( $self->{t}, 'c' ) );
260 $self->{class} = File::Spec->catdir( split( /\:\:/, $self->{name} ) );
261 $self->{mod} = File::Spec->catdir( $self->{lib}, $self->{class} );
262 $self->mk_dir( $self->{mod} );
263 $self->{m} = File::Spec->catdir( $self->{mod}, 'M' );
264 $self->mk_dir( $self->{m} );
265 $self->{v} = File::Spec->catdir( $self->{mod}, 'V' );
266 $self->mk_dir( $self->{v} );
267 $self->{c} = File::Spec->catdir( $self->{mod}, 'C' );
268 $self->mk_dir( $self->{c} );
269 $self->{base} = File::Spec->rel2abs( $self->{dir} );
274 my $mod = $self->{mod};
275 $self->render_file( 'appclass', "$mod.pm" );
280 my $dir = $self->{dir};
281 $self->render_file( 'build', "$dir\/Build.PL" );
286 my $dir = $self->{dir};
287 $self->render_file( 'makefile', "$dir\/Makefile.PL" );
292 my $dir = $self->{dir};
293 $self->render_file( 'readme', "$dir\/README" );
298 my $dir = $self->{dir};
299 my $time = localtime time;
300 $self->render_file( 'changes', "$dir\/Changes", { time => $time } );
306 $self->render_file( 'apptest', "$t\/01app.t" );
307 $self->render_file( 'podtest', "$t\/02pod.t" );
308 $self->render_file( 'podcoveragetest', "$t\/03podcoverage.t" );
313 my $script = $self->{script};
314 my $appprefix = $self->{appprefix};
315 $self->render_file( 'cgi', "$script\/$appprefix\_cgi.pl" );
316 chmod 0700, "$script/$appprefix\_cgi.pl";
321 my $script = $self->{script};
322 my $appprefix = $self->{appprefix};
323 $self->render_file( 'fastcgi', "$script\/$appprefix\_fastcgi.pl" );
324 chmod 0700, "$script/$appprefix\_fastcgi.pl";
329 my $script = $self->{script};
330 my $appprefix = $self->{appprefix};
331 $self->render_file( 'server', "$script\/$appprefix\_server.pl" );
332 chmod 0700, "$script/$appprefix\_server.pl";
337 my $script = $self->{script};
338 my $appprefix = $self->{appprefix};
339 $self->render_file( 'test', "$script/$appprefix\_test.pl" );
340 chmod 0700, "$script/$appprefix\_test.pl";
345 my $script = $self->{script};
346 my $appprefix = $self->{appprefix};
347 $self->render_file( 'create', "$script\/$appprefix\_create.pl" );
348 chmod 0700, "$script/$appprefix\_create.pl";
353 my $file = $self->{file};
354 return $self->render_file( 'compclass', "$file" );
359 my $test = $self->{test};
360 $self->render_file( 'comptest', "$test" );
365 Helpers are classes that provide two methods.
367 * mk_compclass - creates the Component class
368 * mk_comptest - creates the Component test
370 So when you call C<bin/create view MyView TT>, create would try to execute
371 Catalyst::Helper::View::TT->mk_compclass and
372 Catalyst::Helper::View::TT->mk_comptest.
374 See L<Catalyst::Helper::View::TT> and L<Catalyst::Helper::Model::CDBI> for
377 All helper classes should be under one of the following namespaces.
379 Catalyst::Helper::Model::
380 Catalyst::Helper::View::
381 Catalyst::Helper::Controller::
385 The helpers will read author name from /etc/passwd by default.
386 To override, please export the AUTHOR variable.
390 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
391 L<Catalyst::Response>, L<Catalyst>
395 Sebastian Riedel, C<sri@oook.de>
399 This library is free software . You can redistribute it and/or modify
400 it under the same terms as perl itself.
411 use Catalyst qw/-Debug/;
413 our $VERSION = '0.01';
415 [% name %]->config( name => '[% name %]' );
421 [% name %] - Catalyst based application
425 script/[% appprefix %]_server.pl
429 Catalyst based application.
439 sub default : Private {
440 my ( $self, $c ) = @_;
441 $c->res->output('Congratulations, [% name %] is on Catalyst!');
452 This library is free software . You can redistribute it and/or modify
453 it under the same terms as perl itself.
459 unless ( eval "use Module::Build::Compat 0.02; 1" ) {
460 print "This module requires Module::Build to install itself.\n";
462 require ExtUtils::MakeMaker;
464 ExtUtils::MakeMaker::prompt( ' Install Module::Build now from CPAN?', 'y' );
466 unless ( $yn =~ /^y/i ) {
467 die " *** Cannot install without Module::Build. Exiting ...\n";
474 # Save this 'cause CPAN will chdir all over the place.
475 my $cwd = Cwd::cwd();
476 my $makefile = File::Spec->rel2abs($0);
478 CPAN::Shell->install('Module::Build::Compat')
479 or die " *** Cannot install without Module::Build. Exiting ...\n";
481 chdir $cwd or die "Cannot chdir() back to $cwd: $!";
483 eval "use Module::Build::Compat 0.02; 1" or die $@;
484 use lib '_build/lib';
485 Module::Build::Compat->run_build_pl( args => \@ARGV );
486 require Module::Build;
487 Module::Build::Compat->write_makefile( build_class => 'Module::Build' );
492 my $build = Catalyst::Build->new(
493 create_makefile_pl => 'passthrough',
495 module_name => '[% name %]',
496 requires => { Catalyst => '5.10' },
497 create_makefile_pl => 'passthrough',
498 script_files => [ glob('script/*') ],
499 test_files => [ glob('t/*.t'), glob('t/*/*.t') ]
501 $build->create_build_script;
503 Run script/[% apprefix %]_server.pl to test the application.
505 This file documents the revision history for Perl extension [% name %].
507 - initial revision, generated by Catalyst
509 use Test::More tests => 2;
510 use_ok( Catalyst::Test, '[% name %]' );
512 ok( request('/')->is_success );
516 eval "use Test::Pod 1.14";
517 plan skip_all => 'Test::Pod 1.14 required' if $@;
518 plan skip_all => 'set TEST_POD to enable this test' unless $ENV{TEST_POD};
524 eval "use Test::Pod::Coverage 1.04";
525 plan skip_all => 'Test::Pod::Coverage 1.04 required' if $@;
526 plan skip_all => 'set TEST_POD to enable this test' unless $ENV{TEST_POD};
528 all_pod_coverage_ok();
531 BEGIN { $ENV{CATALYST_ENGINE} ||= 'CGI' }
535 use lib "$FindBin::Bin/../lib";
548 See L<Catalyst::Manual>
552 Run a Catalyst application as cgi.
556 Sebastian Riedel, C<sri@oook.de>
560 Copyright 2004 Sebastian Riedel. All rights reserved.
562 This library is free software. You can redistribute it and/or modify
563 it under the same terms as perl itself.
569 BEGIN { $ENV{CATALYST_ENGINE} ||= 'FastCGI' }
573 use lib "$FindBin::Bin/../lib";
582 fastcgi - Catalyst FastCGI
586 See L<Catalyst::Manual>
590 Run a Catalyst application as fastcgi.
594 Sebastian Riedel, C<sri@oook.de>
598 Copyright 2004 Sebastian Riedel. All rights reserved.
600 This library is free software. You can redistribute it and/or modify
601 it under the same terms as perl itself.
608 $ENV{CATALYST_ENGINE} ||= 'HTTP';
609 $ENV{CATALYST_SCRIPT_GEN} = [% scriptgen %];
616 use lib "$FindBin::Bin/../lib";
622 GetOptions( 'help|?' => \$help, 'port=s' => \$port );
624 pod2usage(1) if $help;
626 [% name %]->run($port);
632 server - Catalyst Testserver
639 -? -help display this help and exits
640 -p -port port (defaults to 3000)
643 perldoc Catalyst::Manual
644 perldoc Catalyst::Manual::Intro
648 Run a Catalyst Testserver for this application.
652 Sebastian Riedel, C<sri@oook.de>
656 Copyright 2004 Sebastian Riedel. All rights reserved.
658 This library is free software. You can redistribute it and/or modify
659 it under the same terms as perl itself.
665 BEGIN { $ENV{CATALYST_ENGINE} ||= 'Test' }
671 use lib "$FindBin::Bin/../lib";
676 GetOptions( 'help|?' => \$help );
678 pod2usage(1) if ( $help || !$ARGV[0] );
680 print [% name %]->run($ARGV[0])->content . "\n";
690 test.pl [options] uri
693 -help display this help and exits
696 test.pl http://localhost/some_action
700 perldoc Catalyst::Manual
701 perldoc Catalyst::Manual::Intro
705 Run a Catalyst action from the comand line.
709 Sebastian Riedel, C<sri@oook.de>
713 Copyright 2004 Sebastian Riedel. All rights reserved.
715 This library is free software. You can redistribute it and/or modify
716 it under the same terms as perl itself.
725 use Catalyst::Helper;
729 GetOptions( 'help|?' => \$help );
731 pod2usage(1) if ( $help || !$ARGV[0] );
733 my $helper = Catalyst::Helper->new;
734 pod2usage(1) unless $helper->mk_component( '[% name %]', @ARGV );
740 create - Create a new Catalyst Component
744 create.pl [options] model|view|controller name [helper] [options]
747 -help display this help and exits
750 create.pl controller My::Controller
751 create.pl view My::View
752 create.pl view MyView TT
754 create.pl model My::Model
755 create.pl model SomeDB CDBI dbi:SQLite:/tmp/my.db
756 create.pl model AnotherDB CDBI dbi:Pg:dbname=foo root 4321
759 perldoc Catalyst::Manual
760 perldoc Catalyst::Manual::Intro
764 Create a new Catalyst Component.
768 Sebastian Riedel, C<sri\@oook.de>
772 Copyright 2004 Sebastian Riedel. All rights reserved.
774 This library is free software. You can redistribute it and/or modify
775 it under the same terms as perl itself.
782 use base 'Catalyst::Base';
786 [% class %] - Catalyst component
804 sub default : Private {
805 my ( $self, $c ) = @_;
806 $c->res->output('Congratulations, [% class %] is on Catalyst!');
818 This library is free software . You can redistribute it and/or modify
819 it under the same terms as perl itself.
826 use Test::More tests => 3;
827 use_ok( Catalyst::Test, '[% app %]' );
828 use_ok('[% class %]');
830 ok( request('[% prefix %]')->is_success );
832 use Test::More tests => 1;
833 use_ok('[% class %]');