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;
56 $self->{base} = File::Spec->catdir( $FindBin::Bin, '..' );
57 unless ( $_[0] =~ /^model|m|view|v|controller|c\$/i ) {
60 my $class = "Catalyst::Helper::$helper";
61 eval "require $class";
62 die qq/Couldn't load helper "$class", "$@"/ if $@;
63 if ( $class->can('mk_stuff') ) {
64 return 1 unless $class->mk_stuff( $self, @args );
72 return 0 if $name =~ /[^\w\:]/;
73 $type = 'M' if $type =~ /model|m/i;
74 $type = 'V' if $type =~ /view|v/i;
75 $type = 'C' if $type =~ /controller|c/i;
76 $self->{type} = $type;
77 $self->{name} = $name;
78 $self->{class} = "$app\::$type\::$name";
81 my $appdir = File::Spec->catdir( split /\:\:/, $app );
83 File::Spec->catdir( $FindBin::Bin, '..', 'lib', $appdir, $type );
85 if ( $name =~ /\:/ ) {
86 my @path = split /\:\:/, $name;
88 $path = File::Spec->catdir( $path, @path );
91 $file = File::Spec->catfile( $path, "$file.pm" );
92 $self->{file} = $file;
95 $self->{test_dir} = File::Spec->catdir( $FindBin::Bin, '..', 't' );
96 $self->{test} = $self->next_test;
101 $comp = 'View' if $type eq 'V';
102 $comp = 'Controller' if $type eq 'C';
103 my $class = "Catalyst::Helper::$comp\::$helper";
104 eval "require $class";
105 die qq/Couldn't load helper "$class", "$@"/ if $@;
106 if ( $class->can('mk_compclass') ) {
107 return 1 unless $class->mk_compclass( $self, @args );
109 else { return 1 unless $self->_mk_compclass }
111 if ( $class->can('mk_comptest') ) {
112 $class->mk_comptest( $self, @args );
114 else { $self->_mk_comptest }
119 return 1 unless $self->_mk_compclass;
131 my ( $self, $dir ) = @_;
133 print qq/ exists "$dir"\n/;
137 print qq/created "$dir"\n/;
140 die qq/Couldn't create "$dir", "$!"/;
148 my ( $self, $file, $content ) = @_;
150 print qq/ exists "$file"\n/;
153 if ( my $f = IO::File->new("> $file") ) {
155 print qq/created "$file"\n/;
158 die qq/Couldn't create "$file", "$!"/;
166 my ( $self, $tname ) = @_;
167 if ($tname) { $tname = "$tname.t" }
169 my $name = $self->{name};
172 $prefix = lc $prefix;
173 $tname = $prefix . '.t';
174 $self->{prefix} = $prefix;
176 my $dir = $self->{test_dir};
177 my $type = lc $self->{type};
178 return File::Spec->catfile( $dir, $type, $tname );
183 $self->mk_dir( $self->{dir} );
184 $self->{script} = File::Spec->catdir( $self->{dir}, 'script' );
185 $self->mk_dir( $self->{script} );
186 $self->{lib} = File::Spec->catdir( $self->{dir}, 'lib' );
187 $self->mk_dir( $self->{lib} );
188 $self->{root} = File::Spec->catdir( $self->{dir}, 'root' );
189 $self->mk_dir( $self->{root} );
190 $self->{t} = File::Spec->catdir( $self->{dir}, 't' );
191 $self->mk_dir( $self->{t} );
192 $self->mk_dir( File::Spec->catdir( $self->{t}, 'm' ) );
193 $self->mk_dir( File::Spec->catdir( $self->{t}, 'v' ) );
194 $self->mk_dir( File::Spec->catdir( $self->{t}, 'c' ) );
195 $self->{class} = File::Spec->catdir( split( /\:\:/, $self->{name} ) );
196 $self->{mod} = File::Spec->catdir( $self->{lib}, $self->{class} );
197 $self->mk_dir( $self->{mod} );
198 $self->{m} = File::Spec->catdir( $self->{mod}, 'M' );
199 $self->mk_dir( $self->{m} );
200 $self->{v} = File::Spec->catdir( $self->{mod}, 'V' );
201 $self->mk_dir( $self->{v} );
202 $self->{c} = File::Spec->catdir( $self->{mod}, 'C' );
203 $self->mk_dir( $self->{c} );
204 $self->{base} = File::Spec->rel2abs( $self->{dir} );
209 my $mod = $self->{mod};
210 my $name = $self->{name};
211 my $base = $self->{base};
212 $self->mk_file( "$mod.pm", <<"EOF");
216 use Catalyst qw/-Debug/;
218 our \$VERSION = '0.01';
222 root => '$base/root',
228 my ( \$self, \$c ) = \@_;
229 \$c->res->output('Congratulations, $name is on Catalyst!');
236 $name - A very nice application
244 Very nice application.
252 This library is free software . You can redistribute it and/or modify it under
253 the same terms as perl itself.
263 my $name = $self->{name};
264 my $dir = $self->{dir};
265 my $class = $self->{class};
266 $self->mk_file( "$dir\/Makefile.PL", <<"EOF");
267 use ExtUtils::MakeMaker;
271 VERSION_FROM => 'lib/$class.pm',
272 PREREQ_PM => { Catalyst => 0 },
273 test => { TESTS => join ' ', ( glob('t/*.t'), glob('t/*/*.t') ) }
280 my $dir = $self->{dir};
281 $self->mk_file( "$dir\/README", <<"EOF");
282 Run script/server.pl to test the application.
288 my $name = $self->{name};
289 my $dir = $self->{dir};
290 my $time = localtime time;
291 $self->mk_file( "$dir\/Changes", <<"EOF");
292 This file documents the revision history for Perl extension $name.
295 - initial revision, generated by Catalyst
302 my $name = $self->{name};
303 $self->mk_file( "$t\/01app.t", <<"EOF");
304 use Test::More tests => 2;
305 use_ok( Catalyst::Test, '$name' );
307 ok( request('/')->is_success );
309 $self->mk_file( "$t\/02podcoverage.t", <<"EOF");
312 eval "use Test::Pod::Coverage 1.04";
313 plan skip_all => 'Test::Pod::Coverage 1.04 required' if \$@;
314 plan skip_all => 'set TEST_POD to enable this test' unless \$ENV{TEST_POD};
316 all_pod_coverage_ok();
322 my $name = $self->{name};
323 my $script = $self->{script};
324 $self->mk_file( "$script\/nph-cgi.pl", <<"EOF");
325 $Config{startperl} -w
328 \$ENV{CATALYST_ENGINE} = 'CGI';
329 \$ENV{CATALYST_TEST} = 1;
334 use lib "\$FindBin::Bin/../lib";
344 nph-cgi - Catalyst CGI
348 See L<Catalyst::Manual>
352 Run a Catalyst application as cgi.
356 Sebastian Riedel, C<sri\@oook.de>
360 Copyright 2004 Sebastian Riedel. All rights reserved.
362 This library is free software. You can redistribute it and/or modify it under
363 the same terms as perl itself.
367 chmod 0700, "$script/nph-cgi.pl";
372 my $name = $self->{name};
373 my $script = $self->{script};
374 $self->mk_file( "$script\/server.pl", <<"EOF");
375 $Config{startperl} -w
378 \$ENV{CATALYST_ENGINE} = 'Server';
379 \$ENV{CATALYST_TEST} = 1;
386 use lib "\$FindBin::Bin/../lib";
392 GetOptions( 'help|?' => \\\$help, 'port=s' => \\\$port );
394 pod2usage(1) if \$help;
403 server - Catalyst Testserver
410 -? -help display this help and exits
411 -p -port port (defaults to 3000)
414 perldoc Catalyst::Manual
415 perldoc Catalyst::Manual::Intro
419 Run a Catalyst Testserver for this application.
423 Sebastian Riedel, C<sri\@oook.de>
427 Copyright 2004 Sebastian Riedel. All rights reserved.
429 This library is free software. You can redistribute it and/or modify it under
430 the same terms as perl itself.
434 chmod 0700, "$script/server.pl";
439 my $name = $self->{name};
440 my $script = $self->{script};
441 $self->mk_file( "$script/test.pl", <<"EOF");
442 $Config{startperl} -w
448 use lib "\$FindBin::Bin/../lib";
452 GetOptions( 'help|?' => \\\$help );
454 pod2usage(1) if ( \$help || !\$ARGV[0] );
456 require Catalyst::Test;
457 import Catalyst::Test '$name';
459 print get(\$ARGV[0]) . "\n";
470 test.pl [options] uri
473 -help display this help and exits
476 test.pl http://localhost/some_action
480 perldoc Catalyst::Manual
481 perldoc Catalyst::Manual::Intro
485 Run a Catalyst action from the comand line.
489 Sebastian Riedel, C<sri\@oook.de>
493 Copyright 2004 Sebastian Riedel. All rights reserved.
495 This library is free software. You can redistribute it and/or modify it under
496 the same terms as perl itself.
500 chmod 0700, "$script/test.pl";
505 my $name = $self->{name};
506 my $script = $self->{script};
507 $self->mk_file( "$script\/create.pl", <<"EOF");
508 $Config{startperl} -w
513 use Catalyst::Helper;
517 GetOptions( 'help|?' => \$help );
519 pod2usage(1) if ( \$help || !\$ARGV[0] );
521 my \$helper = Catalyst::Helper->new;
522 pod2usage(1) unless \$helper->mk_component( '$name', \@ARGV );
529 create - Create a new Catalyst Component
533 create.pl [options] model|view|controller name [helper] [options]
536 -help display this help and exits
539 create.pl controller My::Controller
540 create.pl view My::View
541 create.pl view MyView TT
543 create.pl model My::Model
544 create.pl model SomeDB CDBI dbi:SQLite:/tmp/my.db
545 create.pl model AnotherDB CDBI dbi:Pg:dbname=foo root 4321
549 perldoc Catalyst::Manual
550 perldoc Catalyst::Manual::Intro
554 Create a new Catalyst Component.
558 Sebastian Riedel, C<sri\@oook.de>
562 Copyright 2004 Sebastian Riedel. All rights reserved.
564 This library is free software. You can redistribute it and/or modify it under
565 the same terms as perl itself.
569 chmod 0700, "$script/create.pl";
574 my $app = $self->{app};
575 my $class = $self->{class};
576 my $type = $self->{type};
578 $action = <<"EOF" if $type eq 'C';
583 my ( \$self, \$c ) = \@_;
584 \$c->res->output('Congratulations, $class is on Catalyst!');
589 my $file = $self->{file};
590 return $self->mk_file( "$file", <<"EOF");
594 use base 'Catalyst::Base';
614 This library is free software . You can redistribute it and/or modify it under
615 the same terms as perl itself.
625 my $prefix = $self->{prefix};
626 my $type = $self->{type};
627 my $class = $self->{class};
628 my $app = $self->{app};
629 my $test = $self->{test};
630 if ( $self->{type} eq 'C' ) {
631 $self->mk_file( "$test", <<"EOF");
632 use Test::More tests => 3;
633 use_ok( Catalyst::Test, '$app' );
636 ok( request('$prefix')->is_success );
640 $self->mk_file( "$test", <<"EOF");
641 use Test::More tests => 1;
649 Helpers are classes that provide two methods.
651 * mk_compclass - creates the Component class
652 * mk_comptest - creates the Component test
654 So when you call C<bin/create view MyView TT>, create would try to execute
655 Catalyst::Helper::View::TT->mk_compclass and
656 Catalyst::Helper::View::TT->mk_comptest.
658 See L<Catalyst::Helper::View::TT> and L<Catalyst::Helper::Model::CDBI> for
661 All helper classes should be under one of the following namespaces.
663 Catalyst::Helper::Model::
664 Catalyst::Helper::View::
665 Catalyst::Helper::Controller::
669 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
670 L<Catalyst::Response>, L<Catalyst>
674 Sebastian Riedel, C<sri@oook.de>
678 This library is free software . You can redistribute it and/or modify it under
679 the same terms as perl itself.