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] } || 'A clever guy';
86 This method is called by create.pl to make new components
95 $self->{author} = $self->{author} = $ENV{'AUTHOR'}
96 || eval { @{ [ getpwuid($<) ] }[6] } || 'A clever guy';
97 $self->{base} = File::Spec->catdir( $FindBin::Bin, '..' );
98 unless ( $_[0] =~ /^model|m|view|v|controller|c\$/i ) {
101 my $class = "Catalyst::Helper::$helper";
102 eval "require $class";
103 die qq/Couldn't load helper "$class", "$@"/ if $@;
104 if ( $class->can('mk_stuff') ) {
105 return 1 unless $class->mk_stuff( $self, @args );
110 my $name = shift || "Missing name for model/view/controller";
113 return 0 if $name =~ /[^\w\:]/;
114 $type = 'M' if $type =~ /model|m/i;
115 $type = 'V' if $type =~ /view|v/i;
116 $type = 'C' if $type =~ /controller|c/i;
117 $self->{type} = $type;
118 $self->{name} = $name;
119 $self->{class} = "$app\::$type\::$name";
122 my $appdir = File::Spec->catdir( split /\:\:/, $app );
124 File::Spec->catdir( $FindBin::Bin, '..', 'lib', $appdir, $type );
126 if ( $name =~ /\:/ ) {
127 my @path = split /\:\:/, $name;
129 $path = File::Spec->catdir( $path, @path );
132 $file = File::Spec->catfile( $path, "$file.pm" );
133 $self->{file} = $file;
136 $self->{test_dir} = File::Spec->catdir( $FindBin::Bin, '..', 't' );
137 $self->{test} = $self->next_test;
142 $comp = 'View' if $type eq 'V';
143 $comp = 'Controller' if $type eq 'C';
144 my $class = "Catalyst::Helper::$comp\::$helper";
145 eval "require $class";
146 die qq/Couldn't load helper "$class", "$@"/ if $@;
147 if ( $class->can('mk_compclass') ) {
148 return 1 unless $class->mk_compclass( $self, @args );
150 else { return 1 unless $self->_mk_compclass }
152 if ( $class->can('mk_comptest') ) {
153 $class->mk_comptest( $self, @args );
155 else { $self->_mk_comptest }
160 return 1 unless $self->_mk_compclass;
169 Surprisingly, this function makes a directory.
174 my ( $self, $dir ) = @_;
176 print qq/ exists "$dir"\n/;
180 print qq/created "$dir"\n/;
183 die qq/Couldn't create "$dir", "$!"/;
188 writes content to a file.
193 my ( $self, $file, $content ) = @_;
195 print qq/ exists "$file"\n/;
198 if ( my $f = IO::File->new("> $file") ) {
200 print qq/created "$file"\n/;
203 die qq/Couldn't create "$file", "$!"/;
211 my ( $self, $tname ) = @_;
212 if ($tname) { $tname = "$tname.t" }
214 my $name = $self->{name};
217 $prefix = lc $prefix;
218 $tname = $prefix . '.t';
219 $self->{prefix} = $prefix;
221 my $dir = $self->{test_dir};
222 my $type = lc $self->{type};
223 return File::Spec->catfile( $dir, $type, $tname );
228 Render and create a file from a template in DATA using
234 my ( $self, $file, $path, $vars ) = @_;
236 my $t = Template->new;
237 my $template = $self->get_file( ( caller(0) )[0], $file );
238 return 0 unless $template;
240 $t->process( \$template, { %{$self}, %$vars }, \$output );
241 $self->mk_file( $path, $output );
246 $self->mk_dir( $self->{dir} );
247 $self->{script} = File::Spec->catdir( $self->{dir}, 'script' );
248 $self->mk_dir( $self->{script} );
249 $self->{lib} = File::Spec->catdir( $self->{dir}, 'lib' );
250 $self->mk_dir( $self->{lib} );
251 $self->{root} = File::Spec->catdir( $self->{dir}, 'root' );
252 $self->mk_dir( $self->{root} );
253 $self->{t} = File::Spec->catdir( $self->{dir}, 't' );
254 $self->mk_dir( $self->{t} );
255 $self->mk_dir( File::Spec->catdir( $self->{t}, 'm' ) );
256 $self->mk_dir( File::Spec->catdir( $self->{t}, 'v' ) );
257 $self->mk_dir( File::Spec->catdir( $self->{t}, 'c' ) );
258 $self->{class} = File::Spec->catdir( split( /\:\:/, $self->{name} ) );
259 $self->{mod} = File::Spec->catdir( $self->{lib}, $self->{class} );
260 $self->mk_dir( $self->{mod} );
261 $self->{m} = File::Spec->catdir( $self->{mod}, 'M' );
262 $self->mk_dir( $self->{m} );
263 $self->{v} = File::Spec->catdir( $self->{mod}, 'V' );
264 $self->mk_dir( $self->{v} );
265 $self->{c} = File::Spec->catdir( $self->{mod}, 'C' );
266 $self->mk_dir( $self->{c} );
267 $self->{base} = File::Spec->rel2abs( $self->{dir} );
272 my $mod = $self->{mod};
273 $self->render_file( 'appclass', "$mod.pm" );
278 my $dir = $self->{dir};
279 $self->render_file( 'build', "$dir\/Build.PL" );
284 my $dir = $self->{dir};
285 $self->render_file( 'makefile', "$dir\/Makefile.PL" );
290 my $dir = $self->{dir};
291 $self->render_file( 'readme', "$dir\/README" );
296 my $dir = $self->{dir};
297 my $time = localtime time;
298 $self->render_file( 'changes', "$dir\/Changes", { time => $time } );
304 $self->render_file( 'apptest', "$t\/01app.t" );
305 $self->render_file( 'podtest', "$t\/02pod.t" );
306 $self->render_file( 'podcoveragetest', "$t\/03podcoverage.t" );
311 my $script = $self->{script};
312 my $appprefix = $self->{appprefix};
313 $self->render_file( 'cgi', "$script\/$appprefix\_cgi.pl" );
314 chmod 0700, "$script/$appprefix\_cgi.pl";
319 my $script = $self->{script};
320 my $appprefix = $self->{appprefix};
321 $self->render_file( 'fcgi', "$script\/$appprefix\_fcgi.pl" );
322 chmod 0700, "$script/$appprefix\_fcgi.pl";
327 my $script = $self->{script};
328 my $appprefix = $self->{appprefix};
329 $self->render_file( 'server', "$script\/$appprefix\_server.pl" );
330 chmod 0700, "$script/$appprefix\_server.pl";
335 my $script = $self->{script};
336 my $appprefix = $self->{appprefix};
337 $self->render_file( 'test', "$script/$appprefix\_test.pl" );
338 chmod 0700, "$script/$appprefix\_test.pl";
343 my $script = $self->{script};
344 my $appprefix = $self->{appprefix};
345 $self->render_file( 'create', "$script\/$appprefix\_create.pl" );
346 chmod 0700, "$script/$appprefix\_create.pl";
351 my $file = $self->{file};
352 return $self->render_file( 'compclass', "$file" );
357 my $test = $self->{test};
358 $self->render_file( 'comptest', "$test" );
363 Helpers are classes that provide two methods.
365 * mk_compclass - creates the Component class
366 * mk_comptest - creates the Component test
368 So when you call C<bin/create view MyView TT>, create would try to execute
369 Catalyst::Helper::View::TT->mk_compclass and
370 Catalyst::Helper::View::TT->mk_comptest.
372 See L<Catalyst::Helper::View::TT> and L<Catalyst::Helper::Model::CDBI> for
375 All helper classes should be under one of the following namespaces.
377 Catalyst::Helper::Model::
378 Catalyst::Helper::View::
379 Catalyst::Helper::Controller::
383 The helpers will read author name from /etc/passwd by default.
384 To override, please export the AUTHOR variable.
388 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
389 L<Catalyst::Response>, L<Catalyst>
393 Sebastian Riedel, C<sri@oook.de>
397 This library is free software . You can redistribute it and/or modify
398 it under the same terms as perl itself.
409 use Catalyst qw/-Debug/;
411 our $VERSION = '0.01';
413 [% name %]->config( name => '[% name %]' );
417 sub default : Private {
418 my ( $self, $c ) = @_;
419 $c->res->output('Congratulations, [% name %] is on Catalyst!');
424 [% name %] - A very nice application
432 Very nice application.
440 This library is free software . You can redistribute it and/or modify
441 it under the same terms as perl itself.
448 unless ( eval "use Module::Build::Compat 0.02; 1" ) {
449 print "This module requires Module::Build to install itself.\n";
451 require ExtUtils::MakeMaker;
453 ExtUtils::MakeMaker::prompt( ' Install Module::Build now from CPAN?', 'y' );
455 unless ( $yn =~ /^y/i ) {
456 die " *** Cannot install without Module::Build. Exiting ...\n";
463 # Save this 'cause CPAN will chdir all over the place.
464 my $cwd = Cwd::cwd();
465 my $makefile = File::Spec->rel2abs($0);
467 CPAN::Shell->install('Module::Build::Compat')
468 or die " *** Cannot install without Module::Build. Exiting ...\n";
470 chdir $cwd or die "Cannot chdir() back to $cwd: $!";
472 eval "use Module::Build::Compat 0.02; 1" or die $@;
473 use lib '_build/lib';
474 Module::Build::Compat->run_build_pl( args => \@ARGV );
475 require Module::Build;
476 Module::Build::Compat->write_makefile( build_class => 'Module::Build' );
482 my $build = Catalyst::Build->new(
483 create_makefile_pl => 'passthrough',
485 module_name => '[% name %]',
486 requires => { Catalyst => '5.10' },
487 create_makefile_pl => 'passthrough',
488 script_files => [ glob('script/*') ],
489 test_files => [ glob('t/*.t'), glob('t/*/*.t') ]
491 $build->create_build_script;
494 Run script/[% apprefix %]_server.pl to test the application.
497 This file documents the revision history for Perl extension [% name %].
500 - initial revision, generated by Catalyst
503 use Test::More tests => 2;
504 use_ok( Catalyst::Test, '[% name %]' );
506 ok( request('/')->is_success );
511 eval "use Test::Pod 1.14";
512 plan skip_all => 'Test::Pod 1.14 required' if $@;
513 plan skip_all => 'set TEST_POD to enable this test' unless $ENV{TEST_POD};
520 eval "use Test::Pod::Coverage 1.04";
521 plan skip_all => 'Test::Pod::Coverage 1.04 required' if $@;
522 plan skip_all => 'set TEST_POD to enable this test' unless $ENV{TEST_POD};
524 all_pod_coverage_ok();
528 BEGIN { $ENV{CATALYST_ENGINE} = 'CGI' }
532 use lib "$FindBin::Bin/../lib";
545 See L<Catalyst::Manual>
549 Run a Catalyst application as cgi.
553 Sebastian Riedel, C<sri@oook.de>
557 Copyright 2004 Sebastian Riedel. All rights reserved.
559 This library is free software. You can redistribute it and/or modify
560 it under the same terms as perl itself.
567 BEGIN { $ENV{CATALYST_ENGINE} = 'FCGI' }
571 use lib "$FindBin::Bin/../lib";
584 See L<Catalyst::Manual>
588 Run a Catalyst application as fcgi.
592 Sebastian Riedel, C<sri@oook.de>
596 Copyright 2004 Sebastian Riedel. All rights reserved.
598 This library is free software. You can redistribute it and/or modify
599 it under the same terms as perl itself.
607 $ENV{CATALYST_ENGINE} = 'HTTP';
608 $ENV{CATALYST_SCRIPT_GEN} = [% scriptgen %];
615 use lib "$FindBin::Bin/../lib";
621 GetOptions( 'help|?' => \$help, 'port=s' => \$port );
623 pod2usage(1) if $help;
625 [% name %]->run($port);
631 server - Catalyst Testserver
638 -? -help display this help and exits
639 -p -port port (defaults to 3000)
642 perldoc Catalyst::Manual
643 perldoc Catalyst::Manual::Intro
647 Run a Catalyst Testserver for this application.
651 Sebastian Riedel, C<sri@oook.de>
655 Copyright 2004 Sebastian Riedel. All rights reserved.
657 This library is free software. You can redistribute it and/or modify
658 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.
726 use Catalyst::Helper;
730 GetOptions( 'help|?' => \$help );
732 pod2usage(1) if ( $help || !$ARGV[0] );
734 my $helper = Catalyst::Helper->new;
735 pod2usage(1) unless $helper->mk_component( '[% name %]', @ARGV );
741 create - Create a new Catalyst Component
745 create.pl [options] model|view|controller name [helper] [options]
748 -help display this help and exits
751 create.pl controller My::Controller
752 create.pl view My::View
753 create.pl view MyView TT
755 create.pl model My::Model
756 create.pl model SomeDB CDBI dbi:SQLite:/tmp/my.db
757 create.pl model AnotherDB CDBI dbi:Pg:dbname=foo root 4321
761 perldoc Catalyst::Manual
762 perldoc Catalyst::Manual::Intro
766 Create a new Catalyst Component.
770 Sebastian Riedel, C<sri\@oook.de>
774 Copyright 2004 Sebastian Riedel. All rights reserved.
776 This library is free software. You can redistribute it and/or modify
777 it under the same terms as perl itself.
785 use base 'Catalyst::Base';
788 sub default : Private {
789 my ( $self, $c ) = @_;
790 $c->res->output('Congratulations, [% class %] is on Catalyst!');
796 [% class %] - A Component
812 This library is free software . You can redistribute it and/or modify
813 it under the same terms as perl itself.
821 use Test::More tests => 3;
822 use_ok( Catalyst::Test, '[% app %]' );
823 use_ok('[% class %]');
825 ok( request('[% prefix %]')->is_success );
827 use Test::More tests => 1;
828 use_ok('[% class %]');