File Coverage

lib/Serge/Sync/Plugin/TranslationService/lingohub.pm
Criterion Covered Total %
statement 76 78 97.4
branch 17 20 85.0
condition n/a
subroutine 15 15 100.0
pod 0 8 0.0
total 108 121 89.2


line stmt bran cond sub pod time code
1             # ABSTRACT: Lingohub (https://www.lingohub.com) synchronization plugin for Serge
2              
3             package Serge::Sync::Plugin::TranslationService::lingohub;
4 1     1   17629 use parent Serge::Sync::Plugin::Base::TranslationService, Serge::Interface::SysCmdRunner;
  1         2  
  1         8  
5              
6 1     1   1776 use strict;
  1         3  
  1         32  
7              
8 1     1   8 use File::Find qw(find);
  1         2  
  1         50  
9 1     1   6 use File::Spec::Functions qw(catfile abs2rel);
  1         2  
  1         42  
10 1     1   6 use Serge::Util qw(subst_macros);
  1         3  
  1         74  
11              
12 1     1   475 use version;
  1         1998  
  1         6  
13              
14             our $VERSION = qv('0.903.0');
15              
16             sub name {
17 7     7 0 102415 return 'Lingohub translation software (https://www.lingohub.com) synchronization plugin';
18             }
19              
20             sub init {
21 7     7 0 79 my $self = shift;
22              
23 7         37 $self->SUPER::init(@_);
24              
25 7         47 $self->{optimizations} = 1; # set to undef to disable optimizations
26              
27 7         49 $self->merge_schema({
28             project => 'STRING',
29             root_directory => 'STRING',
30             resource_directory => 'STRING',
31             source_language => 'STRING',
32             target_languages => 'ARRAY'
33             });
34             }
35              
36             sub validate_data {
37 7     7 0 7283 my ($self) = @_;
38              
39 7         29 $self->SUPER::validate_data;
40              
41 7         3739 $self->{data}->{project} = subst_macros($self->{data}->{project});
42 7         231 $self->{data}->{root_directory} = subst_macros($self->{data}->{root_directory});
43 7         191 $self->{data}->{resource_directory} = subst_macros($self->{data}->{resource_directory});
44 7         184 $self->{data}->{source_language} = subst_macros($self->{data}->{source_language});
45 7         209 $self->{data}->{target_languages} = subst_macros($self->{data}->{target_languages});
46              
47 7 100       193 die "'project' not defined" unless defined $self->{data}->{project};
48 6 50       73 die "'resource_directory' not defined" unless defined $self->{data}->{resource_directory};
49 6 100       50 die "'root_directory', which is set to '$self->{data}->{root_directory}', does not point to a valid folder." unless -d $self->{data}->{root_directory};
50              
51 5         134 my $target_languages_count = 0;
52              
53 5 100       28 if (defined $self->{data}->{target_languages}) {
54 4         44 $target_languages_count = scalar(@{$self->{data}->{target_languages}});
  4         13  
55             }
56              
57 5 100       56 die "the list of target languages is empty" unless $target_languages_count != 0;
58              
59 4 100       12 $self->{data}->{source_language} = 'en' unless defined $self->{data}->{source_language};
60             }
61              
62             sub pull_ts {
63 4     4 0 1091 my ($self, $langs) = @_;
64              
65 4         19 my $langs_to_pull = $self->get_all_langs($langs);
66              
67 4         12 foreach my $lang (@$langs_to_pull) {
68 15         94 my $directory = catfile($self->{data}->{resource_directory}, $lang);
69 15         161 my $cli_return = $self->run_lingohub_cli("resource:down --locale '$lang' --directory $directory --all");
70              
71 15 50       61 if ($cli_return != 0) {
72 0         0 return $cli_return;
73             }
74             }
75              
76 4         16 return 0;
77             }
78              
79             sub push_ts {
80 4     4 0 2159 my ($self, $langs) = @_;
81              
82 4         14 my $langs_to_push = $self->get_all_langs($langs);
83              
84 4         13 foreach my $lang (@$langs_to_push) {
85 15         106 my $directory = catfile($self->{data}->{resource_directory}, $lang);
86              
87 15         164 my $lang_files_path = catfile($self->{data}->{root_directory}, $directory);
88 15         122 my @files = $self->find_lang_files($lang_files_path);
89              
90 15         45 foreach my $file (@files) {
91 3         17 my $resource = catfile($directory, $file);
92 3         18 my $cli_return = $self->run_lingohub_cli("resource:up $resource --locale '$lang'");
93              
94 3 50       16 if ($cli_return != 0) {
95 0         0 return $cli_return;
96             }
97             }
98             }
99              
100 4         17 return 0;
101             }
102              
103             sub run_lingohub_cli {
104 18     18 0 43 my ($self, $action, $capture) = @_;
105              
106 18         35 my $cli_return = 0;
107              
108 18         35 my $command = $action;
109              
110 18         42 $command = 'lingohub '.$command;
111 18         63 $command .= " --project '$self->{data}->{project}'";
112 18         615 print "Running '$command ...\n";
113              
114 18         147 $cli_return = $self->run_in($self->{data}->{root_directory}, $command, $capture);
115              
116 18         1905 return $cli_return;
117             }
118              
119             sub get_all_langs {
120 8     8 0 23 my ($self, $langs) = @_;
121              
122 8 100       27 if (!$langs) {
123 6         31 $langs = $self->{data}->{target_languages};
124             }
125              
126 8         112 my @all_langs = ($self->{data}->{source_language});
127              
128 8         70 push @all_langs, @$langs;
129              
130 8         22 return \@all_langs;
131             }
132              
133             sub find_lang_files {
134 15     15 0 36 my ($self, $directory) = @_;
135              
136 15         29 my @files = ();
137              
138             find(sub {
139 6 100   6   277 push @files, abs2rel($File::Find::name, $directory) if(-f $_);
140 15         2034 }, $directory);
141              
142 15         412 return @files;
143             }
144              
145             1;