new helper api
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Helper.pm
CommitLineData
fc7ec1d9 1package Catalyst::Helper;
2
3use strict;
4use base 'Class::Accessor::Fast';
5use File::Spec;
6use File::Path;
7use IO::File;
8use FindBin;
9
10=head1 NAME
11
12Catalyst::Helper - Bootstrap a Catalyst application
13
14=head1 SYNOPSIS
15
16See L<Catalyst::Manual::Intro>
17
18=head1 DESCRIPTION
19
20Bootstrap a Catalyst application.
21
22=head2 METHODS
23
24=head3 mk_app
25
26=cut
27
28sub mk_app {
29 my ( $self, $name ) = @_;
30 return 0 if $name =~ /[^\w\:]/;
31 $self->{name} = $name;
32 $self->{dir} = $name;
33 $self->{dir} =~ s/\:\:/-/g;
34 $self->_mk_dirs;
35 $self->_mk_appclass;
36 $self->_mk_makefile;
37 $self->_mk_apptest;
38 $self->_mk_server;
39 $self->_mk_test;
40 $self->_mk_create;
41 return 1;
42}
43
ad41e777 44=head3 mk_component
45
46=cut
47
48sub mk_component {
49 my ( $self, $app, $type, $name, $helper, @args ) = @_;
50 return 0
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";
59 $self->{app} = $app;
60
61 # Class
62 my $appdir = File::Spec->catdir( split /\:\:/, $app );
63 my $path = File::Spec->catdir( $FindBin::Bin, '..', 'lib', $appdir, $type );
64 my $file = $name;
65 if ( $name =~ /\:/ ) {
66 my @path = split /\:\:/, $name;
67 $file = pop @path;
68 $path = File::Spec->catdir( $path, @path );
69 mkpath $path;
70 }
71 $file = File::Spec->catfile( $path, "$file.pm" );
72 $self->{file} = $file;
73
74 # Test
75 $self->{test_dir} = File::Spec->catdir( $FindBin::Bin, '..', 't' );
76 $self->{test} = $self->next_test;
77
78 # Helper
79 if ($helper) {
80 my $comp = 'Model';
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 );
88 }
89 else { return 1 unless $self->_mk_compclass }
90
91 if ( $class->can('mk_comptest') ) {
92 $class->mk_comptest( $self, @args );
93 }
94 else { $self->_mk_comptest }
95 }
96
97 # Fallback
98 else {
99 return 1 unless $self->_mk_compclass;
100 $self->_mk_comptest;
101 }
102 return 1;
103}
104
105=head3 mk_dir
106
107=cut
108
109sub mk_dir {
110 my ( $self, $dir ) = @_;
111 if ( -d $dir ) {
112 print qq/ exists "$dir"\n/;
113 return 0;
114 }
115 if ( mkpath $dir) {
116 print qq/created "$dir"\n/;
117 return 1;
118 }
119 die qq/Couldn't create "$dir", "$!"/;
120}
121
122=head3 mk_file
123
124=cut
125
126sub mk_file {
127 my ( $self, $file, $content ) = @_;
128 if ( -e $file ) {
129 print qq/ exists "$file"\n/;
130 return 0;
131 }
132 if ( my $f = IO::File->new("> $file") ) {
133 print $f $content;
134 print qq/created "$file"\n/;
135 return 1;
136 }
137 die qq/Couldn't create "$file", "$!"/;
138}
139
140=head3 next_test
141
142=cut
143
144sub next_test {
145 my ( $self, $tname ) = @_;
146 my $dir = $self->{test_dir};
147 my $num = '01';
148 for my $i (<$dir/*.t>) {
149 $i =~ /(\d+)[^\/]*.t$/;
150 my $j = $1 || $num;
151 $num = $j if $j > $num;
152 }
153 $num++;
154 $num = sprintf '%02d', $num;
155 if ($tname) { $tname = "$num$tname.t" }
156 else {
157 my $name = $self->{name};
158 my $type = $self->{type};
159 my $prefix = $name;
160 $prefix =~ s/::/_/g;
161 $prefix = lc $prefix;
162 $tname = lc( $num . $type . '_' . $prefix . '.t' );
163 $self->{prefix} = $prefix;
164 }
165 return "$dir/$tname";
166}
167
fc7ec1d9 168sub _mk_dirs {
169 my $self = shift;
ad41e777 170 $self->mk_dir( $self->{dir} );
fc7ec1d9 171 $self->{bin} = File::Spec->catdir( $self->{dir}, 'bin' );
ad41e777 172 $self->mk_dir( $self->{bin} );
fc7ec1d9 173 $self->{lib} = File::Spec->catdir( $self->{dir}, 'lib' );
ad41e777 174 $self->mk_dir( $self->{lib} );
fc7ec1d9 175 $self->{root} = File::Spec->catdir( $self->{dir}, 'root' );
ad41e777 176 $self->mk_dir( $self->{root} );
fc7ec1d9 177 $self->{t} = File::Spec->catdir( $self->{dir}, 't' );
ad41e777 178 $self->mk_dir( $self->{t} );
fc7ec1d9 179 $self->{class} = File::Spec->catdir( split( /\:\:/, $self->{name} ) );
180 $self->{mod} = File::Spec->catdir( $self->{lib}, $self->{class} );
ad41e777 181 $self->mk_dir( $self->{mod} );
fc7ec1d9 182 $self->{m} = File::Spec->catdir( $self->{mod}, 'M' );
ad41e777 183 $self->mk_dir( $self->{m} );
fc7ec1d9 184 $self->{v} = File::Spec->catdir( $self->{mod}, 'V' );
ad41e777 185 $self->mk_dir( $self->{v} );
fc7ec1d9 186 $self->{c} = File::Spec->catdir( $self->{mod}, 'C' );
ad41e777 187 $self->mk_dir( $self->{c} );
fc7ec1d9 188 $self->{base} = File::Spec->rel2abs( $self->{dir} );
189}
190
191sub _mk_appclass {
ad41e777 192 my $self = shift;
193 my $mod = $self->{mod};
194 my $name = $self->{name};
195 my $base = $self->{base};
196 $self->mk_file( "$mod.pm", <<"EOF");
fc7ec1d9 197package $name;
198
199use strict;
200use Catalyst qw/-Debug/;
201
202our \$VERSION = '0.01';
203
204$name->config(
205 name => '$name',
206 root => '$base/root',
207);
208
209$name->action(
210
211 '!default' => sub {
212 my ( \$self, \$c ) = \@_;
213 \$c->res->output('Congratulations, $name is on Catalyst!');
214 },
215
216);
217
218=head1 NAME
219
220$name - A very nice application
221
222=head1 SYNOPSIS
223
224 Very simple to use
225
226=head1 DESCRIPTION
227
228Very nice application.
229
230=head1 AUTHOR
231
232Clever guy
233
234=head1 LICENSE
235
236This library is free software . You can redistribute it and/or modify it under
237the same terms as perl itself.
238
239=cut
240
2411;
242EOF
243}
244
245sub _mk_makefile {
ad41e777 246 my $self = shift;
247 my $name = $self->{name};
248 my $dir = $self->{dir};
249 my $class = $self->{class};
250 $self->mk_file( "$dir\/Makefile.PL", <<"EOF");
fc7ec1d9 251use ExtUtils::MakeMaker;
252
253WriteMakefile(
254 NAME => '$name',
255 VERSION_FROM => 'lib/$class.pm',
256 PREREQ_PM => { Catalyst => 0 }
257);
258EOF
259}
260
261sub _mk_apptest {
262 my $self = shift;
263 my $t = $self->{t};
264 my $name = $self->{name};
ad41e777 265 $self->mk_file( "$t\/01app.t", <<"EOF");
fc7ec1d9 266use Test::More tests => 2;
267use_ok( Catalyst::Test, '$name' );
268
269ok( request('/')->is_success );
270EOF
ad41e777 271 $self->mk_file( "$t\/02podcoverage.t", <<"EOF");
1df125c9 272use Test::More;
273
274eval "use Test::Pod::Coverage 1.04";
275plan skip_all => 'Test::Pod::Coverage 1.04 required' if \$@;
276plan skip_all => 'set TEST_POD to enable this test' unless \$ENV{TEST_POD};
277
278all_pod_coverage_ok();
279EOF
fc7ec1d9 280}
281
282sub _mk_server {
ad41e777 283 my $self = shift;
284 my $name = $self->{name};
285 my $bin = $self->{bin};
286 $self->mk_file( "$bin\/server", <<"EOF");
fc7ec1d9 287#!/usr/bin/perl -w
288
289use strict;
290use Getopt::Long;
291use Pod::Usage;
292use FindBin;
293use lib "\$FindBin::Bin/../lib";
294use Catalyst::Test '$name';
295
296my \$help = 0;
297my \$port = 3000;
298
299GetOptions( 'help|?' => \\\$help, 'port=s' => \\\$port );
300
301pod2usage(1) if \$help;
302
303Catalyst::Test::server(\$port);
304
3051;
306__END__
307
308=head1 NAME
309
310server - Catalyst Testserver
311
312=head1 SYNOPSIS
313
314server [options]
315
316 Options:
ad41e777 317 -? -help display this help and exits
318 -p -port port (defaults to 3000)
fc7ec1d9 319
03a53815 320 See also:
321 perldoc Catalyst::Manual
322 perldoc Catalyst::Manual::Intro
323
fc7ec1d9 324=head1 DESCRIPTION
325
326Run a Catalyst Testserver for this application.
327
328=head1 AUTHOR
329
330Sebastian Riedel, C<sri\@oook.de>
331
332=head1 COPYRIGHT
333
334Copyright 2004 Sebastian Riedel. All rights reserved.
335
336This library is free software. You can redistribute it and/or modify it under
337the same terms as perl itself.
338
339=cut
340EOF
341 chmod 0700, "$bin/server";
342}
343
344sub _mk_test {
345 my $self = shift;
346 my $name = $self->{name};
347 my $bin = $self->{bin};
ad41e777 348 $self->mk_file( "$bin/test", <<"EOF");
fc7ec1d9 349#!/usr/bin/perl -w
350
351use strict;
352use Getopt::Long;
353use Pod::Usage;
354use FindBin;
355use lib "\$FindBin::Bin/../lib";
356
357my \$help = 0;
358
359GetOptions( 'help|?' => \\\$help );
360
361pod2usage(1) if ( \$help || !\$ARGV[0] );
362
363require Catalyst::Test;
364import Catalyst::Test '$name';
365
366print get(\$ARGV[0]) . "\n";
367
3681;
369__END__
370
371=head1 NAME
372
373test - Catalyst Test
374
375=head1 SYNOPSIS
376
377test [options] uri
378
379 Options:
380 -help display this help and exits
381
382 Examples:
d7c505f3 383 perl test http://localhost/some_action
384 perl test /some_action
fc7ec1d9 385
03a53815 386 See also:
387 perldoc Catalyst::Manual
388 perldoc Catalyst::Manual::Intro
389
fc7ec1d9 390=head1 DESCRIPTION
391
392Run a Catalyst action from the comand line.
393
394=head1 AUTHOR
395
396Sebastian Riedel, C<sri\@oook.de>
397
398=head1 COPYRIGHT
399
400Copyright 2004 Sebastian Riedel. All rights reserved.
401
402This library is free software. You can redistribute it and/or modify it under
403the same terms as perl itself.
404
405=cut
406EOF
407 chmod 0700, "$bin/test";
408}
409
410sub _mk_create {
ad41e777 411 my $self = shift;
412 my $name = $self->{name};
413 my $bin = $self->{bin};
414 $self->mk_file( "$bin\/create", <<"EOF");
fc7ec1d9 415#!/usr/bin/perl -w
416
417use strict;
418use Getopt::Long;
419use Pod::Usage;
420use Catalyst::Helper;
421
422my \$help = 0;
423
424GetOptions( 'help|?' => \$help );
425
426pod2usage(1) if ( \$help || !\$ARGV[1] );
427
428my \$helper = Catalyst::Helper->new;
429pod2usage(1) unless \$helper->mk_component( '$name', \@ARGV );
430
4311;
432__END__
433
434=head1 NAME
435
436create - Create a new Catalyst Component
437
438=head1 SYNOPSIS
439
03a53815 440create [options] model|view|controller name [helper] [options]
fc7ec1d9 441
442 Options:
443 -help display this help and exits
444
445 Examples:
d7c505f3 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
03a53815 453
454 See also:
455 perldoc Catalyst::Manual
456 perldoc Catalyst::Manual::Intro
fc7ec1d9 457
458=head1 DESCRIPTION
459
460Create a new Catalyst Component.
461
462=head1 AUTHOR
463
464Sebastian Riedel, C<sri\@oook.de>
465
466=head1 COPYRIGHT
467
468Copyright 2004 Sebastian Riedel. All rights reserved.
469
470This library is free software. You can redistribute it and/or modify it under
471the same terms as perl itself.
472
473=cut
474EOF
475 chmod 0700, "$bin/create";
476}
477
fc7ec1d9 478sub _mk_compclass {
479 my $self = shift;
480 my $app = $self->{app};
481 my $class = $self->{class};
482 my $type = $self->{type};
483 my $action = '';
484 $action = <<"EOF" if $type eq 'C';
485
486$app->action(
487
488 '!?default' => sub {
489 my ( \$self, \$c ) = \@_;
490 \$c->res->output('Congratulations, $class is on Catalyst!');
491 },
492
493);
494EOF
495 my $file = $self->{file};
ad41e777 496 return $self->mk_file( "$file", <<"EOF");
fc7ec1d9 497package $class;
498
499use strict;
500use base 'Catalyst::Base';
501$action
502=head1 NAME
503
504$class - A Component
505
506=head1 SYNOPSIS
507
508 Very simple to use
509
510=head1 DESCRIPTION
511
512Very nice component.
513
514=head1 AUTHOR
515
516Clever guy
517
518=head1 LICENSE
519
520This library is free software . You can redistribute it and/or modify it under
521the same terms as perl itself.
522
523=cut
524
5251;
526EOF
527}
528
529sub _mk_comptest {
530 my $self = shift;
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};
fc7ec1d9 536 if ( $self->{type} eq 'C' ) {
ad41e777 537 $self->mk_file( "$test", <<"EOF");
fc7ec1d9 538use Test::More tests => 3;
539use_ok( Catalyst::Test, '$app' );
540use_ok('$class');
541
542ok( request('$prefix')->is_success );
543EOF
544 }
545 else {
ad41e777 546 $self->mk_file( "$test", <<"EOF");
fc7ec1d9 547use Test::More tests => 1;
548use_ok('$class');
549EOF
550 }
551}
552
7833fdfc 553=head1 HELPERS
554
555Helpers are classes that provide two methods.
556
557 * mk_compclass - creates the Component class
558 * mk_comptest - creates the Component test
559
560So when you call C<bin/create view MyView TT>, create would try to execute
561Catalyst::Helper::View::TT->mk_compclass and
562Catalyst::Helper::View::TT->mk_comptest.
563
564See L<Catalyst::Helper::View::TT> and L<Catalyst::Helper::Model::CDBI> for
565examples.
566
567All helper classes should be under one of the following namespaces.
568
569 Catalyst::Helper::Model::
570 Catalyst::Helper::View::
571 Catalyst::Helper::Controller::
572
fc7ec1d9 573=head1 SEE ALSO
574
575L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
576L<Catalyst::Response>, L<Catalyst>
577
578=head1 AUTHOR
579
580Sebastian Riedel, C<sri@oook.de>
581
582=head1 LICENSE
583
584This library is free software . You can redistribute it and/or modify it under
585the same terms as perl itself.
586
587=cut
588
5891;