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