File Coverage

lib/Smartcat/App/Utils.pm
Criterion Covered Total %
statement 77 94 81.9
branch 16 20 80.0
condition n/a
subroutine 13 17 76.4
pod 0 11 0.0
total 106 142 74.6


line stmt bran cond sub pod time code
1             package Smartcat::App::Utils;
2              
3 3     3   732 use strict;
  3         6  
  3         95  
4 3     3   15 use warnings;
  3         3  
  3         70  
5              
6 3     3   11 use File::Basename;
  3         5  
  3         180  
7 3     3   16 use File::Spec::Functions qw(catfile splitpath splitdir);
  3         4  
  3         173  
8              
9 3         3731 use Smartcat::App::Constants qw(
10             PATH_SEPARATOR
11 3     3   875 );
  3         5  
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   14 my ($project_workdir, $path) = @_;
30              
31 9         21 my ($project_workdir_volume, $project_workdir_dirs, $project_workdir_name) = splitpath($project_workdir);
32 9         113 my ($volume, $dirs, $name) = splitpath($path);
33              
34 9         64 my @project_workdir_dirs = grep {$_ ne ""} splitdir($project_workdir_dirs);
  32         76  
35 9 100       29 push @project_workdir_dirs, $project_workdir_name if $project_workdir_name ne "";
36              
37 9         16 my @result = grep {$_ ne ""} splitdir($dirs);
  59         91  
38 9         13 foreach (@project_workdir_dirs) {
39 30 100       48 shift @result if $_ eq $result[0];
40             }
41 9         9 push @result, $name;
42              
43 9         24 return @result;
44             }
45              
46             sub get_language_from_ts_filepath {
47 2     2 0 1925 my ($project_workdir, $path) = @_;
48              
49 2         5 my @path_items = _get_path_items($project_workdir, $path);
50              
51 2         8 return shift @path_items;
52             }
53              
54             sub get_ts_file_key {
55 5     5 0 11 my ($project_workdir, $path, $should_extract_file_id) = @_;
56              
57 5         10 my @path_items = _get_path_items($project_workdir, $path);
58              
59 5         7 my $language = shift @path_items;
60 5         11 my $filepath = join(PATH_SEPARATOR, @path_items);
61            
62 5 100       9 if ( $should_extract_file_id ) {
63 3         5 my ( $volume, $directories, $filename ) = splitpath( $filepath );
64 3 100       32 if ($filename =~ /^(.+)---([^\.].+?)$/) {
65 2         5 $filepath = $volume.$directories.$2;
66             }
67             }
68              
69 5         21 return "$filepath ($language)";
70             }
71              
72             sub get_document_key {
73 6     6 0 12 my ( $full_path, $target_language, $should_extract_file_id ) = @_;
74 6         7 my $key = $full_path;
75 6         51 $key =~ s/_($target_language)$//i;
76              
77 6 100       14 if ( $should_extract_file_id ) {
78 4         10 my ( $volume, $directories, $filename ) = splitpath( $key );
79 4 50       54 if ($filename =~ /^(.+)---([^\.].+?)$/) {
80 4         11 $key = $volume.$directories.$2;
81             }
82             }
83              
84 6         24 return $key . ' (' . $target_language . ')';
85             }
86              
87             sub get_file_id {
88 5     5 0 10 my ( $filepath ) = @_;
89            
90 5         10 my ($volume, $directories, $name) = splitpath($filepath);
91              
92 5 100       73 if ($name =~ /^(.+)---([^\.].+?)(\..+)?$/) {
93 4         18 return $2;
94             }
95 1         4 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 36 my ( $project_workdir, $path, $filetype, $target_language ) = @_;
108              
109 2         5 $path = join(PATH_SEPARATOR, _get_path_items($project_workdir, $path));
110 2         81 my ( $filename, $dirs, $ext ) = fileparse( $path, $filetype );
111 2         7 my @path_items = grep { $_ ne '' } splitdir($dirs);
  8         17  
112 2         3 shift @path_items;
113 2         4 push @path_items, $filename;
114 2         3 my $filepath = join(PATH_SEPARATOR, @path_items);
115              
116 2         9 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         13 my $regexp = qr/_$document_target_language/;
124 1         16 $document_name =~ s/(.*)$regexp/$1/;
125              
126 1         6 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 7 my $filepaths = shift;
162 3         5 my $empty = 1;
163              
164 3         4 for my $filepath (@$filepaths) {
165 3 50       640 open(my $fh, $filepath) or die "Can't read $filepath: $!\n";
166 3         23 binmode($fh, ':utf8');
167 3         6436 my $text = join('', <$fh>);
168 3         42 close $fh;
169              
170             # join multi-line entries
171 3         49 $text =~ s/"\r?\n"//sg;
172              
173 3 100       25 if ($text =~ m/msgid "[^"]/s) {
174 1         3 $empty = undef;
175 1         4 last;
176             }
177             }
178 3         17 return $empty;
179             }
180              
181              
182             1;