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)
197 Run a Catalyst Testserver for this application.
201 Sebastian Riedel, C<sri\@oook.de>
205 Copyright 2004 Sebastian Riedel. All rights reserved.
207 This library is free software. You can redistribute it and/or modify it under
208 the same terms as perl itself.
212 chmod 0700, "$bin/server";
217 my $name = $self->{name};
218 my $bin = $self->{bin};
219 my $test = IO::File->new("> $bin/test")
220 or die qq/Could't open "$bin\/test", "$!"/;
228 use lib "\$FindBin::Bin/../lib";
232 GetOptions( 'help|?' => \\\$help );
234 pod2usage(1) if ( \$help || !\$ARGV[0] );
236 require Catalyst::Test;
237 import Catalyst::Test '$name';
239 print get(\$ARGV[0]) . "\n";
253 -help display this help and exits
256 test http://localhost/some_action
261 Run a Catalyst action from the comand line.
265 Sebastian Riedel, C<sri\@oook.de>
269 Copyright 2004 Sebastian Riedel. All rights reserved.
271 This library is free software. You can redistribute it and/or modify it under
272 the same terms as perl itself.
276 chmod 0700, "$bin/test";
281 my $name = $self->{name};
282 my $bin = $self->{bin};
283 my $create = IO::File->new("> $bin/create")
284 or die qq/Could't open "$bin\/create", "$!"/;
285 print $create <<"EOF";
291 use Catalyst::Helper;
295 GetOptions( 'help|?' => \$help );
297 pod2usage(1) if ( \$help || !\$ARGV[1] );
299 my \$helper = Catalyst::Helper->new;
300 pod2usage(1) unless \$helper->mk_component( '$name', \@ARGV );
307 create - Create a new Catalyst Component
311 create [options] model|view|controller name
314 -help display this help and exits
317 create controller My::Controller
318 create model My::Model
323 Create a new Catalyst Component.
327 Sebastian Riedel, C<sri\@oook.de>
331 Copyright 2004 Sebastian Riedel. All rights reserved.
333 This library is free software. You can redistribute it and/or modify it under
334 the same terms as perl itself.
338 chmod 0700, "$bin/create";
346 my ( $self, $app, $type, $name, $helper, @args ) = @_;
348 if ( $name =~ /[^\w\:]/ || !\$type =~ /^model|m|view|v|controller|c\$/i );
349 return 0 if $name =~ /[^\w\:]/;
350 $type = 'M' if $type =~ /model|m/i;
351 $type = 'V' if $type =~ /view|v/i;
352 $type = 'C' if $type =~ /controller|c/i;
353 $self->{type} = $type;
354 $self->{name} = $name;
355 $self->{class} = "$app\::$type\::$name";
359 my $appdir = File::Spec->catdir( split /\:\:/, $app );
360 my $path = File::Spec->catdir( $FindBin::Bin, '..', 'lib', $appdir, $type );
362 if ( $name =~ /\:/ ) {
363 my @path = split /\:\:/, $name;
365 $path = File::Spec->catdir( $path, @path );
368 $file = File::Spec->catfile( $path, "$file.pm" );
369 $self->{file} = $file;
372 my $dir = File::Spec->catdir( $FindBin::Bin, '..', 't' );
374 for my $i (<$dir/*.t>) {
375 $i =~ /(\d+)[^\/]*.t$/;
377 $num = $j if $j > $num;
380 $num = sprintf '%02d', $num;
383 $prefix = lc $prefix;
384 my $tname = lc( $num . $type . '_' . $prefix . '.t' );
385 $self->{prefix} = $prefix;
386 $self->{test_dir} = $dir;
387 $self->{test} = "$dir/$tname";
392 $comp = 'View' if $type eq 'V';
393 $comp = 'Controller' if $type eq 'C';
394 my $class = "Catalyst::Helper::$comp\::$helper";
395 eval "require $class";
396 die qq/Couldn't load helper "$class", "$@"/ if $@;
397 if ( $class->can('mk_compclass') ) {
398 $class->mk_compclass( $self, @args );
400 else { $self->_mk_compclass }
402 if ( $class->can('mk_comptest') ) {
403 $class->mk_comptest( $self, @args );
405 else { $self->_mk_comptest }
410 $self->_mk_compclass;
418 my $app = $self->{app};
419 my $class = $self->{class};
420 my $type = $self->{type};
422 $action = <<"EOF" if $type eq 'C';
427 my ( \$self, \$c ) = \@_;
428 \$c->res->output('Congratulations, $class is on Catalyst!');
433 my $file = $self->{file};
434 my $comp = IO::File->new("> $file")
435 or die qq/Couldn't open "$file", "$!"/;
440 use base 'Catalyst::Base';
460 This library is free software . You can redistribute it and/or modify it under
461 the same terms as perl itself.
471 my $prefix = $self->{prefix};
472 my $type = $self->{type};
473 my $class = $self->{class};
474 my $app = $self->{app};
475 my $test = $self->{test};
476 my $t = IO::File->new("> $test") or die qq/Couldn't open "$test", "$!"/;
478 if ( $self->{type} eq 'C' ) {
480 use Test::More tests => 3;
481 use_ok( Catalyst::Test, '$app' );
484 ok( request('$prefix')->is_success );
489 use Test::More tests => 1;
497 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
498 L<Catalyst::Response>, L<Catalyst>
502 Sebastian Riedel, C<sri@oook.de>
506 This library is free software . You can redistribute it and/or modify it under
507 the same terms as perl itself.