X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FHelper.pm;h=ef9b3c17e881c7892821b1cce3b977418dc57874;hb=7025ed899fbbf9d2bac50f85943d66e79d448b8d;hp=508e7788ab85e32e38739a9ccb67336a516eb1f3;hpb=3c24e97e23801a20139ce3c2153f20034a1b5dbe;p=catagits%2FCatalyst-Devel.git diff --git a/lib/Catalyst/Helper.pm b/lib/Catalyst/Helper.pm index 508e778..ef9b3c1 100644 --- a/lib/Catalyst/Helper.pm +++ b/lib/Catalyst/Helper.pm @@ -15,6 +15,9 @@ use Catalyst::Utils; use Catalyst::Exception; use Path::Class qw/dir file/; use File::ShareDir qw/dist_dir/; +use Moose; +use aliased 'Path::Class::Dir'; + my %cache; @@ -28,20 +31,41 @@ Catalyst::Helper - Bootstrap a Catalyst application =cut + + sub get_sharedir_file { my ($self, @filename) = @_; - my $file = file( dist_dir('Catalyst-Devel'), @filename); - warn $file; + my $dist_dir; + if (-d "inc/.author") { # 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); my $contents = $file->slurp; return $contents; } +# Do not touch this method, *EVER*, it is needed for back compat. sub get_file { - my ( $self, $file ) = @_; - - return $self->get_sharedir_file($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 mk_app { my ( $self, $name ) = @_; @@ -246,16 +270,31 @@ sub next_test { return File::Spec->catfile( $dir, "$type\_$tname" ); } +# Do not touch this method, *EVER*, it is needed for back compat. +## addendum: we had to split this method so we could have backwards +## compatability. otherwise, we'd have no way to pass stuff from __DATA__ + sub render_file { my ( $self, $file, $path, $vars ) = @_; + my $template = $self->get_file( ( caller(0) )[0], $file ); + $self->render_file_contents($self, $template, $path, $vars); +} + +sub render_sharedir_file { + my ( $self, $file, $path, $vars ) = @_; + my $template = $self->get_sharedir_file( $file ); + $self->render_file_contents($self, $template, $path, $vars); +} + +sub render_file_contents { + my ( $self, $template, $path, $vars ) = @_; $vars ||= {}; my $t = Template->new; - my $template = $self->get_sharedir_file( 'root', $file ); return 0 unless $template; my $output; $t->process( \$template, { %{$self}, %$vars }, \$output ) || Catalyst::Exception->throw( - message => qq/Couldn't process "$file", / . $t->error() ); + message => qq/Couldn't process "$template", / . $t->error() ); $self->mk_file( $path, $output ); } @@ -421,7 +460,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 $image = $self->get_file("$name.png"); + my $image = $self->get_sharedir_file("root", "$name.png"); $self->mk_file( File::Spec->catfile( $images, "$name.png" ), $image ); } } @@ -456,6 +495,42 @@ sub _deprecate_file { } } + +## this is so you don't have to do make install after every change to test +sub _find_share_dir { + my ($self, $args) = @_; + my $share_name = $self->name; + if ($share_name =~ s!^/(.*?)/!!) { + my $dist = $1; + $args->{share_base_dir} = eval { + Dir->new(File::ShareDir::dist_dir($dist)) + ->subdir('share'); + }; + if ($@) { + # not installed + my $file = __FILE__; + my $dir = Dir->new(dirname($file)); + my $share_base; + while ($dir->parent) { + if (-d $dir->subdir('share') && -d $dir->subdir('share')->subdir('root')) { + $share_base = $dir->subdir('share')->subdir('root'); + last; + } + $dir = $dir->parent; + } + confess "could not find sharebase by recursion. ended up at $dir, from $file" + unless $share_base; + $args->{share_base_dir} = $share_base; + } + } + my $base = $args->{share_base_dir}->subdir($share_name); + confess "No such share base directory ${base}" + unless -d $base; + $self->share_dir($base); +}; + + + =head1 DESCRIPTION This module is used by B to create a set of scripts for a