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