moved this stuff to the right place
[catagits/Catalyst-Devel.git] / lib / Catalyst / Helper / AppGen.pm
1 package Catalyst::Helper::AppGen;
2
3 use Moose;
4 use namespace::autoclean;
5 use Moose::Util::TypeConstraints;
6 use namespace::autoclean;
7
8 extends { 'Catalyst::Helper' };
9
10 has name => ( 
11     is => 'ro', 
12     isa => Str,
13     traits => [qw(Getopt)],
14     cmd_aliases => 'n',
15 );
16
17 has dir  => ( 
18     is => 'ro', 
19     isa => Str,
20     traits => [qw(Getopt)],
21     cmd_aliases => 'n', 
22 ); 
23
24 sub mk_app {
25     my ( $self, $name ) = @_;
26
27     # Needs to be here for PAR
28     require Catalyst;
29
30     if ( $name =~ /[^\w:]/ || $name =~ /^\d/ || $name =~ /\b:\b|:{3,}/) {
31         warn "Error: Invalid application name.\n";
32         return 0;
33     }
34     $self->{name            } = $name;
35     $self->{dir             } = $name;
36     $self->{dir             } =~ s/\:\:/-/g;
37     $self->{script          } = File::Spec->catdir( $self->{dir}, 'script' );
38     $self->{appprefix       } = Catalyst::Utils::appprefix($name);
39     $self->{appenv          } = Catalyst::Utils::class2env($name);
40     $self->{startperl       } = -r '/usr/bin/env'
41                                 ? '#!/usr/bin/env perl'
42                                 : "#!$Config{perlpath} -w";
43     $self->{scriptgen       } = $Catalyst::Devel::CATALYST_SCRIPT_GEN || 4;
44     $self->{catalyst_version} = $Catalyst::VERSION;
45     $self->{author          } = $self->{author} = $ENV{'AUTHOR'}
46       || eval { @{ [ getpwuid($<) ] }[6] }
47       || 'Catalyst developer';
48
49     my $gen_scripts  = ( $self->{makefile} ) ? 0 : 1;
50     my $gen_makefile = ( $self->{scripts} )  ? 0 : 1;
51     my $gen_app = ( $self->{scripts} || $self->{makefile} ) ? 0 : 1;
52
53     if ($gen_app) {
54         $self->_mk_dirs;
55         $self->_mk_config;
56         $self->_mk_appclass;
57         $self->_mk_rootclass;
58         $self->_mk_readme;
59         $self->_mk_changes;
60         $self->_mk_apptest;
61         $self->_mk_images;
62         $self->_mk_favicon;
63     }
64     if ($gen_makefile) {
65         $self->_mk_makefile;
66     }
67     if ($gen_scripts) {
68         $self->_mk_cgi;
69         $self->_mk_fastcgi;
70         $self->_mk_server;
71         $self->_mk_test;
72         $self->_mk_create;
73         $self->_mk_information;
74     }
75     return $self->{dir};
76 }
77
78 sub mk_component {
79     my $self = shift;
80     my $app  = shift;
81     $self->{app} = $app;
82     $self->{author} = $self->{author} = $ENV{'AUTHOR'}
83       || eval { @{ [ getpwuid($<) ] }[6] }
84       || 'A clever guy';
85     $self->{base} ||= File::Spec->catdir( $FindBin::Bin, '..' );
86     unless ( $_[0] =~ /^(?:model|view|controller)$/i ) {
87         my $helper = shift;
88         my @args   = @_;
89         my $class  = "Catalyst::Helper::$helper";
90         eval "require $class";
91
92         if ($@) {
93             Catalyst::Exception->throw(
94                 message => qq/Couldn't load helper "$class", "$@"/ );
95         }
96
97         if ( $class->can('mk_stuff') ) {
98             return 1 unless $class->mk_stuff( $self, @args );
99         }
100     }
101     else {
102         my $type   = shift;
103         my $name   = shift || "Missing name for model/view/controller";
104         my $helper = shift;
105         my @args   = @_;
106        return 0 if $name =~ /[^\w\:]/;
107         $type              = lc $type;
108         $self->{long_type} = ucfirst $type;
109         $type              = 'M' if $type =~ /model/i;
110         $type              = 'V' if $type =~ /view/i;
111         $type              = 'C' if $type =~ /controller/i;
112         my $appdir = File::Spec->catdir( split /\:\:/, $app );
113         my $test_path =
114           File::Spec->catdir( $FindBin::Bin, '..', 'lib', $appdir, 'C' );
115         $type = $self->{long_type} unless -d $test_path;
116         $self->{type}  = $type;
117         $self->{name}  = $name;
118         $self->{class} = "$app\::$type\::$name";
119
120         # Class
121         my $path =
122           File::Spec->catdir( $FindBin::Bin, '..', 'lib', $appdir, $type );
123         my $file = $name;
124         if ( $name =~ /\:/ ) {
125             my @path = split /\:\:/, $name;
126             $file = pop @path;
127             $path = File::Spec->catdir( $path, @path );
128         }
129         $self->mk_dir($path);
130         $file = File::Spec->catfile( $path, "$file.pm" );
131         $self->{file} = $file;
132
133         # Fallback
134         else {
135             return 1 unless $self->_mk_compclass;
136             $self->_mk_comptest;
137         }
138     }
139     return 1;
140 }
141
142 1;