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