use Catalyst::Exception;
use Path::Class qw/dir file/;
use File::ShareDir qw/dist_dir/;
+use Moose;
+use aliased 'Path::Class::Dir';
+
my %cache;
=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, $class, $file ) = @_;
unless ( $cache{$class} ) {
$cache{$class} = eval "package $class; <DATA>";
}
my $data = $cache{$class};
+ Carp::confess("Could not get data from __DATA__ segment for $class")
+ unless $data;
my @files = split /^__(.+)__\r?\n/m, $data;
shift @files;
while (@files) {
sub mk_file {
my ( $self, $file, $content ) = @_;
- if ( -e $file ) {
+ if ( -e $file && -s _ ) {
print qq/ exists "$file"\n/;
return 0
unless ( $self->{'.newfiles'}
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($template, $path, $vars);
+}
+
+sub render_sharedir_file {
+ my ( $self, $file, $path, $vars ) = @_;
+ my $template = $self->get_sharedir_file( $file );
+ $self->render_file_contents($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 );
}
sub _mk_appclass {
my $self = shift;
my $mod = $self->{mod};
- $self->render_file( 'appclass.tt', "$mod.pm" );
+ $self->render_sharedir_file( 'lib/appclass.tt', "$mod.pm" );
}
sub _mk_rootclass {
my $self = shift;
- $self->render_file( 'rootclass.tt',
+ $self->render_sharedir_file( 'lib/MyApp/Controller/rootclass.tt',
File::Spec->catfile( $self->{c}, "Root.pm" ) );
}
$self->{path} = File::Spec->catfile( 'lib', split( '::', $self->{name} ) );
$self->{path} .= '.pm';
my $dir = $self->{dir};
- $self->render_file( 'makefile.tt', "$dir\/Makefile.PL" );
+ $self->render_sharedir_file( 'Makefile.PL.tt', "$dir\/Makefile.PL" );
if ( $self->{makefile} ) {
my $self = shift;
my $dir = $self->{dir};
my $appprefix = $self->{appprefix};
- $self->render_file( 'config.tt',
+ $self->render_sharedir_file( 'myapp.conf.tt',
File::Spec->catfile( $dir, "$appprefix.conf" ) );
}
sub _mk_readme {
my $self = shift;
my $dir = $self->{dir};
- $self->render_file( 'readme.tt', "$dir\/README" );
+ $self->render_sharedir_file( 'README.tt', "$dir\/README" );
}
sub _mk_changes {
my $self = shift;
my $dir = $self->{dir};
my $time = strftime('%Y-%m-%d %H:%M:%S', localtime time);
- $self->render_file( 'changes.tt', "$dir\/Changes", { time => $time } );
+ $self->render_sharedir_file( 'Changes.tt', "$dir\/Changes", { time => $time } );
}
sub _mk_apptest {
my $self = shift;
my $t = $self->{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" );
+ $self->render_sharedir_file( 't/apptest.tt', "$t\/01app.t" );
+ $self->render_sharedir_file( 't/podtest.tt', "$t\/02pod.t" );
+ $self->render_sharedir_file( 't/podcoveragetest.tt', "$t\/03podcoverage.t" );
}
sub _mk_cgi {
my $self = shift;
my $script = $self->{script};
my $appprefix = $self->{appprefix};
- $self->render_file( 'cgi.tt', "$script\/$appprefix\_cgi.pl" );
+ $self->render_sharedir_file( 'script/cgi.tt', "$script\/$appprefix\_cgi.pl" );
chmod 0700, "$script/$appprefix\_cgi.pl";
}
my $self = shift;
my $script = $self->{script};
my $appprefix = $self->{appprefix};
- $self->render_file( 'fastcgi.tt', "$script\/$appprefix\_fastcgi.pl" );
+ $self->render_sharedir_file( 'script/fastcgi.tt', "$script\/$appprefix\_fastcgi.pl" );
chmod 0700, "$script/$appprefix\_fastcgi.pl";
}
my $self = shift;
my $script = $self->{script};
my $appprefix = $self->{appprefix};
- $self->render_file( 'server.tt', "$script\/$appprefix\_server.pl" );
+ $self->render_sharedir_file( 'script/server.tt', "$script\/$appprefix\_server.pl" );
chmod 0700, "$script/$appprefix\_server.pl";
}
my $self = shift;
my $script = $self->{script};
my $appprefix = $self->{appprefix};
- $self->render_file( 'test.tt', "$script/$appprefix\_test.pl" );
+ $self->render_sharedir_file( 'script/test.tt', "$script/$appprefix\_test.pl" );
chmod 0700, "$script/$appprefix\_test.pl";
}
my $self = shift;
my $script = $self->{script};
my $appprefix = $self->{appprefix};
- $self->render_file( 'create.tt', "$script\/$appprefix\_create.pl" );
+ $self->render_sharedir_file( 'script/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.tt', "$file" );
+ return $self->render_sharedir_file( 'compclass.tt', "$file" );
}
sub _mk_comptest {
my $self = shift;
my $test = $self->{test};
- $self->render_file( 'comptest.tt', "$test" );
+ $self->render_sharedir_file( 'comptest.tt', "$test" );
}
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", "static", "images", "$name.png.bin");
$self->mk_file( File::Spec->catfile( $images, "$name.png" ), $image );
}
}
sub _mk_favicon {
my $self = shift;
my $root = $self->{root};
- my $favicon = $self->get_sharedir_file( 'root', 'favicon.ico' );
+ my $favicon = $self->get_sharedir_file( 'root', 'favicon.ico.bin' );
my $dest = File::Spec->catfile( $root, "favicon.ico" );
$self->mk_file( $dest, $favicon );
}
}
+
+## 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<catalyst.pl> to create a set of scripts for a
Calculates the name of the next numbered test file and returns it.
Don't give the number or the .t suffix for the test name.
+=head2 Dir
+
+Alias for L<Path::Class::Dir>
+
+=cut
+
+=head2 get_sharedir_file
+
+Method for getting a file out of share/
+
+=cut
+
+=head2 render_file_contents
+
+Process a L<Template::Toolkit> template.
+
+=cut
+
+=head2 render_sharedir_file
+
+Render a template/image file from our share directory
+
+=cut
+
+
=head1 NOTE
The helpers will read author name from /etc/passwd by default.