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;
49 my ( $self, $app, $type, $name, $helper, @args ) = @_;
51 if ( $name =~ /[^\w\:]/ || !\$type =~ /^model|m|view|v|controller|c\$/i );
52 return 0 if $name =~ /[^\w\:]/;
53 $type = 'M' if $type =~ /model|m/i;
54 $type = 'V' if $type =~ /view|v/i;
55 $type = 'C' if $type =~ /controller|c/i;
56 $self->{type} = $type;
57 $self->{name} = $name;
58 $self->{class} = "$app\::$type\::$name";
62 my $appdir = File::Spec->catdir( split /\:\:/, $app );
63 my $path = File::Spec->catdir( $FindBin::Bin, '..', 'lib', $appdir, $type );
65 if ( $name =~ /\:/ ) {
66 my @path = split /\:\:/, $name;
68 $path = File::Spec->catdir( $path, @path );
71 $file = File::Spec->catfile( $path, "$file.pm" );
72 $self->{file} = $file;
75 $self->{test_dir} = File::Spec->catdir( $FindBin::Bin, '..', 't' );
76 $self->{test} = $self->next_test;
81 $comp = 'View' if $type eq 'V';
82 $comp = 'Controller' if $type eq 'C';
83 my $class = "Catalyst::Helper::$comp\::$helper";
84 eval "require $class";
85 die qq/Couldn't load helper "$class", "$@"/ if $@;
86 if ( $class->can('mk_compclass') ) {
87 return 1 unless $class->mk_compclass( $self, @args );
89 else { return 1 unless $self->_mk_compclass }
91 if ( $class->can('mk_comptest') ) {
92 $class->mk_comptest( $self, @args );
94 else { $self->_mk_comptest }
99 return 1 unless $self->_mk_compclass;
110 my ( $self, $dir ) = @_;
112 print qq/ exists "$dir"\n/;
116 print qq/created "$dir"\n/;
119 die qq/Couldn't create "$dir", "$!"/;
127 my ( $self, $file, $content ) = @_;
129 print qq/ exists "$file"\n/;
132 if ( my $f = IO::File->new("> $file") ) {
134 print qq/created "$file"\n/;
137 die qq/Couldn't create "$file", "$!"/;
145 my ( $self, $tname ) = @_;
146 my $dir = $self->{test_dir};
148 for my $i (<$dir/*.t>) {
149 $i =~ /(\d+)[^\/]*.t$/;
151 $num = $j if $j > $num;
154 $num = sprintf '%02d', $num;
155 if ($tname) { $tname = "$num$tname.t" }
157 my $name = $self->{name};
158 my $type = $self->{type};
161 $prefix = lc $prefix;
162 $tname = lc( $num . $type . '_' . $prefix . '.t' );
163 $self->{prefix} = $prefix;
165 return "$dir/$tname";
170 $self->mk_dir( $self->{dir} );
171 $self->{bin} = File::Spec->catdir( $self->{dir}, 'bin' );
172 $self->mk_dir( $self->{bin} );
173 $self->{lib} = File::Spec->catdir( $self->{dir}, 'lib' );
174 $self->mk_dir( $self->{lib} );
175 $self->{root} = File::Spec->catdir( $self->{dir}, 'root' );
176 $self->mk_dir( $self->{root} );
177 $self->{t} = File::Spec->catdir( $self->{dir}, 't' );
178 $self->mk_dir( $self->{t} );
179 $self->{class} = File::Spec->catdir( split( /\:\:/, $self->{name} ) );
180 $self->{mod} = File::Spec->catdir( $self->{lib}, $self->{class} );
181 $self->mk_dir( $self->{mod} );
182 $self->{m} = File::Spec->catdir( $self->{mod}, 'M' );
183 $self->mk_dir( $self->{m} );
184 $self->{v} = File::Spec->catdir( $self->{mod}, 'V' );
185 $self->mk_dir( $self->{v} );
186 $self->{c} = File::Spec->catdir( $self->{mod}, 'C' );
187 $self->mk_dir( $self->{c} );
188 $self->{base} = File::Spec->rel2abs( $self->{dir} );
193 my $mod = $self->{mod};
194 my $name = $self->{name};
195 my $base = $self->{base};
196 $self->mk_file( "$mod.pm", <<"EOF");
200 use Catalyst qw/-Debug/;
202 our \$VERSION = '0.01';
206 root => '$base/root',
212 my ( \$self, \$c ) = \@_;
213 \$c->res->output('Congratulations, $name is on Catalyst!');
220 $name - A very nice application
228 Very nice application.
236 This library is free software . You can redistribute it and/or modify it under
237 the same terms as perl itself.
247 my $name = $self->{name};
248 my $dir = $self->{dir};
249 my $class = $self->{class};
250 $self->mk_file( "$dir\/Makefile.PL", <<"EOF");
251 use ExtUtils::MakeMaker;
255 VERSION_FROM => 'lib/$class.pm',
256 PREREQ_PM => { Catalyst => 0 }
264 my $name = $self->{name};
265 $self->mk_file( "$t\/01app.t", <<"EOF");
266 use Test::More tests => 2;
267 use_ok( Catalyst::Test, '$name' );
269 ok( request('/')->is_success );
271 $self->mk_file( "$t\/02podcoverage.t", <<"EOF");
274 eval "use Test::Pod::Coverage 1.04";
275 plan skip_all => 'Test::Pod::Coverage 1.04 required' if \$@;
276 plan skip_all => 'set TEST_POD to enable this test' unless \$ENV{TEST_POD};
278 all_pod_coverage_ok();
284 my $name = $self->{name};
285 my $bin = $self->{bin};
286 $self->mk_file( "$bin\/server", <<"EOF");
293 use lib "\$FindBin::Bin/../lib";
294 use Catalyst::Test '$name';
299 GetOptions( 'help|?' => \\\$help, 'port=s' => \\\$port );
301 pod2usage(1) if \$help;
303 Catalyst::Test::server(\$port);
310 server - Catalyst Testserver
317 -? -help display this help and exits
318 -p -port port (defaults to 3000)
321 perldoc Catalyst::Manual
322 perldoc Catalyst::Manual::Intro
326 Run a Catalyst Testserver for this application.
330 Sebastian Riedel, C<sri\@oook.de>
334 Copyright 2004 Sebastian Riedel. All rights reserved.
336 This library is free software. You can redistribute it and/or modify it under
337 the same terms as perl itself.
341 chmod 0700, "$bin/server";
346 my $name = $self->{name};
347 my $bin = $self->{bin};
348 $self->mk_file( "$bin/test", <<"EOF");
355 use lib "\$FindBin::Bin/../lib";
359 GetOptions( 'help|?' => \\\$help );
361 pod2usage(1) if ( \$help || !\$ARGV[0] );
363 require Catalyst::Test;
364 import Catalyst::Test '$name';
366 print get(\$ARGV[0]) . "\n";
380 -help display this help and exits
383 perl test http://localhost/some_action
384 perl test /some_action
387 perldoc Catalyst::Manual
388 perldoc Catalyst::Manual::Intro
392 Run a Catalyst action from the comand line.
396 Sebastian Riedel, C<sri\@oook.de>
400 Copyright 2004 Sebastian Riedel. All rights reserved.
402 This library is free software. You can redistribute it and/or modify it under
403 the same terms as perl itself.
407 chmod 0700, "$bin/test";
412 my $name = $self->{name};
413 my $bin = $self->{bin};
414 $self->mk_file( "$bin\/create", <<"EOF");
420 use Catalyst::Helper;
424 GetOptions( 'help|?' => \$help );
426 pod2usage(1) if ( \$help || !\$ARGV[1] );
428 my \$helper = Catalyst::Helper->new;
429 pod2usage(1) unless \$helper->mk_component( '$name', \@ARGV );
436 create - Create a new Catalyst Component
440 create [options] model|view|controller name [helper] [options]
443 -help display this help and exits
446 perl create controller My::Controller
447 perl create view My::View
448 perl create view MyView TT
449 perl create view TT TT
450 perl create model My::Model
451 perl create model SomeDB CDBI dbi:SQLite:/tmp/my.db
452 perl create model AnotherDB CDBI dbi:Pg:dbname=foo root 4321
455 perldoc Catalyst::Manual
456 perldoc Catalyst::Manual::Intro
460 Create a new Catalyst Component.
464 Sebastian Riedel, C<sri\@oook.de>
468 Copyright 2004 Sebastian Riedel. All rights reserved.
470 This library is free software. You can redistribute it and/or modify it under
471 the same terms as perl itself.
475 chmod 0700, "$bin/create";
480 my $app = $self->{app};
481 my $class = $self->{class};
482 my $type = $self->{type};
484 $action = <<"EOF" if $type eq 'C';
489 my ( \$self, \$c ) = \@_;
490 \$c->res->output('Congratulations, $class is on Catalyst!');
495 my $file = $self->{file};
496 return $self->mk_file( "$file", <<"EOF");
500 use base 'Catalyst::Base';
520 This library is free software . You can redistribute it and/or modify it under
521 the same terms as perl itself.
531 my $prefix = $self->{prefix};
532 my $type = $self->{type};
533 my $class = $self->{class};
534 my $app = $self->{app};
535 my $test = $self->{test};
536 if ( $self->{type} eq 'C' ) {
537 $self->mk_file( "$test", <<"EOF");
538 use Test::More tests => 3;
539 use_ok( Catalyst::Test, '$app' );
542 ok( request('$prefix')->is_success );
546 $self->mk_file( "$test", <<"EOF");
547 use Test::More tests => 1;
555 Helpers are classes that provide two methods.
557 * mk_compclass - creates the Component class
558 * mk_comptest - creates the Component test
560 So when you call C<bin/create view MyView TT>, create would try to execute
561 Catalyst::Helper::View::TT->mk_compclass and
562 Catalyst::Helper::View::TT->mk_comptest.
564 See L<Catalyst::Helper::View::TT> and L<Catalyst::Helper::Model::CDBI> for
567 All helper classes should be under one of the following namespaces.
569 Catalyst::Helper::Model::
570 Catalyst::Helper::View::
571 Catalyst::Helper::Controller::
575 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
576 L<Catalyst::Response>, L<Catalyst>
580 Sebastian Riedel, C<sri@oook.de>
584 This library is free software . You can redistribute it and/or modify it under
585 the same terms as perl itself.