added a few comments so I can get cracking when I'm more lucid in the morning
[catagits/Catalyst-Devel.git] / lib / Catalyst / Helper / AppGen.pm
CommitLineData
143cd997 1package Catalyst::Helper::AppGen;
2
3use Moose;
4use namespace::autoclean;
5use Moose::Util::TypeConstraints;
d7fbf00d 6use MooseX::Types -declare [qw/ ValidAppName ValidAppComponent Dir AppEnv/];
143cd997 7use namespace::autoclean;
8
9extends { 'Catalyst::Helper' };
10
8dccaa92 11my $appname_re = qr/[\w:]+/;
12my $regex = qr/$appname_re::(M|V|C|Model|View|Controller)::.*/;
13
14subtype ValidAppName,
15 as Str,
16 where { /^$appname_re$/ && ! /$regex/ };
17
18subtype ValidAppComponent,
19 as Str,
20 where { /^$regex$/ };
21
22subtype Dir,
23 as Str,
24 where { s/\:\:/-/g };
25
26subtype AppEnv,
27 as Str,
28 where { /\w/ };
29
30coerce ValidAppName,
31 from ValidAppComponent,
32 via { Catalyst::Utils::class2appclass($_); },
33
34coerce AppEnv,
35 from Str,
36 via { Catalyst::Utils::class2env($_) };
37
143cd997 38has name => (
39 is => 'ro',
d7fbf00d 40 isa => ValidAppName,
143cd997 41 traits => [qw(Getopt)],
42 cmd_aliases => 'n',
43);
44
45has dir => (
46 is => 'ro',
d7fbf00d 47 isa => Dir,
143cd997 48 traits => [qw(Getopt)],
8dccaa92 49 cmd_aliases => 'dir',
50
143cd997 51);
52
8dccaa92 53has script => (
54 is => 'ro',
55 isa => Str,
56 traits => [qw(NoGetopt)],
57);
58
59has app_prefix => (
60 is => 'ro',
61 isa => Str,
62 traits => [qw(NoGetopt)],
63);
64
65has app_env => (
66 is => 'ro',
d7fbf00d 67 isa => ValidAppEnv,
8dccaa92 68 traits => [qw(NoGetopt)],
69);
70
71
72
143cd997 73sub mk_app {
74 my ( $self, $name ) = @_;
75
76 # Needs to be here for PAR
77 require Catalyst;
78
79 if ( $name =~ /[^\w:]/ || $name =~ /^\d/ || $name =~ /\b:\b|:{3,}/) {
80 warn "Error: Invalid application name.\n";
81 return 0;
82 }
83 $self->{name } = $name;
84 $self->{dir } = $name;
85 $self->{dir } =~ s/\:\:/-/g;
86 $self->{script } = File::Spec->catdir( $self->{dir}, 'script' );
87 $self->{appprefix } = Catalyst::Utils::appprefix($name);
88 $self->{appenv } = Catalyst::Utils::class2env($name);
89 $self->{startperl } = -r '/usr/bin/env'
90 ? '#!/usr/bin/env perl'
91 : "#!$Config{perlpath} -w";
92 $self->{scriptgen } = $Catalyst::Devel::CATALYST_SCRIPT_GEN || 4;
93 $self->{catalyst_version} = $Catalyst::VERSION;
94 $self->{author } = $self->{author} = $ENV{'AUTHOR'}
95 || eval { @{ [ getpwuid($<) ] }[6] }
96 || 'Catalyst developer';
97
98 my $gen_scripts = ( $self->{makefile} ) ? 0 : 1;
99 my $gen_makefile = ( $self->{scripts} ) ? 0 : 1;
100 my $gen_app = ( $self->{scripts} || $self->{makefile} ) ? 0 : 1;
101
39a0e31c 102 # these die, horribly, today.
143cd997 103 if ($gen_app) {
104 $self->_mk_dirs;
105 $self->_mk_config;
106 $self->_mk_appclass;
107 $self->_mk_rootclass;
108 $self->_mk_readme;
109 $self->_mk_changes;
110 $self->_mk_apptest;
111 $self->_mk_images;
112 $self->_mk_favicon;
113 }
39a0e31c 114
115 # and this
143cd997 116 if ($gen_makefile) {
117 $self->_mk_makefile;
118 }
39a0e31c 119
120 # and this
143cd997 121 if ($gen_scripts) {
122 $self->_mk_cgi;
123 $self->_mk_fastcgi;
124 $self->_mk_server;
125 $self->_mk_test;
126 $self->_mk_create;
127 $self->_mk_information;
128 }
129 return $self->{dir};
130}
131
8dccaa92 132
143cd997 133
1341;