fixed helper
[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
5a8ed4fe 227sub default : Private {
228 my ( \$self, \$c ) = \@_;
229 \$c->res->output('Congratulations, $name is on Catalyst!');
230}
fc7ec1d9 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
242Very nice application.
243
244=head1 AUTHOR
245
246Clever guy
247
248=head1 LICENSE
249
250This library is free software . You can redistribute it and/or modify it under
251the same terms as perl itself.
252
253=cut
254
2551;
256EOF
257}
258
259sub _mk_makefile {
ad41e777 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");
fc7ec1d9 265use ExtUtils::MakeMaker;
266
267WriteMakefile(
268 NAME => '$name',
269 VERSION_FROM => 'lib/$class.pm',
3cb1db8c 270 PREREQ_PM => { Catalyst => 0 },
271 test => { TESTS => join ' ', ( glob('t/*.t'), glob('t/*/*.t') ) }
fc7ec1d9 272);
273EOF
274}
275
91864987 276sub _mk_readme {
277 my $self = shift;
278 my $dir = $self->{dir};
279 $self->mk_file( "$dir\/README", <<"EOF");
280Run script/server.pl to test the application.
281EOF
282}
283
284sub _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");
290This file documents the revision history for Perl extension $name.
291
2920.01 $time
293 - initial revision, generated by Catalyst
294EOF
295}
296
fc7ec1d9 297sub _mk_apptest {
298 my $self = shift;
299 my $t = $self->{t};
300 my $name = $self->{name};
ad41e777 301 $self->mk_file( "$t\/01app.t", <<"EOF");
fc7ec1d9 302use Test::More tests => 2;
303use_ok( Catalyst::Test, '$name' );
304
305ok( request('/')->is_success );
306EOF
5a8fd7e9 307 $self->mk_file( "$t\/02pod.t", <<"EOF");
308use Test::More;
309
310eval "use Test::Pod 1.14";
a5d4355a 311plan skip_all => 'Test::Pod 1.14 required' if \$@;
312plan skip_all => 'set TEST_POD to enable this test' unless \$ENV{TEST_POD};
5a8fd7e9 313
314all_pod_files_ok();
315EOF
316 $self->mk_file( "$t\/03podcoverage.t", <<"EOF");
1df125c9 317use Test::More;
318
319eval "use Test::Pod::Coverage 1.04";
320plan skip_all => 'Test::Pod::Coverage 1.04 required' if \$@;
321plan skip_all => 'set TEST_POD to enable this test' unless \$ENV{TEST_POD};
322
323all_pod_coverage_ok();
324EOF
fc7ec1d9 325}
326
bc024080 327sub _mk_cgi {
328 my $self = shift;
329 my $name = $self->{name};
330 my $script = $self->{script};
d8ab1b45 331 $self->mk_file( "$script\/cgi.pl", <<"EOF");
332$Config{startperl} -w
333
334BEGIN { \$ENV{CATALYST_ENGINE} = 'CGI' }
335
336use strict;
337use FindBin;
338use lib "\$FindBin::Bin/../lib";
339use $name;
340
341$name->run;
342
3431;
344__END__
345
346=head1 NAME
347
348cgi - Catalyst CGI
349
350=head1 SYNOPSIS
351
352See L<Catalyst::Manual>
353
354=head1 DESCRIPTION
355
356Run a Catalyst application as cgi.
357
358=head1 AUTHOR
359
360Sebastian Riedel, C<sri\@oook.de>
361
362=head1 COPYRIGHT
363
364Copyright 2004 Sebastian Riedel. All rights reserved.
365
366This library is free software. You can redistribute it and/or modify it under
367the same terms as perl itself.
368
369=cut
370EOF
371 chmod 0700, "$script/cgi.pl";
372}
373
374sub _mk_nphcgi {
375 my $self = shift;
376 my $name = $self->{name};
377 my $script = $self->{script};
3fde004f 378 $self->mk_file( "$script\/nph-cgi.pl", <<"EOF");
bc024080 379$Config{startperl} -w
380
e646f111 381BEGIN { \$ENV{CATALYST_ENGINE} = 'CGI::NPH' }
e4b4c2b6 382
bc024080 383use strict;
384use FindBin;
385use lib "\$FindBin::Bin/../lib";
386use $name;
387
388$name->run;
389
3901;
391__END__
392
393=head1 NAME
394
3fde004f 395nph-cgi - Catalyst CGI
bc024080 396
397=head1 SYNOPSIS
398
399See L<Catalyst::Manual>
400
401=head1 DESCRIPTION
402
d8ab1b45 403Run a Catalyst application as nph cgi.
bc024080 404
405=head1 AUTHOR
406
407Sebastian Riedel, C<sri\@oook.de>
408
409=head1 COPYRIGHT
410
411Copyright 2004 Sebastian Riedel. All rights reserved.
412
413This library is free software. You can redistribute it and/or modify it under
414the same terms as perl itself.
415
416=cut
417EOF
3fde004f 418 chmod 0700, "$script/nph-cgi.pl";
bc024080 419}
420
cb8d90e5 421sub _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
ffb41d94 428BEGIN { \$ENV{CATALYST_ENGINE} = 'FCGI' }
cb8d90e5 429
430use strict;
431use FindBin;
432use lib "\$FindBin::Bin/../lib";
cb8d90e5 433use $name;
434
ffb41d94 435$name->run;
cb8d90e5 436
4371;
438__END__
439
440=head1 NAME
441
442fcgi - Catalyst FCGI
443
444=head1 SYNOPSIS
445
446See L<Catalyst::Manual>
447
448=head1 DESCRIPTION
449
450Run a Catalyst application as fcgi.
451
452=head1 AUTHOR
453
454Sebastian Riedel, C<sri\@oook.de>
455
456=head1 COPYRIGHT
457
458Copyright 2004 Sebastian Riedel. All rights reserved.
459
460This library is free software. You can redistribute it and/or modify it under
461the same terms as perl itself.
462
463=cut
464EOF
465 chmod 0700, "$script/fcgi.pl";
466}
467
fc7ec1d9 468sub _mk_server {
91864987 469 my $self = shift;
470 my $name = $self->{name};
471 my $script = $self->{script};
472 $self->mk_file( "$script\/server.pl", <<"EOF");
f1d0b7ea 473$Config{startperl} -w
fc7ec1d9 474
ca61af20 475BEGIN { \$ENV{CATALYST_ENGINE} = 'HTTP' }
585893b9 476
fc7ec1d9 477use strict;
478use Getopt::Long;
479use Pod::Usage;
480use FindBin;
481use lib "\$FindBin::Bin/../lib";
585893b9 482use $name;
fc7ec1d9 483
484my \$help = 0;
485my \$port = 3000;
486
487GetOptions( 'help|?' => \\\$help, 'port=s' => \\\$port );
488
489pod2usage(1) if \$help;
490
585893b9 491$name->run(\$port);
fc7ec1d9 492
4931;
494__END__
495
496=head1 NAME
497
498server - Catalyst Testserver
499
500=head1 SYNOPSIS
501
91864987 502server.pl [options]
fc7ec1d9 503
504 Options:
ad41e777 505 -? -help display this help and exits
506 -p -port port (defaults to 3000)
fc7ec1d9 507
03a53815 508 See also:
509 perldoc Catalyst::Manual
510 perldoc Catalyst::Manual::Intro
511
fc7ec1d9 512=head1 DESCRIPTION
513
514Run a Catalyst Testserver for this application.
515
516=head1 AUTHOR
517
518Sebastian Riedel, C<sri\@oook.de>
519
520=head1 COPYRIGHT
521
522Copyright 2004 Sebastian Riedel. All rights reserved.
523
524This library is free software. You can redistribute it and/or modify it under
525the same terms as perl itself.
526
527=cut
528EOF
91864987 529 chmod 0700, "$script/server.pl";
fc7ec1d9 530}
531
532sub _mk_test {
91864987 533 my $self = shift;
534 my $name = $self->{name};
535 my $script = $self->{script};
536 $self->mk_file( "$script/test.pl", <<"EOF");
f1d0b7ea 537$Config{startperl} -w
fc7ec1d9 538
e646f111 539BEGIN { \$ENV{CATALYST_ENGINE} = 'Test' }
540
fc7ec1d9 541use strict;
542use Getopt::Long;
543use Pod::Usage;
544use FindBin;
545use lib "\$FindBin::Bin/../lib";
e646f111 546use $name;
fc7ec1d9 547
548my \$help = 0;
549
550GetOptions( 'help|?' => \\\$help );
551
552pod2usage(1) if ( \$help || !\$ARGV[0] );
553
e646f111 554print $name->run(\$ARGV[0])->content . "\n";
fc7ec1d9 555
5561;
557__END__
558
559=head1 NAME
560
561test - Catalyst Test
562
563=head1 SYNOPSIS
564
91864987 565test.pl [options] uri
fc7ec1d9 566
567 Options:
568 -help display this help and exits
569
570 Examples:
91864987 571 test.pl http://localhost/some_action
572 test.pl /some_action
fc7ec1d9 573
03a53815 574 See also:
575 perldoc Catalyst::Manual
576 perldoc Catalyst::Manual::Intro
577
fc7ec1d9 578=head1 DESCRIPTION
579
580Run a Catalyst action from the comand line.
581
582=head1 AUTHOR
583
584Sebastian Riedel, C<sri\@oook.de>
585
586=head1 COPYRIGHT
587
588Copyright 2004 Sebastian Riedel. All rights reserved.
589
590This library is free software. You can redistribute it and/or modify it under
591the same terms as perl itself.
592
593=cut
594EOF
91864987 595 chmod 0700, "$script/test.pl";
fc7ec1d9 596}
597
598sub _mk_create {
91864987 599 my $self = shift;
600 my $name = $self->{name};
601 my $script = $self->{script};
602 $self->mk_file( "$script\/create.pl", <<"EOF");
f1d0b7ea 603$Config{startperl} -w
fc7ec1d9 604
605use strict;
606use Getopt::Long;
607use Pod::Usage;
608use Catalyst::Helper;
609
610my \$help = 0;
611
612GetOptions( 'help|?' => \$help );
613
b5524568 614pod2usage(1) if ( \$help || !\$ARGV[0] );
fc7ec1d9 615
616my \$helper = Catalyst::Helper->new;
617pod2usage(1) unless \$helper->mk_component( '$name', \@ARGV );
618
6191;
620__END__
621
622=head1 NAME
623
624create - Create a new Catalyst Component
625
626=head1 SYNOPSIS
627
91864987 628create.pl [options] model|view|controller name [helper] [options]
fc7ec1d9 629
630 Options:
631 -help display this help and exits
632
633 Examples:
91864987 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
b5524568 641 create.pl Ajax
03a53815 642
643 See also:
644 perldoc Catalyst::Manual
645 perldoc Catalyst::Manual::Intro
fc7ec1d9 646
647=head1 DESCRIPTION
648
649Create a new Catalyst Component.
650
651=head1 AUTHOR
652
653Sebastian Riedel, C<sri\@oook.de>
654
655=head1 COPYRIGHT
656
657Copyright 2004 Sebastian Riedel. All rights reserved.
658
659This library is free software. You can redistribute it and/or modify it under
660the same terms as perl itself.
661
662=cut
663EOF
91864987 664 chmod 0700, "$script/create.pl";
fc7ec1d9 665}
666
fc7ec1d9 667sub _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
5a8ed4fe 675sub default : Private {
676 my ( \$self, \$c ) = \@_;
677 \$c->res->output('Congratulations, $class is on Catalyst!');
678}
fc7ec1d9 679
fc7ec1d9 680EOF
681 my $file = $self->{file};
ad41e777 682 return $self->mk_file( "$file", <<"EOF");
fc7ec1d9 683package $class;
684
685use strict;
686use 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
698Very nice component.
699
700=head1 AUTHOR
701
702Clever guy
703
704=head1 LICENSE
705
706This library is free software . You can redistribute it and/or modify it under
707the same terms as perl itself.
708
709=cut
710
7111;
712EOF
713}
714
715sub _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};
fc7ec1d9 722 if ( $self->{type} eq 'C' ) {
ad41e777 723 $self->mk_file( "$test", <<"EOF");
fc7ec1d9 724use Test::More tests => 3;
725use_ok( Catalyst::Test, '$app' );
726use_ok('$class');
727
728ok( request('$prefix')->is_success );
729EOF
730 }
731 else {
ad41e777 732 $self->mk_file( "$test", <<"EOF");
fc7ec1d9 733use Test::More tests => 1;
734use_ok('$class');
735EOF
736 }
737}
738
7833fdfc 739=head1 HELPERS
740
741Helpers are classes that provide two methods.
742
743 * mk_compclass - creates the Component class
744 * mk_comptest - creates the Component test
745
746So when you call C<bin/create view MyView TT>, create would try to execute
747Catalyst::Helper::View::TT->mk_compclass and
748Catalyst::Helper::View::TT->mk_comptest.
749
750See L<Catalyst::Helper::View::TT> and L<Catalyst::Helper::Model::CDBI> for
751examples.
752
753All helper classes should be under one of the following namespaces.
754
755 Catalyst::Helper::Model::
756 Catalyst::Helper::View::
757 Catalyst::Helper::Controller::
758
fc7ec1d9 759=head1 SEE ALSO
760
761L<Catalyst::Manual>, L<Catalyst::Test>, L<Catalyst::Request>,
762L<Catalyst::Response>, L<Catalyst>
763
764=head1 AUTHOR
765
766Sebastian Riedel, C<sri@oook.de>
767
768=head1 LICENSE
769
770This library is free software . You can redistribute it and/or modify it under
771the same terms as perl itself.
772
773=cut
774
7751;