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