line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Smartcat::App::Utils; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
725
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
108
|
|
4
|
3
|
|
|
3
|
|
18
|
use warnings; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
83
|
|
5
|
|
|
|
|
|
|
|
6
|
3
|
|
|
3
|
|
18
|
use File::Basename; |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
225
|
|
7
|
3
|
|
|
3
|
|
17
|
use File::Spec::Functions qw(catfile splitpath splitdir); |
|
3
|
|
|
|
|
4
|
|
|
3
|
|
|
|
|
170
|
|
8
|
|
|
|
|
|
|
|
9
|
3
|
|
|
|
|
4108
|
use Smartcat::App::Constants qw( |
10
|
|
|
|
|
|
|
PATH_SEPARATOR |
11
|
3
|
|
|
3
|
|
1040
|
); |
|
3
|
|
|
|
|
6
|
|
12
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our @EXPORT = qw( |
15
|
|
|
|
|
|
|
prepare_document_name |
16
|
|
|
|
|
|
|
prepare_file_name |
17
|
|
|
|
|
|
|
save_file |
18
|
|
|
|
|
|
|
get_language_from_ts_filepath |
19
|
|
|
|
|
|
|
get_ts_file_key |
20
|
|
|
|
|
|
|
get_document_key |
21
|
|
|
|
|
|
|
get_file_id |
22
|
|
|
|
|
|
|
get_file_name |
23
|
|
|
|
|
|
|
format_error_message |
24
|
|
|
|
|
|
|
get_file_path |
25
|
|
|
|
|
|
|
are_po_files_empty |
26
|
|
|
|
|
|
|
); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub _get_path_items { |
29
|
9
|
|
|
9
|
|
16
|
my ($project_workdir, $path) = @_; |
30
|
|
|
|
|
|
|
|
31
|
9
|
|
|
|
|
22
|
my ($project_workdir_volume, $project_workdir_dirs, $project_workdir_name) = splitpath($project_workdir); |
32
|
9
|
|
|
|
|
131
|
my ($volume, $dirs, $name) = splitpath($path); |
33
|
|
|
|
|
|
|
|
34
|
9
|
|
|
|
|
122
|
my @project_workdir_dirs = grep {$_ ne ""} splitdir($project_workdir_dirs); |
|
32
|
|
|
|
|
92
|
|
35
|
9
|
100
|
|
|
|
36
|
push @project_workdir_dirs, $project_workdir_name if $project_workdir_name ne ""; |
36
|
|
|
|
|
|
|
|
37
|
9
|
|
|
|
|
16
|
my @result = grep {$_ ne ""} splitdir($dirs); |
|
59
|
|
|
|
|
117
|
|
38
|
9
|
|
|
|
|
21
|
foreach (@project_workdir_dirs) { |
39
|
30
|
100
|
|
|
|
55
|
shift @result if $_ eq $result[0]; |
40
|
|
|
|
|
|
|
} |
41
|
9
|
|
|
|
|
12
|
push @result, $name; |
42
|
|
|
|
|
|
|
|
43
|
9
|
|
|
|
|
32
|
return @result; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub get_language_from_ts_filepath { |
47
|
2
|
|
|
2
|
0
|
580
|
my ($project_workdir, $path) = @_; |
48
|
|
|
|
|
|
|
|
49
|
2
|
|
|
|
|
5
|
my @path_items = _get_path_items($project_workdir, $path); |
50
|
|
|
|
|
|
|
|
51
|
2
|
|
|
|
|
10
|
return shift @path_items; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub get_ts_file_key { |
55
|
5
|
|
|
5
|
0
|
12
|
my ($project_workdir, $path, $should_extract_file_id) = @_; |
56
|
|
|
|
|
|
|
|
57
|
5
|
|
|
|
|
9
|
my @path_items = _get_path_items($project_workdir, $path); |
58
|
|
|
|
|
|
|
|
59
|
5
|
|
|
|
|
8
|
my $language = shift @path_items; |
60
|
5
|
|
|
|
|
12
|
my $filepath = join(PATH_SEPARATOR, @path_items); |
61
|
|
|
|
|
|
|
|
62
|
5
|
100
|
|
|
|
9
|
if ( $should_extract_file_id ) { |
63
|
3
|
|
|
|
|
6
|
my ( $volume, $directories, $filename ) = splitpath( $filepath ); |
64
|
3
|
100
|
|
|
|
35
|
if ($filename =~ /^(.+)---([^\.].+?)$/) { |
65
|
2
|
|
|
|
|
7
|
$filepath = $volume.$directories.$2; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
} |
68
|
|
|
|
|
|
|
|
69
|
5
|
|
|
|
|
24
|
return "$filepath ($language)"; |
70
|
|
|
|
|
|
|
} |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
sub get_document_key { |
73
|
6
|
|
|
6
|
0
|
15
|
my ( $full_path, $target_language, $should_extract_file_id ) = @_; |
74
|
6
|
|
|
|
|
7
|
my $key = $full_path; |
75
|
6
|
|
|
|
|
58
|
$key =~ s/_($target_language)$//i; |
76
|
|
|
|
|
|
|
|
77
|
6
|
100
|
|
|
|
15
|
if ( $should_extract_file_id ) { |
78
|
4
|
|
|
|
|
12
|
my ( $volume, $directories, $filename ) = splitpath( $key ); |
79
|
4
|
50
|
|
|
|
65
|
if ($filename =~ /^(.+)---([^\.].+?)$/) { |
80
|
4
|
|
|
|
|
12
|
$key = $volume.$directories.$2; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
6
|
|
|
|
|
29
|
return $key . ' (' . $target_language . ')'; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub get_file_id { |
88
|
5
|
|
|
5
|
0
|
11
|
my ( $filepath ) = @_; |
89
|
|
|
|
|
|
|
|
90
|
5
|
|
|
|
|
12
|
my ($volume, $directories, $name) = splitpath($filepath); |
91
|
|
|
|
|
|
|
|
92
|
5
|
100
|
|
|
|
93
|
if ($name =~ /^(.+)---([^\.].+?)(\..+)?$/) { |
93
|
4
|
|
|
|
|
18
|
return $2; |
94
|
|
|
|
|
|
|
} |
95
|
1
|
|
|
|
|
3
|
return undef; |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
sub get_file_name { |
99
|
0
|
|
|
0
|
0
|
0
|
my ( $filepath, $filetype, $target_language ) = @_; |
100
|
|
|
|
|
|
|
|
101
|
0
|
|
|
|
|
0
|
my ( $filename, $dirs, $ext ) = fileparse( $filepath, $filetype ); |
102
|
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
0
|
return $filename . '_' . $target_language; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
sub prepare_document_name { |
107
|
2
|
|
|
2
|
0
|
5
|
my ( $project_workdir, $path, $filetype, $target_language ) = @_; |
108
|
|
|
|
|
|
|
|
109
|
2
|
|
|
|
|
5
|
$path = join(PATH_SEPARATOR, _get_path_items($project_workdir, $path)); |
110
|
2
|
|
|
|
|
87
|
my ( $filename, $dirs, $ext ) = fileparse( $path, $filetype ); |
111
|
2
|
|
|
|
|
8
|
my @path_items = grep { $_ ne '' } splitdir($dirs); |
|
8
|
|
|
|
|
22
|
|
112
|
2
|
|
|
|
|
4
|
shift @path_items; |
113
|
2
|
|
|
|
|
3
|
push @path_items, $filename; |
114
|
2
|
|
|
|
|
5
|
my $filepath = join(PATH_SEPARATOR, @path_items); |
115
|
|
|
|
|
|
|
|
116
|
2
|
|
|
|
|
10
|
return $filepath . '_' . $target_language . $ext; |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub prepare_file_name { |
121
|
1
|
|
|
1
|
0
|
3
|
my ( $document_name, $document_target_language, $ext ) = @_; |
122
|
|
|
|
|
|
|
|
123
|
1
|
|
|
|
|
15
|
my $regexp = qr/_$document_target_language/; |
124
|
1
|
|
|
|
|
19
|
$document_name =~ s/(.*)$regexp/$1/; |
125
|
|
|
|
|
|
|
|
126
|
1
|
|
|
|
|
8
|
return $document_name . $ext; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
sub get_file_path { |
131
|
0
|
|
|
0
|
0
|
0
|
my ( $project_workdir, $document_target_language, $document_name, $ext ) = @_; |
132
|
0
|
|
|
|
|
0
|
my $filename = |
133
|
|
|
|
|
|
|
prepare_file_name( $document_name, $document_target_language, $ext ); |
134
|
|
|
|
|
|
|
|
135
|
0
|
|
|
|
|
0
|
return catfile( $project_workdir, $document_target_language, $filename ); |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub format_error_message { |
140
|
0
|
|
|
0
|
0
|
0
|
my $s = shift; |
141
|
|
|
|
|
|
|
|
142
|
0
|
|
|
|
|
0
|
$s = " " . $s; |
143
|
0
|
|
|
|
|
0
|
$s =~ s/\\r//; |
144
|
0
|
|
|
|
|
0
|
$s =~ s/\\n/\n/; |
145
|
0
|
|
|
|
|
0
|
$s =~ s/\n/\n /; |
146
|
|
|
|
|
|
|
|
147
|
0
|
|
|
|
|
0
|
return $s; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
sub save_file { |
152
|
0
|
|
|
0
|
0
|
0
|
my ( $filepath, $content ) = @_; |
153
|
0
|
0
|
|
|
|
0
|
open( my $fh, '>', $filepath ) or die "Could not open file '$filepath' $!"; |
154
|
0
|
|
|
|
|
0
|
binmode($fh); |
155
|
0
|
|
|
|
|
0
|
print $fh $content; |
156
|
0
|
|
|
|
|
0
|
close $fh; |
157
|
|
|
|
|
|
|
} |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
sub are_po_files_empty { |
161
|
3
|
|
|
3
|
0
|
8
|
my $filepaths = shift; |
162
|
3
|
|
|
|
|
4
|
my $empty = 1; |
163
|
|
|
|
|
|
|
|
164
|
3
|
|
|
|
|
7
|
for my $filepath (@$filepaths) { |
165
|
3
|
50
|
|
|
|
140
|
open(my $fh, $filepath) or die "Can't read $filepath: $!\n"; |
166
|
3
|
|
|
|
|
22
|
binmode($fh, ':utf8'); |
167
|
3
|
|
|
|
|
1865
|
my $text = join('', <$fh>); |
168
|
3
|
|
|
|
|
48
|
close $fh; |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
# join multi-line entries |
171
|
3
|
|
|
|
|
117
|
$text =~ s/"\r?\n"//sg; |
172
|
|
|
|
|
|
|
|
173
|
3
|
100
|
|
|
|
31
|
if ($text =~ m/msgid "[^"]/s) { |
174
|
1
|
|
|
|
|
2
|
$empty = undef; |
175
|
1
|
|
|
|
|
5
|
last; |
176
|
|
|
|
|
|
|
} |
177
|
|
|
|
|
|
|
} |
178
|
3
|
|
|
|
|
17
|
return $empty; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
1; |