line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Jenkins::i18n::ProcOpts; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
149737
|
use 5.014004; |
|
3
|
|
|
|
|
17
|
|
4
|
3
|
|
|
3
|
|
16
|
use strict; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
58
|
|
5
|
3
|
|
|
3
|
|
13
|
use warnings; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
100
|
|
6
|
3
|
|
|
3
|
|
20
|
use base qw(Class::Accessor); |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
1309
|
|
7
|
3
|
|
|
3
|
|
5075
|
use Hash::Util qw(lock_hash unlock_value lock_value); |
|
3
|
|
|
|
|
6051
|
|
|
3
|
|
|
|
|
19
|
|
8
|
3
|
|
|
3
|
|
270
|
use Carp qw(confess); |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
136
|
|
9
|
3
|
|
|
3
|
|
21
|
use File::Spec; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
2762
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
__PACKAGE__->follow_best_practice; |
12
|
|
|
|
|
|
|
__PACKAGE__->mk_ro_accessors(qw(language source_dir target_dir counter)); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = '0.09'; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=pod |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 NAME |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
Jenkins::i18n::ProcOpts - process files definitions based on CLI options |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
=head1 SYNOPSIS |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
use Jenkins::i18n::ProcOpts; |
25
|
|
|
|
|
|
|
my $proc = Jenkins::i18n::ProcOpts->new({ |
26
|
|
|
|
|
|
|
source_dir => $source, |
27
|
|
|
|
|
|
|
target_dir => $target, |
28
|
|
|
|
|
|
|
use_counter => 1, |
29
|
|
|
|
|
|
|
is_remove => 1, |
30
|
|
|
|
|
|
|
is_add => 1, |
31
|
|
|
|
|
|
|
is_debug => 0, |
32
|
|
|
|
|
|
|
lang => 'pt_BR', |
33
|
|
|
|
|
|
|
search => 'foobar' |
34
|
|
|
|
|
|
|
}); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=head1 DESCRIPTION |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
This module define how the translation files should be processed based on the |
39
|
|
|
|
|
|
|
collected CLI options. |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
=head2 EXPORT |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
None by default. |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
=head1 METHODS |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=head2 new |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
Creates a new instance. |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Expects a hash reference as parameter, with the following keys: |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=over |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=item * |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
C: a string representing the path where the files should be |
58
|
|
|
|
|
|
|
reviewed. |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=item * |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
C: a string representing the path where the processed files should |
63
|
|
|
|
|
|
|
be written to. |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=item * |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
C: A boolean (in Perl terms) if a counter is to be used. |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
=item * |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
C: A boolean (in Perl terms) if deprecated files should be removed. |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=item * |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
C: a boolean (in Perl terms) if new files should be added. |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=item * |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
C: a boolean (in Perl terms) if CLI is running in debug mode. |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=item * |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
C: a string identifying the chosen language for processing. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=item * |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
C: optional, an string of a regular expression to match the content of |
88
|
|
|
|
|
|
|
the translated properties, with you want to use that. Otherwise, just provide |
89
|
|
|
|
|
|
|
C as a value. |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=back |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=cut |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
sub new { |
96
|
10
|
|
|
10
|
1
|
8737
|
my ( $class, $params_ref ) = @_; |
97
|
10
|
100
|
|
|
|
57
|
confess 'Must receive an hash reference as parameter' |
98
|
|
|
|
|
|
|
unless ( ref($params_ref) eq 'HASH' ); |
99
|
|
|
|
|
|
|
my @expected_keys |
100
|
9
|
|
|
|
|
52
|
= qw(source_dir target_dir use_counter is_remove is_add is_debug lang search); |
101
|
9
|
|
|
|
|
53
|
my $self = { |
102
|
|
|
|
|
|
|
counter => 0, |
103
|
|
|
|
|
|
|
ext_sep => qr/\./ |
104
|
|
|
|
|
|
|
}; |
105
|
|
|
|
|
|
|
|
106
|
9
|
|
|
|
|
25
|
foreach my $attrib (@expected_keys) { |
107
|
|
|
|
|
|
|
confess "must receive $attrib as parameter" |
108
|
72
|
50
|
|
|
|
148
|
unless ( exists( $params_ref->{$attrib} ) ); |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
my @to_copy |
112
|
9
|
|
|
|
|
60
|
= qw(source_dir target_dir use_counter is_remove is_add is_debug); |
113
|
9
|
|
|
|
|
21
|
foreach my $attrib (@to_copy) { |
114
|
54
|
|
|
|
|
121
|
$self->{$attrib} = $params_ref->{$attrib}; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
9
|
|
|
|
|
28
|
$self->{language} = $params_ref->{lang}; |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
confess |
120
|
|
|
|
|
|
|
'Removing or adding translation files are excluding operations, they cannot be both true at the same time' |
121
|
9
|
50
|
66
|
|
|
37
|
if ( $params_ref->{is_remove} and $params_ref->{is_add} ); |
122
|
|
|
|
|
|
|
|
123
|
8
|
100
|
|
|
|
34
|
if ( defined( $params_ref->{search} ) ) { |
124
|
2
|
|
|
|
|
42
|
$self->{search} = qr/$params_ref->{search}/; |
125
|
2
|
|
|
|
|
7
|
$self->{has_search} = 1; |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
else { |
128
|
6
|
|
|
|
|
28
|
$self->{has_search} = 0; |
129
|
|
|
|
|
|
|
} |
130
|
|
|
|
|
|
|
|
131
|
8
|
|
|
|
|
16
|
bless $self, $class; |
132
|
8
|
|
|
|
|
15
|
lock_hash( %{$self} ); |
|
8
|
|
|
|
|
45
|
|
133
|
8
|
|
|
|
|
299
|
return $self; |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head2 is_to_search |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Returns true (1) or false (0) if there is a defined term to search on the |
139
|
|
|
|
|
|
|
translated properties values. |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
=cut |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub is_to_search { |
144
|
4
|
|
|
4
|
1
|
3424
|
my $self = shift; |
145
|
4
|
|
|
|
|
14
|
return $self->{has_search}; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head2 search_term |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
Returns the compiled regular expression that will be used to match terms in the |
151
|
|
|
|
|
|
|
translated properties values. |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=cut |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
sub search_term { |
156
|
3
|
|
|
3
|
1
|
13
|
my $self = shift; |
157
|
3
|
50
|
|
|
|
14
|
confess 'There is no defined search term' unless ( $self->{has_search} ); |
158
|
3
|
|
|
|
|
12
|
return $self->{search}; |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
=head2 get_language |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
Returns a string identifying the chosen language for processing. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
=head2 get_source_dir |
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
Returns string of the path where the translation files should be looked for. |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
=head2 get_target_dir |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
Returns a string of the path where the reviewed translation files should be |
172
|
|
|
|
|
|
|
written to. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head2 inc |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
Increments the processed files counter. |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=cut |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
sub inc { |
181
|
2
|
|
|
2
|
1
|
2226
|
my $self = shift; |
182
|
|
|
|
|
|
|
|
183
|
2
|
100
|
|
|
|
8
|
if ( $self->use_counter ) { |
184
|
1
|
|
|
|
|
3
|
my $attrib = 'counter'; |
185
|
1
|
|
|
|
|
2
|
unlock_value( %{$self}, $attrib ); |
|
1
|
|
|
|
|
8
|
|
186
|
1
|
|
|
|
|
11
|
$self->{$attrib}++; |
187
|
1
|
|
|
|
|
3
|
lock_value( %{$self}, $attrib ); |
|
1
|
|
|
|
|
5
|
|
188
|
1
|
|
|
|
|
19
|
return 1; |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
1
|
|
|
|
|
12
|
warn "Useless invocation of inc with file counter disabled\n"; |
192
|
1
|
|
|
|
|
8
|
return 0; |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=head2 use_counter |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
Returns true (1) or false (0) if the processed counter is in use. |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=cut |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
sub use_counter { |
202
|
3
|
|
|
3
|
1
|
687
|
my $self = shift; |
203
|
3
|
|
|
|
|
13
|
return $self->{use_counter}; |
204
|
|
|
|
|
|
|
} |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=head2 get_counter |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
Returns an integer representing the number of translation files already |
209
|
|
|
|
|
|
|
processed. |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=head2 is_remove |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
Returns true (1) or false (0) if the outdated translation files should be |
214
|
|
|
|
|
|
|
removed. |
215
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
=cut |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
sub is_remove { |
219
|
1
|
|
|
1
|
1
|
3
|
my $self = shift; |
220
|
1
|
|
|
|
|
5
|
return $self->{is_remove}; |
221
|
|
|
|
|
|
|
} |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
=head2 is_add |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
Returns true (1) or false (0) if the translation files should be added. |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=cut |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
sub is_add { |
230
|
1
|
|
|
1
|
1
|
2
|
my $self = shift; |
231
|
1
|
|
|
|
|
5
|
return $self->{is_add}; |
232
|
|
|
|
|
|
|
} |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=head2 is_debug |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
Returns true (1) or false (0) if the CLI is running in debug mode. |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=cut |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
sub is_debug { |
241
|
16
|
|
|
16
|
1
|
36
|
my $self = shift; |
242
|
16
|
|
|
|
|
523
|
return $self->{is_debug}; |
243
|
|
|
|
|
|
|
} |
244
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=head2 define_files |
246
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
Based on complete path to a translation file as input, defines the resulting |
248
|
|
|
|
|
|
|
expected translation files and their locations, even if they don't yet exist. |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
Expects as parameter the complete path to a translation file (Jelly or Java |
251
|
|
|
|
|
|
|
Properties). |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
Returns an array with the following elements: |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=over |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
=item 1 |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
The path to the current language file location. |
260
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=item 2 |
262
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
The path to the English Properties file location. |
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
=item 3 |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
The path to the corresponding Jelly file. |
268
|
|
|
|
|
|
|
|
269
|
|
|
|
|
|
|
=back |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
All three items are formed based on convention, that doesn't mean that any of |
272
|
|
|
|
|
|
|
them actually exists in the file system. |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
=cut |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
sub define_files { |
277
|
12
|
|
|
12
|
1
|
4308
|
my ( $self, $file_path ) = @_; |
278
|
12
|
50
|
|
|
|
32
|
confess 'Must receive a file path as parameter' unless ($file_path); |
279
|
12
|
|
|
|
|
182
|
my ( $volume, $dirs, $filename ) = File::Spec->splitpath($file_path); |
280
|
12
|
|
|
|
|
72
|
my @file_parts = split( $self->{ext_sep}, $filename ); |
281
|
12
|
|
|
|
|
28
|
my $filename_ext = pop(@file_parts); |
282
|
12
|
|
|
|
|
36
|
my $filename_prefix = join( '.', @file_parts ); |
283
|
12
|
|
|
|
|
23
|
my ( $curr_lang_file, $english_file, $jelly_file ); |
284
|
|
|
|
|
|
|
|
285
|
12
|
100
|
|
|
|
49
|
if ( $filename_ext eq 'jelly' ) { |
|
|
50
|
|
|
|
|
|
286
|
|
|
|
|
|
|
$curr_lang_file |
287
|
6
|
|
|
|
|
21
|
= $filename_prefix . '_' . $self->{language} . '.properties'; |
288
|
6
|
|
|
|
|
13
|
$english_file = "$filename_prefix.properties"; |
289
|
6
|
|
|
|
|
11
|
$jelly_file = $filename; |
290
|
|
|
|
|
|
|
} |
291
|
|
|
|
|
|
|
elsif ( $filename_ext eq 'properties' ) { |
292
|
|
|
|
|
|
|
$curr_lang_file |
293
|
6
|
|
|
|
|
16
|
= $filename_prefix . '_' . $self->{language} . '.properties'; |
294
|
6
|
|
|
|
|
14
|
$english_file = $filename; |
295
|
6
|
|
|
|
|
11
|
$jelly_file = "$filename_prefix.jelly"; |
296
|
|
|
|
|
|
|
} |
297
|
|
|
|
|
|
|
else { |
298
|
0
|
|
|
|
|
0
|
confess "Unexpected file extension '$filename_ext' in $file_path"; |
299
|
|
|
|
|
|
|
} |
300
|
|
|
|
|
|
|
|
301
|
12
|
|
|
|
|
30
|
my @dirs; |
302
|
12
|
50
|
|
|
|
33
|
push( @dirs, $volume ) unless ( $volume eq '' ); |
303
|
12
|
100
|
|
|
|
41
|
push( @dirs, $dirs ) unless ( $dirs eq '' ); |
304
|
|
|
|
|
|
|
|
305
|
12
|
|
|
|
|
128
|
my $english_file_path = File::Spec->catfile( @dirs, $english_file ); |
306
|
12
|
|
|
|
|
89
|
my $jelly_file_path = File::Spec->catfile( @dirs, $jelly_file ); |
307
|
|
|
|
|
|
|
|
308
|
12
|
100
|
|
|
|
48
|
if ( $self->{source_dir} eq $self->{target_dir} ) { |
309
|
8
|
|
|
|
|
76
|
return ( File::Spec->catfile( @dirs, $curr_lang_file ), |
310
|
|
|
|
|
|
|
$english_file_path, $jelly_file_path ); |
311
|
|
|
|
|
|
|
} |
312
|
|
|
|
|
|
|
|
313
|
4
|
100
|
|
|
|
10
|
if ( $dirs ne '' ) { |
314
|
3
|
|
|
|
|
56
|
$dirs =~ s/$self->{source_dir}/$self->{target_dir}/; |
315
|
3
|
|
|
|
|
8
|
@dirs = (); |
316
|
3
|
50
|
|
|
|
8
|
push( @dirs, $volume ) unless ( $volume eq '' ); |
317
|
3
|
|
|
|
|
7
|
push( @dirs, $dirs ); |
318
|
|
|
|
|
|
|
} |
319
|
|
|
|
|
|
|
|
320
|
4
|
|
|
|
|
37
|
return ( File::Spec->catfile( @dirs, $curr_lang_file ), |
321
|
|
|
|
|
|
|
$english_file_path, $jelly_file_path ); |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
} |
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
1; |
326
|
|
|
|
|
|
|
__END__ |