1 package Catalyst::Helper;
4 use base 'Class::Accessor::Fast';
13 Catalyst::Helper - Bootstrap a Catalyst application
17 See L<Catalyst::Manual::Intro>
21 Bootstrap a Catalyst application.
30 my ( $self, $name ) = @_;
31 return 0 if $name =~ /[^\w\:]/;
32 $self->{name} = $name;
34 $self->{dir} =~ s/\:\:/-/g;
52 my ( $self, $app, $type, $name, $helper, @args ) = @_;
54 if ( $name =~ /[^\w\:]/ || !\$type =~ /^model|m|view|v|controller|c\$/i );
55 return 0 if $name =~ /[^\w\:]/;
56 $type = 'M' if $type =~ /model|m/i;
57 $type = 'V' if $type =~ /view|v/i;
58 $type = 'C' if $type =~ /controller|c/i;
59 $self->{type} = $type;
60 $self->{name} = $name;
61 $self->{class} = "$app\::$type\::$name";
65 my $appdir = File::Spec->catdir( split /\:\:/, $app );
66 my $path = File::Spec->catdir( $FindBin::Bin, '..', 'lib', $appdir, $type );
68 if ( $name =~ /\:/ ) {
69 my @path = split /\:\:/, $name;
71 $path = File::Spec->catdir( $path, @path );
74 $file = File::Spec->catfile( $path, "$file.pm" );
75 $self->{file} = $file;
78 $self->{test_dir} = File::Spec->catdir( $FindBin::Bin, '..', 't' );
79 $self->{test} = $self->next_test;
84 $comp = 'View' if $type eq 'V';
85 $comp = 'Controller' if $type eq 'C';
86 my $class = "Catalyst::Helper::$comp\::$helper";
87 eval "require $class";
88 die qq/Couldn't load helper "$class", "$@"/ if $@;
89 if ( $class->can('mk_compclass') ) {
90 return 1 unless $class->mk_compclass( $self, @args );
92 else { return 1 unless $self->_mk_compclass }
94 if ( $class->can('mk_comptest') ) {
95 $class->mk_comptest( $self, @args );
97 else { $self->_mk_comptest }
102 return 1 unless $self->_mk_compclass;
113 my ( $self, $dir ) = @_;
115 print qq/ exists "$dir"\n/;
119 print qq/created "$dir"\n/;
122 die qq/Couldn't create "$dir", "$!"/;
130 my ( $self, $file, $content ) = @_;
132 print qq/ exists "$file"\n/;
135 if ( my $f = IO::File->new("> $file") ) {
137 print qq/created "$file"\n/;
140 die qq/Couldn't create "$file", "$!"/;
148 my ( $self, $tname ) = @_;
149 if ($tname) { $tname = "$tname.t" }
151 my $name = $self->{name};
154 $prefix = lc $prefix;
155 $tname = $prefix . '.t';
156 $self->{prefix} = $prefix;
158 my $dir = $self->{test_dir};
159 my $type = lc $self->{type};
160 return File::Spec->catfile( $dir, $type, $tname );
165 $self->mk_dir( $self->{dir} );
166 $self->{script} = File::Spec->catdir( $self->{dir}, 'script' );
167 $self->mk_dir( $self->{script} );
168 $self->{lib} = File::Spec->catdir( $self->{dir}, 'lib' );
169 $self->mk_dir( $self->{lib} );
170 $self->{root} = File::Spec->catdir( $self->{dir}, 'root' );
171 $self->mk_dir( $self->{root} );
172 $self->{t} = File::Spec->catdir( $self->{dir}, 't' );
173 $self->mk_dir( $self->{t} );
174 $self->mk_dir( File::Spec->catdir( $self->{t}, 'm' ) );
175 $self->mk_dir( File::Spec->catdir( $self->{t}, 'v' ) );
176 $self->mk_dir( File::Spec->catdir( $self->{t}, 'c' ) );
177 $self->{class} = File::Spec->catdir( split( /\:\:/, $self->{name} ) );
178 $self->{mod} = File::Spec->catdir( $self->{lib}, $self->{class} );
179 $self->mk_dir( $self->{mod} );
180 $self->{m} = File::Spec->catdir( $self->{mod}, 'M' );
181 $self->mk_dir( $self->{m} );
182 $self->{v} = File::Spec->catdir( $self->{mod}, 'V' );
183 $self->mk_dir( $self->{v} );
184 $self->{c} = File::Spec->catdir( $self->{mod}, 'C' );
185 $self->mk_dir( $self->{c} );
186 $self->{base} = File::Spec->rel2abs( $self->{dir} );
191 my $mod = $self->{mod};
192 my $name = $self->{name};
193 my $base = $self->{base};
194 $self->mk_file( "$mod.pm", <<"EOF");
198 use Catalyst qw/-Debug/;
200 our \$VERSION = '0.01';
204 root => '$base/root',
210 my ( \$self, \$c ) = \@_;
211 \$c->res->output('Congratulations, $name is on Catalyst!');
218 $name - A very nice application
226 Very nice application.
234 This library is free software . You can redistribute it and/or modify it under
235 the same terms as perl itself.
245 my $name = $self->{name};
246 my $dir = $self->{dir};
247 my $class = $self->{class};
248 $self->mk_file( "$dir\/Makefile.PL", <<"EOF");
249 use ExtUtils::MakeMaker;
253 VERSION_FROM => 'lib/$class.pm',
254 PREREQ_PM => { Catalyst => 0 },
255 test => { TESTS => join ' ', ( glob('t/*.t'), glob('t/*/*.t') ) }
262 my $dir = $self->{dir};
263 $self->mk_file( "$dir\/README", <<"EOF");
264 Run script/server.pl to test the application.
270 my $name = $self->{name};
271 my $dir = $self->{dir};
272 my $time = localtime time;
273 $self->mk_file( "$dir\/Changes", <<"EOF");
274 This file documents the revision history for Perl extension $name.
277 - initial revision, generated by Catalyst
284 my $name = $self->{name};
285 $self->mk_file( "$t\/01app.t", <<"EOF");
286 use Test::More tests => 2;
287 use_ok( Catalyst::Test, '$name' );
289 ok( request('/')->is_success );
291 $self->mk_file( "$t\/02podcoverage.t", <<"EOF");
294 eval "use Test::Pod::Coverage 1.04";
295 plan skip_all => 'Test::Pod::Coverage 1.04 required' if \$@;
296 plan skip_all => 'set TEST_POD to enable this test' unless \$ENV{TEST_POD};
298 all_pod_coverage_ok();
304 my $name = $self->{name};
305 my $script = $self->{script};
306 $self->mk_file( "$script\/server.pl", <<"EOF");
307 $Config{startperl} -w
313 use lib "\$FindBin::Bin/../lib";
314 use Catalyst::Test '$name';
319 GetOptions( 'help|?' => \\\$help, 'port=s' => \\\$port );
321 pod2usage(1) if \$help;
323 Catalyst::Test::server(\$port);
330 server - Catalyst Testserver
337 -? -help display this help and exits
338 -p -port port (defaults to 3000)
341 perldoc Catalyst::Manual
342 perldoc Catalyst::Manual::Intro
346 Run a Catalyst Testserver for this application.
350 Sebastian Riedel, C<sri\@oook.de>
354 Copyright 2004 Sebastian Riedel. All rights reserved.
356 This library is free software. You can redistribute it and/or modify it under
357 the same terms as perl itself.
361 chmod 0700, "$script/server.pl";
366 my $name = $self->{name};
367 my $script = $self->{script};
368 $self->mk_file( "$script/test.pl", <<"EOF");
369 $Config{startperl} -w
375 use lib "\$FindBin::Bin/../lib";
379 GetOptions( 'help|?' => \\\$help );
381 pod2usage(1) if ( \$help || !\$ARGV[0] );
383 require Catalyst::Test;
384 import Catalyst::Test '$name';
386 print get(\$ARGV[0]) . "\n";
397 test.pl [options] uri
400 -help display this help and exits
403 test.pl http://localhost/some_action
407 perldoc Catalyst::Manual
408 perldoc Catalyst::Manual::Intro
412 Run a Catalyst action from the comand line.
416 Sebastian Riedel, C<sri\@oook.de>
420 Copyright 2004 Sebastian Riedel. All rights reserved.
422 This library is free software. You can redistribute it and/or modify it under
423 the same terms as perl itself.
427 chmod 0700, "$script/test.pl";
432 my $name = $self->{name};
433 my $script = $self->{script};
434 $self->mk_file( "$script\/create.pl", <<"EOF");
435 $Config{startperl} -w
440 use Catalyst::Helper;
444 GetOptions( 'help|?' => \$help );
446 pod2usage(1) if ( \$help || !\$ARGV[1] );
448 my \$helper = Catalyst::Helper->new;
449 pod2usage(1) unless \$helper->mk_component( '$name', \@ARGV );
456 create - Create a new Catalyst Component
460 create.pl [options] model|view|controller name [helper] [options]
463 -help display this help and exits
466 create.pl controller My::Controller
467 create.pl view My::View
468 create.pl view MyView TT
470 create.pl model My::Model
471 create.pl model SomeDB CDBI dbi:SQLite:/tmp/my.db
472 create.pl model AnotherDB CDBI dbi:Pg:dbname=foo root 4321
475 perldoc Catalyst::Manual
476 perldoc Catalyst::Manual::Intro
480 Create a new Catalyst Component.
484 Sebastian Riedel, C<sri\@oook.de>
488 Copyright 2004 Sebastian Riedel. All rights reserved.
490 This library is free software. You can redistribute it and/or modify it under
491 the same terms as perl itself.
495 chmod 0700, "$script/create.pl";
500 my $app = $self->{app};
501 my $class = $self->{class};
502 my $type = $self->{type};
504 $action = <<"EOF" if $type eq 'C';
509 my ( \$self, \$c ) = \@_;
510 \$c->res->output('Congratulations, $class is on Catalyst!');
515 my $file = $self->{file};
516 return $self->mk_file( "$file", <<"EOF");
520 use base 'Catalyst::Base';
540 This library is free software . You can redistribute it and/or modify it under
541 the same terms as perl itself.
551 my $prefix = $self->{prefix};
552 my $type = $self->{type};
553 my $class = $self->{class};
554 my $app = $self->{app};
555 my $test = $self->{test};
556 if ( $self->{type} eq 'C' ) {
557 $self->mk_file( "$test", <<"EOF");
558 use Test::More tests => 3;
559 use_ok( Catalyst::Test, '$app' );
562 ok( request('$prefix')->is_success );
566 $self->mk_file( "$test", <<"EOF");
567 use Test::More tests => 1;
575 Helpers are classes that provide two methods.
577 * mk_compclass - creates the Component class
578 * mk_comptest - creates the Component test
580 So when you call C<bin/create view MyView TT>, create would try to execute
581 Catalyst::Helper::View::TT->mk_compclass and
582 Catalyst::Helper::View::TT->mk_comptest.
584 See L<Catalyst::Helper::View::TT> and L<Catalyst::Helper::Model::CDBI> for
587 All helper classes should be under one of the following namespaces.
589 Catalyst::Helper::Model::
590 Catalyst::Helper::View::
591 Catalyst::Helper::Controller::
595 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
596 L<Catalyst::Response>, L<Catalyst>
600 Sebastian Riedel, C<sri@oook.de>
604 This library is free software . You can redistribute it and/or modify it under
605 the same terms as perl itself.