File Coverage

lib/Serge/Sync/Plugin/TranslationService/mojito.pm
Criterion Covered Total %
statement 82 83 98.8
branch 37 38 97.3
condition 2 3 66.6
subroutine 12 12 100.0
pod 0 8 0.0
total 133 144 92.3


line stmt bran cond sub pod time code
1             # ABSTRACT: Mojito (http://www.mojito.global/) synchronization plugin for Serge
2              
3             package Serge::Sync::Plugin::TranslationService::mojito;
4              
5 1     1   24682 use parent Serge::Sync::Plugin::Base::TranslationService, Serge::Interface::SysCmdRunner;
  1         3  
  1         8  
6              
7 1     1   1811 use strict;
  1         4  
  1         34  
8              
9 1     1   5 use Serge::Util qw(subst_macros culture_from_lang locale_from_lang);
  1         2  
  1         78  
10 1     1   448 use version;
  1         1920  
  1         6  
11              
12             our $VERSION = qv('0.906.0');
13              
14             sub name {
15 12     12 0 207145 return 'Mojito translation server (http://www.mojito.global/) synchronization plugin';
16             }
17              
18             sub init {
19 12     12 0 126 my $self = shift;
20              
21 12         49 $self->SUPER::init(@_);
22              
23 12         74 $self->{optimizations} = 1;
24              
25 12         111 $self->merge_schema({
26             repository_name => 'STRING',
27             application_properties => 'STRING',
28             source_files_path => 'STRING',
29             source_locale => 'STRING',
30             localized_files_path => 'STRING',
31             import_translations => 'BOOLEAN',
32             inheritance_mode => 'STRING',
33             file_type => 'STRING',
34             status_equal_target => 'STRING',
35             status_pull => 'STRING',
36             destination_locales => 'ARRAY'
37             });
38             }
39              
40             sub validate_data {
41 12     12 0 21606 my ($self) = @_;
42              
43 12         62 $self->SUPER::validate_data;
44              
45 12         9218 $self->{data}->{application_properties} = subst_macros($self->{data}->{application_properties});
46 12         383 $self->{data}->{repository_name} = subst_macros($self->{data}->{repository_name});
47 12         312 $self->{data}->{source_files_path} = subst_macros($self->{data}->{source_files_path});
48 12         309 $self->{data}->{localized_files_path} = subst_macros($self->{data}->{localized_files_path});
49 12         306 $self->{data}->{import_translations} = subst_macros($self->{data}->{import_translations});
50 12         345 $self->{data}->{source_locale} = subst_macros($self->{data}->{source_locale});
51 12         330 $self->{data}->{inheritance_mode} = subst_macros($self->{data}->{inheritance_mode});
52 12         350 $self->{data}->{file_type} = subst_macros($self->{data}->{file_type});
53 12         311 $self->{data}->{status_equal_target} = subst_macros($self->{data}->{status_equal_target});
54 12         319 $self->{data}->{status_pull} = subst_macros($self->{data}->{status_pull});
55 12         335 $self->{data}->{destination_locales} = subst_macros($self->{data}->{destination_locales});
56              
57 12 100       325 die "'repository_name' not defined" unless defined $self->{data}->{repository_name};
58              
59 11 100       94 if ($self->{data}->{application_properties} ne '') {
60 10 100       83 die "'application_properties', which is set to '$self->{data}->{application_properties}', does not point to a valid file.\n" unless -f $self->{data}->{application_properties};
61             }
62              
63 10 100       324 die "'source_files_path' not defined" unless defined $self->{data}->{source_files_path};
64 9 100       84 die "'source_files_path', which is set to '$self->{data}->{source_files_path}', does not point to a valid directory.\n" unless -d $self->{data}->{source_files_path};
65              
66 7 100       174 die "'localized_files_path' not defined" unless defined $self->{data}->{localized_files_path};
67 6 100       57 die "'localized_files_path', which is set to '$self->{data}->{localized_files_path}', does not point to a valid directory.\n" unless -d $self->{data}->{localized_files_path};
68              
69 5 100       132 $self->{data}->{import_translations} = 1 unless defined $self->{data}->{import_translations};
70 5 100       85 $self->{data}->{inheritance_mode} = 'REMOVE_UNTRANSLATED' unless defined $self->{data}->{inheritance_mode};
71 5 100       76 $self->{data}->{status_pull} = 'ACCEPTED' unless defined $self->{data}->{status_pull};
72              
73 5 100 66     73 if (!defined $self->{data}->{destination_locales} or scalar(@{$self->{data}->{destination_locales}}) == 0) {
  4         35  
74 1         15 die "the list of destination languages is empty";
75             }
76             }
77              
78             sub run_mojito_cli {
79 11     11 0 29 my ($self, $action, $langs, $capture) = @_;
80              
81 11         20 my $command = $action;
82              
83 11         44 $command .= ' -r '.$self->{data}->{repository_name};
84 11         89 $command .= ' -s '.$self->{data}->{source_files_path};
85 11 100       81 if ($self->{data}->{application_properties} ne '') {
86 8         65 $command .= ' --spring.config.location='.$self->{data}->{application_properties};
87             }
88              
89 11 100       87 if (defined $self->{data}->{source_locale}) {
90 3         24 $command .= ' -sl '.$self->{data}->{source_locale};
91             }
92              
93 11 100       83 if ($langs) {
94 7         19 my @locale_mapping = map {$self->get_mojito_locale_mapping($_)} @$langs;
  18         42  
95              
96 7         22 my $locale_mapping_as_string = join(',', @locale_mapping);
97              
98 7         24 $command .= ' --locale-mapping '.$locale_mapping_as_string;
99             }
100              
101 11 100       39 if (defined $self->{data}->{file_type}) {
102 5         40 $command .= ' -ft '.$self->{data}->{file_type};
103             }
104              
105 11         85 $command = 'mojito '.$command;
106 11         404 print "Running '$command'...\n";
107              
108 11         71 return $self->run_cmd($command, $capture);
109             }
110              
111             sub get_mojito_locale_mapping {
112 18     18 0 39 my ($self, $lang) = @_;
113              
114 18         45 my $locale = locale_from_lang($lang);
115              
116 18         274 my $bcp47_locale = culture_from_lang($lang);
117              
118 18         580 return $locale.':'.$bcp47_locale;
119             }
120              
121             sub pull_ts {
122 4     4 0 1063 my ($self, $langs) = @_;
123              
124 4         23 my $action = 'pull --inheritance-mode '.$self->{data}->{inheritance_mode}.' -t '.$self->{data}->{localized_files_path};
125 4         67 $action .= ' --status '.$self->{data}->{status_pull};
126              
127 4         38 return $self->run_mojito_cli($action, $self->get_langs($langs));
128             }
129              
130             sub push_ts {
131 4     4 0 2528 my ($self, $langs) = @_;
132              
133 4         12 my $cli_return = $self->run_mojito_cli('push', ());
134              
135 4 50       223 if ($cli_return != 0) {
136 0         0 return $cli_return;
137             }
138              
139 4 100       23 if ($self->{data}->{import_translations}) {
140 3         27 my $action = 'import -t '.$self->{data}->{localized_files_path};
141              
142 3 100       24 if ($self->{data}->{status_equal_target} ne '') {
143 1         10 $action .= ' --status-equal-target '.$self->{data}->{status_equal_target};
144             }
145              
146 3         26 $cli_return = $self->run_mojito_cli($action, $self->get_langs($langs));
147             }
148              
149 4         175 return $cli_return;
150             }
151              
152             sub get_langs {
153 7     7 0 17 my ($self, $langs) = @_;
154              
155 7 100       21 if (!$langs) {
156 5         15 $langs = $self->{data}->{destination_locales};
157             }
158              
159 7         42 return $langs;
160             }
161              
162             1;