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;
55 $self->{base} = File::Spec->catdir( $FindBin::Bin, '..' );
56 unless ( $_[0] =~ /^model|m|view|v|controller|c\$/i ) {
59 my $class = "Catalyst::Helper::$helper";
60 eval "require $class";
61 die qq/Couldn't load helper "$class", "$@"/ if $@;
62 if ( $class->can('mk_stuff') ) {
63 return 1 unless $class->mk_stuff( $self, @args );
71 return 0 if $name =~ /[^\w\:]/;
72 $type = 'M' if $type =~ /model|m/i;
73 $type = 'V' if $type =~ /view|v/i;
74 $type = 'C' if $type =~ /controller|c/i;
75 $self->{type} = $type;
76 $self->{name} = $name;
77 $self->{class} = "$app\::$type\::$name";
80 my $appdir = File::Spec->catdir( split /\:\:/, $app );
82 File::Spec->catdir( $FindBin::Bin, '..', 'lib', $appdir, $type );
84 if ( $name =~ /\:/ ) {
85 my @path = split /\:\:/, $name;
87 $path = File::Spec->catdir( $path, @path );
90 $file = File::Spec->catfile( $path, "$file.pm" );
91 $self->{file} = $file;
94 $self->{test_dir} = File::Spec->catdir( $FindBin::Bin, '..', 't' );
95 $self->{test} = $self->next_test;
100 $comp = 'View' if $type eq 'V';
101 $comp = 'Controller' if $type eq 'C';
102 my $class = "Catalyst::Helper::$comp\::$helper";
103 eval "require $class";
104 die qq/Couldn't load helper "$class", "$@"/ if $@;
105 if ( $class->can('mk_compclass') ) {
106 return 1 unless $class->mk_compclass( $self, @args );
108 else { return 1 unless $self->_mk_compclass }
110 if ( $class->can('mk_comptest') ) {
111 $class->mk_comptest( $self, @args );
113 else { $self->_mk_comptest }
118 return 1 unless $self->_mk_compclass;
130 my ( $self, $dir ) = @_;
132 print qq/ exists "$dir"\n/;
136 print qq/created "$dir"\n/;
139 die qq/Couldn't create "$dir", "$!"/;
147 my ( $self, $file, $content ) = @_;
149 print qq/ exists "$file"\n/;
152 if ( my $f = IO::File->new("> $file") ) {
154 print qq/created "$file"\n/;
157 die qq/Couldn't create "$file", "$!"/;
165 my ( $self, $tname ) = @_;
166 if ($tname) { $tname = "$tname.t" }
168 my $name = $self->{name};
171 $prefix = lc $prefix;
172 $tname = $prefix . '.t';
173 $self->{prefix} = $prefix;
175 my $dir = $self->{test_dir};
176 my $type = lc $self->{type};
177 return File::Spec->catfile( $dir, $type, $tname );
182 $self->mk_dir( $self->{dir} );
183 $self->{script} = File::Spec->catdir( $self->{dir}, 'script' );
184 $self->mk_dir( $self->{script} );
185 $self->{lib} = File::Spec->catdir( $self->{dir}, 'lib' );
186 $self->mk_dir( $self->{lib} );
187 $self->{root} = File::Spec->catdir( $self->{dir}, 'root' );
188 $self->mk_dir( $self->{root} );
189 $self->{t} = File::Spec->catdir( $self->{dir}, 't' );
190 $self->mk_dir( $self->{t} );
191 $self->mk_dir( File::Spec->catdir( $self->{t}, 'm' ) );
192 $self->mk_dir( File::Spec->catdir( $self->{t}, 'v' ) );
193 $self->mk_dir( File::Spec->catdir( $self->{t}, 'c' ) );
194 $self->{class} = File::Spec->catdir( split( /\:\:/, $self->{name} ) );
195 $self->{mod} = File::Spec->catdir( $self->{lib}, $self->{class} );
196 $self->mk_dir( $self->{mod} );
197 $self->{m} = File::Spec->catdir( $self->{mod}, 'M' );
198 $self->mk_dir( $self->{m} );
199 $self->{v} = File::Spec->catdir( $self->{mod}, 'V' );
200 $self->mk_dir( $self->{v} );
201 $self->{c} = File::Spec->catdir( $self->{mod}, 'C' );
202 $self->mk_dir( $self->{c} );
203 $self->{base} = File::Spec->rel2abs( $self->{dir} );
208 my $mod = $self->{mod};
209 my $name = $self->{name};
210 my $base = $self->{base};
211 $self->mk_file( "$mod.pm", <<"EOF");
215 use Catalyst qw/-Debug/;
217 our \$VERSION = '0.01';
221 root => '$base/root',
227 my ( \$self, \$c ) = \@_;
228 \$c->res->output('Congratulations, $name is on Catalyst!');
235 $name - A very nice application
243 Very nice application.
251 This library is free software . You can redistribute it and/or modify it under
252 the same terms as perl itself.
262 my $name = $self->{name};
263 my $dir = $self->{dir};
264 my $class = $self->{class};
265 $self->mk_file( "$dir\/Makefile.PL", <<"EOF");
266 use ExtUtils::MakeMaker;
270 VERSION_FROM => 'lib/$class.pm',
271 PREREQ_PM => { Catalyst => 0 },
272 test => { TESTS => join ' ', ( glob('t/*.t'), glob('t/*/*.t') ) }
279 my $dir = $self->{dir};
280 $self->mk_file( "$dir\/README", <<"EOF");
281 Run script/server.pl to test the application.
287 my $name = $self->{name};
288 my $dir = $self->{dir};
289 my $time = localtime time;
290 $self->mk_file( "$dir\/Changes", <<"EOF");
291 This file documents the revision history for Perl extension $name.
294 - initial revision, generated by Catalyst
301 my $name = $self->{name};
302 $self->mk_file( "$t\/01app.t", <<"EOF");
303 use Test::More tests => 2;
304 use_ok( Catalyst::Test, '$name' );
306 ok( request('/')->is_success );
308 $self->mk_file( "$t\/02podcoverage.t", <<"EOF");
311 eval "use Test::Pod::Coverage 1.04";
312 plan skip_all => 'Test::Pod::Coverage 1.04 required' if \$@;
313 plan skip_all => 'set TEST_POD to enable this test' unless \$ENV{TEST_POD};
315 all_pod_coverage_ok();
321 my $name = $self->{name};
322 my $script = $self->{script};
323 $self->mk_file( "$script\/server.pl", <<"EOF");
324 $Config{startperl} -w
330 use lib "\$FindBin::Bin/../lib";
331 use Catalyst::Test '$name';
336 GetOptions( 'help|?' => \\\$help, 'port=s' => \\\$port );
338 pod2usage(1) if \$help;
340 Catalyst::Test::server(\$port);
347 server - Catalyst Testserver
354 -? -help display this help and exits
355 -p -port port (defaults to 3000)
358 perldoc Catalyst::Manual
359 perldoc Catalyst::Manual::Intro
363 Run a Catalyst Testserver for this application.
367 Sebastian Riedel, C<sri\@oook.de>
371 Copyright 2004 Sebastian Riedel. All rights reserved.
373 This library is free software. You can redistribute it and/or modify it under
374 the same terms as perl itself.
378 chmod 0700, "$script/server.pl";
383 my $name = $self->{name};
384 my $script = $self->{script};
385 $self->mk_file( "$script/test.pl", <<"EOF");
386 $Config{startperl} -w
392 use lib "\$FindBin::Bin/../lib";
396 GetOptions( 'help|?' => \\\$help );
398 pod2usage(1) if ( \$help || !\$ARGV[0] );
400 require Catalyst::Test;
401 import Catalyst::Test '$name';
403 print get(\$ARGV[0]) . "\n";
414 test.pl [options] uri
417 -help display this help and exits
420 test.pl http://localhost/some_action
424 perldoc Catalyst::Manual
425 perldoc Catalyst::Manual::Intro
429 Run a Catalyst action from the comand line.
433 Sebastian Riedel, C<sri\@oook.de>
437 Copyright 2004 Sebastian Riedel. All rights reserved.
439 This library is free software. You can redistribute it and/or modify it under
440 the same terms as perl itself.
444 chmod 0700, "$script/test.pl";
449 my $name = $self->{name};
450 my $script = $self->{script};
451 $self->mk_file( "$script\/create.pl", <<"EOF");
452 $Config{startperl} -w
457 use Catalyst::Helper;
461 GetOptions( 'help|?' => \$help );
463 pod2usage(1) if ( \$help || !\$ARGV[0] );
465 my \$helper = Catalyst::Helper->new;
466 pod2usage(1) unless \$helper->mk_component( '$name', \@ARGV );
473 create - Create a new Catalyst Component
477 create.pl [options] model|view|controller name [helper] [options]
480 -help display this help and exits
483 create.pl controller My::Controller
484 create.pl view My::View
485 create.pl view MyView TT
487 create.pl model My::Model
488 create.pl model SomeDB CDBI dbi:SQLite:/tmp/my.db
489 create.pl model AnotherDB CDBI dbi:Pg:dbname=foo root 4321
493 perldoc Catalyst::Manual
494 perldoc Catalyst::Manual::Intro
498 Create a new Catalyst Component.
502 Sebastian Riedel, C<sri\@oook.de>
506 Copyright 2004 Sebastian Riedel. All rights reserved.
508 This library is free software. You can redistribute it and/or modify it under
509 the same terms as perl itself.
513 chmod 0700, "$script/create.pl";
518 my $app = $self->{app};
519 my $class = $self->{class};
520 my $type = $self->{type};
522 $action = <<"EOF" if $type eq 'C';
527 my ( \$self, \$c ) = \@_;
528 \$c->res->output('Congratulations, $class is on Catalyst!');
533 my $file = $self->{file};
534 return $self->mk_file( "$file", <<"EOF");
538 use base 'Catalyst::Base';
558 This library is free software . You can redistribute it and/or modify it under
559 the same terms as perl itself.
569 my $prefix = $self->{prefix};
570 my $type = $self->{type};
571 my $class = $self->{class};
572 my $app = $self->{app};
573 my $test = $self->{test};
574 if ( $self->{type} eq 'C' ) {
575 $self->mk_file( "$test", <<"EOF");
576 use Test::More tests => 3;
577 use_ok( Catalyst::Test, '$app' );
580 ok( request('$prefix')->is_success );
584 $self->mk_file( "$test", <<"EOF");
585 use Test::More tests => 1;
593 Helpers are classes that provide two methods.
595 * mk_compclass - creates the Component class
596 * mk_comptest - creates the Component test
598 So when you call C<bin/create view MyView TT>, create would try to execute
599 Catalyst::Helper::View::TT->mk_compclass and
600 Catalyst::Helper::View::TT->mk_comptest.
602 See L<Catalyst::Helper::View::TT> and L<Catalyst::Helper::Model::CDBI> for
605 All helper classes should be under one of the following namespaces.
607 Catalyst::Helper::Model::
608 Catalyst::Helper::View::
609 Catalyst::Helper::Controller::
613 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
614 L<Catalyst::Response>, L<Catalyst>
618 Sebastian Riedel, C<sri@oook.de>
622 This library is free software . You can redistribute it and/or modify it under
623 the same terms as perl itself.