1 package Catalyst::Helper;
4 use base 'Class::Accessor::Fast';
12 Catalyst::Helper - Bootstrap a Catalyst application
16 See L<Catalyst::Manual::Intro>
20 Bootstrap a Catalyst application.
29 my ( $self, $name ) = @_;
30 return 0 if $name =~ /[^\w\:]/;
31 $self->{name} = $name;
33 $self->{dir} =~ s/\:\:/-/g;
51 my ( $self, $app, $type, $name, $helper, @args ) = @_;
53 if ( $name =~ /[^\w\:]/ || !\$type =~ /^model|m|view|v|controller|c\$/i );
54 return 0 if $name =~ /[^\w\:]/;
55 $type = 'M' if $type =~ /model|m/i;
56 $type = 'V' if $type =~ /view|v/i;
57 $type = 'C' if $type =~ /controller|c/i;
58 $self->{type} = $type;
59 $self->{name} = $name;
60 $self->{class} = "$app\::$type\::$name";
64 my $appdir = File::Spec->catdir( split /\:\:/, $app );
65 my $path = File::Spec->catdir( $FindBin::Bin, '..', 'lib', $appdir, $type );
67 if ( $name =~ /\:/ ) {
68 my @path = split /\:\:/, $name;
70 $path = File::Spec->catdir( $path, @path );
73 $file = File::Spec->catfile( $path, "$file.pm" );
74 $self->{file} = $file;
77 $self->{test_dir} = File::Spec->catdir( $FindBin::Bin, '..', 't' );
78 $self->{test} = $self->next_test;
83 $comp = 'View' if $type eq 'V';
84 $comp = 'Controller' if $type eq 'C';
85 my $class = "Catalyst::Helper::$comp\::$helper";
86 eval "require $class";
87 die qq/Couldn't load helper "$class", "$@"/ if $@;
88 if ( $class->can('mk_compclass') ) {
89 return 1 unless $class->mk_compclass( $self, @args );
91 else { return 1 unless $self->_mk_compclass }
93 if ( $class->can('mk_comptest') ) {
94 $class->mk_comptest( $self, @args );
96 else { $self->_mk_comptest }
101 return 1 unless $self->_mk_compclass;
112 my ( $self, $dir ) = @_;
114 print qq/ exists "$dir"\n/;
118 print qq/created "$dir"\n/;
121 die qq/Couldn't create "$dir", "$!"/;
129 my ( $self, $file, $content ) = @_;
131 print qq/ exists "$file"\n/;
134 if ( my $f = IO::File->new("> $file") ) {
136 print qq/created "$file"\n/;
139 die qq/Couldn't create "$file", "$!"/;
147 my ( $self, $tname ) = @_;
148 if ($tname) { $tname = "$tname.t" }
150 my $name = $self->{name};
153 $prefix = lc $prefix;
154 $tname = $prefix . '.t';
155 $self->{prefix} = $prefix;
157 my $dir = $self->{test_dir};
158 my $type = lc $self->{type};
159 return File::Spec->catfile( $dir, $type, $tname );
164 $self->mk_dir( $self->{dir} );
165 $self->{script} = File::Spec->catdir( $self->{dir}, 'script' );
166 $self->mk_dir( $self->{script} );
167 $self->{lib} = File::Spec->catdir( $self->{dir}, 'lib' );
168 $self->mk_dir( $self->{lib} );
169 $self->{root} = File::Spec->catdir( $self->{dir}, 'root' );
170 $self->mk_dir( $self->{root} );
171 $self->{t} = File::Spec->catdir( $self->{dir}, 't' );
172 $self->mk_dir( $self->{t} );
173 $self->mk_dir( File::Spec->catdir( $self->{t}, 'm' ) );
174 $self->mk_dir( File::Spec->catdir( $self->{t}, 'v' ) );
175 $self->mk_dir( File::Spec->catdir( $self->{t}, 'c' ) );
176 $self->{class} = File::Spec->catdir( split( /\:\:/, $self->{name} ) );
177 $self->{mod} = File::Spec->catdir( $self->{lib}, $self->{class} );
178 $self->mk_dir( $self->{mod} );
179 $self->{m} = File::Spec->catdir( $self->{mod}, 'M' );
180 $self->mk_dir( $self->{m} );
181 $self->{v} = File::Spec->catdir( $self->{mod}, 'V' );
182 $self->mk_dir( $self->{v} );
183 $self->{c} = File::Spec->catdir( $self->{mod}, 'C' );
184 $self->mk_dir( $self->{c} );
185 $self->{base} = File::Spec->rel2abs( $self->{dir} );
190 my $mod = $self->{mod};
191 my $name = $self->{name};
192 my $base = $self->{base};
193 $self->mk_file( "$mod.pm", <<"EOF");
197 use Catalyst qw/-Debug/;
199 our \$VERSION = '0.01';
203 root => '$base/root',
209 my ( \$self, \$c ) = \@_;
210 \$c->res->output('Congratulations, $name is on Catalyst!');
217 $name - A very nice application
225 Very nice application.
233 This library is free software . You can redistribute it and/or modify it under
234 the same terms as perl itself.
244 my $name = $self->{name};
245 my $dir = $self->{dir};
246 my $class = $self->{class};
247 $self->mk_file( "$dir\/Makefile.PL", <<"EOF");
248 use ExtUtils::MakeMaker;
252 VERSION_FROM => 'lib/$class.pm',
253 PREREQ_PM => { Catalyst => 0 },
254 test => { TESTS => join ' ', ( glob('t/*.t'), glob('t/*/*.t') ) }
261 my $dir = $self->{dir};
262 $self->mk_file( "$dir\/README", <<"EOF");
263 Run script/server.pl to test the application.
269 my $name = $self->{name};
270 my $dir = $self->{dir};
271 my $time = localtime time;
272 $self->mk_file( "$dir\/Changes", <<"EOF");
273 This file documents the revision history for Perl extension $name.
276 - initial revision, generated by Catalyst
283 my $name = $self->{name};
284 $self->mk_file( "$t\/01app.t", <<"EOF");
285 use Test::More tests => 2;
286 use_ok( Catalyst::Test, '$name' );
288 ok( request('/')->is_success );
290 $self->mk_file( "$t\/02podcoverage.t", <<"EOF");
293 eval "use Test::Pod::Coverage 1.04";
294 plan skip_all => 'Test::Pod::Coverage 1.04 required' if \$@;
295 plan skip_all => 'set TEST_POD to enable this test' unless \$ENV{TEST_POD};
297 all_pod_coverage_ok();
303 my $name = $self->{name};
304 my $script = $self->{script};
305 $self->mk_file( "$script\/server.pl", <<"EOF");
312 use lib "\$FindBin::Bin/../lib";
313 use Catalyst::Test '$name';
318 GetOptions( 'help|?' => \\\$help, 'port=s' => \\\$port );
320 pod2usage(1) if \$help;
322 Catalyst::Test::server(\$port);
329 server - Catalyst Testserver
336 -? -help display this help and exits
337 -p -port port (defaults to 3000)
340 perldoc Catalyst::Manual
341 perldoc Catalyst::Manual::Intro
345 Run a Catalyst Testserver for this application.
349 Sebastian Riedel, C<sri\@oook.de>
353 Copyright 2004 Sebastian Riedel. All rights reserved.
355 This library is free software. You can redistribute it and/or modify it under
356 the same terms as perl itself.
360 chmod 0700, "$script/server.pl";
365 my $name = $self->{name};
366 my $script = $self->{script};
367 $self->mk_file( "$script/test.pl", <<"EOF");
374 use lib "\$FindBin::Bin/../lib";
378 GetOptions( 'help|?' => \\\$help );
380 pod2usage(1) if ( \$help || !\$ARGV[0] );
382 require Catalyst::Test;
383 import Catalyst::Test '$name';
385 print get(\$ARGV[0]) . "\n";
396 test.pl [options] uri
399 -help display this help and exits
402 test.pl http://localhost/some_action
406 perldoc Catalyst::Manual
407 perldoc Catalyst::Manual::Intro
411 Run a Catalyst action from the comand line.
415 Sebastian Riedel, C<sri\@oook.de>
419 Copyright 2004 Sebastian Riedel. All rights reserved.
421 This library is free software. You can redistribute it and/or modify it under
422 the same terms as perl itself.
426 chmod 0700, "$script/test.pl";
431 my $name = $self->{name};
432 my $script = $self->{script};
433 $self->mk_file( "$script\/create.pl", <<"EOF");
439 use Catalyst::Helper;
443 GetOptions( 'help|?' => \$help );
445 pod2usage(1) if ( \$help || !\$ARGV[1] );
447 my \$helper = Catalyst::Helper->new;
448 pod2usage(1) unless \$helper->mk_component( '$name', \@ARGV );
455 create - Create a new Catalyst Component
459 create.pl [options] model|view|controller name [helper] [options]
462 -help display this help and exits
465 create.pl controller My::Controller
466 create.pl view My::View
467 create.pl view MyView TT
469 create.pl model My::Model
470 create.pl model SomeDB CDBI dbi:SQLite:/tmp/my.db
471 create.pl model AnotherDB CDBI dbi:Pg:dbname=foo root 4321
474 perldoc Catalyst::Manual
475 perldoc Catalyst::Manual::Intro
479 Create a new Catalyst Component.
483 Sebastian Riedel, C<sri\@oook.de>
487 Copyright 2004 Sebastian Riedel. All rights reserved.
489 This library is free software. You can redistribute it and/or modify it under
490 the same terms as perl itself.
494 chmod 0700, "$script/create.pl";
499 my $app = $self->{app};
500 my $class = $self->{class};
501 my $type = $self->{type};
503 $action = <<"EOF" if $type eq 'C';
508 my ( \$self, \$c ) = \@_;
509 \$c->res->output('Congratulations, $class is on Catalyst!');
514 my $file = $self->{file};
515 return $self->mk_file( "$file", <<"EOF");
519 use base 'Catalyst::Base';
539 This library is free software . You can redistribute it and/or modify it under
540 the same terms as perl itself.
550 my $prefix = $self->{prefix};
551 my $type = $self->{type};
552 my $class = $self->{class};
553 my $app = $self->{app};
554 my $test = $self->{test};
555 if ( $self->{type} eq 'C' ) {
556 $self->mk_file( "$test", <<"EOF");
557 use Test::More tests => 3;
558 use_ok( Catalyst::Test, '$app' );
561 ok( request('$prefix')->is_success );
565 $self->mk_file( "$test", <<"EOF");
566 use Test::More tests => 1;
574 Helpers are classes that provide two methods.
576 * mk_compclass - creates the Component class
577 * mk_comptest - creates the Component test
579 So when you call C<bin/create view MyView TT>, create would try to execute
580 Catalyst::Helper::View::TT->mk_compclass and
581 Catalyst::Helper::View::TT->mk_comptest.
583 See L<Catalyst::Helper::View::TT> and L<Catalyst::Helper::Model::CDBI> for
586 All helper classes should be under one of the following namespaces.
588 Catalyst::Helper::Model::
589 Catalyst::Helper::View::
590 Catalyst::Helper::Controller::
594 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
595 L<Catalyst::Response>, L<Catalyst>
599 Sebastian Riedel, C<sri@oook.de>
603 This library is free software . You can redistribute it and/or modify it under
604 the same terms as perl itself.