Aline/Robot.pm untested

filter: Statementstmt 0.0% 0 / 285 Branchbran 0.0% 0 / 108 Conditioncond 0.0% 0 / 23 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 0.0% 0 / 33 Podpod 0.0% 0 / 15 totaltotal 0.0% 0 / 476 CC 55 SCAR 40.7
CC
55
Cov
0%
_on_file
156.0
_on_close
132.0
_on_file
72.0
CRAP 58.5
1 {
2 package Workflow::Aline::Robot;
3
4 use IO::Extended ':all';
5
6 use Class::Listener;
7
8 our $VERBOSITY = 3;
9
10 Class::Maker::class
11 {
12 isa => [qw( Class::Listener )],
13
14 public =>
15 {
16 ref => [qw( aline )],
17
18 number => [qw(stage verbosity)],
19 },
20 };
21
22 sub _preinit : method
23 {
24 my $this = shift;
25
26 $this->verbosity( $VERBOSITY );
27
28 $this->stage( undef ); # works in all stages
29 }
30
31 sub if_active : method
32 {
33 my $this = shift;
34
35 my $ok=0;
36
37 if( not defined $this->stage )
38 {
39 $ok = 1;
40 }
41 elsif( $this->stage == $this->aline->current_stage )
42 {
43 $ok = 1;
44 }
45 return $ok;
46 }
47
48 package Workflow::Aline::Robot::Mkdir;
49
50 use IO::Extended ':all';
51
52 use Class::Listener;
53
54 Class::Maker::class
55 {
56 isa => [qw( Workflow::Aline::Robot )],
57 };
58
59 sub _on_dir : method
60 {
61 my $this = shift;
62
63 my $event = shift;
64
65 my $conveyor_belt = shift;
66
67 my $dir = shift;
68
69 $dir->mkpath;
70
71 return undef;
72 }
73
74 package Workflow::Aline::Robot::Decorator;
75
76 use strict;
77
78 use IO::Extended ':all';
79
80
81 Class::Maker::class
82 {
83 isa => [qw( Workflow::Aline::Robot )],
84 };
85
86 use IO::Extended qw(:all);
87
88 sub _on_file : method
89 {
90 my $this = shift;
91
92 my ( $event, $conveyor_belt, $src, $dst ) = @_;
93
94 return unless $dst->stringify =~ /\.(pl|pm|pm\.tmpl)$/i;
95
96 println "decorate" if $this->verbosity > 2;
97
98 indn;
99
100 println $dst if $this->verbosity > 2;
101
102 indn;
103
104 println "execute" if $this->verbosity > 2;
105
106 if( $this->if_active )
107 {
108 if( my $txt = $this->_exec_comp( @_ ) )
109 {
110 println "prepend" if $this->verbosity > 2;
111
112 $this->_prepend( $dst, $txt );
113 }
114 }
115 else
116 {
117 println "This robot is not active in this stage";
118 }
119
120 indb;
121
122 indb;
123
124 return undef;
125 }
126
127 sub _exec_comp
128 {
129 my $this = shift;
130
131 my $event = shift;
132
133 my $conveyor_belt = shift;
134
135 my $src = shift;
136
137 my $dst = shift;
138
139 my $anon_comp = eval
140 {
141 $Workflow::Aline::mason_interp->make_component( comp_source => '<& /maslib/defaults.mas:codecopyright &>' )
142 };
143
144 die $@ if $@;
145
146 my $buffer;
147
148 $Workflow::Aline::mason_interp->out_method( \$buffer );
149
150 eval
151 {
152 $Workflow::Aline::mason_interp->exec( $anon_comp );
153 };
154 die $@ if $@;
155
156 return $buffer;
157 }
158
159 sub _prepend
160 {
161 my $this = shift;
162
163 my $filename = shift;
164
165 my @buffer;
166
167 local *FILE;
168
169 open( FILE, $filename ) or die "cannot open $filename";
170
171 @buffer = <FILE>;
172
173 close( FILE );
174
175 open( FILE, ">".$filename ) or die "cannot open $filename";
176
177 if( $buffer[0] =~ /\#!/ )
178 {
179 println "shebang detectored and preserved" if $this->verbosity > 2;
180
181 my $shebang = shift @buffer;
182
183 print FILE $shebang, @_, @buffer or die "cannot write $filename";
184 }
185 else
186 {
187 print FILE @_, @buffer or die "cannot write $filename";
188 }
189
190 close FILE or die "cannot close $filename";
191 }
192
193 package Workflow::Aline::Robot::Copy;
194
195 use strict;
196
197 use IO::Extended ':all';
198
199
200 Class::Maker::class
201 {
202 isa => [qw( Workflow::Aline::Robot )],
203 };
204
205 use File::Copy;
206
207 sub _on_file : method
208 {
209 my $this = shift;
210
211 my $event = shift;
212
213 my $conveyor_belt = shift;
214
215 my $src = shift;
216
217 my $dst = shift;
218
219
220 println "cp" if $this->verbosity > 1;
221
222 indn;
223
224 if( $this->verbosity > 2 )
225 {
226 println $dst;
227 }
228
229 if( $this->if_active )
230 {
231 copy( $src."", $dst."" ) or die "$!: src=$src, dst=$dst";
232
233 $conveyor_belt->master->Class::Listener::signal( 'copy', $src, $dst );
234
235 println "done" if $this->verbosity > 2;
236 }
237 else
238 {
239 println "This robot is not active in this stage";
240 }
241
242 indb;
243
244 return undef;
245 }
246
247 package Workflow::Aline::Robot::Skip;
248
249 use strict;
250
251 use IO::Extended qw(:all);
252
253 Class::Maker::class
254 {
255 isa => [qw( Workflow::Aline::Robot )],
256
257 public =>
258 {
259 ref => [qw( when )],
260 }
261 };
262
263 sub _on_dir : method
264 {
265 goto &_on_file;
266 }
267
268 sub _on_file : method
269 {
270 my $this = shift;
271
272 my $event = shift;
273
274 my $conveyor_belt = shift;
275
276 my $src = shift;
277
278
279 println "skip" if $this->verbosity > 2;
280
281 indn;
282
283 if( $this->if_active )
284 {
285 printl "decision: " if $this->verbosity > 2;
286
287 if( $this->when->( $this, $event, $conveyor_belt, $src ) )
288 {
289 print "yes\n" if $this->verbosity > 2;
290
291 indb;
292
293 return 1;
294 }
295 else
296 {
297 print "no\n" if $this->verbosity > 2;
298 }
299 }
300 else
301 {
302 println "This robot is not active in this stage" if $this->verbosity > 2;
303 }
304
305 indb;
306
307 # returning undef aborts the conveyorbelt -> what we want from skip !
308
309 return undef;
310 }
311
312 package Workflow::Aline::Robot::Podchecker;
313
314 use strict;
315
316 use IO::Extended qw(:all);
317
318 Class::Maker::class
319 {
320 isa => [qw( Workflow::Aline::Robot )],
321 };
322
323 sub _on_file : method
324 {
325 my $this = shift;
326
327 my $event = shift;
328
329 my $conveyor_belt = shift;
330
331 my $src = shift;
332
333 my $dst = shift;
334
335 println 'podcheck' if $this->verbosity > 1;
336
337 indn;
338
339 if( $this->if_active )
340 {
341 return unless $dst->stringify =~ /tmpl|pm$/;
342
343 println qx{podchecker $dst} if $this->verbosity > 2;
344 }
345 else
346 {
347 println "This robot is not active in this stage";
348 }
349
350 indb;
351
352 return undef;
353 }
354
355 package Workflow::Aline::Robot::Template;
356
357 use strict;
358
359 use IO::Extended qw(:all);
360
361 Class::Maker::class
362 {
363 isa => [qw( Workflow::Aline::Robot )],
364
365 public =>
366 {
367 ref => [qw( detector )],
368 },
369 };
370
371 sub test_detector : method
372 {
373 my $this = shift;
374
375 my $src = shift;
376
377 my $dst = shift;
378
379 return 1 if $this->detector->( $src, $dst );
380 }
381
382 sub _on_file : method
383 {
384 my $this = shift;
385
386 my $event = shift;
387
388 my $conveyor_belt = shift;
389
390 my $src = shift;
391
392 my $dst = shift;
393
394 my $comp_dir_unix = Path::Class::File->new( $dst )->relative( $conveyor_belt->master->comp_dir->absolute )->as_foreign( 'Unix' );
395
396 return unless $this->test_detector( $src, $dst );
397
398 println "mason component" if $this->verbosity > 1;
399
400 indn; #
401
402 if( $this->if_active )
403 {
404 if( $this->verbosity > 2 )
405 {
406 println $_ for ( $dst, $conveyor_belt->master->comp_dir->absolute, $comp_dir_unix );
407 }
408
409 unless( $conveyor_belt->master->is_testrun )
410 {
411 my $buffer;
412
413 indn; ##
414
415 println "executing component '$comp_dir_unix'" if $this->verbosity > 1;
416
417 $Workflow::Aline::mason_interp->out_method( \$buffer );
418
419 eval
420 {
421 $Workflow::Aline::mason_interp->exec( '/'.$comp_dir_unix );
422 };
423 die $@ if $@;
424
425 indn; ###
426
427 warn "WARNING: empty component output" and next unless length( $buffer );
428
429 println "output file '$dst'" if $this->verbosity > 2;
430
431 $this->_write( $dst, \$buffer );
432
433 $conveyor_belt->Class::Listener::signal( 'copy', $src, $dst );
434
435 # Create template output copy file
436
437 $dst =~ s/\.tmpl$//;
438
439 println "output copy '$dst'" if $this->verbosity > 2;
440
441 $this->_write( $dst, \$buffer );
442
443 $conveyor_belt->Class::Listener::signal( 'copy', $src, $dst );
444
445 println "component output written" if $this->verbosity > 2;
446
447 indb; ###
448
449 indb; ##
450 }
451 }
452 else
453 {
454 println "This robot is not active in this stage";
455 }
456
457 indb; #
458
459 return undef;
460 }
461
462 sub _write
463 {
464 my $this = shift;
465
466 my $filename = shift;
467
468 my $buffer = shift;
469
470 local *RESULT;
471
472 open( RESULT, "> ".$filename ) or die "cannot open $filename";
473
474 print RESULT $$buffer or die "cannot write $filename";
475
476 close RESULT or die "cannot close $filename";
477 }
478
479 package Workflow::Aline::Robot::Eval;
480
481 use IO::Extended qw(:all);
482
483 Class::Maker::class
484 {
485 isa => [qw( Workflow::Aline::Robot )],
486
487 public =>
488 {
489 string => [qw( code_text )],
490
491 bool => [qw( modify_inc )],
492
493 array => [qw( add_to_inc )],
494 },
495
496 default =>
497 {
498 modify_inc => 0,
499 },
500 };
501
502 sub _on_post_session : method
503 {
504 my $this = shift;
505
506 my $event = shift;
507
508 my $conveyor_belt = shift;
509
510 println "eval perl";
511
512 indn;
513
514 if( $this->if_active )
515 {
516 if( $this->modify_inc )
517 {
518 unshift @INC, $conveyor_belt->sprintf_current_target_dir( $conveyor_belt->master->current_stage )->absolute->stringify;
519 }
520
521 if( $this->add_to_inc )
522 {
523 unshift @INC, $this->add_to_inc;
524 }
525
526 eval $this->code_text;
527
528 die $@ if $@;
529 }
530 else
531 {
532 println "This robot is not active in this stage";
533 }
534
535 indb;
536
537 # printfln "Loaded post-publish module Data::Type VERSION %s", Data::Type->VERSION();
538
539 return undef;
540 };
541
542 package Workflow::Aline::Robot::Finalize;
543
544 use IO::Extended qw(:all);
545
546
547 Class::Maker::class
548 {
549 isa => [qw(Workflow::Aline::Robot)]
550 };
551
552 sub _on_close
553 {
554 my $this = shift;
555
556 my $event = shift;
557
558 my $conveyor_belt = shift;
559
560
561 println "finalize";
562
563 indn;
564
565 if( $this->if_active )
566 {
567 my $fin_file = $conveyor_belt->target_dir->file( 'finalize.pl' );
568
569 if( -e $fin_file->stringify )
570 {
571 my $lev = ind;
572
573 indn;
574
575 printfln "finalize" if $this->verbosity > 1;
576
577 indn;
578
579 printfln "load file %s", $fin_file->stringify if $this->verbosity > 2;
580
581 open( CODE, $fin_file->stringify );
582
583 printfln "chdir %s", $conveyor_belt->target_dir if $this->verbosity > 2;
584
585 chdir( $conveyor_belt->target_dir );
586
587 println "eval code" if $this->verbosity > 2;
588
589 my $result = eval join( '', <CODE> );
590
591 indn; println "" if $this->verbosity > 2;
592
593 if( $this->verbosity > 2 )
594 {
595 println "success" if defined $result;
596 }
597
598 warnfln "execution failed: %s", $! || $@ unless defined $result;
599
600 ind( $lev );
601 }
602 }
603 else
604 {
605 println "This robot is not active in this stage";
606 }
607
608 indb;
609
610 return undef;
611 }
612 }
613
614 1;