File Coverage

lib/Serge/Sync/Plugin/TranslationService/zanata.pm
Criterion Covered Total %
statement 75 81 92.5
branch 30 36 83.3
condition 15 21 71.4
subroutine 11 11 100.0
pod 0 7 0.0
total 131 156 83.9


line stmt bran cond sub pod time code
1             # ABSTRACT: Zanata (http://zanata.org/) synchronization plugin for Serge
2              
3             package Serge::Sync::Plugin::TranslationService::zanata;
4              
5 1     1   20049 use parent Serge::Sync::Plugin::Base::TranslationService, Serge::Interface::SysCmdRunner;
  1         2  
  1         9  
6              
7 1     1   1880 use strict;
  1         3  
  1         32  
8              
9 1     1   6 use Serge::Util qw(subst_macros);
  1         2  
  1         83  
10 1     1   468 use version;
  1         2042  
  1         7  
11              
12             our $VERSION = qv('0.905.9');
13              
14             sub name {
15 11     11 0 176196 return 'Zanata translation server (http://zanata.org/) synchronization plugin';
16             }
17              
18             sub init {
19 11     11 0 128 my $self = shift;
20              
21 11         56 $self->SUPER::init(@_);
22              
23 11         74 $self->{optimizations} = 1;
24              
25 11         124 $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 11     11 0 20038 my ($self) = @_;
55              
56 11         52 $self->SUPER::validate_data;
57              
58 11         6019 $self->{data}->{project_config} = subst_macros($self->{data}->{project_config});
59 11         364 $self->{data}->{user_config} = subst_macros($self->{data}->{user_config});
60 11         300 $self->{data}->{push_type} = subst_macros($self->{data}->{push_type});
61 11         286 $self->{data}->{cache_dir} = subst_macros($self->{data}->{cache_dir});
62 11         283 $self->{data}->{use_cache} = subst_macros($self->{data}->{use_cache});
63 11         287 $self->{data}->{purge_cache} = subst_macros($self->{data}->{purge_cache});
64 11         290 $self->{data}->{file_types} = subst_macros($self->{data}->{file_types});
65 11         289 $self->{data}->{debug} = subst_macros($self->{data}->{debug});
66 11         301 $self->{data}->{errors} = subst_macros($self->{data}->{errors});
67 11         288 $self->{data}->{disable_ssl_cert} = subst_macros($self->{data}->{disable_ssl_cert});
68 11         332 $self->{data}->{dry_run} = subst_macros($self->{data}->{dry_run});
69              
70 11 100       298 die "'project_config' not defined" unless defined $self->{data}->{project_config};
71 10 100       93 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 9 100       298 if (defined $self->{data}->{cache_dir}) {
74 5 100       54 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 7 100       113 if (defined $self->{data}->{user_config}) {
78 2 100       19 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 6 100       84 if (!(defined $self->{data}->{push_type})) {
82 2         22 $self->{data}->{push_type} = 'both';
83             }
84              
85 6 100 100     61 if (($self->{data}->{push_type} ne 'both') and ($self->{data}->{push_type} ne 'source')) {
86 1         17 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 10     10 0 31 my ($self, $action, $langs, $capture) = @_;
92              
93 10         27 my $command = '--batch-mode '.$action;
94              
95 10         35 $command .= ' --project-config '.$self->{data}->{project_config};
96              
97 10 100       84 if (defined $self->{data}->{user_config}) {
98 2         16 $command .= ' --user-config '.$self->{data}->{user_config};
99             }
100              
101 10 100       121 if ($langs) {
102 2         9 my @locales = map {$self->get_zanata_locale($_)} @$langs;
  4         12  
103              
104 2         7 my $locales_as_string = join(',', @locales);
105              
106 2         7 $command .= ' --locales '.$locales_as_string;
107             }
108              
109 10 50 66     35 if (defined $self->{data}->{debug} && $self->{data}->{debug}) {
110 0         0 $command .= ' --debug';
111             }
112              
113 10 50 66     105 if (defined $self->{data}->{errors} && $self->{data}->{errors}) {
114 0         0 $command .= ' --errors';
115             }
116              
117 10 50 66     93 if (defined $self->{data}->{disable_ssl_cert} && $self->{data}->{disable_ssl_cert}) {
118 0         0 $command .= ' --disable-ssl-cert';
119             }
120              
121 10 50 66     99 if (defined $self->{data}->{dry_run} && $self->{data}->{dry_run}) {
122 0         0 $command .= ' --dry-run';
123             }
124              
125 10         91 $command = 'zanata-cli '.$command;
126              
127 10         402 print "Running '$command'...\n";
128 10         71 return $self->run_cmd($command, $capture);
129             }
130              
131             sub get_zanata_locale {
132 4     4 0 11 my ($self, $lang) = @_;
133              
134 4         19 $lang =~ s/-(\w+)$/'-'.uc($1)/e; # convert e.g. 'pt-br' to 'pt-BR'
  2         12  
135              
136 4         16 return $lang;
137             }
138              
139             sub pull_ts {
140 5     5 0 1409 my ($self, $langs) = @_;
141              
142 5         17 my $action = 'pull';
143              
144 5 100       29 if (defined $self->{data}->{cache_dir}) {
145 3         31 $action .= ' --cache-dir '.$self->{data}->{cache_dir};
146             }
147              
148 5 50 66     53 if (defined $self->{data}->{use_cache} && $self->{data}->{use_cache}) {
149 0         0 $action .= ' --use-cache';
150             }
151              
152 5 50 66     57 if (defined $self->{data}->{purge_cache} && $self->{data}->{purge_cache}) {
153 0         0 $action .= ' --purge-cache';
154             }
155              
156 5         61 return $self->run_zanata_cli($action, $langs);
157             }
158              
159             sub push_ts {
160 5     5 0 3212 my ($self, $langs) = @_;
161              
162 5         31 my $action = "push --push-type $self->{data}->{push_type}";
163              
164 5 100       53 if (defined $self->{data}->{file_types}) {
165 1         9 my $file_types = $self->{data}->{file_types};
166              
167 1         10 my $joined_file_types = join(',', @$file_types);
168              
169 1         5 $action .= ' --file-types '.$joined_file_types;
170             }
171              
172 5         43 $self->run_zanata_cli($action, $langs);
173             }
174              
175             1;