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 );
157 my $name = $self->{name};
158 my $bin = $self->{bin};
159 my $server = IO::File->new("> $bin/server")
160 or die qq/Could't open "$bin\/server", "$!"/;
161 print $server <<"EOF";
168 use lib "\$FindBin::Bin/../lib";
169 use Catalyst::Test '$name';
174 GetOptions( 'help|?' => \\\$help, 'port=s' => \\\$port );
176 pod2usage(1) if \$help;
178 Catalyst::Test::server(\$port);
185 server - Catalyst Testserver
192 -help display this help and exits
193 -port port (defaults to 3000)
196 perldoc Catalyst::Manual
197 perldoc Catalyst::Manual::Intro
201 Run a Catalyst Testserver for this application.
205 Sebastian Riedel, C<sri\@oook.de>
209 Copyright 2004 Sebastian Riedel. All rights reserved.
211 This library is free software. You can redistribute it and/or modify it under
212 the same terms as perl itself.
216 chmod 0700, "$bin/server";
221 my $name = $self->{name};
222 my $bin = $self->{bin};
223 my $test = IO::File->new("> $bin/test")
224 or die qq/Could't open "$bin\/test", "$!"/;
232 use lib "\$FindBin::Bin/../lib";
236 GetOptions( 'help|?' => \\\$help );
238 pod2usage(1) if ( \$help || !\$ARGV[0] );
240 require Catalyst::Test;
241 import Catalyst::Test '$name';
243 print get(\$ARGV[0]) . "\n";
257 -help display this help and exits
260 perl test http://localhost/some_action
261 perl test /some_action
264 perldoc Catalyst::Manual
265 perldoc Catalyst::Manual::Intro
269 Run a Catalyst action from the comand line.
273 Sebastian Riedel, C<sri\@oook.de>
277 Copyright 2004 Sebastian Riedel. All rights reserved.
279 This library is free software. You can redistribute it and/or modify it under
280 the same terms as perl itself.
284 chmod 0700, "$bin/test";
289 my $name = $self->{name};
290 my $bin = $self->{bin};
291 my $create = IO::File->new("> $bin/create")
292 or die qq/Could't open "$bin\/create", "$!"/;
293 print $create <<"EOF";
299 use Catalyst::Helper;
303 GetOptions( 'help|?' => \$help );
305 pod2usage(1) if ( \$help || !\$ARGV[1] );
307 my \$helper = Catalyst::Helper->new;
308 pod2usage(1) unless \$helper->mk_component( '$name', \@ARGV );
315 create - Create a new Catalyst Component
319 create [options] model|view|controller name [helper] [options]
322 -help display this help and exits
325 perl create controller My::Controller
326 perl create view My::View
327 perl create view MyView TT
328 perl create view TT TT
329 perl create model My::Model
330 perl create model SomeDB CDBI dbi:SQLite:/tmp/my.db
331 perl create model AnotherDB CDBI dbi:Pg:dbname=foo root 4321
334 perldoc Catalyst::Manual
335 perldoc Catalyst::Manual::Intro
339 Create a new Catalyst Component.
343 Sebastian Riedel, C<sri\@oook.de>
347 Copyright 2004 Sebastian Riedel. All rights reserved.
349 This library is free software. You can redistribute it and/or modify it under
350 the same terms as perl itself.
354 chmod 0700, "$bin/create";
362 my ( $self, $app, $type, $name, $helper, @args ) = @_;
364 if ( $name =~ /[^\w\:]/ || !\$type =~ /^model|m|view|v|controller|c\$/i );
365 return 0 if $name =~ /[^\w\:]/;
366 $type = 'M' if $type =~ /model|m/i;
367 $type = 'V' if $type =~ /view|v/i;
368 $type = 'C' if $type =~ /controller|c/i;
369 $self->{type} = $type;
370 $self->{name} = $name;
371 $self->{class} = "$app\::$type\::$name";
375 my $appdir = File::Spec->catdir( split /\:\:/, $app );
376 my $path = File::Spec->catdir( $FindBin::Bin, '..', 'lib', $appdir, $type );
378 if ( $name =~ /\:/ ) {
379 my @path = split /\:\:/, $name;
381 $path = File::Spec->catdir( $path, @path );
384 $file = File::Spec->catfile( $path, "$file.pm" );
385 $self->{file} = $file;
388 my $dir = File::Spec->catdir( $FindBin::Bin, '..', 't' );
390 for my $i (<$dir/*.t>) {
391 $i =~ /(\d+)[^\/]*.t$/;
393 $num = $j if $j > $num;
396 $num = sprintf '%02d', $num;
399 $prefix = lc $prefix;
400 my $tname = lc( $num . $type . '_' . $prefix . '.t' );
401 $self->{prefix} = $prefix;
402 $self->{test_dir} = $dir;
403 $self->{test} = "$dir/$tname";
408 $comp = 'View' if $type eq 'V';
409 $comp = 'Controller' if $type eq 'C';
410 my $class = "Catalyst::Helper::$comp\::$helper";
411 eval "require $class";
412 die qq/Couldn't load helper "$class", "$@"/ if $@;
413 if ( $class->can('mk_compclass') ) {
414 $class->mk_compclass( $self, @args );
416 else { $self->_mk_compclass }
418 if ( $class->can('mk_comptest') ) {
419 $class->mk_comptest( $self, @args );
421 else { $self->_mk_comptest }
426 $self->_mk_compclass;
434 my $app = $self->{app};
435 my $class = $self->{class};
436 my $type = $self->{type};
438 $action = <<"EOF" if $type eq 'C';
443 my ( \$self, \$c ) = \@_;
444 \$c->res->output('Congratulations, $class is on Catalyst!');
449 my $file = $self->{file};
450 my $comp = IO::File->new("> $file")
451 or die qq/Couldn't open "$file", "$!"/;
456 use base 'Catalyst::Base';
476 This library is free software . You can redistribute it and/or modify it under
477 the same terms as perl itself.
487 my $prefix = $self->{prefix};
488 my $type = $self->{type};
489 my $class = $self->{class};
490 my $app = $self->{app};
491 my $test = $self->{test};
492 my $t = IO::File->new("> $test") or die qq/Couldn't open "$test", "$!"/;
494 if ( $self->{type} eq 'C' ) {
496 use Test::More tests => 3;
497 use_ok( Catalyst::Test, '$app' );
500 ok( request('$prefix')->is_success );
505 use Test::More tests => 1;
513 Helpers are classes that provide two methods.
515 * mk_compclass - creates the Component class
516 * mk_comptest - creates the Component test
518 So when you call C<bin/create view MyView TT>, create would try to execute
519 Catalyst::Helper::View::TT->mk_compclass and
520 Catalyst::Helper::View::TT->mk_comptest.
522 See L<Catalyst::Helper::View::TT> and L<Catalyst::Helper::Model::CDBI> for
525 All helper classes should be under one of the following namespaces.
527 Catalyst::Helper::Model::
528 Catalyst::Helper::View::
529 Catalyst::Helper::Controller::
533 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
534 L<Catalyst::Response>, L<Catalyst>
538 Sebastian Riedel, C<sri@oook.de>
542 This library is free software . You can redistribute it and/or modify it under
543 the same terms as perl itself.