Fixed typo
[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         $prefix = lc $prefix;
223         $prefix =~ s/-/\//g;
224         $self->{uri} = $prefix;
225     }
226     my $dir  = $self->{test_dir};
227     my $type = $self->{type};
228     return File::Spec->catfile( $dir, $type, $tname );
229 }
230
231 =head3 render_file
232
233 Render and create a file from a template in DATA using 
234 Template Toolkit.
235
236 =cut
237
238 sub render_file {
239     my ( $self, $file, $path, $vars ) = @_;
240     $vars ||= {};
241     my $t = Template->new;
242     my $template = $self->get_file( ( caller(0) )[0], $file );
243     return 0 unless $template;
244     my $output;
245     $t->process( \$template, { %{$self}, %$vars }, \$output );
246     $self->mk_file( $path, $output );
247 }
248
249 sub _mk_dirs {
250     my $self = shift;
251     $self->mk_dir( $self->{dir} );
252     $self->{script} = File::Spec->catdir( $self->{dir}, 'script' );
253     $self->mk_dir( $self->{script} );
254     $self->{lib} = File::Spec->catdir( $self->{dir}, 'lib' );
255     $self->mk_dir( $self->{lib} );
256     $self->{root} = File::Spec->catdir( $self->{dir}, 'root' );
257     $self->mk_dir( $self->{root} );
258     $self->{t} = File::Spec->catdir( $self->{dir}, 't' );
259     $self->mk_dir( $self->{t} );
260     $self->mk_dir( File::Spec->catdir( $self->{t}, 'M' ) );
261     $self->mk_dir( File::Spec->catdir( $self->{t}, 'V' ) );
262     $self->mk_dir( File::Spec->catdir( $self->{t}, 'C' ) );
263     $self->{class} = File::Spec->catdir( split( /\:\:/, $self->{name} ) );
264     $self->{mod} = File::Spec->catdir( $self->{lib}, $self->{class} );
265     $self->mk_dir( $self->{mod} );
266     $self->{m} = File::Spec->catdir( $self->{mod}, 'M' );
267     $self->mk_dir( $self->{m} );
268     $self->{v} = File::Spec->catdir( $self->{mod}, 'V' );
269     $self->mk_dir( $self->{v} );
270     $self->{c} = File::Spec->catdir( $self->{mod}, 'C' );
271     $self->mk_dir( $self->{c} );
272     $self->{base} = File::Spec->rel2abs( $self->{dir} );
273 }
274
275 sub _mk_appclass {
276     my $self = shift;
277     my $mod  = $self->{mod};
278     $self->render_file( 'appclass', "$mod.pm" );
279 }
280
281 sub _mk_build {
282     my $self = shift;
283     my $dir  = $self->{dir};
284     $self->render_file( 'build', "$dir\/Build.PL" );
285 }
286
287 sub _mk_makefile {
288     my $self = shift;
289     my $dir  = $self->{dir};
290     $self->render_file( 'makefile', "$dir\/Makefile.PL" );
291 }
292
293 sub _mk_readme {
294     my $self = shift;
295     my $dir  = $self->{dir};
296     $self->render_file( 'readme', "$dir\/README" );
297 }
298
299 sub _mk_changes {
300     my $self = shift;
301     my $dir  = $self->{dir};
302     my $time = localtime time;
303     $self->render_file( 'changes', "$dir\/Changes", { time => $time } );
304 }
305
306 sub _mk_apptest {
307     my $self = shift;
308     my $t    = $self->{t};
309     $self->render_file( 'apptest',         "$t\/01app.t" );
310     $self->render_file( 'podtest',         "$t\/02pod.t" );
311     $self->render_file( 'podcoveragetest', "$t\/03podcoverage.t" );
312 }
313
314 sub _mk_cgi {
315     my $self      = shift;
316     my $script    = $self->{script};
317     my $appprefix = $self->{appprefix};
318     $self->render_file( 'cgi', "$script\/$appprefix\_cgi.pl" );
319     chmod 0700, "$script/$appprefix\_cgi.pl";
320 }
321
322 sub _mk_fastcgi {
323     my $self      = shift;
324     my $script    = $self->{script};
325     my $appprefix = $self->{appprefix};
326     $self->render_file( 'fastcgi', "$script\/$appprefix\_fastcgi.pl" );
327     chmod 0700, "$script/$appprefix\_fastcgi.pl";
328 }
329
330 sub _mk_server {
331     my $self      = shift;
332     my $script    = $self->{script};
333     my $appprefix = $self->{appprefix};
334     $self->render_file( 'server', "$script\/$appprefix\_server.pl" );
335     chmod 0700, "$script/$appprefix\_server.pl";
336 }
337
338 sub _mk_test {
339     my $self      = shift;
340     my $script    = $self->{script};
341     my $appprefix = $self->{appprefix};
342     $self->render_file( 'test', "$script/$appprefix\_test.pl" );
343     chmod 0700, "$script/$appprefix\_test.pl";
344 }
345
346 sub _mk_create {
347     my $self      = shift;
348     my $script    = $self->{script};
349     my $appprefix = $self->{appprefix};
350     $self->render_file( 'create', "$script\/$appprefix\_create.pl" );
351     chmod 0700, "$script/$appprefix\_create.pl";
352 }
353
354 sub _mk_compclass {
355     my $self = shift;
356     my $file = $self->{file};
357     return $self->render_file( 'compclass', "$file" );
358 }
359
360 sub _mk_comptest {
361     my $self = shift;
362     my $test = $self->{test};
363     $self->render_file( 'comptest', "$test" );
364 }
365
366 =head1 HELPERS
367
368 Helpers are classes that provide two methods.
369
370     * mk_compclass - creates the Component class
371     * mk_comptest  - creates the Component test
372
373 So when you call C<bin/create view MyView TT>, create would try to execute
374 Catalyst::Helper::View::TT->mk_compclass and
375 Catalyst::Helper::View::TT->mk_comptest.
376
377 See L<Catalyst::Helper::View::TT> and L<Catalyst::Helper::Model::CDBI> for
378 examples.
379
380 All helper classes should be under one of the following namespaces.
381
382     Catalyst::Helper::Model::
383     Catalyst::Helper::View::
384     Catalyst::Helper::Controller::
385
386 =head1 NOTE
387
388 The helpers will read author name from /etc/passwd by default.
389 To override, please export the AUTHOR variable.
390
391 =head1 SEE ALSO
392
393 L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
394 L<Catalyst::Response>, L<Catalyst>
395
396 =head1 AUTHOR
397
398 Sebastian Riedel, C<sri@oook.de>
399
400 =head1 LICENSE
401
402 This library is free software . You can redistribute it and/or modify
403 it under the same terms as perl itself.
404
405 =cut
406
407 1;
408 __DATA__
409
410 __appclass__
411 package [% name %];
412
413 use strict;
414 use Catalyst qw/-Debug/;
415
416 our $VERSION = '0.01';
417
418 [% name %]->config( name => '[% name %]' );
419
420 [% name %]->setup;
421
422 =head1 NAME
423
424 [% name %] - Catalyst based application
425
426 =head1 SYNOPSIS
427
428     script/[% appprefix %]_server.pl
429
430 =head1 DESCRIPTION
431
432 Catalyst based application.
433
434 =head1 METHODS
435
436 =over 4
437
438 =item default
439
440 =cut
441
442 sub default : Private {
443     my ( $self, $c ) = @_;
444     $c->res->output('Congratulations, [% name %] is on Catalyst!');
445 }
446
447 =back
448
449 =head1 AUTHOR
450
451 [%author%]
452
453 =head1 LICENSE
454
455 This library is free software . You can redistribute it and/or modify
456 it under the same terms as perl itself.
457
458 =cut
459
460 1;
461 __makefile__
462     unless ( eval "use Module::Build::Compat 0.02; 1" ) {
463         print "This module requires Module::Build to install itself.\n";
464
465         require ExtUtils::MakeMaker;
466         my $yn =
467           ExtUtils::MakeMaker::prompt( '  Install Module::Build now from CPAN?',            'y' );
468
469         unless ( $yn =~ /^y/i ) {
470             die " *** Cannot install without Module::Build.  Exiting ...\n";
471         }
472
473         require Cwd;
474         require File::Spec;
475         require CPAN;
476
477         # Save this 'cause CPAN will chdir all over the place.
478         my $cwd      = Cwd::cwd();
479         my $makefile = File::Spec->rel2abs($0);
480
481         CPAN::Shell->install('Module::Build::Compat')
482           or die " *** Cannot install without Module::Build.  Exiting ...\n";
483
484         chdir $cwd or die "Cannot chdir() back to $cwd: $!";
485     }
486     eval "use Module::Build::Compat 0.02; 1" or die $@;
487     use lib '_build/lib';
488     Module::Build::Compat->run_build_pl( args => \@ARGV );
489     require Module::Build;
490     Module::Build::Compat->write_makefile( build_class => 'Module::Build' );
491 __build__
492 use strict;
493 use Catalyst::Build;
494
495 my $build = Catalyst::Build->new(
496     create_makefile_pl => 'passthrough',
497     license            => 'perl',
498     module_name        => '[% name %]',
499     requires           => { Catalyst => '5.10' },
500     create_makefile_pl => 'passthrough',
501     script_files       => [ glob('script/*') ],
502     test_files         => [ glob('t/*.t'), glob('t/*/*.t') ]
503 );
504 $build->create_build_script;
505 __readme__
506 Run script/[% appprefix %]_server.pl to test the application.
507 __changes__
508 This file documents the revision history for Perl extension [% name %].
509 0.01  [% time %]
510         - initial revision, generated by Catalyst
511 __apptest__
512 use Test::More tests => 2;
513 use_ok( Catalyst::Test, '[% name %]' );
514
515 ok( request('/')->is_success );
516 __podtest__
517 use Test::More;
518
519 eval "use Test::Pod 1.14";
520 plan skip_all => 'Test::Pod 1.14 required' if $@;
521 plan skip_all => 'set TEST_POD to enable this test' unless $ENV{TEST_POD};
522
523 all_pod_files_ok();
524 __podcoveragetest__
525 use Test::More;
526
527 eval "use Test::Pod::Coverage 1.04";
528 plan skip_all => 'Test::Pod::Coverage 1.04 required' if $@;
529 plan skip_all => 'set TEST_POD to enable this test' unless $ENV{TEST_POD};
530
531 all_pod_coverage_ok();
532 __cgi__
533 [% startperl %] -w
534
535 BEGIN { $ENV{CATALYST_ENGINE} ||= 'CGI' }
536
537 use strict;
538 use FindBin;
539 use lib "$FindBin::Bin/../lib";
540 use [% name %];
541
542 [% name %]->run;
543
544 1;
545
546 =head1 NAME
547
548 [% appprefix %]_cgi.pl - Catalyst CGI
549
550 =head1 SYNOPSIS
551
552 See L<Catalyst::Manual>
553
554 =head1 DESCRIPTION
555
556 Run a Catalyst application as cgi.
557
558 =head1 AUTHOR
559
560 Sebastian Riedel, C<sri@oook.de>
561
562 =head1 COPYRIGHT
563
564 Copyright 2004 Sebastian Riedel. All rights reserved.
565
566 This library is free software. You can redistribute it and/or modify
567 it under the same terms as perl itself.
568
569 =cut
570 __fastcgi__
571 [% startperl %] -w
572
573 BEGIN { $ENV{CATALYST_ENGINE} ||= 'FastCGI' }
574
575 use strict;
576 use FindBin;
577 use lib "$FindBin::Bin/../lib";
578 use [% name %];
579
580 [% name %]->run;
581
582 1;
583
584 =head1 NAME
585
586 [% appprefix %]_fastcgi.pl - Catalyst FastCGI
587
588 =head1 SYNOPSIS
589
590 See L<Catalyst::Manual>
591
592 =head1 DESCRIPTION
593
594 Run a Catalyst application as fastcgi.
595
596 =head1 AUTHOR
597
598 Sebastian Riedel, C<sri@oook.de>
599
600 =head1 COPYRIGHT
601
602 Copyright 2004 Sebastian Riedel. All rights reserved.
603
604 This library is free software. You can redistribute it and/or modify
605 it under the same terms as perl itself.
606
607 =cut
608 __server__
609 [% startperl %] -w
610
611 BEGIN { 
612     $ENV{CATALYST_ENGINE} ||= 'HTTP';
613     $ENV{CATALYST_SCRIPT_GEN} = [% scriptgen %];
614 }  
615
616 use strict;
617 use Getopt::Long;
618 use Pod::Usage;
619 use FindBin;
620 use lib "$FindBin::Bin/../lib";
621 use [% name %];
622
623 my $help = 0;
624 my $port = 3000;
625
626 GetOptions( 'help|?' => \$help, 'port=s' => \$port );
627
628 pod2usage(1) if $help;
629
630 [% name %]->run($port);
631
632 1;
633
634 =head1 NAME
635
636 [% appprefix %]_server.pl - Catalyst Testserver
637
638 =head1 SYNOPSIS
639
640 [% appprefix %]_server.pl [options]
641
642  Options:
643    -? -help    display this help and exits
644    -p -port    port (defaults to 3000)
645
646  See also:
647    perldoc Catalyst::Manual
648    perldoc Catalyst::Manual::Intro
649
650 =head1 DESCRIPTION
651
652 Run a Catalyst Testserver for this application.
653
654 =head1 AUTHOR
655
656 Sebastian Riedel, C<sri@oook.de>
657
658 =head1 COPYRIGHT
659
660 Copyright 2004 Sebastian Riedel. All rights reserved.
661
662 This library is free software. You can redistribute it and/or modify
663 it under the same terms as perl itself.
664
665 =cut
666 __test__
667 [% startperl %] -w
668
669 BEGIN { $ENV{CATALYST_ENGINE} ||= 'Test' }
670
671 use strict;
672 use Getopt::Long;
673 use Pod::Usage;
674 use FindBin;
675 use lib "$FindBin::Bin/../lib";
676 use [% name %];
677
678 my $help = 0;
679
680 GetOptions( 'help|?' => \$help );
681
682 pod2usage(1) if ( $help || !$ARGV[0] );
683
684 print [% name %]->run($ARGV[0])->content . "\n";
685
686 1;
687
688 =head1 NAME
689
690 [% appprefix %]_test.pl - Catalyst Test
691
692 =head1 SYNOPSIS
693
694 [% appprefix %]_test.pl [options] uri
695
696  Options:
697    -help    display this help and exits
698
699  Examples:
700    [% appprefix %]_test.pl http://localhost/some_action
701    [% appprefix %]_test.pl /some_action
702
703  See also:
704    perldoc Catalyst::Manual
705    perldoc Catalyst::Manual::Intro
706
707 =head1 DESCRIPTION
708
709 Run a Catalyst action from the comand line.
710
711 =head1 AUTHOR
712
713 Sebastian Riedel, C<sri@oook.de>
714
715 =head1 COPYRIGHT
716
717 Copyright 2004 Sebastian Riedel. All rights reserved.
718
719 This library is free software. You can redistribute it and/or modify
720 it under the same terms as perl itself.
721
722 =cut
723 __create__
724 [% startperl %] -w
725
726 use strict;
727 use Getopt::Long;
728 use Pod::Usage;
729 use Catalyst::Helper;
730
731 my $help = 0;
732
733 GetOptions( 'help|?' => \$help );
734
735 pod2usage(1) if ( $help || !$ARGV[0] );
736
737 my $helper = Catalyst::Helper->new;
738 pod2usage(1) unless $helper->mk_component( '[% name %]', @ARGV );
739
740 1;
741
742 =head1 NAME
743
744 [% appprefix %]_create.pl - Create a new Catalyst Component
745
746 =head1 SYNOPSIS
747
748 [% appprefix %]_create.pl [options] model|view|controller name [helper] [options]
749
750  Options:
751    -help    display this help and exits
752
753  Examples:
754    [% appprefix %]_create.pl controller My::Controller
755    [% appprefix %]_create.pl view My::View
756    [% appprefix %]_create.pl view MyView TT
757    [% appprefix %]_create.pl view TT TT
758    [% appprefix %]_create.pl model My::Model
759    [% appprefix %]_create.pl model SomeDB CDBI dbi:SQLite:/tmp/my.db
760    [% appprefix %]_create.pl model AnotherDB CDBI dbi:Pg:dbname=foo root 4321
761
762  See also:
763    perldoc Catalyst::Manual
764    perldoc Catalyst::Manual::Intro
765
766 =head1 DESCRIPTION
767
768 Create a new Catalyst Component.
769
770 =head1 AUTHOR
771
772 Sebastian Riedel, C<sri\@oook.de>
773
774 =head1 COPYRIGHT
775
776 Copyright 2004 Sebastian Riedel. All rights reserved.
777
778 This library is free software. You can redistribute it and/or modify
779 it under the same terms as perl itself.
780
781 =cut
782 __compclass__
783 package [% class %];
784
785 use strict;
786 use base 'Catalyst::Base';
787
788 =head1 NAME
789
790 [% class %] - Catalyst component
791
792 =head1 SYNOPSIS
793
794 See L<[% app %]>
795
796 =head1 DESCRIPTION
797
798 Catalyst component.
799 [% IF type == 'C' %]
800 =head1 METHODS
801
802 =over 4
803
804 =item default
805
806 =cut
807
808 sub default : Private {
809     my ( $self, $c ) = @_;
810     $c->res->output('Congratulations, [% class %] is on Catalyst!');
811 }
812
813 =back
814 [% END %]
815 =head1 AUTHOR
816
817 [%author%]
818
819 =head1 LICENSE
820
821 This library is free software . You can redistribute it and/or modify
822 it under the same terms as perl itself.
823
824 =cut
825
826 1;
827 __comptest__
828 [% IF type == 'C' %]
829 use Test::More tests => 3;
830 use_ok( Catalyst::Test, '[% app %]' );
831 use_ok('[% class %]');
832
833 ok( request('[% uri %]')->is_success );
834 [% ELSE %]
835 use Test::More tests => 1;
836 use_ok('[% class %]');
837 [% END %]