whitespace cleanup
[catagits/Catalyst-Devel.git] / lib / Catalyst / Helper.pm
index 1bb9f97..9785aec 100644 (file)
@@ -1,7 +1,8 @@
 package Catalyst::Helper;
-#use Moose;
+use Moose;
 use Config;
 use File::Spec;
+use File::Spec::Unix;
 use File::Path;
 use FindBin;
 use IO::File;
@@ -12,24 +13,23 @@ use Catalyst::Utils;
 use Catalyst::Exception;
 use Path::Class qw/dir file/;
 use File::ShareDir qw/dist_dir/;
-#use namespace::autoclean;
+use YAML::Tiny;
+use namespace::autoclean;
 
-my %cache;
+with 'MooseX::Emulate::Class::Accessor::Fast';
 
-=head1 NAME
+our $VERSION = '1.41';
+$VERSION =~ tr/_//d;
 
-Catalyst::Helper - Bootstrap a Catalyst application
-
-=head1 SYNOPSIS
-
-  catalyst.pl <myappname>
-
-=cut
+my %cache;
 
 sub get_sharedir_file {
     my ($self, @filename) = @_;
     my $dist_dir;
-    if (-d "inc/.author" && -f "lib/Catalyst/Helper.pm"
+    if (exists $ENV{CATALYST_DEVEL_SHAREDIR}) {
+        $dist_dir = $ENV{CATALYST_DEVEL_SHAREDIR};
+    }
+    elsif (-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';
@@ -39,7 +39,7 @@ sub get_sharedir_file {
     }
     my $file = file( $dist_dir, @filename);
     Carp::confess("Cannot find $file") unless -r $file;
-    my $contents = $file->slurp;
+    my $contents = $file->slurp(iomode =>  "<:raw");
     return $contents;
 }
 
@@ -69,22 +69,40 @@ sub mk_app {
     # Needs to be here for PAR
     require Catalyst;
 
+    if($name eq '.') {
+        if(!-e 'META.yml') {
+            system perl => 'Makefile.PL'
+                and Catalyst::Exception->throw(message => q(
+                    Failed to run "perl Makefile.PL".
+                ));
+        }
+
+        $name = YAML::Tiny->read('META.yml')->[0]->{'name'};
+        $name =~ s/-/::/g;
+        $self->{dir} = '.';
+    }
+
     if ( $name =~ /[^\w:]/ || $name =~ /^\d/ || $name =~ /\b:\b|:{3,}/) {
         warn "Error: Invalid application name.\n";
         return 0;
     }
+
+
+    if(!defined $self->{'dir'}) {
+        $self->{dir} = $name;
+        $self->{dir} =~ s/\:\:/-/g;
+    }
+
     $self->{name            } = $name;
-    $self->{dir             } = $name;
-    $self->{dir             } =~ s/\:\:/-/g;
     $self->{script          } = dir( $self->{dir}, 'script' );
     $self->{appprefix       } = Catalyst::Utils::appprefix($name);
     $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;
+                                : "#!$Config{perlpath}";
+    $self->{scriptgen       } = $Catalyst::Devel::CATALYST_SCRIPT_GEN;
     $self->{catalyst_version} = $Catalyst::VERSION;
-    $self->{author          } = $self->{author} = $ENV{'AUTHOR'}
+    $self->{author          } ||= $ENV{'AUTHOR'}
       || eval { @{ [ getpwuid($<) ] }[6] }
       || 'Catalyst developer';
 
@@ -93,31 +111,26 @@ sub mk_app {
     my $gen_app = ( $self->{scripts} || $self->{makefile} ) ? 0 : 1;
 
     if ($gen_app) {
-    
-        for ( qw/ _mk_dirs _mk_config _mk_appclass _mk_rootclass _mk_readme
-              _mk_changes _mk_apptest _mk_images _mk_favicon/ ) {
-            
+        for ( qw/ _mk_dirs _mk_config _mk_psgi _mk_appclass _mk_rootclass
+              _mk_readme _mk_changes _mk_apptest _mk_podtest _mk_podcoveragetest
+              _mk_images _mk_favicon/ ) {
             $self->$_;
-       
         }
     }
     if ($gen_makefile) {
         $self->_mk_makefile;
     }
     if ($gen_scripts) {
-        for ( qw/ _mk_cgi _mk_fastcgi _mk_server 
-                  _mk_test _mk_create _mk_information / ) {
-        $self->$_;
-      #  probably want to only do this if a DBIC schema is specified, or move it
-      #  to C::H::Model::DBIC::Schema
-      #  $self->_mk_dbic_deploy; 
-        
+        for ( qw/ _mk_cgi _mk_fastcgi _mk_server
+                  _mk_test _mk_create _mk_information
+        / ) {
+              $self->$_;
+        }
     }
     return $self->{dir};
 }
 
-
-## not much of this can really be changed, mk_compclass must be left for 
+## not much of this can really be changed, mk_compclass must be left for
 ## backcompat
 sub mk_component {
     my $self = shift;
@@ -189,17 +202,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
@@ -241,12 +256,12 @@ sub mk_file {
             $file .= '.new';
         }
     }
-    
+
     if ( my $f = IO::File->new("> $file") ) {
         binmode $f;
         print $f $content;
         print qq/created "$file"\n/;
-        return 1;
+        return $file;
     }
 
     Catalyst::Exception->throw( message => qq/Couldn't create "$file", "$!"/ );
@@ -274,23 +289,23 @@ sub next_test {
 
 # 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__
+## compatibility.  otherwise, we'd have no way to pass stuff from __DATA__
 
 sub render_file {
-    my ( $self, $file, $path, $vars ) = @_;
+    my ( $self, $file, $path, $vars, $perms ) = @_;
     my $template = $self->get_file( ( caller(0) )[0], $file );
-    $self->render_file_contents($template, $path, $vars);
+    $self->render_file_contents($template, $path, $vars, $perms);
 }
 
 sub render_sharedir_file {
-    my ( $self, $file, $path, $vars ) = @_;
+    my ( $self, $file, $path, $vars, $perms ) = @_;
     my $template = $self->get_sharedir_file( $file );
     die("Cannot get template from $file for $self\n") unless $template;
-    $self->render_file_contents($template, $path, $vars);
+    $self->render_file_contents($template, $path, $vars, $perms);
 }
 
 sub render_file_contents {
-    my ( $self, $template, $path, $vars ) = @_;
+    my ( $self, $template, $path, $vars, $perms ) = @_;
     $vars ||= {};
     my $t = Template->new;
     return 0 unless $template;
@@ -298,7 +313,9 @@ sub render_file_contents {
     $t->process( \$template, { %{$self}, %$vars }, \$output )
       || Catalyst::Exception->throw(
         message => qq/Couldn't process "$template", / . $t->error() );
-    $self->mk_file( $path, $output );
+    my $file = $self->mk_file( $path, $output );
+    chmod $perms, file($file) if defined $perms;
+    return $file;
 }
 
 sub _mk_information {
@@ -361,7 +378,7 @@ sub _mk_rootclass {
 
 sub _mk_makefile {
     my $self = shift;
-    $self->{path} = dir( 'lib', split( '::', $self->{name} ) );
+    $self->{path} = join('/', 'lib', split( '::', $self->{name} ) );
     $self->{path} .= '.pm';
     my $dir = $self->{dir};
     $self->render_sharedir_file( 'Makefile.PL.tt', file($dir, "Makefile.PL") );
@@ -374,6 +391,14 @@ sub _mk_makefile {
     }
 }
 
+sub _mk_psgi {
+    my $self      = shift;
+    my $dir       = $self->{dir};
+    my $appprefix = $self->{appprefix};
+    $self->render_sharedir_file( 'myapp.psgi.tt',
+        file( $dir, "$appprefix.psgi" ) );
+}
+
 sub _mk_config {
     my $self      = shift;
     my $dir       = $self->{dir};
@@ -392,14 +417,24 @@ 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 {
     my $self = shift;
     my $t    = $self->{t};
     $self->render_sharedir_file( file('t', '01app.t.tt'),         file($t, "01app.t") );
+}
+
+sub _mk_podtest {
+    my $self = shift;
+    my $t    = $self->{t};
     $self->render_sharedir_file( file('t', '02pod.t.tt'),         file($t, "02pod.t") );
+}
+
+sub _mk_podcoveragetest {
+    my $self = shift;
+    my $t    = $self->{t};
     $self->render_sharedir_file( file('t', '03podcoverage.t.tt'), file($t, "03podcoverage.t") );
 }
 
@@ -407,40 +442,40 @@ sub _mk_cgi {
     my $self      = shift;
     my $script    = $self->{script};
     my $appprefix = $self->{appprefix};
-    $self->render_sharedir_file( file('script', 'myapp_cgi.pl.tt'), file($script,"$appprefix\_cgi.pl") );
-    chmod 0700, file($script,"$appprefix\_cgi.pl");
+    $self->render_sharedir_file( file('script', 'myapp_cgi.pl.tt'),
+        file($script,"$appprefix\_cgi.pl"), undef, 0755 );
 }
 
 sub _mk_fastcgi {
     my $self      = shift;
     my $script    = $self->{script};
     my $appprefix = $self->{appprefix};
-    $self->render_sharedir_file( file('script', 'myapp_fastcgi.pl.tt'), file($script, "$appprefix\_fastcgi.pl") );
-    chmod 0700, file($script, "$appprefix\_fastcgi.pl");
+    $self->render_sharedir_file( file('script', 'myapp_fastcgi.pl.tt'),
+        file($script, "$appprefix\_fastcgi.pl"), undef, 0755 );
 }
 
 sub _mk_server {
     my $self      = shift;
     my $script    = $self->{script};
     my $appprefix = $self->{appprefix};
-    $self->render_sharedir_file( file('script', 'myapp_server.pl.tt'), file($script, "$appprefix\_server.pl") );
-    chmod 0700, file($script, "$appprefix\_server.pl");
+    $self->render_sharedir_file( file('script', 'myapp_server.pl.tt'),
+        file($script, "$appprefix\_server.pl"), undef, 0755 );
 }
 
 sub _mk_test {
     my $self      = shift;
     my $script    = $self->{script};
     my $appprefix = $self->{appprefix};
-    $self->render_sharedir_file( file('script', 'myapp_test.pl.tt'), file($script, "$appprefix\_test.pl") );
-    chmod 0700, file($script, "$appprefix\_test.pl");
+    $self->render_sharedir_file( file('script', 'myapp_test.pl.tt'),
+        file($script, "$appprefix\_test.pl"), undef, 0755 );
 }
 
 sub _mk_create {
     my $self      = shift;
     my $script    = $self->{script};
     my $appprefix = $self->{appprefix};
-    $self->render_sharedir_file( file('script', 'myapp_create.pl.tt'), file($script, "$appprefix\_create.pl") );
-    chmod 0700, file($script, "$appprefix\_create.pl");
+    $self->render_sharedir_file( file('script', 'myapp_create.pl.tt'),
+        file($script, "$appprefix\_create.pl"), undef, 0755 );
 }
 
 sub _mk_compclass {
@@ -477,23 +512,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/;
@@ -506,6 +533,17 @@ sub _deprecate_file {
     }
 }
 
+1;
+__END__
+
+=head1 NAME
+
+Catalyst::Helper - Bootstrap a Catalyst application
+
+=head1 SYNOPSIS
+
+  catalyst.pl <myappname>
+
 =head1 DESCRIPTION
 
 This module is used by B<catalyst.pl> to create a set of scripts for a
@@ -528,10 +566,6 @@ development stage.
 The catalyst test server, starts an HTTPD which outputs debugging to
 the terminal.
 
-=head2 _deploy_dbic.pl
-
-Deploy a L<DBIx::Class> schema to the database of your choice. 
-
 =head2 _test.pl
 
 A script for running tests from the command-line.
@@ -593,7 +627,8 @@ L<Catalyst::Helper::Model::Adaptor> - wrap any class into a Catalyst model
 
 =head3 NOTE
 
-The helpers will read author name from /etc/passwd by default. + To override, please export the AUTHOR variable.
+The helpers will read author name from /etc/passwd by default.
+To override, please export the AUTHOR variable.
 
 =head1 METHODS
 
@@ -633,12 +668,13 @@ There is no fallback for this method.
 These are the methods that the Helper classes can call on the
 <$helper> object passed to them.
 
-=head2 render_file ($file, $path, $vars)
+=head2 render_file ($file, $path, $vars, $perms)
 
 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<Template Toolkit|Template>.
+Toolkit. $file is the relevant chunk of the __DATA__ section, $path is
+the path to the file, $vars is the hashref as expected by
+L<Template Toolkit|Template> and $perms are desired permissions (or system
+defaults if not set).
 
 =head2 get_file ($class, $file)
 
@@ -656,7 +692,7 @@ Create the main application skeleton. This is called by L<catalyst.pl>.
 This method is called by L<create.pl> to make new components
 for your application.
 
-=head3 mk_dir ($path)
+=head2 mk_dir ($path)
 
 Surprisingly, this function makes a directory.
 
@@ -675,21 +711,14 @@ Don't give the number or the .t suffix for the test name.
 
 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.
@@ -709,9 +738,4 @@ Catalyst Contributors, see Catalyst.pm
 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;
-