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;
46 mkpath $self->{dir} unless -d $self->{dir};
47 $self->{bin} = File::Spec->catdir( $self->{dir}, 'bin' );
49 $self->{lib} = File::Spec->catdir( $self->{dir}, 'lib' );
51 $self->{root} = File::Spec->catdir( $self->{dir}, 'root' );
53 $self->{t} = File::Spec->catdir( $self->{dir}, 't' );
55 $self->{class} = File::Spec->catdir( split( /\:\:/, $self->{name} ) );
56 $self->{mod} = File::Spec->catdir( $self->{lib}, $self->{class} );
58 $self->{m} = File::Spec->catdir( $self->{mod}, 'M' );
60 $self->{v} = File::Spec->catdir( $self->{mod}, 'V' );
62 $self->{c} = File::Spec->catdir( $self->{mod}, 'C' );
64 $self->{base} = File::Spec->rel2abs( $self->{dir} );
69 my $mod = $self->{mod};
70 my $name = $self->{name};
71 my $base = $self->{base};
72 my $class = IO::File->new("> $mod.pm")
73 or die qq/Couldn't open "$mod.pm", "$!"/;
78 use Catalyst qw/-Debug/;
80 our \$VERSION = '0.01';
90 my ( \$self, \$c ) = \@_;
91 \$c->res->output('Congratulations, $name is on Catalyst!');
98 $name - A very nice application
106 Very nice application.
114 This library is free software . You can redistribute it and/or modify it under
115 the same terms as perl itself.
125 my $name = $self->{name};
126 my $dir = $self->{dir};
127 my $class = $self->{class};
128 my $makefile = IO::File->new("> $dir/Makefile.PL")
129 or die qq/Couldn't open "$dir\/Makefile.PL", "$!"/;
130 print $makefile <<"EOF";
131 use ExtUtils::MakeMaker;
135 VERSION_FROM => 'lib/$class.pm',
136 PREREQ_PM => { Catalyst => 0 }
144 my $name = $self->{name};
145 my $test = IO::File->new("> $t/01app.t")
146 or die qq/Couldn't open "$t\/01app.t", "$!"/;
148 use Test::More tests => 2;
149 use_ok( Catalyst::Test, '$name' );
151 ok( request('/')->is_success );
153 my $pc = IO::File->new("> $t/02podcoverage.t")
154 or die qq/Couldn't open "$t\/02podcoverage.t", "$!"/;
158 eval "use Test::Pod::Coverage 1.04";
159 plan skip_all => 'Test::Pod::Coverage 1.04 required' if \$@;
160 plan skip_all => 'set TEST_POD to enable this test' unless \$ENV{TEST_POD};
162 all_pod_coverage_ok();
168 my $name = $self->{name};
169 my $bin = $self->{bin};
170 my $server = IO::File->new("> $bin/server")
171 or die qq/Could't open "$bin\/server", "$!"/;
172 print $server <<"EOF";
179 use lib "\$FindBin::Bin/../lib";
180 use Catalyst::Test '$name';
185 GetOptions( 'help|?' => \\\$help, 'port=s' => \\\$port );
187 pod2usage(1) if \$help;
189 Catalyst::Test::server(\$port);
196 server - Catalyst Testserver
203 -help display this help and exits
204 -port port (defaults to 3000)
207 perldoc Catalyst::Manual
208 perldoc Catalyst::Manual::Intro
212 Run a Catalyst Testserver for this application.
216 Sebastian Riedel, C<sri\@oook.de>
220 Copyright 2004 Sebastian Riedel. All rights reserved.
222 This library is free software. You can redistribute it and/or modify it under
223 the same terms as perl itself.
227 chmod 0700, "$bin/server";
232 my $name = $self->{name};
233 my $bin = $self->{bin};
234 my $test = IO::File->new("> $bin/test")
235 or die qq/Could't open "$bin\/test", "$!"/;
243 use lib "\$FindBin::Bin/../lib";
247 GetOptions( 'help|?' => \\\$help );
249 pod2usage(1) if ( \$help || !\$ARGV[0] );
251 require Catalyst::Test;
252 import Catalyst::Test '$name';
254 print get(\$ARGV[0]) . "\n";
268 -help display this help and exits
271 perl test http://localhost/some_action
272 perl test /some_action
275 perldoc Catalyst::Manual
276 perldoc Catalyst::Manual::Intro
280 Run a Catalyst action from the comand line.
284 Sebastian Riedel, C<sri\@oook.de>
288 Copyright 2004 Sebastian Riedel. All rights reserved.
290 This library is free software. You can redistribute it and/or modify it under
291 the same terms as perl itself.
295 chmod 0700, "$bin/test";
300 my $name = $self->{name};
301 my $bin = $self->{bin};
302 my $create = IO::File->new("> $bin/create")
303 or die qq/Could't open "$bin\/create", "$!"/;
304 print $create <<"EOF";
310 use Catalyst::Helper;
314 GetOptions( 'help|?' => \$help );
316 pod2usage(1) if ( \$help || !\$ARGV[1] );
318 my \$helper = Catalyst::Helper->new;
319 pod2usage(1) unless \$helper->mk_component( '$name', \@ARGV );
326 create - Create a new Catalyst Component
330 create [options] model|view|controller name [helper] [options]
333 -help display this help and exits
336 perl create controller My::Controller
337 perl create view My::View
338 perl create view MyView TT
339 perl create view TT TT
340 perl create model My::Model
341 perl create model SomeDB CDBI dbi:SQLite:/tmp/my.db
342 perl create model AnotherDB CDBI dbi:Pg:dbname=foo root 4321
345 perldoc Catalyst::Manual
346 perldoc Catalyst::Manual::Intro
350 Create a new Catalyst Component.
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, "$bin/create";
373 my ( $self, $app, $type, $name, $helper, @args ) = @_;
375 if ( $name =~ /[^\w\:]/ || !\$type =~ /^model|m|view|v|controller|c\$/i );
376 return 0 if $name =~ /[^\w\:]/;
377 $type = 'M' if $type =~ /model|m/i;
378 $type = 'V' if $type =~ /view|v/i;
379 $type = 'C' if $type =~ /controller|c/i;
380 $self->{type} = $type;
381 $self->{name} = $name;
382 $self->{class} = "$app\::$type\::$name";
386 my $appdir = File::Spec->catdir( split /\:\:/, $app );
387 my $path = File::Spec->catdir( $FindBin::Bin, '..', 'lib', $appdir, $type );
389 if ( $name =~ /\:/ ) {
390 my @path = split /\:\:/, $name;
392 $path = File::Spec->catdir( $path, @path );
395 $file = File::Spec->catfile( $path, "$file.pm" );
396 $self->{file} = $file;
399 my $dir = File::Spec->catdir( $FindBin::Bin, '..', 't' );
401 for my $i (<$dir/*.t>) {
402 $i =~ /(\d+)[^\/]*.t$/;
404 $num = $j if $j > $num;
407 $num = sprintf '%02d', $num;
410 $prefix = lc $prefix;
411 my $tname = lc( $num . $type . '_' . $prefix . '.t' );
412 $self->{prefix} = $prefix;
413 $self->{test_dir} = $dir;
414 $self->{test} = "$dir/$tname";
419 $comp = 'View' if $type eq 'V';
420 $comp = 'Controller' if $type eq 'C';
421 my $class = "Catalyst::Helper::$comp\::$helper";
422 eval "require $class";
423 die qq/Couldn't load helper "$class", "$@"/ if $@;
424 if ( $class->can('mk_compclass') ) {
425 $class->mk_compclass( $self, @args );
427 else { $self->_mk_compclass }
429 if ( $class->can('mk_comptest') ) {
430 $class->mk_comptest( $self, @args );
432 else { $self->_mk_comptest }
437 $self->_mk_compclass;
445 my $app = $self->{app};
446 my $class = $self->{class};
447 my $type = $self->{type};
449 $action = <<"EOF" if $type eq 'C';
454 my ( \$self, \$c ) = \@_;
455 \$c->res->output('Congratulations, $class is on Catalyst!');
460 my $file = $self->{file};
461 my $comp = IO::File->new("> $file")
462 or die qq/Couldn't open "$file", "$!"/;
467 use base 'Catalyst::Base';
487 This library is free software . You can redistribute it and/or modify it under
488 the same terms as perl itself.
498 my $prefix = $self->{prefix};
499 my $type = $self->{type};
500 my $class = $self->{class};
501 my $app = $self->{app};
502 my $test = $self->{test};
503 my $t = IO::File->new("> $test") or die qq/Couldn't open "$test", "$!"/;
505 if ( $self->{type} eq 'C' ) {
507 use Test::More tests => 3;
508 use_ok( Catalyst::Test, '$app' );
511 ok( request('$prefix')->is_success );
516 use Test::More tests => 1;
524 Helpers are classes that provide two methods.
526 * mk_compclass - creates the Component class
527 * mk_comptest - creates the Component test
529 So when you call C<bin/create view MyView TT>, create would try to execute
530 Catalyst::Helper::View::TT->mk_compclass and
531 Catalyst::Helper::View::TT->mk_comptest.
533 See L<Catalyst::Helper::View::TT> and L<Catalyst::Helper::Model::CDBI> for
536 All helper classes should be under one of the following namespaces.
538 Catalyst::Helper::Model::
539 Catalyst::Helper::View::
540 Catalyst::Helper::Controller::
544 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
545 L<Catalyst::Response>, L<Catalyst>
549 Sebastian Riedel, C<sri@oook.de>
553 This library is free software . You can redistribute it and/or modify it under
554 the same terms as perl itself.