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