Jifty/Script/App.pm untested

filter: Statementstmt 0.0% 0 / 85 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 131 CC 5 SCAR 14.9
CC
5
Cov
0%
run
12.0
_install_jifty_binary
6.0
_make_directories
6.0
CRAP 4.4
1 use warnings;
2 use strict;
3  
4 package Jifty::Script::App;
5 use base qw(Jifty::Script Class::Accessor::Fast);
6  
7 use File::Copy;
8 use Jifty::Config;
9 use Jifty::YAML;
10 use File::Basename;
11  
12 __PACKAGE__->mk_accessors(qw/prefix dist_name mod_name/);
13  
14  
15 =head1 NAME
16  
17 Jifty::Script::App - Create the skeleton of a Jifty application
18  
19 =head1 SYNOPSIS
20  
21 jifty --name MyApp Creates an application
22  
23 Options:
24 --name application name
25  
26 --help brief help message
27 --man full documentation
28  
29 =head1 DESCRIPTION
30  
31 This script creates the skeleton of your application. See
32 L<Jifty::Manual::Tutorial> for more information.
33  
34 =head2 options
35  
36 =over 8
37  
38 =item B<--name>
39  
40 Required option. It is the name of the application to create.
41 Jifty will create a directory with that name, and place all of
42 the files it creates inside that directory.
43  
44 =item B<--help>
45  
46 Print a brief help message and exits.
47  
48 =item B<--man>
49  
50 Prints the manual page and exits.
51  
52 =back
53  
54 =cut
55  
56 sub options {
57 my $self = shift;
58 return (
59 $self->SUPER::options,
60 'n|name=s' => 'name',
61 )
62 }
63  
64 =head1 DESCRIPTION
65  
66 Creates a skeleton of a new Jifty application. See
67 L<Jifty::Manual::Tutorial> for an example of its use.
68  
69 =head1 METHODS
70  
71 =head2 run
72  
73 Create a directory for the application, a skeleton directory
74 structure, and a C<Makefile.PL> for you application.
75  
76 =cut
77  
78 sub run {
79 my $self = shift;
80  
81 my $name = $self->{'name'};
82 $name = '' unless defined $name;
83 $name =~ s/::/-/g;
84 $self->prefix( $name );
85  
86 $self->print_help;
87 unless ( $self->prefix =~ /\w+/ ) {
88 $self->print_help("You need to give your new Jifty app a --name");
89 }
90  
91 # Turn my-app-name into My::App::Name.
92  
93 $self->mod_name (join ("::", split (/\-/, $self->prefix)));
94 my $dist = $self->mod_name;
95 $self->dist_name($self->prefix);
96  
97 print("Creating new application ".$self->mod_name."\n");
98 $self->_make_directories();
99 $self->_install_jifty_binary();
100 $self->_write_makefile();
101 $self->_write_dotpsgi();
102 $self->_write_config();
103  
104  
105 }
106  
107 sub _install_jifty_binary {
108 my $self = shift;
109 my $prefix = $self->prefix;
110 my $basename = basename($0);
111  
112 # Copy our running copy of 'jifty' to bin/jifty
113 copy($0, "$prefix/bin/$basename");
114 # Mark it executable
115 chmod(0555, "$prefix/bin/$basename");
116  
117 # Do the same for .bat if we are on a DOSish platform
118 if (-e "$0.bat") {
119 copy("$0.bat", "$prefix/bin/$basename.bat");
120 chmod(0555, "$prefix/bin/$basename.bat");
121 }
122 }
123  
124  
125  
126 sub _write_makefile {
127 my $self = shift;
128 my $mod_name = $self->mod_name;
129 my $prefix = $self->prefix;
130 # Write a makefile
131 open(MAKEFILE, ">$prefix/Makefile.PL") or die "Can't write Makefile.PL: $!";
132 print MAKEFILE <<"EOT";
133 use inc::Module::Install;
134  
135 name '$mod_name';
136 version '0.01';
137 requires 'Jifty' => '@{[$Jifty::VERSION]}';
138  
139 WriteAll;
140 EOT
141 close MAKEFILE;
142 }
143  
144 sub _write_dotpsgi {
145 my $self = shift;
146 my $prefix = $self->prefix;
147 open(my $fh, ">$prefix/app.psgi") or die "Can't write app.psgi: $!";
148 print $fh <<"EOT";
149 use Jifty;
150 Jifty->new;
151 Jifty->handler->psgi_app;
152 EOT
153 }
154  
155 sub _make_directories {
156 my $self = shift;
157  
158 mkdir($self->prefix) or die("Can't create " . $self->prefix . ": $!");
159 my @dirs = qw( lib );
160 my @dir_parts = split('::',$self->mod_name);
161 my $lib_dir = "";
162 foreach my $part (@dir_parts) {
163 $lib_dir .= '/' if length $lib_dir;
164 $lib_dir .= $part;
165 push @dirs, "lib/$lib_dir";
166 }
167  
168 @dirs = (@dirs, $self->_directories);
169  
170 foreach my $dir (@dirs) {
171 $dir =~ s/__APP__/$lib_dir/;
172 print("Creating directory @{[$self->prefix]}/$dir\n");
173 mkdir( $self->prefix."/$dir") or die "Can't create ". $self->prefix."/$dir: $!";
174  
175 }
176 }
177 sub _directories {
178 return qw(
179 bin
180 etc
181 doc
182 log
183 var
184 var/mason
185 share
186 share/po
187 share/web
188 share/web/templates
189 share/web/static
190 lib/__APP__/Model
191 lib/__APP__/Action
192 t
193 );
194 }
195  
196  
197 sub _write_config {
198 my $self = shift;
199 my $cfg = Jifty::Config->new(load_config => 0);
200 my $default_config = $cfg->initial_config($self->dist_name);
201 my $file = join("/",$self->prefix, 'etc','config.yml');
202  
203 # Open the file ourselves so we can print a comment to it before the
204 # default YAML config
205 open my $fh, '>', $file
206 or die "Can't create configuration file '$file': $!\n";
207 binmode $fh, ':utf8';
208  
209 print("Creating configuration file $file\n");
210  
211 print $fh "# See perldoc Jifty::Config for more information about config files\n";
212 Jifty::YAML::DumpFile($fh => $default_config);
213 close $fh;
214 }
215  
216  
217 1;
218