File Coverage

blib/lib/Enbld.pm
Criterion Covered Total %
statement 21 193 10.8
branch 0 74 0.0
condition 0 3 0.0
subroutine 7 37 18.9
pod 4 22 18.1
total 32 329 9.7


line stmt bran cond sub pod time code
1             package Enbld;
2              
3 1     1   22194 use strict;
  1         2  
  1         44  
4 1     1   6 use warnings;
  1         2  
  1         30  
5              
6 1     1   5 use Carp;
  1         15  
  1         91  
7              
8 1     1   23 use 5.010001;
  1         12  
  1         47  
9              
10             our $VERSION = '0.7039';
11              
12 1     1   896 use FindBin qw/$Script/;
  1         1140  
  1         124  
13 1     1   1302 use Getopt::Long;
  1         20218  
  1         10  
14 1     1   1046 use Try::Lite;
  1         1772  
  1         2714  
15              
16             require Exporter;
17             our @ISA = qw(Exporter);
18             our @EXPORT = qw/
19             enbld
20             build
21             target
22             define
23             version
24             make_test
25             module_file
26             modules
27             arguments
28             annotation
29             conf
30             load
31             copy
32             set
33             from
34             to
35             content
36             /;
37              
38             require Enbld::App::Configuration;
39             require Enbld::Logger;
40             require Enbld::Target;
41             require Enbld::Condition;
42             require Enbld::Error;
43             require Enbld::Exception;
44             require Enbld::RcFile;
45             require Enbld::Deployed;
46              
47             our $initialized;
48             our %target_result;
49             our %rcfile_result;
50              
51             our %rcfile_collection;
52             our %target_collection;
53              
54             sub enbld($$) {
55 0     0 0   my ( $envname, $coderef ) = @_;
56              
57 0 0         if ( ref( $envname ) ) {
58 0           _err(
59             "Function 'enbld' first requires " .
60             "string type parameter 'env name'."
61             );
62             }
63              
64 0 0         if ( $envname =~ /[^0-9a-zA-Z_]/ ) {
65 0           _err(
66             "Env name '$envname' contains invalid character.",
67             $envname
68             );
69             }
70              
71 0 0         if ( ref( $coderef ) ne 'CODE' ) {
72 0           _err( "Function 'enbld' second requires code reference parameter." );
73             }
74              
75 0           require Enbld::Message;
76 0           Enbld::Message->set_verbose;
77              
78 0           parse_option();
79              
80 0           require Enbld::Home;
81 0           Enbld::Home->initialize;
82              
83 0           Enbld::App::Configuration->read_file;
84 0           Enbld::App::Configuration->set_envname( $_[0] );
85              
86 0           $initialized++;
87              
88 0           $_[1]->();
89              
90 0           Enbld->_setup_directory;
91              
92 0           foreach my $name ( sort keys %target_collection ) {
93 0           build_target( $name );
94             }
95              
96 0           foreach my $filepath ( sort keys %rcfile_collection ) {
97 0           do_rcfile( $rcfile_collection{$filepath} );
98             }
99              
100 0           undef $initialized;
101              
102 0           show_result_message();
103              
104 0           check_targets_in_DSL();
105              
106 0 0         if ( ! Enbld::App::Configuration->is_dirty ) {
107 0           Enbld::Message->notify(
108             "INFO:No builded targets & loaded configuration file."
109             );
110             }
111              
112 0           return 1;
113             }
114              
115             sub build_target {
116 0     0 0   my $name = shift;
117              
118 0 0         my $config = Enbld::Feature->is_deploy_mode ? undef :
119             Enbld::App::Configuration->search_config( $name );
120              
121 0           my $target = Enbld::Target->new( $name, $config );
122              
123             my $installed = try {
124 0 0   0     return Enbld::Feature->is_deploy_mode ?
125             $target->deploy_declared( \%target_collection ) :
126             $target->install_declared( \%target_collection );
127             } ( 'Enbld::Error' => sub {
128 0     0     Enbld::Message->alert( $@ );
129              
130 0 0         if ( $^O ne 'darwin' ) {
131 0           say "If you run Enbld at Linux or BSD, there is a possibility " .
132             "that the Software which depends is not installed.";
133             }
134              
135 0           say "\n" . "Please check build logile:" . Enbld::Logger->logfile;
136              
137 0           $target_result{$name} = $name . ' is failure to build.';
138              
139 0           return;
140             }
141 0           );
142              
143             # Target is installed.
144 0 0         if ( $installed ) {
145 0           $target_result{$name} = $name . ' ' . $installed->enabled .
146             " is installed.";
147              
148 0           Enbld::App::Configuration->set_config( $installed );
149 0           Enbld::App::Configuration->write_file;
150              
151 0 0         if ( Enbld::Feature->is_deploy_mode ) {
152 0           Enbld::Deployed->add( $installed );
153             }
154              
155 0           return $name;
156             }
157              
158             # Target is up-to-date.
159 0           $target_result{$name} = $name . ' is up-to-date.';
160              
161 0           return $name;
162             }
163              
164             sub check_targets_in_DSL {
165              
166 0 0   0 0   return if Enbld::Feature->is_deploy_mode;
167              
168 0           my %not_in_dsl;
169 0           foreach my $name ( keys %{ Enbld::App::Configuration->config } ) {
  0            
170 0           my $config = Enbld::App::Configuration->search_config( $name );
171              
172 0 0         $not_in_dsl{$name}++ unless defined $target_result{$name};
173             }
174              
175 0 0         if ( keys %not_in_dsl ) {
176 0           Enbld::Message->notify(
177             "WARN:The following targets are not defined in DSL $Script.\n" .
178             "Please check $Script."
179             );
180              
181 0           foreach my $target ( sort keys %not_in_dsl ) {
182 0           Enbld::Message->notify( " " . $target );
183             }
184             }
185             }
186              
187             sub show_result_message {
188              
189 0     0 0   foreach my $target ( sort keys %target_result ) {
190 0           Enbld::Message->notify( $target_result{$target} );
191             }
192              
193 0           foreach my $file ( sort keys %rcfile_result ) {
194 0           Enbld::Message->notify( $rcfile_result{$file} );
195             }
196             }
197              
198             sub build(&) {
199 0     0 0   return $_[0];
200             }
201              
202             our $condition_ref;
203              
204             sub target($$) {
205 0     0 1   my ( $targetname, $coderef ) = @_;
206              
207 0 0         if ( ! $initialized ) {
208 0           _err(
209             "Environment is not initialized.".
210             "Isn't the syntax of DSL possibly mistaken?"
211             );
212             }
213              
214 0 0         if ( ref( $targetname ) ) {
215 0           _err(
216             "Function 'target' first requsres " .
217             "string type parameter 'target name'."
218             );
219             }
220              
221 0 0         if( $targetname =~ /[^0-9a-z]/ ) {
222 0           _err( "Target name '$targetname' contains invalid character." );
223             }
224              
225 0 0         if ( ref( $coderef ) ne 'CODE' ) {
226 0           _err( "Function 'target' seconde requires code reference parameter." );
227             }
228              
229             $condition_ref = {
230 0           name => $targetname,
231             };
232              
233 0           $coderef->();
234              
235 0           my $condition = Enbld::Condition->new( %{ $condition_ref } );
  0            
236 0           $target_collection{$targetname} = $condition;
237              
238 0           undef $condition_ref;
239             }
240              
241             sub define(&) {
242 0     0 0   my $coderef = shift;
243              
244 0           return $coderef;
245             }
246              
247             sub version($) {
248 0     0 1   my $version = shift;
249              
250 0 0         if ( ref( $version ) ) {
251 0           _err( "Function 'version' requires string type parameter." );
252             }
253              
254 0           $condition_ref->{version} = $version;
255             }
256              
257             sub make_test(;$) {
258 0     0 0   my $make_test = shift;
259              
260 0 0 0       if ( $make_test && ref( $make_test ) ) {
261 0           _err( "Function 'make_test' requires string type parameter." );
262             }
263              
264 0 0         $condition_ref->{make_test} = $make_test if $make_test;
265             }
266              
267             sub arguments($) {
268 0     0 0   my $arguments = shift;
269              
270 0 0         if ( ref( $arguments ) ) {
271 0           _err( "Function 'arguments' requires string type parameter." );
272             }
273              
274 0           $condition_ref->{arguments} = $arguments;
275             }
276              
277             sub annotation($) {
278 0     0 0   my $annotation = shift;
279              
280 0 0         if ( ref( $annotation ) ) {
281 0           _err( "Function 'annotation' requires string type parameter." );
282             }
283              
284 0           $condition_ref->{annotation} = $annotation;
285             }
286              
287             sub module_file($) {
288 0     0 0   my $module_file = shift;
289              
290 0 0         if ( ref( $module_file ) ) {
291 0           _err( "Function 'module_file' requires string type parameter." );
292             }
293              
294 0           $condition_ref->{module_file} = $module_file;
295             }
296              
297             sub modules {
298 0     0 0   croak "'modules' function is deparecated. Please use 'module_file'.";
299             }
300              
301             our $rcfile_condition;
302             sub conf($$) {
303 0     0 0   my ( $filepath, $coderef ) = @_;
304              
305 0 0         if ( ! $initialized ) {
306 0           _err(
307             "Environment is not initialized.".
308             "Isn't the syntax of DSL possibly mistaken?"
309             );
310             }
311              
312 0 0         if ( ref( $filepath ) ) {
313 0           _err( "Function 'conf' first requires string type parameter." );
314             }
315              
316 0 0         if ( $filepath =~ /\s/ ) {
317 0           _err(
318             "Configuration file path parameter must " .
319             "not contain space character."
320             );
321             }
322              
323 0 0         if ( ref( $coderef ) ne 'CODE' ) {
324 0           _err( "Function 'conf' requires code reference type parameter." );
325             }
326              
327 0           $coderef->();
328              
329 0           $rcfile_condition->{filepath} = $filepath;
330 0           $rcfile_collection{$filepath} = Enbld::RcFile->new( %{ $rcfile_condition } );
  0            
331              
332 0           undef $rcfile_condition;
333             }
334              
335             sub do_rcfile {
336 0     0 0   my $rcfile = shift;
337              
338             my $result = try {
339 0     0     return $rcfile->do;
340             } ( 'Enbld::Error' => sub {
341 0     0     Enbld::Message->alert( $@ );
342              
343 0           say "\n" . "Please check build logile:" . Enbld::Logger->logfile;
344              
345 0           $rcfile_result{$rcfile->filepath} =
346             $rcfile->filepath . ' is failure to create.';
347              
348 0           return;
349             }
350 0           );
351              
352             # Configuration file is loaded or set.
353 0 0         if ( $result ) {
354              
355 0           Enbld::App::Configuration->set_rcfile( $rcfile );
356 0           Enbld::App::Configuration->write_file;
357              
358 0           $rcfile_result{$rcfile->filepath} =
359             $rcfile->filepath . ' is created.';
360              
361 0           return $result;
362             }
363              
364             # Configuration file is not loaded or set.
365 0           $rcfile_result{$rcfile->filepath} =
366             $rcfile->filepath . ' is not created.';
367              
368 0           return;
369             }
370              
371             sub load(&) {
372 0     0 0   $rcfile_condition->{command} = 'load';
373              
374 0           return $_[0];
375             }
376              
377             sub set(&) {
378 0     0 0   $rcfile_condition->{command} = 'set';
379              
380 0           return $_[0];
381             }
382              
383             sub copy(&) {
384 0     0 0   $rcfile_condition->{command} = 'copy';
385              
386 0           return $_[0];
387             }
388              
389             sub from($) {
390 0     0 1   my $from = shift;
391              
392 0 0         if ( ref( $from ) ) {
393 0           _err( "Function 'from' requsres string type parameter." );
394             }
395              
396 0           $rcfile_condition->{from} = $from;
397             }
398              
399             sub to($) {
400 0     0 1   my $to = shift;
401              
402 0 0         if ( ref( $to ) ) {
403 0           _err( "Function 'to' requsres string type parameter." );
404             }
405              
406 0 0         if ( $to =~ /\s/ ) {
407 0           _err( "Function 'to' must not contain space character." );
408             }
409              
410 0           $rcfile_condition->{directory} = $to;
411             }
412              
413             sub content($) {
414 0     0 0   my $content = shift;
415              
416 0 0         if ( ref( $content ) ) {
417 0           _err( "Function 'content' reuqires string type parameter." );
418             }
419              
420 0           chomp( $content );
421              
422 0           $rcfile_condition->{contents} .= $content . "\n";
423             }
424              
425             our $setuped;
426             sub _setup_directory {
427              
428 0 0   0     unless ( $setuped ) {
429 0           Enbld::Home->create_build_directory;
430 0           Enbld::Logger->rotate( Enbld::Home->log );
431              
432 0           $setuped++;
433             }
434             }
435              
436             sub parse_option {
437              
438 0     0 0   my $make_test;
439             my $force;
440 0           my $deploy_path;
441              
442 0           Getopt::Long::Configure( "bundling" );
443             Getopt::Long::GetOptions(
444 0     0     't|test' => sub { $make_test++ },
445 0     0     'f|force' => sub { $force++ },
446 0           'd|deploy=s' => \$deploy_path,
447             );
448              
449             ### to absolute path
450 0 0         if ( $deploy_path ) {
451 0 0         unless ( File::Spec->file_name_is_absolute( $deploy_path ) ) {
452 0           $deploy_path = File::Spec->rel2abs( $deploy_path );
453             }
454             }
455              
456 0           require Enbld::Feature;
457 0           Enbld::Feature->initialize(
458             make_test => $make_test,
459             force => $force,
460             deploy => $deploy_path,
461             );
462              
463 0 0         if ( Enbld::Feature->is_deploy_mode ) {
464 0           Enbld::Message->notify(
465             "INFO:Enbld is set 'deploy mode'.\n" .
466             "All targets will be deployed in $deploy_path."
467             );
468             }
469              
470 0 0         if ( Enbld::Feature->is_make_test_all ) {
471 0           Enbld::Message->notify(
472             "INFO:Enbld is set 'make test mode'.\n" .
473             "All targets will be tested."
474             );
475             }
476              
477 0 0         if ( Enbld::Feature->is_force_install ) {
478 0           Enbld::Message->notify(
479             "INFO:Enbld is set 'force install mode'.\n" .
480             "All targets will be builded by force."
481             );
482             }
483              
484             }
485              
486             sub _err {
487 0     0     my ( $msg, $param ) = @_;
488              
489 0           Enbld::Error->throw( $msg, $param );
490             }
491              
492             1;
493              
494             __END__