X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FCatalyst%2FHelper.pm;h=61b5a2d4d40669928fd7b37a6eb45b23a71eb4b8;hb=43f0a258422110d22afd39ca42d746a3ef4f2916;hp=21cb7e74ed82c9322f2bc66bea1dadfb641cde5b;hpb=1b2b8bdfa9e663a01bf0470180b355b4d5633b83;p=catagits%2FCatalyst-Runtime.git diff --git a/lib/Catalyst/Helper.pm b/lib/Catalyst/Helper.pm index 21cb7e7..61b5a2d 100644 --- a/lib/Catalyst/Helper.pm +++ b/lib/Catalyst/Helper.pm @@ -9,6 +9,7 @@ use IO::File; use FindBin; use Template; use Catalyst; +use Catalyst::Exception; my %cache; @@ -63,7 +64,7 @@ sub mk_app { $self->{appprefix} = lc $self->{dir}; $self->{appprefix} =~ s/-/_/g; $self->{startperl} = $Config{startperl}; - $self->{scriptgen} = $Catalyst::CATALYST_SCRIPT_GEN; + $self->{scriptgen} = $Catalyst::CATALYST_SCRIPT_GEN || 4; $self->{author} = $self->{author} = $ENV{'AUTHOR'} || eval { @{ [ getpwuid($<) ] }[6] } || 'Catalyst developer'; @@ -102,7 +103,13 @@ sub mk_component { my @args = @_; my $class = "Catalyst::Helper::$helper"; eval "require $class"; - die qq/Couldn't load helper "$class", "$@"/ if $@; + + if ( $@ ) { + Catalyst::Exception->throw( + message => qq/Couldn't load helper "$class", "$@"/ + ); + } + if ( $class->can('mk_stuff') ) { return 1 unless $class->mk_stuff( $self, @args ); } @@ -129,7 +136,7 @@ sub mk_component { my @path = split /\:\:/, $name; $file = pop @path; $path = File::Spec->catdir( $path, @path ); - mkpath $path; + mkpath [$path]; } $file = File::Spec->catfile( $path, "$file.pm" ); $self->{file} = $file; @@ -145,7 +152,13 @@ sub mk_component { $comp = 'Controller' if $type eq 'C'; my $class = "Catalyst::Helper::$comp\::$helper"; eval "require $class"; - die qq/Couldn't load helper "$class", "$@"/ if $@; + + if ( $@ ) { + Catalyst::Exception->throw( + message => qq/Couldn't load helper "$class", "$@"/ + ); + } + if ( $class->can('mk_compclass') ) { return 1 unless $class->mk_compclass( $self, @args ); } @@ -178,11 +191,14 @@ sub mk_dir { print qq/ exists "$dir"\n/; return 0; } - if ( mkpath $dir) { + if ( mkpath [$dir] ) { print qq/created "$dir"\n/; return 1; } - die qq/Couldn't create "$dir", "$!"/; + + Catalyst::Exception->throw( + message => qq/Couldn't create "$dir", "$!"/ + ); } =head3 mk_file @@ -195,14 +211,22 @@ sub mk_file { my ( $self, $file, $content ) = @_; if ( -e $file ) { print qq/ exists "$file"\n/; - return 0; + return 0 unless $self->{'.newfiles'}; + if ( my $f = IO::File->new("< $file") ) { + my $oldcontent = join('', (<$f>)); + return 0 if $content eq $oldcontent; + } + $file .= '.new'; } if ( my $f = IO::File->new("> $file") ) { print $f $content; print qq/created "$file"\n/; return 1; } - die qq/Couldn't create "$file", "$!"/; + + Catalyst::Exception->throw( + message => qq/Couldn't create "$file", "$!"/ + ); } =head3 next_test @@ -729,12 +753,14 @@ use Pod::Usage; use Catalyst::Helper; my $help = 0; +my $nonew = 0; -GetOptions( 'help|?' => \$help ); +GetOptions( 'help|?' => \$help, + 'nonew' => \$nonew ); pod2usage(1) if ( $help || !$ARGV[0] ); -my $helper = Catalyst::Helper->new; +my $helper = Catalyst::Helper->new({'.newfiles' => !$nonew}); pod2usage(1) unless $helper->mk_component( '[% name %]', @ARGV ); 1; @@ -749,6 +775,7 @@ pod2usage(1) unless $helper->mk_component( '[% name %]', @ARGV ); Options: -help display this help and exits + -nonew don't create a .new file where a file to be created exists Examples: [% appprefix %]_create.pl controller My::Controller @@ -767,6 +794,10 @@ pod2usage(1) unless $helper->mk_component( '[% name %]', @ARGV ); Create a new Catalyst Component. +Existing component files are not overwritten. If any of the component files +to be created already exist the file will be written with a '.new' suffix. +This behaviour can be supressed with the C<-nonew> option. + =head1 AUTHOR Sebastian Riedel, C @@ -811,6 +842,7 @@ sub default : Private { } =back + [% END %] =head1 AUTHOR