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