| 1 | # ABSTRACT: Zanata (http://zanata.org/) synchronization plugin for Serge | ||
| 2 | |||
| 3 | package Serge::Sync::Plugin::TranslationService::zanata; | ||
| 4 | |||
| 5 | 1 | ▶ | use parent Serge::Sync::Plugin::Base::TranslationService, Serge::Interface::SysCmdRunner; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | use strict; | ||
| 8 | |||
| 9 | use Serge::Util qw(subst_macros); | ||
| 10 | use version; | ||
| 11 | |||
| 12 | our $VERSION = qv('0.907.1'); | ||
| 13 | |||
| 14 | sub name { | ||
| 15 | return 'Zanata translation server (http://zanata.org/) synchronization plugin'; | ||
| 16 | } | ||
| 17 | |||
| 18 | sub init { | ||
| 19 | my $self = shift; | ||
| 20 | |||
| 21 | $self->SUPER::init(@_); | ||
| 22 | |||
| 23 | $self->{optimizations} = 1; | ||
| 24 | |||
| 25 | $self->merge_schema({ | ||
| 26 | # Project configuration file, eg zanata.xml | ||
| 27 | project_config => 'STRING', | ||
| 28 | # User configuration, eg /home/user/.config/zanata.ini | ||
| 29 | user_config => 'STRING', | ||
| 30 | # Type of push to perform on the server: | ||
| 31 | # 'source' pushes source documents only | ||
| 32 | # 'both' (default) pushes both source and translation documents | ||
| 33 | push_type => 'STRING', | ||
| 34 | # The base directory for storing zanata cache files. Default is current directory. | ||
| 35 | cache_dir => 'STRING', | ||
| 36 | # Whether to use an Entity cache when fetching documents. | ||
| 37 | use_cache => 'BOOLEAN', | ||
| 38 | # Whether to purge the cache before performing the pull operation | ||
| 39 | purge_cache => 'BOOLEAN', | ||
| 40 | # File types to locate and transmit to the server when using project type 'file' | ||
| 41 | file_types => 'ARRAY', | ||
| 42 | # Whether to enable debug logging. Default is false. | ||
| 43 | debug => 'BOOLEAN', | ||
| 44 | # Whether to output full execution error messages (stacktraces). Default is false. | ||
| 45 | errors => 'BOOLEAN', | ||
| 46 | # Whether verification of SSL certificates should be disabled. Default is false. | ||
| 47 | disable_ssl_cert => 'BOOLEAN', | ||
| 48 | # Dry run: don't change any data, on the server or on the filesystem. Default is false. | ||
| 49 | dry_run => 'BOOLEAN' | ||
| 50 | }); | ||
| 51 | } | ||
| 52 | |||
| 53 | sub validate_data { | ||
| 54 | my ($self) = @_; | ||
| 55 | |||
| 56 | $self->SUPER::validate_data; | ||
| 57 | |||
| 58 | $self->{data}->{project_config} = subst_macros($self->{data}->{project_config}); | ||
| 59 | $self->{data}->{user_config} = subst_macros($self->{data}->{user_config}); | ||
| 60 | $self->{data}->{push_type} = subst_macros($self->{data}->{push_type}); | ||
| 61 | $self->{data}->{cache_dir} = subst_macros($self->{data}->{cache_dir}); | ||
| 62 | $self->{data}->{use_cache} = subst_macros($self->{data}->{use_cache}); | ||
| 63 | $self->{data}->{purge_cache} = subst_macros($self->{data}->{purge_cache}); | ||
| 64 | $self->{data}->{file_types} = subst_macros($self->{data}->{file_types}); | ||
| 65 | $self->{data}->{debug} = subst_macros($self->{data}->{debug}); | ||
| 66 | $self->{data}->{errors} = subst_macros($self->{data}->{errors}); | ||
| 67 | $self->{data}->{disable_ssl_cert} = subst_macros($self->{data}->{disable_ssl_cert}); | ||
| 68 | $self->{data}->{dry_run} = subst_macros($self->{data}->{dry_run}); | ||
| 69 | |||
| 70 | die "'project_config' not defined" unless defined $self->{data}->{project_config}; | ||
| 71 | die "'project_config', which is set to '$self->{data}->{project_config}', does not point to a valid file.\n" unless -f $self->{data}->{project_config}; | ||
| 72 | |||
| 73 | if (defined $self->{data}->{cache_dir}) { | ||
| 74 | die "'cache_dir', which is set to '$self->{data}->{cache_dir}', does not point to a valid dir.\n" unless -d $self->{data}->{cache_dir}; | ||
| 75 | } | ||
| 76 | |||
| 77 | if (defined $self->{data}->{user_config}) { | ||
| 78 | die "'user_config', which is set to '$self->{data}->{user_config}', does not point to a valid file.\n" unless -f $self->{data}->{user_config}; | ||
| 79 | } | ||
| 80 | |||
| 81 | if (!(defined $self->{data}->{push_type})) { | ||
| 82 | $self->{data}->{push_type} = 'both'; | ||
| 83 | } | ||
| 84 | |||
| 85 | if (($self->{data}->{push_type} ne 'both') and ($self->{data}->{push_type} ne 'source')) { | ||
| 86 | die "'push_type', which is set to $self->{data}->{push_type}, is not one of the valid options: 'source' or 'both'"; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | sub run_zanata_cli { | ||
| 91 | my ($self, $action, $langs, $capture) = @_; | ||
| 92 | |||
| 93 | my $command = '--batch-mode '.$action; | ||
| 94 | |||
| 95 | $command .= ' --project-config '.$self->{data}->{project_config}; | ||
| 96 | |||
| 97 | if (defined $self->{data}->{user_config}) { | ||
| 98 | $command .= ' --user-config '.$self->{data}->{user_config}; | ||
| 99 | } | ||
| 100 | |||
| 101 | if ($langs) { | ||
| 102 | my @locales = map {$self->get_zanata_locale($_)} @$langs; | ||
| 103 | |||
| 104 | my $locales_as_string = join(',', @locales); | ||
| 105 | |||
| 106 | $command .= ' --locales '.$locales_as_string; | ||
| 107 | } | ||
| 108 | |||
| 109 | if (defined $self->{data}->{debug} && $self->{data}->{debug}) { | ||
| 110 | $command .= ' --debug'; | ||
| 111 | } | ||
| 112 | |||
| 113 | if (defined $self->{data}->{errors} && $self->{data}->{errors}) { | ||
| 114 | $command .= ' --errors'; | ||
| 115 | } | ||
| 116 | |||
| 117 | if (defined $self->{data}->{disable_ssl_cert} && $self->{data}->{disable_ssl_cert}) { | ||
| 118 | $command .= ' --disable-ssl-cert'; | ||
| 119 | } | ||
| 120 | |||
| 121 | if (defined $self->{data}->{dry_run} && $self->{data}->{dry_run}) { | ||
| 122 | $command .= ' --dry-run'; | ||
| 123 | } | ||
| 124 | |||
| 125 | $command = 'zanata-cli '.$command; | ||
| 126 | |||
| 127 | print "Running '$command'...\n"; | ||
| 128 | return $self->run_cmd($command, $capture); | ||
| 129 | } | ||
| 130 | |||
| 131 | sub get_zanata_locale { | ||
| 132 | my ($self, $lang) = @_; | ||
| 133 | |||
| 134 | $lang =~ s/-(\w+)$/'-'.uc($1)/e; # convert e.g. 'pt-br' to 'pt-BR' | ||
| 135 | |||
| 136 | return $lang; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub pull_ts { | ||
| 140 | my ($self, $langs) = @_; | ||
| 141 | |||
| 142 | my $action = 'pull'; | ||
| 143 | |||
| 144 | if (defined $self->{data}->{cache_dir}) { | ||
| 145 | $action .= ' --cache-dir '.$self->{data}->{cache_dir}; | ||
| 146 | } | ||
| 147 | |||
| 148 | if (defined $self->{data}->{use_cache} && $self->{data}->{use_cache}) { | ||
| 149 | $action .= ' --use-cache'; | ||
| 150 | } | ||
| 151 | |||
| 152 | if (defined $self->{data}->{purge_cache} && $self->{data}->{purge_cache}) { | ||
| 153 | $action .= ' --purge-cache'; | ||
| 154 | } | ||
| 155 | |||
| 156 | return $self->run_zanata_cli($action, $langs); | ||
| 157 | } | ||
| 158 | |||
| 159 | sub push_ts { | ||
| 160 | my ($self, $langs) = @_; | ||
| 161 | |||
| 162 | my $action = "push --push-type $self->{data}->{push_type}"; | ||
| 163 | |||
| 164 | if (defined $self->{data}->{file_types}) { | ||
| 165 | my $file_types = $self->{data}->{file_types}; | ||
| 166 | |||
| 167 | my $joined_file_types = join(',', @$file_types); | ||
| 168 | |||
| 169 | $action .= ' --file-types '.$joined_file_types; | ||
| 170 | } | ||
| 171 | |||
| 172 | $self->run_zanata_cli($action, $langs); | ||
| 173 | } | ||
| 174 | |||
| 175 | 1; | ||