MiniMVC/Installer.pm

filter: Statementstmt 15.6% 10 / 64 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 15.0% 3 / 20 Podpod 100% 5 / 5 totaltotal 17.8% 18 / 101 CC 9 SCAR 19.3
CC
9
Cov
13%
write_stub_files
20.0
check_empty_dir
12.0
build_dir_structure
12.0
CRAP 6.9
« MiniMVC/Dispatcher.pm ↑ Summary MiniMVC.pm »
1 package MasonX::MiniMVC::Installer;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 MasonX::MiniMVC::Installer -- Install a MiniMVC webapp
11  
12 =head1 SYNOPSIS
13  
14 minimvc-install MyApp
15  
16 =head1 DESCRIPTION
17  
18 This module shouldn't be used directly. Use the C<minimvc-install>
19 script instead.
20  
21 =head1 INTERNAL DOCUMENTATION
22  
23 =head2 install()
24  
25 Install a new stub application.
26  
27 =cut
28  
29 0 ▶sub install {
Subroutine install: not called CC=1 SCAR=6.9
Pod documented
30 0 my ($class, $app_name, @args) = @_;
31 0 check_app_name($app_name);
32 0 check_empty_dir(getcwd());
33 0 build_dir_structure($app_name);
34 0 write_stub_files($app_name);
35 }
36  
37 =head2 check_app_name()
38  
39 Checks that the application name supplied on the command line looks
40 like a Perl application name, i.e. matches \w+. Dies if it's not OK.
41  
42 =cut
43  
44 0 ▶sub check_app_name {
Subroutine check_app_name: not called CC=2 SCAR=17.9
Pod documented
45 0 my ($app_name) = @_;
46 0 ▶ die "$app_name doesn't look like a suitable application name.\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $app_name =~ /^\w+$/ 0 0 0
47 unless $app_name =~ /^\w+$/;
48 }
49  
50 =head2 check_empty_dir
51  
52 Checks that the current directory is empty before installing MiniMVC.
53 Dies if it's not.
54  
55 =cut
56  
57 0 ▶sub check_empty_dir {
Subroutine check_empty_dir: not called CC=3 SCAR=24.8
Pod documented
58 0 my ($dir) = @_;
59  
60 0 ▶ opendir DIR, $dir or die "Can't open current directory to check if it's empty: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $dir or die "Can't open current directory to check if it's empty: $!\n" 0 0 0
61 0 my @files = grep !/^\.+$/, readdir(DIR);
62 0 closedir DIR;
63  
64 0 ▶ if (@files) {
Branch: 1 branch
BranchTrueFalseTotal
if (@files) 0 0 0
65 0 die "Directory isn't empty. Remove files and try again.\n";
66 }
67 }
68  
69 =head2 build_dir_structure()
70  
71 Builds the directory structure for the application, i.e. lib/, t/, etc.
72  
73 =cut
74  
75 0 ▶sub build_dir_structure {
Subroutine build_dir_structure: not called CC=3 SCAR=24.8
Pod documented
76 0 my ($app_name) = @_;
77  
78 0 print "Creating directory structure...\n";
79 0 foreach my $dir (
80 "lib/",
81 "lib/$app_name/",
82 "lib/$app_name/Controller/",
83 "lib/$app_name/Model/",
84 "t/",
85 "view/",
86 "view/sample/",
87 ) {
88 0 ▶ if (mkdir $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (mkdir $dir) { } 0 0 0
89 0 print " $dir\n";
90 } else {
91 0 warn "Couldn't make directory $dir: $!";
92 }
93 }
94 }
95  
96 =head2 write_stub_files()
97  
98 Writes stub files for the application, i.e. autohandler, dhandler,
99 sample controllers, etc.
100  
101 =cut
102  
103 0 ▶sub write_stub_files {
Subroutine write_stub_files: not called CC=4 SCAR=30.0
Pod documented
104 0 my ($app_name) = @_;
105  
106 0 print "Creating stub/sample files...\n";
107  
108 0 foreach my $file (
109 {
110 name => "dhandler",
111 creator => \&_create_dhandler,
112 },
113 {
114 name => "autohandler",
115 creator => \&_create_autohandler,
116 },
117 {
118 name => "index.mhtml",
119 creator => \&_create_index,
120 },
121 {
122 name => "lib/$app_name/Dispatcher.pm",
123 creator => \&_create_dispatcher,
124 },
125 {
126 name => "lib/$app_name/Controller/Sample.pm",
127 creator => \&_create_controller_sample,
128 },
129 {
130 name => "lib/$app_name/Model/Sample.pm",
131 creator => \&_create_model_sample,
132 },
133 {
134 name => "t/controller_sample.t",
135 creator => \&_create_controller_sample_test,
136 },
137 {
138 name => "t/model_sample.t",
139 creator => \&_create_model_sample_test,
140 },
141 {
142 name => "view/default.mhtml",
143 creator => \&_create_view_default,
144 },
145 {
146 name => "view/sample/default.mhtml",
147 creator => \&_create_view_sample_default,
148 },
149 {
150 name => ".htaccess",
151 creator => \&_htaccess,
152 },
153 {
154 name => "view/.htaccess",
155 creator => \&_htaccess_deny,
156 },
157 {
158 name => "lib/.htaccess",
159 creator => \&_htaccess_deny,
160 },
161 {
162 name => "t/.htaccess",
163 creator => \&_htaccess_deny,
164 },
165 ) {
166  
167 0 my $sub = $file->{creator};
168 0 my $content = &$sub($app_name);
169 0 ▶ if ($content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content) { } 0 0 0
170 0 ▶ if (open my $fh, '>', $file->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (open my $fh, '>', $file->{'name'}) { } 0 0 0
171 0 print $fh $content;
172 0 close $fh;
173 0 print " $file->{name}\n";
174 } else {
175 0 warn "Couldn't open $file->{name} to write: $!\n";
176 }
177 } else {
178 0 warn "Couldn't get content of file $file->{name}\n";
179 }
180 }
181 }
182  
183 0 ▶sub _create_dhandler {
Subroutine _create_dhandler: not called CC=1 SCAR=6.9
184 0 my ($app_name) = @_;
185 0 return << "EOF";
186 <\%init>
187 use ${app_name}::Dispatcher;
188 my \$dispatcher = ${app_name}::Dispatcher->new();
189 \$dispatcher->dispatch(\$m);
190 </\%init>
191 EOF
192  
193 }
194  
195 0 ▶sub _create_autohandler {
Subroutine _create_autohandler: not called CC=1 SCAR=6.9
196 0 my ($app_name) = @_;
197 0 return <<"EOF";
198 <html>
199 <head>
200 <title><% \$m->notes("title") || "$app_name" %></title>
201 </head>
202 <body>
203  
204 <% \$content %>
205  
206 </body>
207 </html>
208  
209 <\%init>
210 # Before we generate our autohandler HTML, we locate the next component
211 # (in our case the MiniMVC dhandler) and call it, capturing its output.
212 # This means that notes() can be set before the autohandler generates a
213 # title and other component-dependent output.
214 my \$next = \$m->fetch_next;
215 my \$content = \$m->scomp(\$next);
216 </\%init>
217 EOF
218  
219 }
220  
221 0 ▶sub _create_index {
Subroutine _create_index: not called CC=1 SCAR=6.9
222 0 my ($app_name) = @_;
223 0 return <<"EOF";
224 <& view/default.mhtml &>
225 EOF
226  
227 }
228  
229 0 ▶sub _create_dispatcher {
Subroutine _create_dispatcher: not called CC=1 SCAR=6.9
230 0 my ($app_name) = @_;
231 0 return <<"EOF";
232 package ${app_name}::Dispatcher;
233  
234 use base MasonX::MiniMVC::Dispatcher;
235  
236 sub new {
237 my (\$class) = \@_;
238 my \$self = \$class->SUPER::new({
239 'sample' => '${app_name}::Controller::Sample',
240 });
241 }
242  
243 1;
244 EOF
245  
246 }
247  
248 0 ▶sub _create_controller_sample {
Subroutine _create_controller_sample: not called CC=1 SCAR=6.9
249 0 my ($app_name) = @_;
250 0 return <<"EOF";
251 package ${app_name}::Controller::Sample;
252  
253 use strict;
254 use warnings;
255  
256 =head1 NAME
257  
258 ${app_name}::Controller::Sample -- Sample MiniMVC controller
259  
260 =head1 DESCRIPTION
261  
262 This controller handles the sample/ part of the website.
263  
264 =head1 METHODS
265  
266 =head2 default()
267  
268 This is called when someone goes to http://example.com/sample/
269  
270 =cut
271  
272 sub default {
273 my (\$self, \$m, \@args) = \@_;
274 \$m->notes("title", "$app_name: Sample Controller");
275 \$m->comp("view/sample/default.mhtml");
276 }
277  
278 1;
279 EOF
280  
281 }
282  
283 0 ▶sub _create_model_sample {
Subroutine _create_model_sample: not called CC=1 SCAR=6.9
284 0 my ($app_name) = @_;
285 0 return <<"EOF";
286 package ${app_name}::Model::Sample;
287  
288 use strict;
289 use warnings;
290  
291 =head1 NAME
292  
293 ${app_name}::Model::Sample -- Sample MiniMVC model
294  
295 =head1 DESCRIPTION
296  
297 This is a stub model class for your MiniMVC application. You could use
298 DBIx::Class, Class::DBI, or some other method to represent your data
299 here in an OO way.
300  
301 You would then use ${app_name}::Model::Whatever from within your
302 Controller classes to access data.
303  
304 =cut
305  
306 1;
307 EOF
308  
309 }
310  
311 0 ▶sub _create_controller_sample_test {
Subroutine _create_controller_sample_test: not called CC=1 SCAR=6.9
312 0 my ($app_name) = @_;
313 0 return <<"EOF";
314 use strict;
315 use warnings;
316  
317 use Test::More qw(no_plan);
318  
319 use_ok('${app_name}::Controller::Sample');
320 can_ok('${app_name}::Controller::Sample', 'default');
321 EOF
322 }
323  
324 0 ▶sub _create_model_sample_test {
Subroutine _create_model_sample_test: not called CC=1 SCAR=6.9
325 0 my ($app_name) = @_;
326 0 return <<"EOF";
327 use strict;
328 use warnings;
329  
330 use Test::More qw(no_plan);
331  
332 use_ok('${app_name}::Model::Sample');
333 EOF
334 }
335  
336 0 ▶sub _create_view_default {
Subroutine _create_view_default: not called CC=1 SCAR=6.9
337 0 my ($app_name) = @_;
338 0 return <<"EOF";
339 <h1>MiniMVC Installed</h1>
340  
341 <p>
342 Installed application <code>$app_name</code>.
343 </p>
344 <p>
345 <a href="sample/">View sample controller-generated page</a>.
346 </p>
347 <p>
348 To change the content of your front page, edit
349 <code>view/default.mhtml</code>.
350 </p>
351 EOF
352 }
353  
354 0 ▶sub _create_view_sample_default {
Subroutine _create_view_sample_default: not called CC=1 SCAR=6.9
355 0 my ($app_name) = @_;
356 0 return <<"EOF";
357 <h1>Sample View</h1>
358  
359 This view is called by
360 <code>${app_name}::Controller::Sample::default()</code>.
361  
362 If you wanted to pass data, you'd do it via Mason's &lt;\%args&gt;
363 section.
364  
365 <\%args>
366 \$foo => undef;
367 \@bar => undef;
368 </\%args>
369 EOF
370 }
371  
372 0 ▶sub _htaccess {
Subroutine _htaccess: not called CC=1 SCAR=6.9
373 0 return <<"EOF";
374 SetHandler mason-handler
375  
376 DirectoryIndex index.mhtml
377  
378 <Files autohandler>
379 Order deny,allow
380 Deny from all
381 </Files>
382  
383 <Files dhandler>
384 Order deny,allow
385 Deny from all
386 </Files>
387 EOF
388 }
389  
390 0 ▶sub _htaccess_deny {
Subroutine _htaccess_deny: not called CC=1 SCAR=6.9
391 0 my ($app_name) = @_;
392 0 return <<"EOF";
393 Order deny,allow
394 Deny from all
395 EOF
396 }
397  
398 1 1;
« MiniMVC/Dispatcher.pm ↑ Summary MiniMVC.pm »