lib/Serge/Engine/Job.pm

filter: Statementstmt 90.9% 120 / 132 Branchbran 50.0% 25 / 50 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 100% 21 / 21 Podpod 0.0% 0 / 11 totaltotal 74.5% 167 / 224 SCAR 37.3
CC
31
Cov
78%
new
27.6
load_plugin_and_register_callbacks
7.1
run_callbacks
6.1
CRAP 41.7
« lib/Serge/Engine/Plugin/apply_xslt.pm ↑ Summary lib/Serge/Engine/Plugin/Transform/endpunc.pm »
1 package Serge::Engine::Job;
2 2 ▶use parent Serge::Interface::PluginHost;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Serge::Util qw(subst_macros generate_hash is_flag_set);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 #
12 # Initialize object
13 #
14 sub new {
15 7 ▶ my ($class, $data, $engine, $base_dir) = @_;
Subroutine new: called CC=15 SCAR=33.2
Pod undocumented
16  
17 7 ▶ die "data not provided" unless $data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 7 7
18  
19 7 my $self = $data;
20 7 bless $self, $class;
21  
22 # expand environment variables before calculating the job hash
23 7 $self->expand_env_vars;
24  
25 # calculate hash before we add additional calculated fields and bless
26 # the data
27 7 my $dumper = Data::Dumper->new([$data]);
28 7 $self->{hash} = generate_hash($dumper->Terse(1)->Deepcopy(1)->Indent(0)->Dump);
29  
30 # convert paths relative to the config path to absolute ones
31 7 $self->expand_paths;
32  
33 7 ▶ $self->{engine} = $engine if $engine;
Branch: 1 branch
BranchTrueFalseTotal
if $engine 7 0 7
34 7 ▶ $self->{base_dir} = $base_dir if $base_dir;
Branch: 1 branch
BranchTrueFalseTotal
if $base_dir 7 0 7
35  
36 7 ▶ $self->{debug} = $engine->{debug} if $engine;
Branch: 1 branch
BranchTrueFalseTotal
if $engine 7 0 7
37  
38 # Check job config validity
39  
40 7 ▶ die "job has an empty 'id' property" if $self->{id} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"id"} eq "" 0 7 7
41 7 ▶ die "job has an empty 'db_source' property" if $self->{db_source} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"db_source"} eq "" 0 7 7
42 7 ▶ die "job has an empty 'db_namespace' property" if $self->{db_namespace} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"db_namespace"} eq "" 0 7 7
43  
44 7 ▶ if (!exists $self->{destination_languages} or scalar(@{$self->{destination_languages}}) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $self->{"destination_languages"} or scalar @{$self->{"destination_languages"};} == 0) 0 7 7
Condition33%1/3
not exists $self->{"destination_languages"} or scalar @{$self->{"destination_languages"};} == 0
l!l&&r!l&&!r
007
MC/DC0%0/2
not exists $self->{"destination_languages"} or scalar @{$self->{"destination_languages"};} == 0
not exists $self->{"destination_languages"} scalar @{$self->{"destination_languages"};} == 0
Truth table33%1/3
not(A) or B
A= not exists $self->{"destination_languages"}B= scalar @{$self->{"destination_languages"};} == 0
ABresult
1X1
011
000
45 0 die "the list of destination languages is empty";
46 }
47  
48 7 ▶ if (scalar @{$self->{destination_languages}} > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"destination_languages"};} > 1) 0 7 7
49 0 ▶ if ($self->{ts_file_path} !~ m/%(LANG|LOCALE|CULTURE|LANGNAME|LANGID)(:\w+)*%/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ts_file_path"} =~ /%(LANG|LOCALE|CULTURE|LANGNAME|LANGID)(:\w+)*%/) 0 0 0
50 0 die "when there's more than one destination language, 'ts_file_path' should have %LANG%, %LOCALE%, %CULTURE%, %LANGNAME%, or %LANGID% macro defined";
51 }
52  
53 0 ▶ if ($self->{output_lang_files} && ($self->{output_file_path} !~ m/%(LANG|LOCALE|CULTURE|LANGNAME|LANGID)(:\w+)*%/)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"output_lang_files"} and not $self->{"output_file_path"} =~ /%(LANG|LOCALE|CULTURE|LANGNAME|LANGID)(:\w+)*%/) 0 0 0
Condition0%0/3
$self->{"output_lang_files"} and not $self->{"output_file_path"} =~ /%(LANG|LOCALE|CULTURE|LANGNAME|LANGID)(:\w+)*%/
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"output_lang_files"} and not $self->{"output_file_path"} =~ /%(LANG|LOCALE|CULTURE|LANGNAME|LANGID)(:\w+)*%/
$self->{"output_lang_files"} not $self->{"output_file_path"} =~ /%(LANG|LOCALE|CULTURE|LANGNAME|LANGID)(:\w+)*%/
Truth table0%0/3
A and not(B)
A= $self->{"output_lang_files"}B= not $self->{"output_file_path"} =~ /%(LANG|LOCALE|CULTURE|LANGNAME|LANGID)(:\w+)*%/
ABresult
0X0
100
111
54 0 die "when there's more than one destination language, 'output_file_path' should have %LANG%, %LOCALE%, %CULTURE%, %LANGNAME%, or %LANGID% macro defined";
55 }
56 }
57  
58 # load parser plugin
59  
60 7 $self->{parser_object} = $self->load_plugin_and_register_callbacks($self->{parser});
61  
62 7 my $plugin_name = $self->{parser}->{plugin};
63 7 my $class = ref $self->{parser_object};
64 7 $self->{plugin_version} = $plugin_name.'.'.eval('$'.$class.'::VERSION');
65  
66 # load serializer plugin
67  
68 # for backward compatibility, a missing `serializer` job config section
69 # means 'use `serialize_po` plugin with default settings'
70 7 my $plugin_name = 'serialize_po';
71 7 ▶ if (exists $self->{serializer}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'serializer'}) { } 0 7 7
72 0 $self->{serializer_object} = $self->load_plugin_and_register_callbacks($self->{serializer});
73 0 $plugin_name = $self->{serializer}->{plugin};
74 } else {
75 7 $self->{serializer_object} = $self->load_plugin_and_register_callbacks({plugin => $plugin_name});
76 }
77 7 my $class = ref $self->{serializer_object};
78 7 $self->{serializer_version} = $plugin_name.'.'.eval('$'.$class.'::VERSION');
79  
80 # load callback plugins
81  
82 map {
83 21 $self->load_plugin_and_register_callbacks($_);
84 7 } @{$self->{callback_plugins}};
85  
86 6 return $self;
87 }
88  
89 sub expand_env_vars {
90 7 ▶ my ($self) = @_;
Subroutine expand_env_vars: called CC=1 SCAR=0.0
Pod undocumented
91  
92 map {
93 7 $self->{$_} = subst_macros($self->{$_});
94 } qw(
95 db_source db_username db_password db_namespace
96 source_dir ts_file_path output_file_path
97 );
98 }
99  
100 sub expand_paths {
101 7 ▶ my ($self) = @_;
Subroutine expand_paths: called CC=1 SCAR=0.0
Pod undocumented
102  
103 map {
104 7 $self->{$_} = $self->abspath($self->{$_});
105 } qw(source_dir ts_file_path output_file_path);
106 }
107  
108 sub load_plugin_and_register_callbacks {
109 35 ▶ my ($self, $node) = @_;
Subroutine load_plugin_and_register_callbacks: called CC=7 SCAR=19.6
Pod undocumented
110  
111 35 my $plugin = $node->{plugin};
112  
113 35 my $p = $self->load_plugin_from_node('Serge::Engine::Plugin', $node);
114  
115 34 my @phases = $p->get_phases;
116  
117 # if specific phase was specified, limit to this specific phase
118 34 ▶ if (exists $node->{phase}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $node->{"phase"}) 12 22 34
119 # check the validity of phase names
120 12 my $used_phases = {};
121 map {
122 12 ▶ die "phase '$_' specified twice" if exists $used_phases->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $used_phases->{$_} 0 12 12
123 12 ▶ die "phase '$_' is not supported by '$node->{plugin}' plugin" unless is_flag_set(\@phases, $_);
Branch: 1 branch
BranchTrueFalseTotal
unless is_flag_set(\@phases, $_) 0 12 12
124 12 $used_phases->{$_} = 1;
125 12 } @{$node->{phase}};
126  
127 12 @phases = @{$node->{phase}};
128 }
129  
130 34 $p->adjust_phases(\@phases);
131  
132 34 foreach my $phase (@phases) {
133 # Deprecation notice
134 85 ▶ if ($phase eq 'before_update_database_from_ts_file') {
Branch: 1 branch
BranchTrueFalseTotal
if ($phase eq "before_update_database_from_ts_file") 0 85 85
135 0 print "WARNING: 'before_update_database_from_ts_file' phase name is deprecated; use 'before_update_database_from_ts_files' instead\n";
136 }
137  
138 85 my $a = $self->{callback_phases}->{$phase};
139 85 ▶ $self->{callback_phases}->{$phase} = $a = [] unless defined $a;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $a 55 30 85
140 85 push @$a, $p;
141 }
142  
143 34 return $p;
144 }
145  
146 sub has_callbacks {
147 914 ▶ my ($self, $phase) = @_;
Subroutine has_callbacks: called CC=1 SCAR=0.0
Pod undocumented
148 914 return exists $self->{callback_phases}->{$phase};
149 }
150  
151 sub run_callbacks {
152 860 ▶ my ($self, $phase, @params) = @_;
Subroutine run_callbacks: called CC=6 SCAR=18.1
Pod undocumented
153  
154 860 ▶ if ($self->has_callbacks($phase)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_callbacks($phase)) { } 202 658 860
155 202 ▶ print "::phase '$phase' has callbacks, running...\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 202 202
156  
157 202 my @result;
158  
159 202 foreach my $p (@{$self->{callback_phases}->{$phase}}) {
160 249 my @res = $p->callback($phase, @params);
161 249 ▶ print "::plugin '".ref($p)."' returned [".join(',', @res)."]\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 249 249
162 249 push @result, @res;
163 }
164 202 return @result;
165 } else {
166 658 ▶ print "::phase '$phase' has no callbacks defined\n" if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 658 658
167 }
168 658 return; # return nothing
169 }
170  
171 sub get_hash {
172 6 ▶ my ($self) = @_;
Subroutine get_hash: called CC=1 SCAR=0.0
Pod undocumented
173 6 return $self->{hash};
174 }
175  
176 sub abspath {
177 21 ▶ my ($self, $path) = @_;
Subroutine abspath: called CC=1 SCAR=0.0
Pod undocumented
178  
179 21 return Serge::Util::abspath($self->{base_dir}, $path);
180 }
181  
182 sub get_full_ts_file_path {
183 16 ▶ my ($self, $file, $lang) = @_;
Subroutine get_full_ts_file_path: called CC=1 SCAR=0.0
Pod undocumented
184  
185 16 return subst_macros($self->{ts_file_path}, $file, $lang, $self->{source_language});
186 }
187  
188 sub render_full_output_path {
189 86 ▶ my ($self, $path, $file, $lang) = @_;
Subroutine render_full_output_path: called CC=3 SCAR=11.2
Pod undocumented
190  
191 # get the relative file path sans the source_path_prefix
192 # note that this would be a virtual path that might have been changed
193 # in rewrite_path callback
194 86 my $prefix = $self->{source_path_prefix};
195 86 ▶ $file =~ s/^\Q$prefix\E// if $prefix;
Branch: 1 branch
BranchTrueFalseTotal
if $prefix 0 86 86
196  
197 86 my $r = $self->{output_lang_rewrite};
198 86 ▶ $lang = $r->{$lang} if exists($r->{$lang});
Branch: 1 branch
BranchTrueFalseTotal
if exists $r->{$lang} 0 86 86
199  
200 86 return subst_macros($path, $file, $lang, $self->{source_language});
201 }
202  
203 sub gather_similar_languages_for_lang {
204 8 ▶ my ($self, $lang) = @_;
Subroutine gather_similar_languages_for_lang: called CC=4 SCAR=17.9
Pod undocumented
205  
206 8 my %out;
207 8 ▶ if (exists $self->{similar_languages}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"similar_languages"}) 8 0 8
208 8 foreach my $rule (@{$self->{similar_languages}}) {
209 0 ▶ if ($rule->{destination} eq $lang) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule->{"destination"} eq $lang) 0 0 0
210 0 map { $out{$_} = 1 } @{$rule->{source}};
211 }
212 }
213 }
214 8 return keys %out;
215 }
216  
217 2 1;
« lib/Serge/Engine/Plugin/apply_xslt.pm ↑ Summary lib/Serge/Engine/Plugin/Transform/endpunc.pm »