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 /^__(.+)__\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", "$@"/
113 if ( $class->can('mk_stuff') ) {
114 return 1 unless $class->mk_stuff( $self, @args );
119 my $name = shift || "Missing name for model/view/controller";
122 return 0 if $name =~ /[^\w\:]/;
123 $type = 'M' if $type =~ /model|m/i;
124 $type = 'V' if $type =~ /view|v/i;
125 $type = 'C' if $type =~ /controller|c/i;
126 $self->{type} = $type;
127 $self->{name} = $name;
128 $self->{class} = "$app\::$type\::$name";
131 my $appdir = File::Spec->catdir( split /\:\:/, $app );
133 File::Spec->catdir( $FindBin::Bin, '..', 'lib', $appdir, $type );
135 if ( $name =~ /\:/ ) {
136 my @path = split /\:\:/, $name;
138 $path = File::Spec->catdir( $path, @path );
141 $file = File::Spec->catfile( $path, "$file.pm" );
142 $self->{file} = $file;
145 $self->{test_dir} = File::Spec->catdir( $FindBin::Bin, '..', 't' );
146 $self->{test} = $self->next_test;
151 $comp = 'View' if $type eq 'V';
152 $comp = 'Controller' if $type eq 'C';
153 my $class = "Catalyst::Helper::$comp\::$helper";
154 eval "require $class";
157 Catalyst::Exception->throw(
158 message => qq/Couldn't load helper "$class", "$@"/
162 if ( $class->can('mk_compclass') ) {
163 return 1 unless $class->mk_compclass( $self, @args );
165 else { return 1 unless $self->_mk_compclass }
167 if ( $class->can('mk_comptest') ) {
168 $class->mk_comptest( $self, @args );
170 else { $self->_mk_comptest }
175 return 1 unless $self->_mk_compclass;
184 Surprisingly, this function makes a directory.
189 my ( $self, $dir ) = @_;
191 print qq/ exists "$dir"\n/;
194 if ( mkpath [$dir] ) {
195 print qq/created "$dir"\n/;
199 Catalyst::Exception->throw(
200 message => qq/Couldn't create "$dir", "$!"/
206 writes content to a file.
211 my ( $self, $file, $content ) = @_;
213 print qq/ exists "$file"\n/;
214 return 0 unless $self->{'.newfiles'};
215 if ( my $f = IO::File->new("< $file") ) {
216 my $oldcontent = join('', (<$f>));
217 return 0 if $content eq $oldcontent;
221 if ( my $f = IO::File->new("> $file") ) {
223 print qq/created "$file"\n/;
227 Catalyst::Exception->throw(
228 message => qq/Couldn't create "$file", "$!"/
237 my ( $self, $tname ) = @_;
238 if ($tname) { $tname = "$tname.t" }
240 my $name = $self->{name};
244 $tname = $prefix . '.t';
245 $self->{prefix} = $prefix;
246 $prefix = lc $prefix;
248 $self->{uri} = $prefix;
250 my $dir = $self->{test_dir};
251 my $type = $self->{type};
252 return File::Spec->catfile( $dir, $type, $tname );
257 Render and create a file from a template in DATA using
263 my ( $self, $file, $path, $vars ) = @_;
265 my $t = Template->new;
266 my $template = $self->get_file( ( caller(0) )[0], $file );
267 return 0 unless $template;
269 $t->process( \$template, { %{$self}, %$vars }, \$output );
270 $self->mk_file( $path, $output );
275 $self->mk_dir( $self->{dir} );
276 $self->{script} = File::Spec->catdir( $self->{dir}, 'script' );
277 $self->mk_dir( $self->{script} );
278 $self->{lib} = File::Spec->catdir( $self->{dir}, 'lib' );
279 $self->mk_dir( $self->{lib} );
280 $self->{root} = File::Spec->catdir( $self->{dir}, 'root' );
281 $self->mk_dir( $self->{root} );
282 $self->{t} = File::Spec->catdir( $self->{dir}, 't' );
283 $self->mk_dir( $self->{t} );
284 $self->mk_dir( File::Spec->catdir( $self->{t}, 'M' ) );
285 $self->mk_dir( File::Spec->catdir( $self->{t}, 'V' ) );
286 $self->mk_dir( File::Spec->catdir( $self->{t}, 'C' ) );
287 $self->{class} = File::Spec->catdir( split( /\:\:/, $self->{name} ) );
288 $self->{mod} = File::Spec->catdir( $self->{lib}, $self->{class} );
289 $self->mk_dir( $self->{mod} );
290 $self->{m} = File::Spec->catdir( $self->{mod}, 'M' );
291 $self->mk_dir( $self->{m} );
292 $self->{v} = File::Spec->catdir( $self->{mod}, 'V' );
293 $self->mk_dir( $self->{v} );
294 $self->{c} = File::Spec->catdir( $self->{mod}, 'C' );
295 $self->mk_dir( $self->{c} );
296 $self->{base} = File::Spec->rel2abs( $self->{dir} );
301 my $mod = $self->{mod};
302 $self->render_file( 'appclass', "$mod.pm" );
307 my $dir = $self->{dir};
308 $self->render_file( 'build', "$dir\/Build.PL" );
313 my $dir = $self->{dir};
314 $self->render_file( 'makefile', "$dir\/Makefile.PL" );
319 my $dir = $self->{dir};
320 $self->render_file( 'readme', "$dir\/README" );
325 my $dir = $self->{dir};
326 my $time = localtime time;
327 $self->render_file( 'changes', "$dir\/Changes", { time => $time } );
333 $self->render_file( 'apptest', "$t\/01app.t" );
334 $self->render_file( 'podtest', "$t\/02pod.t" );
335 $self->render_file( 'podcoveragetest', "$t\/03podcoverage.t" );
340 my $script = $self->{script};
341 my $appprefix = $self->{appprefix};
342 $self->render_file( 'cgi', "$script\/$appprefix\_cgi.pl" );
343 chmod 0700, "$script/$appprefix\_cgi.pl";
348 my $script = $self->{script};
349 my $appprefix = $self->{appprefix};
350 $self->render_file( 'fastcgi', "$script\/$appprefix\_fastcgi.pl" );
351 chmod 0700, "$script/$appprefix\_fastcgi.pl";
356 my $script = $self->{script};
357 my $appprefix = $self->{appprefix};
358 $self->render_file( 'server', "$script\/$appprefix\_server.pl" );
359 chmod 0700, "$script/$appprefix\_server.pl";
364 my $script = $self->{script};
365 my $appprefix = $self->{appprefix};
366 $self->render_file( 'test', "$script/$appprefix\_test.pl" );
367 chmod 0700, "$script/$appprefix\_test.pl";
372 my $script = $self->{script};
373 my $appprefix = $self->{appprefix};
374 $self->render_file( 'create', "$script\/$appprefix\_create.pl" );
375 chmod 0700, "$script/$appprefix\_create.pl";
380 my $file = $self->{file};
381 return $self->render_file( 'compclass', "$file" );
386 my $test = $self->{test};
387 $self->render_file( 'comptest', "$test" );
392 Helpers are classes that provide two methods.
394 * mk_compclass - creates the Component class
395 * mk_comptest - creates the Component test
397 So when you call C<bin/create view MyView TT>, create would try to execute
398 Catalyst::Helper::View::TT->mk_compclass and
399 Catalyst::Helper::View::TT->mk_comptest.
401 See L<Catalyst::Helper::View::TT> and L<Catalyst::Helper::Model::CDBI> for
404 All helper classes should be under one of the following namespaces.
406 Catalyst::Helper::Model::
407 Catalyst::Helper::View::
408 Catalyst::Helper::Controller::
412 The helpers will read author name from /etc/passwd by default.
413 To override, please export the AUTHOR variable.
417 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
418 L<Catalyst::Response>, L<Catalyst>
422 Sebastian Riedel, C<sri@oook.de>
426 This library is free software . You can redistribute it and/or modify
427 it under the same terms as perl itself.
438 use Catalyst qw/-Debug/;
440 our $VERSION = '0.01';
442 [% name %]->config( name => '[% name %]' );
448 [% name %] - Catalyst based application
452 script/[% appprefix %]_server.pl
456 Catalyst based application.
466 sub default : Private {
467 my ( $self, $c ) = @_;
468 $c->res->output('Congratulations, [% name %] is on Catalyst!');
479 This library is free software . You can redistribute it and/or modify
480 it under the same terms as perl itself.
486 unless ( eval "use Module::Build::Compat 0.02; 1" ) {
487 print "This module requires Module::Build to install itself.\n";
489 require ExtUtils::MakeMaker;
491 ExtUtils::MakeMaker::prompt( ' Install Module::Build now from CPAN?', 'y' );
493 unless ( $yn =~ /^y/i ) {
494 die " *** Cannot install without Module::Build. Exiting ...\n";
501 # Save this 'cause CPAN will chdir all over the place.
502 my $cwd = Cwd::cwd();
503 my $makefile = File::Spec->rel2abs($0);
505 CPAN::Shell->install('Module::Build::Compat')
506 or die " *** Cannot install without Module::Build. Exiting ...\n";
508 chdir $cwd or die "Cannot chdir() back to $cwd: $!";
510 eval "use Module::Build::Compat 0.02; 1" or die $@;
511 use lib '_build/lib';
512 Module::Build::Compat->run_build_pl( args => \@ARGV );
513 require Module::Build;
514 Module::Build::Compat->write_makefile( build_class => 'Module::Build' );
519 my $build = Catalyst::Build->new(
520 create_makefile_pl => 'passthrough',
522 module_name => '[% name %]',
523 requires => { Catalyst => '5.10' },
524 create_makefile_pl => 'passthrough',
525 script_files => [ glob('script/*') ],
526 test_files => [ glob('t/*.t'), glob('t/*/*.t') ]
528 $build->create_build_script;
530 Run script/[% appprefix %]_server.pl to test the application.
532 This file documents the revision history for Perl extension [% name %].
534 - initial revision, generated by Catalyst
536 use Test::More tests => 2;
537 use_ok( Catalyst::Test, '[% name %]' );
539 ok( request('/')->is_success );
543 eval "use Test::Pod 1.14";
544 plan skip_all => 'Test::Pod 1.14 required' if $@;
545 plan skip_all => 'set TEST_POD to enable this test' unless $ENV{TEST_POD};
551 eval "use Test::Pod::Coverage 1.04";
552 plan skip_all => 'Test::Pod::Coverage 1.04 required' if $@;
553 plan skip_all => 'set TEST_POD to enable this test' unless $ENV{TEST_POD};
555 all_pod_coverage_ok();
559 BEGIN { $ENV{CATALYST_ENGINE} ||= 'CGI' }
563 use lib "$FindBin::Bin/../lib";
572 [% appprefix %]_cgi.pl - Catalyst CGI
576 See L<Catalyst::Manual>
580 Run a Catalyst application as cgi.
584 Sebastian Riedel, C<sri@oook.de>
588 Copyright 2004 Sebastian Riedel. All rights reserved.
590 This library is free software. You can redistribute it and/or modify
591 it under the same terms as perl itself.
597 BEGIN { $ENV{CATALYST_ENGINE} ||= 'FastCGI' }
601 use lib "$FindBin::Bin/../lib";
610 [% appprefix %]_fastcgi.pl - Catalyst FastCGI
614 See L<Catalyst::Manual>
618 Run a Catalyst application as fastcgi.
622 Sebastian Riedel, C<sri@oook.de>
626 Copyright 2004 Sebastian Riedel. All rights reserved.
628 This library is free software. You can redistribute it and/or modify
629 it under the same terms as perl itself.
636 $ENV{CATALYST_ENGINE} ||= 'HTTP';
637 $ENV{CATALYST_SCRIPT_GEN} = [% scriptgen %];
644 use lib "$FindBin::Bin/../lib";
650 GetOptions( 'help|?' => \$help, 'port=s' => \$port );
652 pod2usage(1) if $help;
654 [% name %]->run($port);
660 [% appprefix %]_server.pl - Catalyst Testserver
664 [% appprefix %]_server.pl [options]
667 -? -help display this help and exits
668 -p -port port (defaults to 3000)
671 perldoc Catalyst::Manual
672 perldoc Catalyst::Manual::Intro
676 Run a Catalyst Testserver for this application.
680 Sebastian Riedel, C<sri@oook.de>
684 Copyright 2004 Sebastian Riedel. All rights reserved.
686 This library is free software. You can redistribute it and/or modify
687 it under the same terms as perl itself.
693 BEGIN { $ENV{CATALYST_ENGINE} ||= 'Test' }
699 use lib "$FindBin::Bin/../lib";
704 GetOptions( 'help|?' => \$help );
706 pod2usage(1) if ( $help || !$ARGV[0] );
708 print [% name %]->run($ARGV[0])->content . "\n";
714 [% appprefix %]_test.pl - Catalyst Test
718 [% appprefix %]_test.pl [options] uri
721 -help display this help and exits
724 [% appprefix %]_test.pl http://localhost/some_action
725 [% appprefix %]_test.pl /some_action
728 perldoc Catalyst::Manual
729 perldoc Catalyst::Manual::Intro
733 Run a Catalyst action from the comand line.
737 Sebastian Riedel, C<sri@oook.de>
741 Copyright 2004 Sebastian Riedel. All rights reserved.
743 This library is free software. You can redistribute it and/or modify
744 it under the same terms as perl itself.
753 use Catalyst::Helper;
758 GetOptions( 'help|?' => \$help,
759 'nonew' => \$nonew );
761 pod2usage(1) if ( $help || !$ARGV[0] );
763 my $helper = Catalyst::Helper->new({'.newfiles' => !$nonew});
764 pod2usage(1) unless $helper->mk_component( '[% name %]', @ARGV );
770 [% appprefix %]_create.pl - Create a new Catalyst Component
774 [% appprefix %]_create.pl [options] model|view|controller name [helper] [options]
777 -help display this help and exits
778 -nonew don't create a .new file where a file to be created exists
781 [% appprefix %]_create.pl controller My::Controller
782 [% appprefix %]_create.pl view My::View
783 [% appprefix %]_create.pl view MyView TT
784 [% appprefix %]_create.pl view TT TT
785 [% appprefix %]_create.pl model My::Model
786 [% appprefix %]_create.pl model SomeDB CDBI dbi:SQLite:/tmp/my.db
787 [% appprefix %]_create.pl model AnotherDB CDBI dbi:Pg:dbname=foo root 4321
790 perldoc Catalyst::Manual
791 perldoc Catalyst::Manual::Intro
795 Create a new Catalyst Component.
797 Existing component files are not overwritten. If any of the component files
798 to be created already exist the file will be written with a '.new' suffix.
799 This behaviour can be supressed with the C<-nonew> option.
803 Sebastian Riedel, C<sri\@oook.de>
807 Copyright 2004 Sebastian Riedel. All rights reserved.
809 This library is free software. You can redistribute it and/or modify
810 it under the same terms as perl itself.
817 use base 'Catalyst::Base';
821 [% class %] - Catalyst component
839 sub default : Private {
840 my ( $self, $c ) = @_;
841 $c->res->output('Congratulations, [% class %] is on Catalyst!');
853 This library is free software . You can redistribute it and/or modify
854 it under the same terms as perl itself.
861 use Test::More tests => 3;
862 use_ok( Catalyst::Test, '[% app %]' );
863 use_ok('[% class %]');
865 ok( request('[% uri %]')->is_success );
867 use Test::More tests => 1;
868 use_ok('[% class %]');