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