1 package Catalyst::Helper;
4 use base 'Class::Accessor::Fast';
13 Catalyst::Helper - Bootstrap a Catalyst application
17 See L<Catalyst::Manual::Intro>
21 Bootstrap a Catalyst application.
30 my ( $self, $name ) = @_;
31 return 0 if $name =~ /[^\w\:]/;
32 $self->{name} = $name;
34 $self->{dir} =~ s/\:\:/-/g;
57 $self->{base} = File::Spec->catdir( $FindBin::Bin, '..' );
58 unless ( $_[0] =~ /^model|m|view|v|controller|c\$/i ) {
61 my $class = "Catalyst::Helper::$helper";
62 eval "require $class";
63 die qq/Couldn't load helper "$class", "$@"/ if $@;
64 if ( $class->can('mk_stuff') ) {
65 return 1 unless $class->mk_stuff( $self, @args );
73 return 0 if $name =~ /[^\w\:]/;
74 $type = 'M' if $type =~ /model|m/i;
75 $type = 'V' if $type =~ /view|v/i;
76 $type = 'C' if $type =~ /controller|c/i;
77 $self->{type} = $type;
78 $self->{name} = $name;
79 $self->{class} = "$app\::$type\::$name";
82 my $appdir = File::Spec->catdir( split /\:\:/, $app );
84 File::Spec->catdir( $FindBin::Bin, '..', 'lib', $appdir, $type );
86 if ( $name =~ /\:/ ) {
87 my @path = split /\:\:/, $name;
89 $path = File::Spec->catdir( $path, @path );
92 $file = File::Spec->catfile( $path, "$file.pm" );
93 $self->{file} = $file;
96 $self->{test_dir} = File::Spec->catdir( $FindBin::Bin, '..', 't' );
97 $self->{test} = $self->next_test;
102 $comp = 'View' if $type eq 'V';
103 $comp = 'Controller' if $type eq 'C';
104 my $class = "Catalyst::Helper::$comp\::$helper";
105 eval "require $class";
106 die qq/Couldn't load helper "$class", "$@"/ if $@;
107 if ( $class->can('mk_compclass') ) {
108 return 1 unless $class->mk_compclass( $self, @args );
110 else { return 1 unless $self->_mk_compclass }
112 if ( $class->can('mk_comptest') ) {
113 $class->mk_comptest( $self, @args );
115 else { $self->_mk_comptest }
120 return 1 unless $self->_mk_compclass;
132 my ( $self, $dir ) = @_;
134 print qq/ exists "$dir"\n/;
138 print qq/created "$dir"\n/;
141 die qq/Couldn't create "$dir", "$!"/;
149 my ( $self, $file, $content ) = @_;
151 print qq/ exists "$file"\n/;
154 if ( my $f = IO::File->new("> $file") ) {
156 print qq/created "$file"\n/;
159 die qq/Couldn't create "$file", "$!"/;
167 my ( $self, $tname ) = @_;
168 if ($tname) { $tname = "$tname.t" }
170 my $name = $self->{name};
173 $prefix = lc $prefix;
174 $tname = $prefix . '.t';
175 $self->{prefix} = $prefix;
177 my $dir = $self->{test_dir};
178 my $type = lc $self->{type};
179 return File::Spec->catfile( $dir, $type, $tname );
184 $self->mk_dir( $self->{dir} );
185 $self->{script} = File::Spec->catdir( $self->{dir}, 'script' );
186 $self->mk_dir( $self->{script} );
187 $self->{lib} = File::Spec->catdir( $self->{dir}, 'lib' );
188 $self->mk_dir( $self->{lib} );
189 $self->{root} = File::Spec->catdir( $self->{dir}, 'root' );
190 $self->mk_dir( $self->{root} );
191 $self->{t} = File::Spec->catdir( $self->{dir}, 't' );
192 $self->mk_dir( $self->{t} );
193 $self->mk_dir( File::Spec->catdir( $self->{t}, 'm' ) );
194 $self->mk_dir( File::Spec->catdir( $self->{t}, 'v' ) );
195 $self->mk_dir( File::Spec->catdir( $self->{t}, 'c' ) );
196 $self->{class} = File::Spec->catdir( split( /\:\:/, $self->{name} ) );
197 $self->{mod} = File::Spec->catdir( $self->{lib}, $self->{class} );
198 $self->mk_dir( $self->{mod} );
199 $self->{m} = File::Spec->catdir( $self->{mod}, 'M' );
200 $self->mk_dir( $self->{m} );
201 $self->{v} = File::Spec->catdir( $self->{mod}, 'V' );
202 $self->mk_dir( $self->{v} );
203 $self->{c} = File::Spec->catdir( $self->{mod}, 'C' );
204 $self->mk_dir( $self->{c} );
205 $self->{base} = File::Spec->rel2abs( $self->{dir} );
210 my $mod = $self->{mod};
211 my $name = $self->{name};
212 my $base = $self->{base};
213 $self->mk_file( "$mod.pm", <<"EOF");
217 use Catalyst qw/-Debug/;
219 our \$VERSION = '0.01';
223 root => '$base/root',
229 my ( \$self, \$c ) = \@_;
230 \$c->res->output('Congratulations, $name is on Catalyst!');
237 $name - A very nice application
245 Very nice application.
253 This library is free software . You can redistribute it and/or modify it under
254 the same terms as perl itself.
264 my $name = $self->{name};
265 my $dir = $self->{dir};
266 my $class = $self->{class};
267 $self->mk_file( "$dir\/Makefile.PL", <<"EOF");
268 use ExtUtils::MakeMaker;
272 VERSION_FROM => 'lib/$class.pm',
273 PREREQ_PM => { Catalyst => 0 },
274 test => { TESTS => join ' ', ( glob('t/*.t'), glob('t/*/*.t') ) }
281 my $dir = $self->{dir};
282 $self->mk_file( "$dir\/README", <<"EOF");
283 Run script/server.pl to test the application.
289 my $name = $self->{name};
290 my $dir = $self->{dir};
291 my $time = localtime time;
292 $self->mk_file( "$dir\/Changes", <<"EOF");
293 This file documents the revision history for Perl extension $name.
296 - initial revision, generated by Catalyst
303 my $name = $self->{name};
304 $self->mk_file( "$t\/01app.t", <<"EOF");
305 use Test::More tests => 2;
306 use_ok( Catalyst::Test, '$name' );
308 ok( request('/')->is_success );
310 $self->mk_file( "$t\/02podcoverage.t", <<"EOF");
313 eval "use Test::Pod::Coverage 1.04";
314 plan skip_all => 'Test::Pod::Coverage 1.04 required' if \$@;
315 plan skip_all => 'set TEST_POD to enable this test' unless \$ENV{TEST_POD};
317 all_pod_coverage_ok();
323 my $name = $self->{name};
324 my $script = $self->{script};
325 $self->mk_file( "$script\/nph-cgi.pl", <<"EOF");
326 $Config{startperl} -w
328 BEGIN { \$ENV{CATALYST_ENGINE} = 'CGI' }
332 use lib "\$FindBin::Bin/../lib";
342 nph-cgi - Catalyst CGI
346 See L<Catalyst::Manual>
350 Run a Catalyst application as cgi.
354 Sebastian Riedel, C<sri\@oook.de>
358 Copyright 2004 Sebastian Riedel. All rights reserved.
360 This library is free software. You can redistribute it and/or modify it under
361 the same terms as perl itself.
365 chmod 0700, "$script/nph-cgi.pl";
370 my $name = $self->{name};
371 my $script = $self->{script};
372 $self->mk_file( "$script\/server.pl", <<"EOF");
373 $Config{startperl} -w
376 \$ENV{CATALYST_ENGINE} = 'Server';
383 use lib "\$FindBin::Bin/../lib";
389 GetOptions( 'help|?' => \\\$help, 'port=s' => \\\$port );
391 pod2usage(1) if \$help;
400 server - Catalyst Testserver
407 -? -help display this help and exits
408 -p -port port (defaults to 3000)
411 perldoc Catalyst::Manual
412 perldoc Catalyst::Manual::Intro
416 Run a Catalyst Testserver for this application.
420 Sebastian Riedel, C<sri\@oook.de>
424 Copyright 2004 Sebastian Riedel. All rights reserved.
426 This library is free software. You can redistribute it and/or modify it under
427 the same terms as perl itself.
431 chmod 0700, "$script/server.pl";
436 my $name = $self->{name};
437 my $script = $self->{script};
438 $self->mk_file( "$script\/cgi-server.pl", <<"EOF");
439 $Config{startperl} -w
442 \$ENV{CATALYST_ENGINE} = 'Server';
449 use lib "\$FindBin::Bin/../lib";
456 GetOptions( 'help|?' => \\\$help, 'port=s' => \\\$port );
458 pod2usage(1) if \$help;
460 $name->run( \$port, File::Spec->catfile( \$FindBin::Bin, 'nph-cgi.pl' ) );
467 cgi-server - Catalyst CGI Testserver
471 cgi-server.pl [options]
474 -? -help display this help and exits
475 -p -port port (defaults to 3000)
478 perldoc Catalyst::Manual
479 perldoc Catalyst::Manual::Intro
483 Run a Catalyst CGI Testserver for this application.
485 Similar to the regular server but doesn't require a restart
490 Sebastian Riedel, C<sri\@oook.de>
494 Copyright 2004 Sebastian Riedel. All rights reserved.
496 This library is free software. You can redistribute it and/or modify it under
497 the same terms as perl itself.
501 chmod 0700, "$script/cgi-server.pl";
506 my $name = $self->{name};
507 my $script = $self->{script};
508 $self->mk_file( "$script/test.pl", <<"EOF");
509 $Config{startperl} -w
515 use lib "\$FindBin::Bin/../lib";
519 GetOptions( 'help|?' => \\\$help );
521 pod2usage(1) if ( \$help || !\$ARGV[0] );
523 require Catalyst::Test;
524 import Catalyst::Test '$name';
526 print get(\$ARGV[0]) . "\n";
537 test.pl [options] uri
540 -help display this help and exits
543 test.pl http://localhost/some_action
547 perldoc Catalyst::Manual
548 perldoc Catalyst::Manual::Intro
552 Run a Catalyst action from the comand line.
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 it under
563 the same terms as perl itself.
567 chmod 0700, "$script/test.pl";
572 my $name = $self->{name};
573 my $script = $self->{script};
574 $self->mk_file( "$script\/create.pl", <<"EOF");
575 $Config{startperl} -w
580 use Catalyst::Helper;
584 GetOptions( 'help|?' => \$help );
586 pod2usage(1) if ( \$help || !\$ARGV[0] );
588 my \$helper = Catalyst::Helper->new;
589 pod2usage(1) unless \$helper->mk_component( '$name', \@ARGV );
596 create - Create a new Catalyst Component
600 create.pl [options] model|view|controller name [helper] [options]
603 -help display this help and exits
606 create.pl controller My::Controller
607 create.pl view My::View
608 create.pl view MyView TT
610 create.pl model My::Model
611 create.pl model SomeDB CDBI dbi:SQLite:/tmp/my.db
612 create.pl model AnotherDB CDBI dbi:Pg:dbname=foo root 4321
616 perldoc Catalyst::Manual
617 perldoc Catalyst::Manual::Intro
621 Create a new Catalyst Component.
625 Sebastian Riedel, C<sri\@oook.de>
629 Copyright 2004 Sebastian Riedel. All rights reserved.
631 This library is free software. You can redistribute it and/or modify it under
632 the same terms as perl itself.
636 chmod 0700, "$script/create.pl";
641 my $app = $self->{app};
642 my $class = $self->{class};
643 my $type = $self->{type};
645 $action = <<"EOF" if $type eq 'C';
650 my ( \$self, \$c ) = \@_;
651 \$c->res->output('Congratulations, $class is on Catalyst!');
656 my $file = $self->{file};
657 return $self->mk_file( "$file", <<"EOF");
661 use base 'Catalyst::Base';
681 This library is free software . You can redistribute it and/or modify it under
682 the same terms as perl itself.
692 my $prefix = $self->{prefix};
693 my $type = $self->{type};
694 my $class = $self->{class};
695 my $app = $self->{app};
696 my $test = $self->{test};
697 if ( $self->{type} eq 'C' ) {
698 $self->mk_file( "$test", <<"EOF");
699 use Test::More tests => 3;
700 use_ok( Catalyst::Test, '$app' );
703 ok( request('$prefix')->is_success );
707 $self->mk_file( "$test", <<"EOF");
708 use Test::More tests => 1;
716 Helpers are classes that provide two methods.
718 * mk_compclass - creates the Component class
719 * mk_comptest - creates the Component test
721 So when you call C<bin/create view MyView TT>, create would try to execute
722 Catalyst::Helper::View::TT->mk_compclass and
723 Catalyst::Helper::View::TT->mk_comptest.
725 See L<Catalyst::Helper::View::TT> and L<Catalyst::Helper::Model::CDBI> for
728 All helper classes should be under one of the following namespaces.
730 Catalyst::Helper::Model::
731 Catalyst::Helper::View::
732 Catalyst::Helper::Controller::
736 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
737 L<Catalyst::Response>, L<Catalyst>
741 Sebastian Riedel, C<sri@oook.de>
745 This library is free software . You can redistribute it and/or modify it under
746 the same terms as perl itself.