X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FHelper.pm;h=cb7f1400ef455b7c312f33732b9bc1e5450a4c23;hb=refs%2Fheads%2Fcustom_templates;hp=4c537f8fe33182a6cec44f9bac68267f3b0b0120;hpb=cf042872e38d51533829985f8fc62b18cd46a135;p=catagits%2FCatalyst-Devel.git diff --git a/lib/Catalyst/Helper.pm b/lib/Catalyst/Helper.pm index 4c537f8..cb7f140 100644 --- a/lib/Catalyst/Helper.pm +++ b/lib/Catalyst/Helper.pm @@ -1,8 +1,8 @@ package Catalyst::Helper; -use strict; -use warnings; +use Moose; use Config; use File::Spec; +use File::Spec::Unix; use File::Path; use FindBin; use IO::File; @@ -12,7 +12,16 @@ use Catalyst::Devel; use Catalyst::Utils; use Catalyst::Exception; use Path::Class qw/dir file/; -use File::ShareDir qw/dist_dir/; +use File::HomeDir; +use Path::Resolver::Resolver::Mux::Ordered; +use Path::Resolver::Resolver::FileSystem; +use Path::Resolver::Resolver::DistDir; +use namespace::autoclean; + +with 'MooseX::Emulate::Class::Accessor::Fast'; + +# Change Catalyst/Devel.pm also +our $VERSION = '1.23'; my %cache; @@ -26,21 +35,60 @@ Catalyst::Helper - Bootstrap a Catalyst application =cut +# Return the (cached) path resolver +{ + my $resolver; + + sub get_resolver { + my $self = shift; + + # Avoid typing this over and over + my $fs_path = sub { + Path::Resolver::Resolver::FileSystem->new({ root => shift }) + }; + + unless ($resolver) { + my @resolvers; + # Search path: first try the environment variable + if (exists $ENV{CATALYST_DEVEL_SHAREDIR}) { + push @resolvers, $fs_path->($ENV{CATALYST_DEVEL_SHAREDIR}); + } + # Then the application's "helper" directory + if (exists $self->{base}) { + push @resolvers, $fs_path->(dir($self->{base}, "helper")); + } + # Then ~/.catalyst/helper + push @resolvers, $fs_path->( + dir(File::HomeDir->my_home, ".catalyst", "helper") + ); + # Finally the Catalyst default + if (-d "inc/.author" && -f "lib/Catalyst/Helper.pm" + ) { # Can't use sharedir if we're in a checkout + # this feels horrible, better ideas? + push @resolvers, $fs_path->('share'); + } + else { + push @resolvers, Path::Resolver::Resolver::DistDir->new({ + dist_name => "Catalyst-Devel" + }); + } + + $resolver = Path::Resolver::Resolver::Mux::Ordered->new({ + resolvers => \@resolvers + }); + } + + return $resolver; + } +} + sub get_sharedir_file { my ($self, @filename) = @_; - my $dist_dir; - if (-d "inc/.author" && -f "lib/Catalyst/Helper.pm" - ) { # Can't use sharedir if we're in a checkout - # this feels horrible, better ideas? - $dist_dir = 'share'; - } - else { - $dist_dir = dist_dir('Catalyst-Devel'); - } - my $file = file( $dist_dir, @filename); - Carp::confess("Cannot find $file") unless -r $file; - my $contents = $file->slurp; - return $contents; + + my $filepath = file(@filename); + my $file = $self->get_resolver->entity_at("$filepath") # doesn't like object + or Carp::confess("Cannot find $filepath"); + return $file->content; } # Do not touch this method, *EVER*, it is needed for back compat. @@ -82,7 +130,7 @@ sub mk_app { $self->{startperl } = -r '/usr/bin/env' ? '#!/usr/bin/env perl' : "#!$Config{perlpath} -w"; - $self->{scriptgen } = $Catalyst::Devel::CATALYST_SCRIPT_GEN || 34; + $self->{scriptgen } = $Catalyst::Devel::CATALYST_SCRIPT_GEN; $self->{catalyst_version} = $Catalyst::VERSION; $self->{author } = $self->{author} = $ENV{'AUTHOR'} || eval { @{ [ getpwuid($<) ] }[6] } @@ -104,13 +152,14 @@ sub mk_app { } if ($gen_scripts) { for ( qw/ _mk_cgi _mk_fastcgi _mk_server - _mk_test _mk_create _mk_information / ) { - $self->$_; + _mk_test _mk_create _mk_information + / ) { + $self->$_; + } } return $self->{dir}; } - ## not much of this can really be changed, mk_compclass must be left for ## backcompat sub mk_component { @@ -183,17 +232,19 @@ sub mk_component { message => qq/Couldn't load helper "$class", "$@"/ ); } - ## must be left for back compat! ################################### if ( $class->can('mk_compclass') ) { return 1 unless $class->mk_compclass( $self, @args ); } - else { return 1 unless $self->_mk_compclass } + else { + return 1 unless $self->_mk_compclass + } if ( $class->can('mk_comptest') ) { $class->mk_comptest( $self, @args ); } - else { $self->_mk_comptest } - #################################################################### + else { + $self->_mk_comptest + } } # Fallback @@ -386,7 +437,7 @@ sub _mk_changes { my $self = shift; my $dir = $self->{dir}; my $time = strftime('%Y-%m-%d %H:%M:%S', localtime time); - $self->render_sharedir_file( 'Changes.tt', file($dir, "Changes", { time => $time } ); + $self->render_sharedir_file( 'Changes.tt', file($dir, "Changes"), { time => $time } ); } sub _mk_apptest { @@ -471,23 +522,15 @@ sub _mk_favicon { } -sub _mk_dbic_deploy { - my $self = shift; - my $script = $self->{script}; - my $appprefix = $self->{appprefix}; - $self->render_sharedir_file( file('script', 'myapp_deploy_schema.pl.tt'), file($script, "$appprefix\_deploy_schema.pl") ); - chmod 0700, file($script, "$appprefix\_deploy_schema.pl"); -} - sub _deprecate_file { my ( $self, $file ) = @_; if ( -e $file ) { - my $oldcontent; - if ( my $f = IO::File->new("< $file") ) { + my ($f, $oldcontent); + if ( $f = IO::File->new("< $file") ) { $oldcontent = join( '', (<$f>) ); } my $newfile = $file . '.deprecated'; - if ( my $f = IO::File->new("> $newfile") ) { + if ( $f = IO::File->new("> $newfile") ) { binmode $f; print $f $oldcontent; print qq/created "$newfile"\n/; @@ -647,7 +690,7 @@ Create the main application skeleton. This is called by L. This method is called by L to make new components for your application. -=head3 mk_dir ($path) +=head2 mk_dir ($path) Surprisingly, this function makes a directory. @@ -702,4 +745,3 @@ it under the same terms as Perl itself. =cut 1; -