X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FHelper.pm;h=508e7788ab85e32e38739a9ccb67336a516eb1f3;hb=3c24e97e23801a20139ce3c2153f20034a1b5dbe;hp=4746661283bdd836ed6e598e38ffd2629acfaf0c;hpb=3579bac61617a1c28f8e3852af5e9306f58caa8d;p=catagits%2FCatalyst-Devel.git diff --git a/lib/Catalyst/Helper.pm b/lib/Catalyst/Helper.pm index 4746661..508e778 100644 --- a/lib/Catalyst/Helper.pm +++ b/lib/Catalyst/Helper.pm @@ -13,51 +13,34 @@ use Template; use Catalyst::Devel; use Catalyst::Utils; use Catalyst::Exception; +use Path::Class qw/dir file/; +use File::ShareDir qw/dist_dir/; my %cache; - =head1 NAME Catalyst::Helper - Bootstrap a Catalyst application =head1 SYNOPSIS -See L - -=head1 DESCRIPTION - -Bootstrap a Catalyst application. Autogenerates scripts. - -=head2 METHODS - -=head3 get_file - -Slurp file from DATA. + catalyst.pl =cut -sub get_file { - my ( $self, $class, $file ) = @_; - unless ( $cache{$class} ) { - local $/; - $cache{$class} = eval "package $class; "; - } - my $data = $cache{$class}; - my @files = split /^__(.+)__\r?\n/m, $data; - shift @files; - while (@files) { - my ( $name, $content ) = splice @files, 0, 2; - return $content if $name eq $file; - } - return 0; +sub get_sharedir_file { + my ($self, @filename) = @_; + my $file = file( dist_dir('Catalyst-Devel'), @filename); + warn $file; + my $contents = $file->slurp; + return $contents; } -=head3 mk_app - -Create the main application skeleton. - -=cut +sub get_file { + my ( $self, $file ) = @_; + + return $self->get_sharedir_file($file); +} sub mk_app { my ( $self, $name ) = @_; @@ -65,7 +48,7 @@ sub mk_app { # Needs to be here for PAR require Catalyst; - if ( $name =~ /[^\w\:]/ ) { + if ( $name =~ /[^\w:]/ || $name =~ /^\d/ || $name =~ /\b:\b|:{3,}/) { warn "Error: Invalid application name.\n"; return 0; } @@ -74,7 +57,10 @@ sub mk_app { $self->{dir } =~ s/\:\:/-/g; $self->{script } = File::Spec->catdir( $self->{dir}, 'script' ); $self->{appprefix } = Catalyst::Utils::appprefix($name); - $self->{startperl } = "#!$Config{perlpath} -w"; + $self->{appenv } = Catalyst::Utils::class2env($name); + $self->{startperl } = -r '/usr/bin/env' + ? '#!/usr/bin/env perl' + : "#!$Config{perlpath} -w"; $self->{scriptgen } = $Catalyst::Devel::CATALYST_SCRIPT_GEN || 4; $self->{catalyst_version} = $Catalyst::VERSION; $self->{author } = $self->{author} = $ENV{'AUTHOR'} @@ -105,17 +91,11 @@ sub mk_app { $self->_mk_server; $self->_mk_test; $self->_mk_create; + $self->_mk_information; } return $self->{dir}; } -=head3 mk_component - -This method is called by create.pl to make new components -for your application. - -=cut - sub mk_component { my $self = shift; my $app = shift; @@ -144,7 +124,7 @@ sub mk_component { my $name = shift || "Missing name for model/view/controller"; my $helper = shift; my @args = @_; - return 0 if $name =~ /[^\w\:]/; + return 0 if $name =~ /[^\w\:]/; $type = lc $type; $self->{long_type} = ucfirst $type; $type = 'M' if $type =~ /model/i; @@ -206,12 +186,6 @@ sub mk_component { return 1; } -=head3 mk_dir - -Surprisingly, this function makes a directory. - -=cut - sub mk_dir { my ( $self, $dir ) = @_; if ( -d $dir ) { @@ -226,12 +200,6 @@ sub mk_dir { Catalyst::Exception->throw( message => qq/Couldn't create "$dir", "$!"/ ); } -=head3 mk_file - -writes content to a file. - -=cut - sub mk_file { my ( $self, $file, $content ) = @_; if ( -e $file ) { @@ -258,10 +226,6 @@ sub mk_file { Catalyst::Exception->throw( message => qq/Couldn't create "$file", "$!"/ ); } -=head3 next_test - -=cut - sub next_test { my ( $self, $tname ) = @_; if ($tname) { $tname = "$tname.t" } @@ -282,18 +246,11 @@ sub next_test { return File::Spec->catfile( $dir, "$type\_$tname" ); } -=head3 render_file - -Render and create a file from a template in DATA using -Template Toolkit. - -=cut - sub render_file { my ( $self, $file, $path, $vars ) = @_; $vars ||= {}; my $t = Template->new; - my $template = $self->get_file( ( caller(0) )[0], $file ); + my $template = $self->get_sharedir_file( 'root', $file ); return 0 unless $template; my $output; $t->process( \$template, { %{$self}, %$vars }, \$output ) @@ -302,6 +259,11 @@ sub render_file { $self->mk_file( $path, $output ); } +sub _mk_information { + my $self = shift; + print qq/Change to application directory and Run "perl Makefile.PL" to make sure your install is complete\n/; +} + sub _mk_dirs { my $self = shift; $self->mk_dir( $self->{dir} ); @@ -346,12 +308,12 @@ sub _mk_dirs { sub _mk_appclass { my $self = shift; my $mod = $self->{mod}; - $self->render_file( 'appclass', "$mod.pm" ); + $self->render_file( 'appclass.tt', "$mod.pm" ); } sub _mk_rootclass { my $self = shift; - $self->render_file( 'rootclass', + $self->render_file( 'rootclass.tt', File::Spec->catfile( $self->{c}, "Root.pm" ) ); } @@ -360,7 +322,7 @@ sub _mk_makefile { $self->{path} = File::Spec->catfile( 'lib', split( '::', $self->{name} ) ); $self->{path} .= '.pm'; my $dir = $self->{dir}; - $self->render_file( 'makefile', "$dir\/Makefile.PL" ); + $self->render_file( 'makefile.tt', "$dir\/Makefile.PL" ); if ( $self->{makefile} ) { @@ -374,36 +336,36 @@ sub _mk_config { my $self = shift; my $dir = $self->{dir}; my $appprefix = $self->{appprefix}; - $self->render_file( 'config', - File::Spec->catfile( $dir, "$appprefix.yml" ) ); + $self->render_file( 'config.tt', + File::Spec->catfile( $dir, "$appprefix.conf" ) ); } sub _mk_readme { my $self = shift; my $dir = $self->{dir}; - $self->render_file( 'readme', "$dir\/README" ); + $self->render_file( 'readme.tt', "$dir\/README" ); } sub _mk_changes { my $self = shift; my $dir = $self->{dir}; - my $time = strftime('%Y-%m-%d %T', localtime time); - $self->render_file( 'changes', "$dir\/Changes", { time => $time } ); + my $time = strftime('%Y-%m-%d %H:%M:%S', localtime time); + $self->render_file( 'changes.tt', "$dir\/Changes", { time => $time } ); } sub _mk_apptest { my $self = shift; my $t = $self->{t}; - $self->render_file( 'apptest', "$t\/01app.t" ); - $self->render_file( 'podtest', "$t\/02pod.t" ); - $self->render_file( 'podcoveragetest', "$t\/03podcoverage.t" ); + $self->render_file( 'apptest.tt', "$t\/01app.t" ); + $self->render_file( 'podtest.tt', "$t\/02pod.t" ); + $self->render_file( 'podcoveragetest.tt', "$t\/03podcoverage.t" ); } sub _mk_cgi { my $self = shift; my $script = $self->{script}; my $appprefix = $self->{appprefix}; - $self->render_file( 'cgi', "$script\/$appprefix\_cgi.pl" ); + $self->render_file( 'cgi.tt', "$script\/$appprefix\_cgi.pl" ); chmod 0700, "$script/$appprefix\_cgi.pl"; } @@ -411,7 +373,7 @@ sub _mk_fastcgi { my $self = shift; my $script = $self->{script}; my $appprefix = $self->{appprefix}; - $self->render_file( 'fastcgi', "$script\/$appprefix\_fastcgi.pl" ); + $self->render_file( 'fastcgi.tt', "$script\/$appprefix\_fastcgi.pl" ); chmod 0700, "$script/$appprefix\_fastcgi.pl"; } @@ -419,7 +381,7 @@ sub _mk_server { my $self = shift; my $script = $self->{script}; my $appprefix = $self->{appprefix}; - $self->render_file( 'server', "$script\/$appprefix\_server.pl" ); + $self->render_file( 'server.tt', "$script\/$appprefix\_server.pl" ); chmod 0700, "$script/$appprefix\_server.pl"; } @@ -427,7 +389,7 @@ sub _mk_test { my $self = shift; my $script = $self->{script}; my $appprefix = $self->{appprefix}; - $self->render_file( 'test', "$script/$appprefix\_test.pl" ); + $self->render_file( 'test.tt', "$script/$appprefix\_test.pl" ); chmod 0700, "$script/$appprefix\_test.pl"; } @@ -435,20 +397,20 @@ sub _mk_create { my $self = shift; my $script = $self->{script}; my $appprefix = $self->{appprefix}; - $self->render_file( 'create', "$script\/$appprefix\_create.pl" ); + $self->render_file( 'create.tt', "$script\/$appprefix\_create.pl" ); chmod 0700, "$script/$appprefix\_create.pl"; } sub _mk_compclass { my $self = shift; my $file = $self->{file}; - return $self->render_file( 'compclass', "$file" ); + return $self->render_file( 'compclass.tt', "$file" ); } sub _mk_comptest { my $self = shift; my $test = $self->{test}; - $self->render_file( 'comptest', "$test" ); + $self->render_file( 'comptest.tt', "$test" ); } sub _mk_images { @@ -459,8 +421,7 @@ sub _mk_images { btn_120x50_powered btn_120x50_powered_shadow btn_88x31_built btn_88x31_built_shadow btn_88x31_powered btn_88x31_powered_shadow/; for my $name (@images) { - my $hex = $self->get_file( ( caller(0) )[0], $name ); - my $image = pack "H*", $hex; + my $image = $self->get_file("$name.png"); $self->mk_file( File::Spec->catfile( $images, "$name.png" ), $image ); } } @@ -468,9 +429,9 @@ sub _mk_images { sub _mk_favicon { my $self = shift; my $root = $self->{root}; - my $hex = $self->get_file( ( caller(0) )[0], 'favicon' ); - my $favicon = pack "H*", $hex; - $self->mk_file( File::Spec->catfile( $root, "favicon.ico" ), $favicon ); + my $favicon = $self->get_sharedir_file( 'root', 'favicon.ico' ); + my $dest = File::Spec->catfile( $root, "favicon.ico" ); + $self->mk_file( $dest, $favicon ); } @@ -495,671 +456,187 @@ sub _deprecate_file { } } -=head1 HELPERS - -Helpers are classes that provide two methods. - - * mk_compclass - creates the Component class - * mk_comptest - creates the Component test - -So when you call C, create would try to execute -Catalyst::Helper::View::TT->mk_compclass and -Catalyst::Helper::View::TT->mk_comptest. - -See L and L for -examples. - -All helper classes should be under one of the following namespaces. - - Catalyst::Helper::Model:: - Catalyst::Helper::View:: - Catalyst::Helper::Controller:: - -=head1 NOTE - -The helpers will read author name from /etc/passwd by default. -To override, please export the AUTHOR variable. - -=head1 SEE ALSO - -L, L, L, -L, L - -=head1 AUTHOR - -Sebastian Riedel, C - -=head1 LICENSE - -This library is free software, you can redistribute it and/or modify -it under the same terms as Perl itself. - -=begin pod_to_ignore - -=cut - -1; -__DATA__ - -__appclass__ -package [% name %]; - -use strict; -use warnings; - -use Catalyst::Runtime '5.70'; - -# Set flags and add plugins for the application -# -# -Debug: activates the debug mode for very useful log messages -# ConfigLoader: will load the configuration from a YAML file in the -# application's home directory -# Static::Simple: will serve static files from the application's root -# directory - -use Catalyst qw/-Debug ConfigLoader Static::Simple/; - -our $VERSION = '0.01'; - -# Configure the application. -# -# Note that settings in [% name %].yml (or other external -# configuration file that you set up manually) take precedence -# over this when using ConfigLoader. Thus configuration -# details given here can function as a default configuration, -# with a external configuration file acting as an override for -# local deployment. - -__PACKAGE__->config( name => '[% name %]' ); - -# Start the application -__PACKAGE__->setup; - - -=head1 NAME - -[% name %] - Catalyst based application - -=head1 SYNOPSIS - - script/[% appprefix %]_server.pl - -=head1 DESCRIPTION - -[enter your description here] - -=head1 SEE ALSO - -L<[% rootname %]>, L - -=head1 AUTHOR - -[% author %] - -=head1 LICENSE - -This library is free software, you can redistribute it and/or modify -it under the same terms as Perl itself. - -=cut - -1; -__rootclass__ -package [% rootname %]; - -use strict; -use warnings; -use base 'Catalyst::Controller'; - -# -# Sets the actions in this controller to be registered with no prefix -# so they function identically to actions created in MyApp.pm -# -__PACKAGE__->config->{namespace} = ''; - -=head1 NAME - -[% rootname %] - Root Controller for [% name %] - =head1 DESCRIPTION -[enter your description here] +This module is used by B to create a set of scripts for a +new catalyst application. The scripts each contain documentation and +will output help on how to use them if called incorrectly or in some +cases, with no arguments. -=head1 METHODS +It also provides some useful methods for a Helper module to call when +creating a component. See L. -=cut +=head1 SCRIPTS -=head2 default +=head2 _create.pl -=cut +Used to create new components for a catalyst application at the +development stage. -sub default : Private { - my ( $self, $c ) = @_; +=head2 _server.pl - # Hello World - $c->response->body( $c->welcome_message ); -} +The catalyst test server, starts an HTTPD which outputs debugging to +the terminal. -=head2 end +=head2 _test.pl -Attempt to render a view, if needed. +A script for running tests from the command-line. -=cut +=head2 _cgi.pl -sub end : ActionClass('RenderView') {} +Run your application as a CGI. -=head1 AUTHOR +=head2 _fastcgi.pl -[% author %] +Run the application as a fastcgi app. Either by hand, or call this +from FastCgiServer in your http server config. -=head1 LICENSE +=head1 HELPERS -This library is free software, you can redistribute it and/or modify -it under the same terms as Perl itself. +The L script creates application components using Helper +modules. The Catalyst team provides a good number of Helper modules +for you to use. You can also add your own. -=cut +Helpers are classes that provide two methods. -1; -__makefile__ -use inc::Module::Install; - -name '[% dir %]'; -all_from '[% path %]'; - -requires 'Catalyst' => '[% catalyst_version %]'; -requires 'Catalyst::Plugin::ConfigLoader'; -requires 'Catalyst::Plugin::Static::Simple'; -requires 'Catalyst::Action::RenderView'; -requires 'YAML'; # This should reflect the config file format you've chosen - # See Catalyst::Plugin::ConfigLoader for supported formats -catalyst; - -install_script glob('script/*.pl'); -auto_install; -WriteAll; -__config__ ---- -name: [% name %] -__readme__ -Run script/[% appprefix %]_server.pl to test the application. -__changes__ -This file documents the revision history for Perl extension [% name %]. - -0.01 [% time %] - - initial revision, generated by Catalyst -__apptest__ -use strict; -use warnings; -use Test::More tests => 2; + * mk_compclass - creates the Component class + * mk_comptest - creates the Component test -BEGIN { use_ok 'Catalyst::Test', '[% name %]' } +So when you call C, create +will try to execute Catalyst::Helper::View::TT->mk_compclass and +Catalyst::Helper::View::TT->mk_comptest. -ok( request('/')->is_success, 'Request should succeed' ); -__podtest__ -use strict; -use warnings; -use Test::More; +See L and +L for examples. -eval "use Test::Pod 1.14"; -plan skip_all => 'Test::Pod 1.14 required' if $@; -plan skip_all => 'set TEST_POD to enable this test' unless $ENV{TEST_POD}; +All helper classes should be under one of the following namespaces. -all_pod_files_ok(); -__podcoveragetest__ -use strict; -use warnings; -use Test::More; + Catalyst::Helper::Model:: + Catalyst::Helper::View:: + Catalyst::Helper::Controller:: -eval "use Test::Pod::Coverage 1.04"; -plan skip_all => 'Test::Pod::Coverage 1.04 required' if $@; -plan skip_all => 'set TEST_POD to enable this test' unless $ENV{TEST_POD}; +=head2 COMMON HELPERS -all_pod_coverage_ok(); -__cgi__ -[% startperl %] +=over -BEGIN { $ENV{CATALYST_ENGINE} ||= 'CGI' } +=item * -use strict; -use warnings; -use FindBin; -use lib "$FindBin::Bin/../lib"; -use [% name %]; +L - DBIx::Class models -[% name %]->run; +=item * -1; +L - Template Toolkit view -=head1 NAME +=item * -[% appprefix %]_cgi.pl - Catalyst CGI +L -=head1 SYNOPSIS +=item * -See L +L - wrap any class into a Catalyst model -=head1 DESCRIPTION +=back -Run a Catalyst application as a cgi script. +=head3 NOTE -=head1 AUTHOR +The helpers will read author name from /etc/passwd by default. + To override, please export the AUTHOR variable. -Sebastian Riedel, C +=head1 METHODS -=head1 COPYRIGHT +=head2 mk_compclass +This method in your Helper module is called with C<$helper> +which is a L object, and whichever other arguments +the user added to the command-line. You can use the $helper to call methods +described below. -This library is free software, you can redistribute it and/or modify -it under the same terms as Perl itself. +If the Helper module does not contain a C method, it +will fall back to calling L, with an argument of +C. -=cut -__fastcgi__ -[% startperl %] +=head2 mk_comptest -BEGIN { $ENV{CATALYST_ENGINE} ||= 'FastCGI' } +This method in your Helper module is called with C<$helper> +which is a L object, and whichever other arguments +the user added to the command-line. You can use the $helper to call methods +described below. -use strict; -use warnings; -use Getopt::Long; -use Pod::Usage; -use FindBin; -use lib "$FindBin::Bin/../lib"; -use [% name %]; - -my $help = 0; -my ( $listen, $nproc, $pidfile, $manager, $detach, $keep_stderr ); - -GetOptions( - 'help|?' => \$help, - 'listen|l=s' => \$listen, - 'nproc|n=i' => \$nproc, - 'pidfile|p=s' => \$pidfile, - 'manager|M=s' => \$manager, - 'daemon|d' => \$detach, - 'keeperr|e' => \$keep_stderr, -); - -pod2usage(1) if $help; - -[% name %]->run( - $listen, - { nproc => $nproc, - pidfile => $pidfile, - manager => $manager, - detach => $detach, - keep_stderr => $keep_stderr, - } -); +If the Helper module does not contain a C method, it +will fall back to calling L, with an argument of +C. -1; +=head2 mk_stuff -=head1 NAME +This method is called if the user does not supply any of the usual +component types C, C, C. It is passed the +C<$helper> object (an instance of L), and any other +arguments the user typed. -[% appprefix %]_fastcgi.pl - Catalyst FastCGI +There is no fallback for this method. -=head1 SYNOPSIS +=head1 INTERNAL METHODS -[% appprefix %]_fastcgi.pl [options] - - Options: - -? -help display this help and exits - -l -listen Socket path to listen on - (defaults to standard input) - can be HOST:PORT, :PORT or a - filesystem path - -n -nproc specify number of processes to keep - to serve requests (defaults to 1, - requires -listen) - -p -pidfile specify filename for pid file - (requires -listen) - -d -daemon daemonize (requires -listen) - -M -manager specify alternate process manager - (FCGI::ProcManager sub-class) - or empty string to disable - -e -keeperr send error messages to STDOUT, not - to the webserver +These are the methods that the Helper classes can call on the +<$helper> object passed to them. -=head1 DESCRIPTION +=head2 render_file ($file, $path, $vars) -Run a Catalyst application as fastcgi. +Render and create a file from a template in DATA using Template +Toolkit. $file is the relevent chunk of the __DATA__ section, $path is +the path to the file and $vars is the hashref as expected by +L