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