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