| 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 |  | 23917 | use parent Serge::Sync::Plugin::Base::TranslationService, Serge::Interface::SysCmdRunner; | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 8 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 1 |  |  | 1 |  | 1869 | use strict; | 
|  | 1 |  |  |  |  | 4 |  | 
|  | 1 |  |  |  |  | 33 |  | 
| 8 |  |  |  |  |  |  |  | 
| 9 | 1 |  |  | 1 |  | 5 | use Serge::Util qw(subst_macros culture_from_lang locale_from_lang); | 
|  | 1 |  |  |  |  | 3 |  | 
|  | 1 |  |  |  |  | 82 |  | 
| 10 | 1 |  |  | 1 |  | 463 | use version; | 
|  | 1 |  |  |  |  | 1979 |  | 
|  | 1 |  |  |  |  | 6 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | our $VERSION = qv('0.906.2'); | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | sub name { | 
| 15 | 12 |  |  | 12 | 0 | 209448 | return 'Mojito translation server (http://www.mojito.global/) synchronization plugin'; | 
| 16 |  |  |  |  |  |  | } | 
| 17 |  |  |  |  |  |  |  | 
| 18 |  |  |  |  |  |  | sub init { | 
| 19 | 12 |  |  | 12 | 0 | 151 | my $self = shift; | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 12 |  |  |  |  | 64 | $self->SUPER::init(@_); | 
| 22 |  |  |  |  |  |  |  | 
| 23 | 12 |  |  |  |  | 86 | $self->{optimizations} = 1; | 
| 24 |  |  |  |  |  |  |  | 
| 25 | 12 |  |  |  |  | 155 | $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 | 21977 | my ($self) = @_; | 
| 42 |  |  |  |  |  |  |  | 
| 43 | 12 |  |  |  |  | 56 | $self->SUPER::validate_data; | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 12 |  |  |  |  | 9273 | $self->{data}->{application_properties} = subst_macros($self->{data}->{application_properties}); | 
| 46 | 12 |  |  |  |  | 416 | $self->{data}->{repository_name} = subst_macros($self->{data}->{repository_name}); | 
| 47 | 12 |  |  |  |  | 321 | $self->{data}->{source_files_path} = subst_macros($self->{data}->{source_files_path}); | 
| 48 | 12 |  |  |  |  | 347 | $self->{data}->{localized_files_path} = subst_macros($self->{data}->{localized_files_path}); | 
| 49 | 12 |  |  |  |  | 319 | $self->{data}->{import_translations} = subst_macros($self->{data}->{import_translations}); | 
| 50 | 12 |  |  |  |  | 389 | $self->{data}->{source_locale} = subst_macros($self->{data}->{source_locale}); | 
| 51 | 12 |  |  |  |  | 362 | $self->{data}->{inheritance_mode} = subst_macros($self->{data}->{inheritance_mode}); | 
| 52 | 12 |  |  |  |  | 332 | $self->{data}->{file_type} = subst_macros($self->{data}->{file_type}); | 
| 53 | 12 |  |  |  |  | 323 | $self->{data}->{status_equal_target} = subst_macros($self->{data}->{status_equal_target}); | 
| 54 | 12 |  |  |  |  | 344 | $self->{data}->{status_pull} = subst_macros($self->{data}->{status_pull}); | 
| 55 | 12 |  |  |  |  | 334 | $self->{data}->{destination_locales} = subst_macros($self->{data}->{destination_locales}); | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 12 | 100 |  |  |  | 327 | die "'repository_name' not defined" unless defined $self->{data}->{repository_name}; | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 11 | 100 |  |  |  | 104 | if ($self->{data}->{application_properties} ne '') { | 
| 60 | 10 | 100 |  |  |  | 87 | 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 |  |  |  | 322 | die "'source_files_path' not defined" unless defined $self->{data}->{source_files_path}; | 
| 64 | 9 | 100 |  |  |  | 89 | 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 |  |  |  | 179 | die "'localized_files_path' not defined" unless defined $self->{data}->{localized_files_path}; | 
| 67 | 6 | 100 |  |  |  | 58 | 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 |  |  |  | 115 | $self->{data}->{import_translations} = 1 unless defined $self->{data}->{import_translations}; | 
| 70 | 5 | 100 |  |  |  | 91 | $self->{data}->{inheritance_mode} = 'REMOVE_UNTRANSLATED' unless defined $self->{data}->{inheritance_mode}; | 
| 71 | 5 | 100 |  |  |  | 73 | $self->{data}->{status_pull} = 'ACCEPTED' unless defined $self->{data}->{status_pull}; | 
| 72 |  |  |  |  |  |  |  | 
| 73 | 5 | 100 | 66 |  |  | 71 | if (!defined $self->{data}->{destination_locales} or scalar(@{$self->{data}->{destination_locales}}) == 0) { | 
|  | 4 |  |  |  |  | 38 |  | 
| 74 | 1 |  |  |  |  | 18 | die "the list of destination languages is empty"; | 
| 75 |  |  |  |  |  |  | } | 
| 76 |  |  |  |  |  |  | } | 
| 77 |  |  |  |  |  |  |  | 
| 78 |  |  |  |  |  |  | sub run_mojito_cli { | 
| 79 | 11 |  |  | 11 | 0 | 31 | my ($self, $action, $langs, $capture) = @_; | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 11 |  |  |  |  | 25 | my $command = $action; | 
| 82 |  |  |  |  |  |  |  | 
| 83 | 11 |  |  |  |  | 44 | $command .= ' -r '.$self->{data}->{repository_name}; | 
| 84 | 11 |  |  |  |  | 95 | $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 |  |  |  | 86 | if (defined $self->{data}->{source_locale}) { | 
| 90 | 3 |  |  |  |  | 25 | $command .= ' -sl '.$self->{data}->{source_locale}; | 
| 91 |  |  |  |  |  |  | } | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 11 | 100 |  |  |  | 85 | if ($langs) { | 
| 94 | 7 |  |  |  |  | 19 | my @locale_mapping = map {$self->get_mojito_locale_mapping($_)} @$langs; | 
|  | 18 |  |  |  |  | 41 |  | 
| 95 |  |  |  |  |  |  |  | 
| 96 | 7 |  |  |  |  | 24 | my $locale_mapping_as_string = join(',', @locale_mapping); | 
| 97 |  |  |  |  |  |  |  | 
| 98 | 7 |  |  |  |  | 23 | $command .= ' --locale-mapping '.$locale_mapping_as_string; | 
| 99 |  |  |  |  |  |  | } | 
| 100 |  |  |  |  |  |  |  | 
| 101 | 11 | 100 |  |  |  | 41 | if (defined $self->{data}->{file_type}) { | 
| 102 | 5 |  |  |  |  | 42 | $command .= ' -ft '.$self->{data}->{file_type}; | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  |  | 
| 105 | 11 |  |  |  |  | 86 | $command = 'mojito '.$command; | 
| 106 | 11 |  |  |  |  | 428 | print "Running '$command'...\n"; | 
| 107 |  |  |  |  |  |  |  | 
| 108 | 11 |  |  |  |  | 74 | return $self->run_cmd($command, $capture); | 
| 109 |  |  |  |  |  |  | } | 
| 110 |  |  |  |  |  |  |  | 
| 111 |  |  |  |  |  |  | sub get_mojito_locale_mapping { | 
| 112 | 18 |  |  | 18 | 0 | 40 | my ($self, $lang) = @_; | 
| 113 |  |  |  |  |  |  |  | 
| 114 | 18 |  |  |  |  | 43 | my $locale = locale_from_lang($lang); | 
| 115 |  |  |  |  |  |  |  | 
| 116 | 18 |  |  |  |  | 221 | my $bcp47_locale = culture_from_lang($lang); | 
| 117 |  |  |  |  |  |  |  | 
| 118 | 18 |  |  |  |  | 583 | return $locale.':'.$bcp47_locale; | 
| 119 |  |  |  |  |  |  | } | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | sub pull_ts { | 
| 122 | 4 |  |  | 4 | 0 | 1126 | my ($self, $langs) = @_; | 
| 123 |  |  |  |  |  |  |  | 
| 124 | 4 |  |  |  |  | 24 | my $action = 'pull --inheritance-mode '.$self->{data}->{inheritance_mode}.' -t '.$self->{data}->{localized_files_path}; | 
| 125 | 4 |  |  |  |  | 62 | $action .= ' --status '.$self->{data}->{status_pull}; | 
| 126 |  |  |  |  |  |  |  | 
| 127 | 4 |  |  |  |  | 37 | return $self->run_mojito_cli($action, $self->get_langs($langs)); | 
| 128 |  |  |  |  |  |  | } | 
| 129 |  |  |  |  |  |  |  | 
| 130 |  |  |  |  |  |  | sub push_ts { | 
| 131 | 4 |  |  | 4 | 0 | 2548 | my ($self, $langs) = @_; | 
| 132 |  |  |  |  |  |  |  | 
| 133 | 4 |  |  |  |  | 15 | my $cli_return = $self->run_mojito_cli('push', ()); | 
| 134 |  |  |  |  |  |  |  | 
| 135 | 4 | 50 |  |  |  | 224 | if ($cli_return != 0) { | 
| 136 | 0 |  |  |  |  | 0 | return $cli_return; | 
| 137 |  |  |  |  |  |  | } | 
| 138 |  |  |  |  |  |  |  | 
| 139 | 4 | 100 |  |  |  | 22 | if ($self->{data}->{import_translations}) { | 
| 140 | 3 |  |  |  |  | 28 | my $action = 'import -t '.$self->{data}->{localized_files_path}; | 
| 141 |  |  |  |  |  |  |  | 
| 142 | 3 | 100 |  |  |  | 27 | if ($self->{data}->{status_equal_target} ne '') { | 
| 143 | 1 |  |  |  |  | 11 | $action .= ' --status-equal-target '.$self->{data}->{status_equal_target}; | 
| 144 |  |  |  |  |  |  | } | 
| 145 |  |  |  |  |  |  |  | 
| 146 | 3 |  |  |  |  | 30 | $cli_return = $self->run_mojito_cli($action, $self->get_langs($langs)); | 
| 147 |  |  |  |  |  |  | } | 
| 148 |  |  |  |  |  |  |  | 
| 149 | 4 |  |  |  |  | 176 | return $cli_return; | 
| 150 |  |  |  |  |  |  | } | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | sub get_langs { | 
| 153 | 7 |  |  | 7 | 0 | 19 | my ($self, $langs) = @_; | 
| 154 |  |  |  |  |  |  |  | 
| 155 | 7 | 100 |  |  |  | 22 | if (!$langs) { | 
| 156 | 5 |  |  |  |  | 19 | $langs = $self->{data}->{destination_locales}; | 
| 157 |  |  |  |  |  |  | } | 
| 158 |  |  |  |  |  |  |  | 
| 159 | 7 |  |  |  |  | 44 | return $langs; | 
| 160 |  |  |  |  |  |  | } | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  | 1; |