improved documentation
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Helper.pm
1 package Catalyst::Helper;
2
3 use strict;
4 use base 'Class::Accessor::Fast';
5 use Config;
6 use File::Spec;
7 use File::Path;
8 use IO::File;
9 use FindBin;
10
11 =head1 NAME
12
13 Catalyst::Helper - Bootstrap a Catalyst application
14
15 =head1 SYNOPSIS
16
17 See L<Catalyst::Manual::Intro>
18
19 =head1 DESCRIPTION
20
21 Bootstrap a Catalyst application.
22
23 =head2 METHODS
24
25 =head3 mk_app
26
27 =cut
28
29 sub mk_app {
30     my ( $self, $name ) = @_;
31     return 0 if $name =~ /[^\w\:]/;
32     $self->{name} = $name;
33     $self->{dir}  = $name;
34     $self->{dir} =~ s/\:\:/-/g;
35     $self->_mk_dirs;
36     $self->_mk_appclass;
37     $self->_mk_makefile;
38     $self->_mk_readme;
39     $self->_mk_changes;
40     $self->_mk_apptest;
41     $self->_mk_cgi;
42     $self->_mk_nphcgi;
43     $self->_mk_fcgi;
44     $self->_mk_server;
45     $self->_mk_test;
46     $self->_mk_create;
47     return 1;
48 }
49
50 =head3 mk_component
51
52 =cut
53
54 sub mk_component {
55     my $self = shift;
56     my $app  = shift;
57     $self->{app} = $app;
58     $self->{base} = File::Spec->catdir( $FindBin::Bin, '..' );
59     unless ( $_[0] =~ /^model|m|view|v|controller|c\$/i ) {
60         my $helper = shift;
61         my @args   = @_;
62         my $class  = "Catalyst::Helper::$helper";
63         eval "require $class";
64         die qq/Couldn't load helper "$class", "$@"/ if $@;
65         if ( $class->can('mk_stuff') ) {
66             return 1 unless $class->mk_stuff( $self, @args );
67         }
68     }
69     else {
70         my $type   = shift;
71         my $name   = shift;
72         my $helper = shift;
73         my @args   = @_;
74         return 0 if $name =~ /[^\w\:]/;
75         $type = 'M' if $type =~ /model|m/i;
76         $type = 'V' if $type =~ /view|v/i;
77         $type = 'C' if $type =~ /controller|c/i;
78         $self->{type}  = $type;
79         $self->{name}  = $name;
80         $self->{class} = "$app\::$type\::$name";
81
82         # Class
83         my $appdir = File::Spec->catdir( split /\:\:/, $app );
84         my $path =
85           File::Spec->catdir( $FindBin::Bin, '..', 'lib', $appdir, $type );
86         my $file = $name;
87         if ( $name =~ /\:/ ) {
88             my @path = split /\:\:/, $name;
89             $file = pop @path;
90             $path = File::Spec->catdir( $path, @path );
91             mkpath $path;
92         }
93         $file = File::Spec->catfile( $path, "$file.pm" );
94         $self->{file} = $file;
95
96         # Test
97         $self->{test_dir} = File::Spec->catdir( $FindBin::Bin, '..', 't' );
98         $self->{test}     = $self->next_test;
99
100         # Helper
101         if ($helper) {
102             my $comp = 'Model';
103             $comp = 'View'       if $type eq 'V';
104             $comp = 'Controller' if $type eq 'C';
105             my $class = "Catalyst::Helper::$comp\::$helper";
106             eval "require $class";
107             die qq/Couldn't load helper "$class", "$@"/ if $@;
108             if ( $class->can('mk_compclass') ) {
109                 return 1 unless $class->mk_compclass( $self, @args );
110             }
111             else { return 1 unless $self->_mk_compclass }
112
113             if ( $class->can('mk_comptest') ) {
114                 $class->mk_comptest( $self, @args );
115             }
116             else { $self->_mk_comptest }
117         }
118
119         # Fallback
120         else {
121             return 1 unless $self->_mk_compclass;
122             $self->_mk_comptest;
123         }
124     }
125     return 1;
126 }
127
128 =head3 mk_dir
129
130 =cut
131
132 sub mk_dir {
133     my ( $self, $dir ) = @_;
134     if ( -d $dir ) {
135         print qq/ exists "$dir"\n/;
136         return 0;
137     }
138     if ( mkpath $dir) {
139         print qq/created "$dir"\n/;
140         return 1;
141     }
142     die qq/Couldn't create "$dir", "$!"/;
143 }
144
145 =head3 mk_file
146
147 =cut
148
149 sub mk_file {
150     my ( $self, $file, $content ) = @_;
151     if ( -e $file ) {
152         print qq/ exists "$file"\n/;
153         return 0;
154     }
155     if ( my $f = IO::File->new("> $file") ) {
156         print $f $content;
157         print qq/created "$file"\n/;
158         return 1;
159     }
160     die qq/Couldn't create "$file", "$!"/;
161 }
162
163 =head3 next_test
164
165 =cut
166
167 sub next_test {
168     my ( $self, $tname ) = @_;
169     if ($tname) { $tname = "$tname.t" }
170     else {
171         my $name   = $self->{name};
172         my $prefix = $name;
173         $prefix =~ s/::/_/g;
174         $prefix         = lc $prefix;
175         $tname          = $prefix . '.t';
176         $self->{prefix} = $prefix;
177     }
178     my $dir  = $self->{test_dir};
179     my $type = lc $self->{type};
180     return File::Spec->catfile( $dir, $type, $tname );
181 }
182
183 sub _mk_dirs {
184     my $self = shift;
185     $self->mk_dir( $self->{dir} );
186     $self->{script} = File::Spec->catdir( $self->{dir}, 'script' );
187     $self->mk_dir( $self->{script} );
188     $self->{lib} = File::Spec->catdir( $self->{dir}, 'lib' );
189     $self->mk_dir( $self->{lib} );
190     $self->{root} = File::Spec->catdir( $self->{dir}, 'root' );
191     $self->mk_dir( $self->{root} );
192     $self->{t} = File::Spec->catdir( $self->{dir}, 't' );
193     $self->mk_dir( $self->{t} );
194     $self->mk_dir( File::Spec->catdir( $self->{t}, 'm' ) );
195     $self->mk_dir( File::Spec->catdir( $self->{t}, 'v' ) );
196     $self->mk_dir( File::Spec->catdir( $self->{t}, 'c' ) );
197     $self->{class} = File::Spec->catdir( split( /\:\:/, $self->{name} ) );
198     $self->{mod} = File::Spec->catdir( $self->{lib}, $self->{class} );
199     $self->mk_dir( $self->{mod} );
200     $self->{m} = File::Spec->catdir( $self->{mod}, 'M' );
201     $self->mk_dir( $self->{m} );
202     $self->{v} = File::Spec->catdir( $self->{mod}, 'V' );
203     $self->mk_dir( $self->{v} );
204     $self->{c} = File::Spec->catdir( $self->{mod}, 'C' );
205     $self->mk_dir( $self->{c} );
206     $self->{base} = File::Spec->rel2abs( $self->{dir} );
207 }
208
209 sub _mk_appclass {
210     my $self = shift;
211     my $mod  = $self->{mod};
212     my $name = $self->{name};
213     my $base = $self->{base};
214     $self->mk_file( "$mod.pm", <<"EOF");
215 package $name;
216
217 use strict;
218 use Catalyst qw/-Debug/;
219
220 our \$VERSION = '0.01';
221
222 $name->config(
223     name => '$name',
224     root => '$base/root',
225 );
226
227 sub default : Private {
228     my ( \$self, \$c ) = \@_;
229     \$c->res->output('Congratulations, $name is on Catalyst!');
230 }
231
232 =head1 NAME
233
234 $name - A very nice application
235
236 =head1 SYNOPSIS
237
238     Very simple to use
239
240 =head1 DESCRIPTION
241
242 Very nice application.
243
244 =head1 AUTHOR
245
246 Clever guy
247
248 =head1 LICENSE
249
250 This library is free software . You can redistribute it and/or modify it under
251 the same terms as perl itself.
252
253 =cut
254
255 1;
256 EOF
257 }
258
259 sub _mk_makefile {
260     my $self  = shift;
261     my $name  = $self->{name};
262     my $dir   = $self->{dir};
263     my $class = $self->{class};
264     $self->mk_file( "$dir\/Makefile.PL", <<"EOF");
265 use ExtUtils::MakeMaker;
266
267 WriteMakefile(
268     NAME         => '$name',
269     VERSION_FROM => 'lib/$class.pm',
270     PREREQ_PM    => { Catalyst => 0 },
271     test         => { TESTS => join ' ', ( glob('t/*.t'), glob('t/*/*.t') ) }
272 );
273 EOF
274 }
275
276 sub _mk_readme {
277     my $self = shift;
278     my $dir  = $self->{dir};
279     $self->mk_file( "$dir\/README", <<"EOF");
280 Run script/server.pl to test the application.
281 EOF
282 }
283
284 sub _mk_changes {
285     my $self = shift;
286     my $name = $self->{name};
287     my $dir  = $self->{dir};
288     my $time = localtime time;
289     $self->mk_file( "$dir\/Changes", <<"EOF");
290 This file documents the revision history for Perl extension $name.
291
292 0.01  $time
293         - initial revision, generated by Catalyst
294 EOF
295 }
296
297 sub _mk_apptest {
298     my $self = shift;
299     my $t    = $self->{t};
300     my $name = $self->{name};
301     $self->mk_file( "$t\/01app.t", <<"EOF");
302 use Test::More tests => 2;
303 use_ok( Catalyst::Test, '$name' );
304
305 ok( request('/')->is_success );
306 EOF
307     $self->mk_file( "$t\/02pod.t", <<"EOF");
308 use Test::More;
309
310 eval "use Test::Pod 1.14";
311 plan skip_all => 'Test::Pod 1.14 required' if $@;
312 plan skip_all => 'set TEST_POD to enable this test' unless $ENV{TEST_POD};
313
314 all_pod_files_ok();
315 EOF
316     $self->mk_file( "$t\/03podcoverage.t", <<"EOF");
317 use Test::More;
318
319 eval "use Test::Pod::Coverage 1.04";
320 plan skip_all => 'Test::Pod::Coverage 1.04 required' if \$@;
321 plan skip_all => 'set TEST_POD to enable this test' unless \$ENV{TEST_POD};
322
323 all_pod_coverage_ok();
324 EOF
325 }
326
327 sub _mk_cgi {
328     my $self   = shift;
329     my $name   = $self->{name};
330     my $script = $self->{script};
331     $self->mk_file( "$script\/cgi.pl", <<"EOF");
332 $Config{startperl} -w
333
334 BEGIN { \$ENV{CATALYST_ENGINE} = 'CGI' }
335
336 use strict;
337 use FindBin;
338 use lib "\$FindBin::Bin/../lib";
339 use $name;
340
341 $name->run;
342
343 1;
344 __END__
345
346 =head1 NAME
347
348 cgi - Catalyst CGI
349
350 =head1 SYNOPSIS
351
352 See L<Catalyst::Manual>
353
354 =head1 DESCRIPTION
355
356 Run a Catalyst application as cgi.
357
358 =head1 AUTHOR
359
360 Sebastian Riedel, C<sri\@oook.de>
361
362 =head1 COPYRIGHT
363
364 Copyright 2004 Sebastian Riedel. All rights reserved.
365
366 This library is free software. You can redistribute it and/or modify it under
367 the same terms as perl itself.
368
369 =cut
370 EOF
371     chmod 0700, "$script/cgi.pl";
372 }
373
374 sub _mk_nphcgi {
375     my $self   = shift;
376     my $name   = $self->{name};
377     my $script = $self->{script};
378     $self->mk_file( "$script\/nph-cgi.pl", <<"EOF");
379 $Config{startperl} -w
380
381 BEGIN { \$ENV{CATALYST_ENGINE} = 'CGI::NPH' }
382
383 use strict;
384 use FindBin;
385 use lib "\$FindBin::Bin/../lib";
386 use $name;
387
388 $name->run;
389
390 1;
391 __END__
392
393 =head1 NAME
394
395 nph-cgi - Catalyst CGI
396
397 =head1 SYNOPSIS
398
399 See L<Catalyst::Manual>
400
401 =head1 DESCRIPTION
402
403 Run a Catalyst application as nph cgi.
404
405 =head1 AUTHOR
406
407 Sebastian Riedel, C<sri\@oook.de>
408
409 =head1 COPYRIGHT
410
411 Copyright 2004 Sebastian Riedel. All rights reserved.
412
413 This library is free software. You can redistribute it and/or modify it under
414 the same terms as perl itself.
415
416 =cut
417 EOF
418     chmod 0700, "$script/nph-cgi.pl";
419 }
420
421 sub _mk_fcgi {
422     my $self   = shift;
423     my $name   = $self->{name};
424     my $script = $self->{script};
425     $self->mk_file( "$script\/fcgi.pl", <<"EOF");
426 $Config{startperl} -w
427
428 BEGIN { \$ENV{CATALYST_ENGINE} = 'FCGI' }
429
430 use strict;
431 use FindBin;
432 use lib "\$FindBin::Bin/../lib";
433 use $name;
434
435 $name->run;
436
437 1;
438 __END__
439
440 =head1 NAME
441
442 fcgi - Catalyst FCGI
443
444 =head1 SYNOPSIS
445
446 See L<Catalyst::Manual>
447
448 =head1 DESCRIPTION
449
450 Run a Catalyst application as fcgi.
451
452 =head1 AUTHOR
453
454 Sebastian Riedel, C<sri\@oook.de>
455
456 =head1 COPYRIGHT
457
458 Copyright 2004 Sebastian Riedel. All rights reserved.
459
460 This library is free software. You can redistribute it and/or modify it under
461 the same terms as perl itself.
462
463 =cut
464 EOF
465     chmod 0700, "$script/fcgi.pl";
466 }
467
468 sub _mk_server {
469     my $self   = shift;
470     my $name   = $self->{name};
471     my $script = $self->{script};
472     $self->mk_file( "$script\/server.pl", <<"EOF");
473 $Config{startperl} -w
474
475 BEGIN { \$ENV{CATALYST_ENGINE} = 'HTTP::Daemon' }
476
477 use strict;
478 use Getopt::Long;
479 use Pod::Usage;
480 use FindBin;
481 use lib "\$FindBin::Bin/../lib";
482 use $name;
483
484 my \$help = 0;
485 my \$port = 3000;
486
487 GetOptions( 'help|?' => \\\$help, 'port=s' => \\\$port );
488
489 pod2usage(1) if \$help;
490
491 $name->run(\$port);
492
493 1;
494 __END__
495
496 =head1 NAME
497
498 server - Catalyst Testserver
499
500 =head1 SYNOPSIS
501
502 server.pl [options]
503
504  Options:
505    -? -help    display this help and exits
506    -p -port    port (defaults to 3000)
507
508  See also:
509    perldoc Catalyst::Manual
510    perldoc Catalyst::Manual::Intro
511
512 =head1 DESCRIPTION
513
514 Run a Catalyst Testserver for this application.
515
516 =head1 AUTHOR
517
518 Sebastian Riedel, C<sri\@oook.de>
519
520 =head1 COPYRIGHT
521
522 Copyright 2004 Sebastian Riedel. All rights reserved.
523
524 This library is free software. You can redistribute it and/or modify it under
525 the same terms as perl itself.
526
527 =cut
528 EOF
529     chmod 0700, "$script/server.pl";
530 }
531
532 sub _mk_test {
533     my $self   = shift;
534     my $name   = $self->{name};
535     my $script = $self->{script};
536     $self->mk_file( "$script/test.pl", <<"EOF");
537 $Config{startperl} -w
538
539 BEGIN { \$ENV{CATALYST_ENGINE} = 'Test' }
540
541 use strict;
542 use Getopt::Long;
543 use Pod::Usage;
544 use FindBin;
545 use lib "\$FindBin::Bin/../lib";
546 use $name;
547
548 my \$help = 0;
549
550 GetOptions( 'help|?' => \\\$help );
551
552 pod2usage(1) if ( \$help || !\$ARGV[0] );
553
554 print $name->run(\$ARGV[0])->content . "\n";
555
556 1;
557 __END__
558
559 =head1 NAME
560
561 test - Catalyst Test
562
563 =head1 SYNOPSIS
564
565 test.pl [options] uri
566
567  Options:
568    -help    display this help and exits
569
570  Examples:
571    test.pl http://localhost/some_action
572    test.pl /some_action
573
574  See also:
575    perldoc Catalyst::Manual
576    perldoc Catalyst::Manual::Intro
577
578 =head1 DESCRIPTION
579
580 Run a Catalyst action from the comand line.
581
582 =head1 AUTHOR
583
584 Sebastian Riedel, C<sri\@oook.de>
585
586 =head1 COPYRIGHT
587
588 Copyright 2004 Sebastian Riedel. All rights reserved.
589
590 This library is free software. You can redistribute it and/or modify it under
591 the same terms as perl itself.
592
593 =cut
594 EOF
595     chmod 0700, "$script/test.pl";
596 }
597
598 sub _mk_create {
599     my $self   = shift;
600     my $name   = $self->{name};
601     my $script = $self->{script};
602     $self->mk_file( "$script\/create.pl", <<"EOF");
603 $Config{startperl} -w
604
605 use strict;
606 use Getopt::Long;
607 use Pod::Usage;
608 use Catalyst::Helper;
609
610 my \$help = 0;
611
612 GetOptions( 'help|?' => \$help );
613
614 pod2usage(1) if ( \$help || !\$ARGV[0] );
615
616 my \$helper = Catalyst::Helper->new;
617 pod2usage(1) unless \$helper->mk_component( '$name', \@ARGV );
618
619 1;
620 __END__
621
622 =head1 NAME
623
624 create - Create a new Catalyst Component
625
626 =head1 SYNOPSIS
627
628 create.pl [options] model|view|controller name [helper] [options]
629
630  Options:
631    -help    display this help and exits
632
633  Examples:
634    create.pl controller My::Controller
635    create.pl view My::View
636    create.pl view MyView TT
637    create.pl view TT TT
638    create.pl model My::Model
639    create.pl model SomeDB CDBI dbi:SQLite:/tmp/my.db
640    create.pl model AnotherDB CDBI dbi:Pg:dbname=foo root 4321
641    create.pl Ajax
642
643  See also:
644    perldoc Catalyst::Manual
645    perldoc Catalyst::Manual::Intro
646
647 =head1 DESCRIPTION
648
649 Create a new Catalyst Component.
650
651 =head1 AUTHOR
652
653 Sebastian Riedel, C<sri\@oook.de>
654
655 =head1 COPYRIGHT
656
657 Copyright 2004 Sebastian Riedel. All rights reserved.
658
659 This library is free software. You can redistribute it and/or modify it under
660 the same terms as perl itself.
661
662 =cut
663 EOF
664     chmod 0700, "$script/create.pl";
665 }
666
667 sub _mk_compclass {
668     my $self   = shift;
669     my $app    = $self->{app};
670     my $class  = $self->{class};
671     my $type   = $self->{type};
672     my $action = '';
673     $action = <<"EOF" if $type eq 'C';
674
675 sub default : Private {
676     my ( \$self, \$c ) = \@_;
677     \$c->res->output('Congratulations, $class is on Catalyst!');
678 }
679
680 EOF
681     my $file = $self->{file};
682     return $self->mk_file( "$file", <<"EOF");
683 package $class;
684
685 use strict;
686 use base 'Catalyst::Base';
687 $action
688 =head1 NAME
689
690 $class - A Component
691
692 =head1 SYNOPSIS
693
694     Very simple to use
695
696 =head1 DESCRIPTION
697
698 Very nice component.
699
700 =head1 AUTHOR
701
702 Clever guy
703
704 =head1 LICENSE
705
706 This library is free software . You can redistribute it and/or modify it under
707 the same terms as perl itself.
708
709 =cut
710
711 1;
712 EOF
713 }
714
715 sub _mk_comptest {
716     my $self   = shift;
717     my $prefix = $self->{prefix};
718     my $type   = $self->{type};
719     my $class  = $self->{class};
720     my $app    = $self->{app};
721     my $test   = $self->{test};
722     if ( $self->{type} eq 'C' ) {
723         $self->mk_file( "$test", <<"EOF");
724 use Test::More tests => 3;
725 use_ok( Catalyst::Test, '$app' );
726 use_ok('$class');
727
728 ok( request('$prefix')->is_success );
729 EOF
730     }
731     else {
732         $self->mk_file( "$test", <<"EOF");
733 use Test::More tests => 1;
734 use_ok('$class');
735 EOF
736     }
737 }
738
739 =head1 HELPERS
740
741 Helpers are classes that provide two methods.
742
743     * mk_compclass - creates the Component class
744     * mk_comptest  - creates the Component test
745
746 So when you call C<bin/create view MyView TT>, create would try to execute
747 Catalyst::Helper::View::TT->mk_compclass and
748 Catalyst::Helper::View::TT->mk_comptest.
749
750 See L<Catalyst::Helper::View::TT> and L<Catalyst::Helper::Model::CDBI> for
751 examples.
752
753 All helper classes should be under one of the following namespaces.
754
755     Catalyst::Helper::Model::
756     Catalyst::Helper::View::
757     Catalyst::Helper::Controller::
758
759 =head1 SEE ALSO
760
761 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
762 L<Catalyst::Response>, L<Catalyst>
763
764 =head1 AUTHOR
765
766 Sebastian Riedel, C<sri@oook.de>
767
768 =head1 LICENSE
769
770 This library is free software . You can redistribute it and/or modify it under
771 the same terms as perl itself.
772
773 =cut
774
775 1;