lib/Serge/Engine/Plugin/run_command.pm

filter: Statementstmt 32.6% 16 / 49 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 45.4% 5 / 11 Podpod 0.0% 0 / 6 totaltotal 24.7% 21 / 85 SCAR 41.5
CC
11
Cov
24%
validate_data
42.0
process_then_block
30.0
adjust_phases
6.0
CRAP 63.6
« lib/Serge/Engine/Plugin/serialize_csv.pm ↑ Summary lib/Serge/Util/Pager.pm »
1 package Serge::Engine::Plugin::run_command;
2 1 ▶use parent Serge::Engine::Plugin::if;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Serge::Util qw(remove_flags set_flag subst_macros);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub name {
11 0 ▶ return 'Run shell command plugin';
Subroutine name: not called CC=1 SCAR=6.9
Pod undocumented
12 }
13  
14 sub init {
15 0 ▶ my $self = shift;
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
16  
17 0 $self->SUPER::init(@_);
18  
19 0 $self->merge_schema({
20 command => {'' => 'LIST',
21 '*' => 'STRING'
22 },
23  
24 if => {
25 '*' => {
26 then => {
27 command => {'' => 'LIST',
28 '*' => 'STRING'
29 },
30 },
31 },
32 },
33 });
34  
35 0 $self->add({
36 after_save_localized_file => \&check
37 });
38 }
39  
40 sub validate_data {
41 0 ▶ my $self = shift;
Subroutine validate_data: not called CC=6 SCAR=37.4
Pod undocumented
42  
43 0 $self->SUPER::validate_data;
44  
45 0 ▶ die "'command' parameter is not specified and no 'if' blocks found" if !exists $self->{data}->{if} && !$self->{data}->{command};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"data"}{"if"} or $self->{"data"}{"command"} 0 0 0
Condition0%0/3
exists $self->{"data"}{"if"} or $self->{"data"}{"command"}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $self->{"data"}{"if"} or $self->{"data"}{"command"}
exists $self->{"data"}{"if"} $self->{"data"}{"command"}
Truth table0%0/3
A or B
A= exists $self->{"data"}{"if"}B= $self->{"data"}{"command"}
ABresult
1X1
011
000
46  
47 0 ▶ if (exists $self->{data}->{if}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"data"}{"if"}) 0 0 0
48 0 foreach my $block (@{$self->{data}->{if}}) {
49 0 ▶ die "'command' parameter is not specified inside if/then block" if !$block->{then}->{command};
Branch: 1 branch
BranchTrueFalseTotal
unless $block->{"then"}{"command"} 0 0 0
50 }
51 }
52 }
53  
54 sub adjust_phases {
55 0 ▶ my ($self, $phases) = @_;
Subroutine adjust_phases: not called CC=2 SCAR=17.9
Pod undocumented
56  
57 # remove unused flags added by default by the parent 'if' plugin
58 0 remove_flags($phases, qw(after_load_file after_load_source_file_for_processing before_save_localized_file before_deserialize_ts_file after_serialize_ts_file));
59  
60 # always tie to 'after_save_localized_file' phase
61 0 set_flag($phases, 'after_save_localized_file');
62  
63 # this plugin makes sense only when applied to a single phase
64 # (in addition to 'before_job' phase inherited from Serge::Engine::Plugin::if plugin)
65 0 ▶ die "This plugin needs to be attached to only one 'after_save_localized_file' phase" unless @$phases == 2;
Branch: 1 branch
BranchTrueFalseTotal
unless @$phases == 2 0 0 0
66 }
67  
68 sub process_then_block {
69 0 ▶ my ($self, $phase, $block, $file, $lang, $strref) = @_;
Subroutine process_then_block: not called CC=5 SCAR=34.0
Pod undocumented
70  
71 0 ▶ die "This plugin should only be used in 'after_save_localized_file' phase (current phase: '$phase')" unless $phase eq 'after_save_localized_file';
Branch: 1 branch
BranchTrueFalseTotal
unless $phase eq "after_save_localized_file" 0 0 0
72  
73 0 my $outfile = $self->{parent}->{engine}->get_full_output_path($file, $lang);
74 0 ($_, my $outpath, $_) = fileparse($outfile); # this way $outpath will include the trailing delimiter
75  
76 0 foreach my $command (@{$block->{command}}) {
77 # substitute %FILE% and target language-based macros
78 # with the full path to the saved file
79 0 $command = subst_macros($command, $file, $lang);
80  
81 # substitute %OUTFILE% macro with the full path to the saved file
82 0 $command =~ s/%OUTFILE%/$outfile/sg;
83 # substitute %OUTPATH% macro with the full directory path
84 0 $command =~ s/%OUTPATH%/$outpath/sg;
85  
86 0 ▶ die "After macro substitution, 'command' parameter evaluates to an empty string" if $command eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $command eq "" 0 0 0
87  
88 0 print "RUN: $command\n";
89 0 system($command);
90  
91 0 my $error_code = unpack 'c', pack 'C', $? >> 8; # error code
92 0 ▶ die "Exit code: $error_code\n" if $error_code != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $error_code != 0 0 0 0
93 }
94  
95 0 return (shift @_)->SUPER::process_then_block(@_);
96 }
97  
98 sub check {
99 0 ▶ my $self = shift;
Subroutine check: not called CC=1 SCAR=6.9
Pod undocumented
100 0 return $self->SUPER::check(@_);
101 }
102  
103 1 1;
« lib/Serge/Engine/Plugin/serialize_csv.pm ↑ Summary lib/Serge/Util/Pager.pm »