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 my $dir = $self->{test_dir};
150 for my $i (<$dir/*.t>) {
151 $i =~ /(\d+)[^\/]*.t$/;
153 $num = $j if $j > $num;
156 $num = sprintf '%02d', $num;
157 if ($tname) { $tname = "$num$tname.t" }
159 my $name = $self->{name};
160 my $type = $self->{type};
163 $prefix = lc $prefix;
164 $tname = lc( $num . $type . '_' . $prefix . '.t' );
165 $self->{prefix} = $prefix;
167 return "$dir/$tname";
172 $self->mk_dir( $self->{dir} );
173 $self->{script} = File::Spec->catdir( $self->{dir}, 'script' );
174 $self->mk_dir( $self->{script} );
175 $self->{lib} = File::Spec->catdir( $self->{dir}, 'lib' );
176 $self->mk_dir( $self->{lib} );
177 $self->{root} = File::Spec->catdir( $self->{dir}, 'root' );
178 $self->mk_dir( $self->{root} );
179 $self->{t} = File::Spec->catdir( $self->{dir}, 't' );
180 $self->mk_dir( $self->{t} );
181 $self->{class} = File::Spec->catdir( split( /\:\:/, $self->{name} ) );
182 $self->{mod} = File::Spec->catdir( $self->{lib}, $self->{class} );
183 $self->mk_dir( $self->{mod} );
184 $self->{m} = File::Spec->catdir( $self->{mod}, 'M' );
185 $self->mk_dir( $self->{m} );
186 $self->{v} = File::Spec->catdir( $self->{mod}, 'V' );
187 $self->mk_dir( $self->{v} );
188 $self->{c} = File::Spec->catdir( $self->{mod}, 'C' );
189 $self->mk_dir( $self->{c} );
190 $self->{base} = File::Spec->rel2abs( $self->{dir} );
195 my $mod = $self->{mod};
196 my $name = $self->{name};
197 my $base = $self->{base};
198 $self->mk_file( "$mod.pm", <<"EOF");
202 use Catalyst qw/-Debug/;
204 our \$VERSION = '0.01';
208 root => '$base/root',
214 my ( \$self, \$c ) = \@_;
215 \$c->res->output('Congratulations, $name is on Catalyst!');
222 $name - A very nice application
230 Very nice application.
238 This library is free software . You can redistribute it and/or modify it under
239 the same terms as perl itself.
249 my $name = $self->{name};
250 my $dir = $self->{dir};
251 my $class = $self->{class};
252 $self->mk_file( "$dir\/Makefile.PL", <<"EOF");
253 use ExtUtils::MakeMaker;
257 VERSION_FROM => 'lib/$class.pm',
258 PREREQ_PM => { Catalyst => 0 }
265 my $dir = $self->{dir};
266 $self->mk_file( "$dir\/README", <<"EOF");
267 Run script/server.pl to test the application.
273 my $name = $self->{name};
274 my $dir = $self->{dir};
275 my $time = localtime time;
276 $self->mk_file( "$dir\/Changes", <<"EOF");
277 This file documents the revision history for Perl extension $name.
280 - initial revision, generated by Catalyst
287 my $name = $self->{name};
288 $self->mk_file( "$t\/01app.t", <<"EOF");
289 use Test::More tests => 2;
290 use_ok( Catalyst::Test, '$name' );
292 ok( request('/')->is_success );
294 $self->mk_file( "$t\/02podcoverage.t", <<"EOF");
297 eval "use Test::Pod::Coverage 1.04";
298 plan skip_all => 'Test::Pod::Coverage 1.04 required' if \$@;
299 plan skip_all => 'set TEST_POD to enable this test' unless \$ENV{TEST_POD};
301 all_pod_coverage_ok();
307 my $name = $self->{name};
308 my $script = $self->{script};
309 $self->mk_file( "$script\/server.pl", <<"EOF");
316 use lib "\$FindBin::Bin/../lib";
317 use Catalyst::Test '$name';
322 GetOptions( 'help|?' => \\\$help, 'port=s' => \\\$port );
324 pod2usage(1) if \$help;
326 Catalyst::Test::server(\$port);
333 server - Catalyst Testserver
340 -? -help display this help and exits
341 -p -port port (defaults to 3000)
344 perldoc Catalyst::Manual
345 perldoc Catalyst::Manual::Intro
349 Run a Catalyst Testserver for this application.
353 Sebastian Riedel, C<sri\@oook.de>
357 Copyright 2004 Sebastian Riedel. All rights reserved.
359 This library is free software. You can redistribute it and/or modify it under
360 the same terms as perl itself.
364 chmod 0700, "$script/server.pl";
369 my $name = $self->{name};
370 my $script = $self->{script};
371 $self->mk_file( "$script/test.pl", <<"EOF");
378 use lib "\$FindBin::Bin/../lib";
382 GetOptions( 'help|?' => \\\$help );
384 pod2usage(1) if ( \$help || !\$ARGV[0] );
386 require Catalyst::Test;
387 import Catalyst::Test '$name';
389 print get(\$ARGV[0]) . "\n";
400 test.pl [options] uri
403 -help display this help and exits
406 test.pl http://localhost/some_action
410 perldoc Catalyst::Manual
411 perldoc Catalyst::Manual::Intro
415 Run a Catalyst action from the comand line.
419 Sebastian Riedel, C<sri\@oook.de>
423 Copyright 2004 Sebastian Riedel. All rights reserved.
425 This library is free software. You can redistribute it and/or modify it under
426 the same terms as perl itself.
430 chmod 0700, "$script/test.pl";
435 my $name = $self->{name};
436 my $script = $self->{script};
437 $self->mk_file( "$script\/create.pl", <<"EOF");
443 use Catalyst::Helper;
447 GetOptions( 'help|?' => \$help );
449 pod2usage(1) if ( \$help || !\$ARGV[1] );
451 my \$helper = Catalyst::Helper->new;
452 pod2usage(1) unless \$helper->mk_component( '$name', \@ARGV );
459 create - Create a new Catalyst Component
463 create.pl [options] model|view|controller name [helper] [options]
466 -help display this help and exits
469 create.pl controller My::Controller
470 create.pl view My::View
471 create.pl view MyView TT
473 create.pl model My::Model
474 create.pl model SomeDB CDBI dbi:SQLite:/tmp/my.db
475 create.pl model AnotherDB CDBI dbi:Pg:dbname=foo root 4321
478 perldoc Catalyst::Manual
479 perldoc Catalyst::Manual::Intro
483 Create a new Catalyst Component.
487 Sebastian Riedel, C<sri\@oook.de>
491 Copyright 2004 Sebastian Riedel. All rights reserved.
493 This library is free software. You can redistribute it and/or modify it under
494 the same terms as perl itself.
498 chmod 0700, "$script/create.pl";
503 my $app = $self->{app};
504 my $class = $self->{class};
505 my $type = $self->{type};
507 $action = <<"EOF" if $type eq 'C';
512 my ( \$self, \$c ) = \@_;
513 \$c->res->output('Congratulations, $class is on Catalyst!');
518 my $file = $self->{file};
519 return $self->mk_file( "$file", <<"EOF");
523 use base 'Catalyst::Base';
543 This library is free software . You can redistribute it and/or modify it under
544 the same terms as perl itself.
554 my $prefix = $self->{prefix};
555 my $type = $self->{type};
556 my $class = $self->{class};
557 my $app = $self->{app};
558 my $test = $self->{test};
559 if ( $self->{type} eq 'C' ) {
560 $self->mk_file( "$test", <<"EOF");
561 use Test::More tests => 3;
562 use_ok( Catalyst::Test, '$app' );
565 ok( request('$prefix')->is_success );
569 $self->mk_file( "$test", <<"EOF");
570 use Test::More tests => 1;
578 Helpers are classes that provide two methods.
580 * mk_compclass - creates the Component class
581 * mk_comptest - creates the Component test
583 So when you call C<bin/create view MyView TT>, create would try to execute
584 Catalyst::Helper::View::TT->mk_compclass and
585 Catalyst::Helper::View::TT->mk_comptest.
587 See L<Catalyst::Helper::View::TT> and L<Catalyst::Helper::Model::CDBI> for
590 All helper classes should be under one of the following namespaces.
592 Catalyst::Helper::Model::
593 Catalyst::Helper::View::
594 Catalyst::Helper::Controller::
598 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
599 L<Catalyst::Response>, L<Catalyst>
603 Sebastian Riedel, C<sri@oook.de>
607 This library is free software . You can redistribute it and/or modify it under
608 the same terms as perl itself.