lib/Serge/Engine/Plugin/feature_branch.pm

filter: Statementstmt 20.7% 16 / 77 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 27.7% 5 / 18 Podpod 0.0% 0 / 11 totaltotal 13.6% 21 / 154 SCAR 57.3
CC
21
Cov
13%
before_job
56.0
can_extract
20.0
validate_data
12.0
CRAP 308.1
« lib/Serge/ScanTSFiles.pm ↑ Summary lib/Serge/Sync/Plugin/VCS/svn.pm »
1 package Serge::Engine::Plugin::feature_branch;
2 1 ▶use parent Serge::Plugin::Base::Callback;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶no warnings qw(uninitialized);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Serge::Util qw(generate_hash set_flags);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 sub name {
12 0 ▶ return 'Extract only strings missing in the master job';
Subroutine name: not called CC=1 SCAR=6.9
Pod undocumented
13 }
14  
15 sub init {
16 0 ▶ my $self = shift;
Subroutine init: not called CC=1 SCAR=6.9
Pod undocumented
17  
18 0 $self->SUPER::init(@_);
19  
20 0 $self->merge_schema({
21 master_job => 'STRING',
22 });
23  
24 0 $self->add({
25 before_job => \&before_job,
26 before_generate_localized_files => \&before_generate_localized_files,
27 before_save_localized_file => \&before_save_localized_file,
28 after_save_localized_file => \&after_save_localized_file,
29 can_extract => \&can_extract,
30 get_translation_pre => \&get_translation_pre,
31 log_translation => \&log_translation,
32 });
33  
34 0 $self->{master_translations} = {};
35 }
36  
37 sub validate_data {
38 0 ▶ my $self = shift;
Subroutine validate_data: not called CC=3 SCAR=24.8
Pod undocumented
39  
40 0 $self->SUPER::validate_data;
41  
42 0 $self->{master_mode} = $self->{parent}->{id} eq $self->{data}->{master_job};
43  
44 0 ▶ die "You must set 'source_path_prefix' parameter for the slave job that uses 'feature_branch' plugin" if $self->{parent}->{source_path_prefix} eq '' && !$self->{master_mode};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent"}{"source_path_prefix"} eq "" and not $self->{"master_mode"} 0 0 0
Condition0%0/3
$self->{"parent"}{"source_path_prefix"} eq "" and not $self->{"master_mode"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"parent"}{"source_path_prefix"} eq "" and not $self->{"master_mode"}
$self->{"parent"}{"source_path_prefix"} eq "" not $self->{"master_mode"}
Truth table0%0/3
A and not(B)
A= $self->{"parent"}{"source_path_prefix"} eq ""B= not $self->{"master_mode"}
ABresult
0X0
100
111
45 }
46  
47 sub adjust_phases {
48 0 ▶ my ($self, $phases) = @_;
Subroutine adjust_phases: not called CC=2 SCAR=17.9
Pod undocumented
49  
50 0 $self->SUPER::adjust_phases($phases);
51  
52 # always tie to these phases
53 0 set_flags($phases,
54 'before_job', 'before_generate_localized_files',
55 'before_save_localized_file', 'after_save_localized_file',
56 'can_extract', 'get_translation_pre', 'log_translation'
57 );
58  
59 0 ▶ die "This plugin attaches itself to specific phases; please don't specify phases in the configuration file" unless @$phases == 7;
Branch: 1 branch
BranchTrueFalseTotal
unless @$phases == 7 0 0 0
60 }
61  
62 sub before_job {
63 0 ▶ my ($self, $phase) = @_;
Subroutine before_job: not called CC=7 SCAR=40.3
Pod undocumented
64  
65 # preload the list of strings for current namespace and given job id
66  
67 0 my $master_job_id = $self->{data}->{master_job};
68 0 my $engine = $self->{parent}->{engine};
69  
70 #$self->{master_mode} = $self->{parent}->{id} eq $master_job_id;
71  
72 0 ▶ if ($self->{master_mode}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'master_mode'}) { } 0 0 0
73 0 print "Running feature_branch plugin in master mode\n";
74  
75 # initialize engine-wide plugin data structure for the current job
76 0 ▶ $engine->{plugin_data} = {} unless exists $engine->{plugin_data};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $engine->{"plugin_data"} 0 0 0
77 0 ▶ $engine->{plugin_data}->{feature_branch} = {} unless exists $engine->{plugin_data}->{feature_branch};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $engine->{"plugin_data"}{"feature_branch"} 0 0 0
78 0 $self->{master_translations} = $engine->{plugin_data}->{feature_branch}->{$master_job_id} = {};
79  
80 } else {
81 0 print "Running feature_branch plugin in slave mode\n";
82  
83 0 ▶ if (!exists $engine->{plugin_data} ||
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $engine->{"plugin_data"} and exists $engine->{"plugin_data"}{"feature_branch"} and exists $engine->{"plugin_data"}{"feature_branch"}{$master_job_id}) 0 0 0
Condition0%0/6
exists $engine->{"plugin_data"} and exists $engine->{"plugin_data"}{"feature_branch"} and exists $engine->{"plugin_data"}{"feature_branch"}{$master_job_id}
!ll&&!rl&&r
000
exists $engine->{"plugin_data"} and exists $engine->{"plugin_data"}{"feature_branch"}
!ll&&!rl&&r
000
MC/DC0%0/3
exists $engine->{"plugin_data"} and exists $engine->{"plugin_data"}{"feature_branch"} and exists $engine->{"plugin_data"}{"feature_branch"}{$master_job_id}
exists $engine->{"plugin_data"} exists $engine->{"plugin_data"}{"feature_branch"} exists $engine->{"plugin_data"}{"feature_branch"}{$master_job_id}
Truth table0%0/4
A and B and C
A= exists $engine->{"plugin_data"}B= exists $engine->{"plugin_data"}{"feature_branch"}C= exists $engine->{"plugin_data"}{"feature_branch"}{$master_job_id}
ABCresult
0XX0
10X0
1100
1111
84 !exists $engine->{plugin_data}->{feature_branch} ||
85 !exists $engine->{plugin_data}->{feature_branch}->{$master_job_id}) {
86 0 die "Can't run job with feature_branch plugin in slave mode before (or without) the master job\n";
87 }
88  
89 0 $self->{master_translations} = $engine->{plugin_data}->{feature_branch}->{$master_job_id};
90 }
91 }
92  
93 sub before_generate_localized_files {
94 0 ▶ my ($self) = @_;
Subroutine before_generate_localized_files: not called CC=2 SCAR=17.9
Pod undocumented
95  
96 0 ▶ return () unless $self->{master_mode}; # do nothing in slave mode
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"master_mode"} 0 0 0
97  
98 # disable optimizations for localized file generation
99 # for a master job, so that master files are always parsed,
100 # and their translations are always resolved and stored in
101 # `log_translation` phase. Since this is the last step
102 # before the job finishes execution, there's no need to restore
103 # optimizations mode after, but we still want to preserve
104 # optimizations at file saving time, so the original value
105 # of the job `optimizations` param is preserved here
106 # to be temporarily restored between `before_save_localized_file`
107 # and `after_save_localized_file` phases
108 0 $self->{optimizations} = $self->{parent}->{optimizations};
109 0 $self->{parent}->{optimizations} = undef;
110 }
111  
112 sub before_save_localized_file {
113 0 ▶ my ($self) = @_;
Subroutine before_save_localized_file: not called CC=2 SCAR=17.9
Pod undocumented
114  
115 0 ▶ return () unless $self->{master_mode}; # do nothing in slave mode
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"master_mode"} 0 0 0
116  
117 # restore the original value of `optimizations` job config value
118 # to avoid saving the localized file if it didin't change
119 0 $self->{parent}->{optimizations} = $self->{optimizations};
120 }
121  
122 sub after_save_localized_file {
123 0 ▶ my ($self) = @_;
Subroutine after_save_localized_file: not called CC=2 SCAR=17.9
Pod undocumented
124  
125 0 ▶ return () unless $self->{master_mode}; # do nothing in slave mode
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"master_mode"} 0 0 0
126  
127 # disable optimizations again
128 0 $self->{parent}->{optimizations} = undef;
129 }
130  
131 # Make the key that best describes a given string;
132 # if the source key is extracted from the file, then use filepath+key+string+context,
133 # otherwise, use filepath+string+context. This will allow to expose for translation
134 # strings in feature branches that have the same key in the same file,
135 # but a different string or context.
136 sub _make_cache_key {
137 0 ▶ my ($self, $lang, $file, $key, $stringref, $context) = @_;
Subroutine _make_cache_key: not called CC=2 SCAR=17.9
138 0 ▶ return $key ne '' ?
Branch: 1 branch
BranchTrueFalseTotal
$key ne '' ? : 0 0 0
139 generate_hash($lang, $file, $key, $$stringref, $context) :
140 generate_hash($lang, $file, $$stringref, $context);
141 }
142  
143 # Remove the virtual prefix to get the 'base' filepath of a given file
144 # for both master and slave jobs for better file/string alignment.
145 # Slave jobs are always expected to set a `source_path_prefix`
146 # to disambiguate file paths between files in branches;
147 # setting `source_path_prefix` for the master job is optional.
148 sub _get_base_filepath {
149 0 ▶ my ($self, $filepath) = @_;
Subroutine _get_base_filepath: not called CC=2 SCAR=17.9
150 0 my $prefix = $self->{parent}->{source_path_prefix};
151 0 ▶ $filepath =~ s/^\Q$prefix\E// if $prefix ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $prefix ne "" 0 0 0
152 0 return $filepath;
153 }
154  
155 sub can_extract {
156 0 ▶ my ($self, $phase, $filepath, $lang, $stringref, $hintref, $context, $key) = @_;
Subroutine can_extract: not called CC=4 SCAR=30.0
Pod undocumented
157  
158 # extract everything for a master job
159 0 ▶ return 1 if $self->{master_mode};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"master_mode"} 0 0 0
160  
161 # otherwise, we're in a slave job
162  
163 # if we're generating localized files, $lang will be set;
164 # extract everything to translate all the strings, not just overlay ones
165 0 ▶ return 1 if defined $lang;
Branch: 1 branch
BranchTrueFalseTotal
if defined $lang 0 0 0
166  
167 # when $lang is not set, this means we're parsing the source file;
168 # extract only strings which are missing from the master job
169 # so that translation interchange files will contain only these
170 # extra overlay strings
171 0 my $base_filepath = $self->_get_base_filepath($filepath);
172 0 my $cache_key = $self->_make_cache_key(undef, $base_filepath, $key, $stringref, $context);
173 0 ▶ return exists $self->{master_translations}->{$cache_key} ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'master_translations'}{$cache_key} ? : 0 0 0
174 }
175  
176 sub get_translation_pre {
177 0 ▶ my ($self, $phase, $string, $context, $namespace, $filepath, $lang, $disallow_similar_lang, $item_id, $key) = @_;
Subroutine get_translation_pre: not called CC=3 SCAR=24.8
Pod undocumented
178  
179 0 ▶ return () if $self->{master_mode}; # do nothing in master mode
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"master_mode"} 0 0 0
180  
181 0 my $base_filepath = $self->_get_base_filepath($filepath);
182 0 my $cache_key = $self->_make_cache_key($lang, $base_filepath, $key, \$string, $context);
183  
184 # if the string is not present in the master job, return
185 # an empty array, so the translation can be resolved
186 # from .po files / plugins / database
187 0 ▶ return () unless defined $self->{master_translations}->{$cache_key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"master_translations"}{$cache_key} 0 0 0
188  
189 # otherwise, return the actual translation as not fuzzy,
190 # and do not save it do the database
191 0 return ($self->{master_translations}->{$cache_key}, undef, undef, undef)
192 }
193  
194 sub log_translation {
195 0 ▶ my ($self, $phase, $string, $context, $hint, $flags,
Subroutine log_translation: not called CC=2 SCAR=17.9
Pod undocumented
196 $lang, $key, $translation, $namespace, $filepath) = @_;
197  
198 0 ▶ return () unless $self->{master_mode}; # do nothing in slave mode
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"master_mode"} 0 0 0
199  
200 # save master translation into the cache for later exact string retrieval
201 # in get_translation_pre()
202 0 my $base_filepath = $self->_get_base_filepath($filepath);
203 0 my $cache_key = $self->_make_cache_key($lang, $base_filepath, $key, \$string, $context);
204 0 $self->{master_translations}->{$cache_key} = $translation;
205  
206 # also, save a 'source string exists' flag for can_extract(),
207 # where $lang is undefined
208 0 $cache_key = $self->_make_cache_key(undef, $base_filepath, $key, \$string, $context);
209 0 $self->{master_translations}->{$cache_key} = 1;
210 }
211  
212 1 1;
« lib/Serge/ScanTSFiles.pm ↑ Summary lib/Serge/Sync/Plugin/VCS/svn.pm »