script/eidolon.pl untested

filter: Statementstmt 0.0% 0 / 91 Branchbran 0.0% 0 / 32 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 7 Podpod 0.0% 0 / 4 totaltotal 0.0% 0 / 146 CC 17 SCAR 38.3
CC
17
Cov
0%
main
72.0
create_file
56.0
create_directory
20.0
CRAP 45.9
1 #!/usr/local/bin/perl
2 # ==============================================================================
3 #
4 # Eidolon
5 # Copyright (c) 2009, Atma 7
6 # ---
7 # eidolon.pl - application code generator
8 #
9 # ==============================================================================
10  
11 use Getopt::Long;
12 use warnings;
13 use strict;
14  
15 &main;
16  
17 # ------------------------------------------------------------------------------
18 # print_usage()
19 # print usage information
20 # ------------------------------------------------------------------------------
21 sub print_usage
22 {
23 print << "USAGE";
24 Usage: eidolon.pl [options] <application_name>
25  
26 Generates code for application <application_name> with given options.
27  
28 Options:
29 -v --verbose verbose output
30 -h --help print this help
31  
32 USAGE
33 }
34  
35 # ------------------------------------------------------------------------------
36 # create_directory(\%app, \%options, $dir)
37 # create directory
38 # ------------------------------------------------------------------------------
39 sub create_directory
40 {
41 my ($app, $options, $dir, $len, $status);
42  
43 ($app, $options, $dir) = @_;
44  
45 # delete starting "dir:"
46 substr($dir, 0, 4) = "";
47 $dir =~ s/\[\$app_name\]/$app->{"name"}/g;
48  
49 $status = mkdir($dir);
50  
51 if ($options->{"verbose"})
52 {
53 $len = length($app->{"name"}) + 45;
54  
55 printf(" > %-".$len."s", $dir);
56 print $status ? "OK" : "FAIL";
57 print "\n";
58 }
59  
60 shift @{ $app->{"skel"} };
61 $app->{"fail"} = 1 unless $status;
62 }
63  
64 # ------------------------------------------------------------------------------
65 # create_file(\%app, \%options, $file)
66 # create file
67 # ------------------------------------------------------------------------------
68 sub create_file
69 {
70 my ($app, $options, $file, $content, $time, $type, $hex, $len, $status);
71  
72 ($app, $options, $file) = @_;
73  
74 # delete starting "file:"
75 substr($file, 0, 5) = "";
76 $hex = 0;
77  
78 # is it a hex-encoded file?
79 if ($file =~ /^hex:/)
80 {
81 substr($file, 0, 4) = "";
82 $hex = 1;
83 }
84  
85 $file =~ s/\[\$app_name\]/$app->{"name"}/g;
86  
87 if ($status = open FILE, ">$file")
88 {
89 $time = localtime;
90 $content = shift @{ $app->{"skel"} };
91 $content =~ s/\n$//;
92  
93 if ($hex)
94 {
95 # hex-encoded binary file
96 binmode FILE;
97 $content =~ s/\n//g;
98 $len = 0;
99  
100 while ($len < length($content))
101 {
102 print FILE pack("H2", substr($content, $len, 2));
103 $len += 2;
104 }
105 }
106 else
107 {
108 # text file
109 $content =~ s/\[\$app_name\]/$app->{"name"}/g;
110 $content =~ s/\[\$time\]/$time/g;
111 print FILE $content;
112 }
113  
114 close FILE;
115 }
116  
117 if ($options->{"verbose"})
118 {
119 $len = length($app->{"name"}) + 45;
120  
121 printf(" > %-".$len."s", $file);
122 print $status ? "OK" : "FAIL";
123 print "\n";
124 }
125  
126 $app->{"fail"} = 1 unless $status;
127 }
128  
129 # ------------------------------------------------------------------------------
130 # main()
131 # main function
132 # ------------------------------------------------------------------------------
133 sub main
134 {
135 my (%app, %options, $data_section, $state, $part, $answer);
136  
137 # get command line options
138 Getopt::Long::Configure("bundling");
139  
140 GetOptions
141 (
142 \%options,
143 "verbose|v",
144 "help|h"
145 );
146  
147 $app{"name"} = shift @ARGV || "";
148 $app{"fail"} = 0;
149  
150 # check for mandatory options
151 if ($options{"help"} || !$app{"name"} || $app{"name"} =~ /[^\w\d]+/)
152 {
153 print_usage;
154 exit 0;
155 }
156  
157 # proceed
158 print "Creating application \"$app{'name'}\"\n";
159  
160 {
161 # read entire data section
162 local $/;
163 $data_section = <DATA>;
164 }
165  
166 # get files & directories structure
167 $app{"skel"} = [ split /__([\w\d:\[\]\$\/\-\.]+|\_{1})__\n/, $data_section ];
168 $state = 0;
169  
170 # main cycle
171 while (scalar @{ $app{"skel"} })
172 {
173 $part = shift @{ $app{"skel"} };
174  
175 next if $part =~ /^\n$/;
176
177 # directory section
178 if ($part =~ /^dir:/)
179 {
180 if ($state == 0)
181 {
182 print "Creating directories...\n";
183 $state = 1;
184 }
185  
186 create_directory(\%app, \%options, $part);
187 next;
188 }
189  
190 # file section
191 if ($part =~ /^file:/)
192 {
193 if ($state == 1)
194 {
195 print "Creating files...\n";
196 $state = 2;
197 }
198  
199 create_file(\%app, \%options, $part);
200 next;
201 }
202  
203 $app{"fail"} = 1;
204 last;
205 }
206  
207 print "\n";
208 print $app{"fail"} ? "Failed creating application" : "Application successfully created";
209 print ".\n\n";
210 }
211  
212 =pod
213  
214 =head1 NAME
215  
216 eidolon.pl - Eidolon application code generator.
217  
218 =head1 SYNOPSIS
219  
220 eidolon.pl [options] <application_name>
221  
222 Options:
223  
224 -v --verbose verbose output
225 -h --help print this help
226  
227 Examples:
228  
229 eidolon.pl -v Example
230 eidolon.pl AnotherExample
231  
232 =head1 DESCRIPTION
233  
234 The I<eidolon.pl> code generator will help you to create the application skeleton, so
235 you won't need to make applications from scratch. It creates a simple example
236 application with one default controller and a basic configuration with
237 L<Eidolon::Driver::Router::Basic> as a router driver, so you need this package to
238 be installed.
239  
240 The application name must consist of one or more alphanumeric characters.
241  
242 Using the example application name I<Example> the application directory will
243 contain the following items:
244  
245 =over 4
246  
247 =item bin/
248  
249 A directory with executable contents. All your application scripts are placed
250 here. Contents of this directory should be moved into C<cgi-bin> directory on
251 your web server.
252  
253 =over 4
254  
255 =item index.cgi
256  
257 CGI gateway of the application. All user requests will come through this file.
258  
259 =item index.fcgi
260  
261 FastCGI gateway of the application. You need to install the L<FCGI> module to
262 use this type of gateway.
263  
264 =item lib/
265  
266 A directory containing all application modules. If your application will use
267 other modules, place them here too.
268  
269 =over 4
270  
271 =item Example.pm
272  
273 A main module of the application. Contains almost nothing - some subclassing
274 stuff and application version definition.
275  
276 =item Example/
277  
278 A directory with application configuration and other stuff used to interact with
279 I<Eidolon>.
280  
281 =over 4
282  
283 =item Config.pm
284  
285 Application configuration. Generated configuration contains only basic settings,
286 so you should edit this file first and specify application settings manually.
287  
288 =item Controller/
289  
290 A directory containing application controllers.
291  
292 =over 4
293  
294 =item Default.pm
295  
296 A default controller of the application.
297  
298 =back
299  
300 =back
301  
302 =back
303  
304 =back
305  
306 =item static/
307  
308 A directory containing static data - images, stylesheets and javascript.
309 Contents of this directory should be moved into C<htdocs> (C<httpdocs>,
310 C<www> or C<wwwroot>) directory of your web server.
311  
312 =over 4
313  
314 =item config/
315  
316 Web-server configuration files.
317  
318 =over 4
319  
320 =item apache_cgi
321  
322 I<Apache> configuration file for CGI application. Contains I<mod_rewrite> rules
323 for the application. Rename this file to C<.htaccess> while deploying the
324 application to web server.
325  
326 =item apache_fcgi
327  
328 I<Apache> configuration file for FastCGI application. Rename this file to
329 C<.htaccess> while deploying the application to web server.
330  
331 =back
332  
333 =item img/
334  
335 A directory containing images for the example application.
336  
337 =over 4
338  
339 =item flag.png
340  
341 Nice flag image :)
342  
343 =item logo.png
344  
345 I<Eidolon> logo.
346  
347 =back
348  
349 =back
350  
351 =back
352  
353 =head1 NOTES
354  
355 The application generated using this script will work only on I<Apache> web
356 server with I<mod_rewrite> module installed. Support of other servers will
357 be added in future versions of this script.
358  
359 =head1 DEPLOYMENT
360  
361 To deploy your application perform the following actions:
362  
363 1. Copy one of the files in C<bin> directory into the C<cgi-bin> directory of
364 your web server. For CGI application, you should copy a C<index.cgi>, for
365 FastCGI application - copy a C<index.fcgi> file.
366  
367 2. Set C<rwxr-xr-x> (755) permissions on file that you copied at first step
368 (i.e. C<index.cgi> or C<index.fcgi>).
369  
370 3. Copy the C<lib> directory into the C<cgi-bin> directory of your web server.
371 Actually, you can place this directory at your wish - in this case you will
372 need to fix a path to this directory in a file that you copied at first step
373 (C<index.cgi> or C<index.fcgi>, line 11) and to specify a new path in your
374 application configuration.
375  
376 4. Copy one of the C<config> directory files into the C<htdocs>
377 (C<httpdocs>, C<www> or C<wwwroot>) directory of your web server. Choose a file
378 to copy according to file you copied at first step - if you want a CGI
379 application - you will need to copy C<apache_cgi> and if you want a FastCGI
380 application - use C<apache_fcgi> instead. After copying, rename this file to
381 C<.htaccess>.
382  
383 5. Copy the entire C<img> directory into the C<htdocs> (C<httpdocs>,
384 C<www> or C<wwwroot>) directory of you web server.
385  
386 =head1 SEE ALSO
387  
388 L<Eidolon>, L<Eidolon::Application>
389  
390 L<http://www.atma7.com/en/products/eidolon/> - official project web page.
391  
392 =head1 LICENSE
393  
394 This library is free software; you can redistribute it and/or modify it under
395 the same terms as Perl itself.
396  
397 =head1 AUTHOR
398  
399 Anton Belousov, E<lt>abel@cpan.orgE<gt>
400  
401 =head1 COPYRIGHT
402  
403 Copyright (c) 2009, Atma 7, L<http://www.atma7.com>
404  
405 =cut
406  
407 __DATA__