| 1 | package Serge::Sync::Plugin::TranslationService::command; | ||||||||||||
| 2 | 1 | ▶ | use parent Serge::Sync::Plugin::Base::TranslationService, Serge::Interface::SysCmdRunner; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | |||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 1 | ▶ | use Serge::Util qw(subst_macros); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | sub name { | ||||||||||||
| 9 | 0 | ▶ | return 'Universal command-based synchronization plugin'; | ||||||||||
|
Subroutine
name: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 10 | } | ||||||||||||
| 11 | |||||||||||||
| 12 | sub init { | ||||||||||||
| 13 | 0 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 14 | |||||||||||||
| 15 | 0 | $self->SUPER::init(@_); | |||||||||||
| 16 | |||||||||||||
| 17 | # this flag will be set to undef by Command/sync.pm | ||||||||||||
| 18 | # when `--force` flag is passed down to `serge pull-ts` or `serge push-ts` | ||||||||||||
| 19 | 0 | $self->{optimizations} = 1; | |||||||||||
| 20 | |||||||||||||
| 21 | # TODO: support global `--dry-run` flag | ||||||||||||
| 22 | 0 | $self->{dry_run} = undef; | |||||||||||
| 23 | |||||||||||||
| 24 | 0 | $self->merge_schema({ | |||||||||||
| 25 | executable => 'STRING', | ||||||||||||
| 26 | force_pull_command => 'STRING', | ||||||||||||
| 27 | force_push_command => 'STRING', | ||||||||||||
| 28 | pull_command => 'STRING', | ||||||||||||
| 29 | push_command => 'STRING', | ||||||||||||
| 30 | lang_format => 'STRING', | ||||||||||||
| 31 | lang_separator => 'STRING', | ||||||||||||
| 32 | }); | ||||||||||||
| 33 | |||||||||||||
| 34 | # ------------------------------------------------- | ||||||||||||
| 35 | # Pootle defaults: | ||||||||||||
| 36 | # ------------------------------------------------- | ||||||||||||
| 37 | # # extra parameter: `project` (required) | ||||||||||||
| 38 | |||||||||||||
| 39 | # executable => 'pootle' # or '/path/to/manage_py' | ||||||||||||
| 40 | # force_pull_command => '%EXECUTABLE% sync_stores --overwrite --project=%PROJECT% --skip-missing %LANGUAGES%' | ||||||||||||
| 41 | # force_push_command => '%EXECUTABLE% update_stores --force --project=%PROJECT% %LANGUAGES%' | ||||||||||||
| 42 | # pull_command => '%EXECUTABLE% sync_stores --project=%PROJECT% --skip-missing %LANGUAGES%' | ||||||||||||
| 43 | # push_command => '%EXECUTABLE% update_stores --project=%PROJECT% %LANGUAGES%' | ||||||||||||
| 44 | # lang_format => '--language=%LOCALE%' | ||||||||||||
| 45 | # lang_separator => ' ' | ||||||||||||
| 46 | |||||||||||||
| 47 | # ------------------------------------------------- | ||||||||||||
| 48 | # Zing defaults: | ||||||||||||
| 49 | # ------------------------------------------------- | ||||||||||||
| 50 | # # extra parameter: `project` (required) | ||||||||||||
| 51 | |||||||||||||
| 52 | # executable => 'zing' | ||||||||||||
| 53 | # force_pull_command => '%EXECUTABLE% sync_stores --overwrite --project=%PROJECT% --skip-missing %LANGUAGES%' | ||||||||||||
| 54 | # force_push_command => '%EXECUTABLE% update_stores --force --project=%PROJECT% %LANGUAGES%' | ||||||||||||
| 55 | # pull_command => '%EXECUTABLE% sync_stores --project=%PROJECT% --skip-missing %LANGUAGES%' | ||||||||||||
| 56 | # push_command => '%EXECUTABLE% update_stores --project=%PROJECT% %LANGUAGES%' | ||||||||||||
| 57 | # lang_format => '--language=%LOCALE%' | ||||||||||||
| 58 | # lang_separator => ' ' | ||||||||||||
| 59 | |||||||||||||
| 60 | # ------------------------------------------------- | ||||||||||||
| 61 | # Abstract SergeCAT defaults: | ||||||||||||
| 62 | # ------------------------------------------------- | ||||||||||||
| 63 | # # extra parameter: `project` (required) | ||||||||||||
| 64 | |||||||||||||
| 65 | # my $common_params = '--project=%PROJECT% %LANGUAGES%'; | ||||||||||||
| 66 | |||||||||||||
| 67 | # executable => 'serge-cat', | ||||||||||||
| 68 | # force_pull_command => "%EXECUTABLE% pull --force $common_params", | ||||||||||||
| 69 | # force_push_command => "%EXECUTABLE% push --force $common_params", | ||||||||||||
| 70 | # pull_command => "%EXECUTABLE% pull $common_params", | ||||||||||||
| 71 | # push_command => "%EXECUTABLE% push $common_params", | ||||||||||||
| 72 | |||||||||||||
| 73 | # lang_format => '--language=%LOCALE%', | ||||||||||||
| 74 | # lang_separator => ' ', | ||||||||||||
| 75 | |||||||||||||
| 76 | # ------------------------------------------------- | ||||||||||||
| 77 | # Smartcat defaults: | ||||||||||||
| 78 | # ------------------------------------------------- | ||||||||||||
| 79 | # my $common_params = '--token_id=%TOKEN_ID% '. | ||||||||||||
| 80 | # '--project_id=%PROJECT_ID% '. | ||||||||||||
| 81 | # '--workdir=%WORKDIR% '. | ||||||||||||
| 82 | # '--token=%TOKEN% '. | ||||||||||||
| 83 | # '--log=%LOGFILE% '. | ||||||||||||
| 84 | # '%LANGUAGES%' | ||||||||||||
| 85 | |||||||||||||
| 86 | # executable => 'smartcat-cli' | ||||||||||||
| 87 | |||||||||||||
| 88 | # pull_command => '%EXECUTABLE% pull '. | ||||||||||||
| 89 | # $common_params | ||||||||||||
| 90 | |||||||||||||
| 91 | # push_command => '%EXECUTABLE% push '. | ||||||||||||
| 92 | # '--disassemble-algorithm-name="Serge.io PO" '. | ||||||||||||
| 93 | # $common_params | ||||||||||||
| 94 | |||||||||||||
| 95 | # lang_format => '--language=%LOCALE%' | ||||||||||||
| 96 | # lang_separator => ' ' | ||||||||||||
| 97 | } | ||||||||||||
| 98 | |||||||||||||
| 99 | sub validate_data { | ||||||||||||
| 100 | 0 | ▶ | my ($self) = @_; | ||||||||||
|
Subroutine
validate_data: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||
| 101 | |||||||||||||
| 102 | 0 | $self->SUPER::validate_data; | |||||||||||
| 103 | |||||||||||||
| 104 | 0 | $self->{data}->{executable} = subst_macros($self->{data}->{executable}); | |||||||||||
| 105 | 0 | $self->{data}->{force_pull_command} = subst_macros($self->{data}->{force_pull_command}); | |||||||||||
| 106 | 0 | $self->{data}->{force_push_command} = subst_macros($self->{data}->{force_push_command}); | |||||||||||
| 107 | 0 | $self->{data}->{pull_command} = subst_macros($self->{data}->{pull_command}); | |||||||||||
| 108 | 0 | $self->{data}->{push_command} = subst_macros($self->{data}->{push_command}); | |||||||||||
| 109 | #$self->{data}->{lang_format} = "%LANG%" unless defined $self->{data}->{lang_format}; | ||||||||||||
| 110 | #$self->{data}->{lang_separator} = "," unless defined $self->{data}->{lang_separator}; | ||||||||||||
| 111 | |||||||||||||
| 112 | 0 | ▶ | die "'executable' not defined" if $self->{data}->{executable} eq ''; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 113 | 0 | ▶ | die "'pull_command' not defined" if $self->{data}->{pull_command} eq ''; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 114 | 0 | ▶ | die "'push_command' not defined" if $self->{data}->{push_command} eq ''; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 115 | #die "'lang_format' not defined" if $self->{data}->{lang_format} eq ''; | ||||||||||||
| 116 | #die "'pull_command' doesn't have the %LANGUAGES% macro" unless $self->{data}->{pull_command} ~= m/%LANGUAGES%/; | ||||||||||||
| 117 | #die "'push_command' doesn't have the %LANGUAGES% macro" unless $self->{data}->{push_command} ~= m/%LANGUAGES%/; | ||||||||||||
| 118 | |||||||||||||
| 119 | 0 | ▶ | $self->{data}->{force_pull_command} = $self->{data}->{pull_command} unless defined $self->{data}->{force_pull_command}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 120 | 0 | ▶ | $self->{data}->{force_push_command} = $self->{data}->{push_command} unless defined $self->{data}->{force_push_command}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 121 | } | ||||||||||||
| 122 | |||||||||||||
| 123 | sub run_command { | ||||||||||||
| 124 | 0 | ▶ | my ($self, $command, $langs) = @_; | ||||||||||
|
Subroutine
run_command: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||
| 125 | |||||||||||||
| 126 | 0 | my @out_langs; | |||||||||||
| 127 | 0 | ▶ | if ($langs) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 128 | 0 | foreach my $lang (sort @$langs) { | |||||||||||
| 129 | # format lang according to lang_format | ||||||||||||
| 130 | #FIXME: ???: push @out_langs, subst_macros($self->{data}->{lang_format}, $lang???); | ||||||||||||
| 131 | } | ||||||||||||
| 132 | } | ||||||||||||
| 133 | |||||||||||||
| 134 | # format the command according to command_format | ||||||||||||
| 135 | 0 | ▶ | my $params = $self->{data}->{params} || {}; | ||||||||||
|
Condition0%0/2
$self->{'data'}{'params'} || {}
MC/DC0%0/1
$self->{'data'}{'params'} || {}
$self->{'data'}{'params'}
Truth table0%0/2
A
A= $self->{'data'}{'params'}
| |||||||||||||
| 136 | 0 | $params->{EXECUTABLE} = $self->{data}->{executable}; | |||||||||||
| 137 | 0 | $params->{LANGUAGES} = join($self->{data}->{lang_separator}, @out_langs); | |||||||||||
| 138 | |||||||||||||
| 139 | # TODO: pass extra params to subst_macros() | ||||||||||||
| 140 | #FIXME: ???: $command = subst_macros($command, $params???); | ||||||||||||
| 141 | |||||||||||||
| 142 | 0 | ▶ | if ($self->{dry_run}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 143 | 0 | print "DRY_RUN: $command\n"; | |||||||||||
| 144 | 0 | return; | |||||||||||
| 145 | } | ||||||||||||
| 146 | 0 | print "Running '$command'...\n"; | |||||||||||
| 147 | 0 | $self->run_cmd($command); | |||||||||||
| 148 | } | ||||||||||||
| 149 | |||||||||||||
| 150 | sub pull_ts { | ||||||||||||
| 151 | 0 | ▶ | my ($self, $langs) = @_; | ||||||||||
|
Subroutine
pull_ts: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 152 | |||||||||||||
| 153 | 0 | $self->run_command($self->{data}->{pull_command}, $langs); | |||||||||||
| 154 | } | ||||||||||||
| 155 | |||||||||||||
| 156 | sub push_ts { | ||||||||||||
| 157 | 0 | ▶ | my ($self, $langs) = @_; | ||||||||||
|
Subroutine
push_ts: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 158 | |||||||||||||
| 159 | 0 | $self->run_command($self->{data}->{push_command}, $langs); | |||||||||||
| 160 | } | ||||||||||||
| 161 | |||||||||||||
| 162 | 1 | 1; | |||||||||||