| 1 | # ABSTRACT: PhraseApp (https://phraseapp.com) synchronization plugin for Serge | ||
| 2 | |||
| 3 | package Serge::Sync::Plugin::TranslationService::phraseapp; | ||
| 4 | 1 | ▶ | use parent Serge::Sync::Plugin::Base::TranslationService, Serge::Interface::SysCmdRunner; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | use strict; | ||
| 7 | |||
| 8 | use Serge::Util qw(subst_macros); | ||
| 9 | use version; | ||
| 10 | |||
| 11 | our $VERSION = qv('0.902.1'); | ||
| 12 | |||
| 13 | sub name { | ||
| 14 | return 'PhraseApp translation software (https://phraseapp.com) synchronization plugin'; | ||
| 15 | } | ||
| 16 | |||
| 17 | sub init { | ||
| 18 | my $self = shift; | ||
| 19 | |||
| 20 | $self->SUPER::init(@_); | ||
| 21 | |||
| 22 | $self->{optimizations} = 1; # set to undef to disable optimizations | ||
| 23 | |||
| 24 | $self->merge_schema({ | ||
| 25 | config_file => 'STRING', | ||
| 26 | wait_for_uploads => 'BOOLEAN', | ||
| 27 | verbose => 'BOOLEAN' | ||
| 28 | }); | ||
| 29 | } | ||
| 30 | |||
| 31 | sub validate_data { | ||
| 32 | my ($self) = @_; | ||
| 33 | |||
| 34 | $self->SUPER::validate_data; | ||
| 35 | |||
| 36 | $self->{data}->{config_file} = subst_macros($self->{data}->{config_file}); | ||
| 37 | $self->{data}->{wait_for_uploads} = subst_macros($self->{data}->{wait_for_uploads}); | ||
| 38 | $self->{data}->{verbose} = subst_macros($self->{data}->{verbose}); | ||
| 39 | |||
| 40 | die "'config_file' not defined" unless defined $self->{data}->{config_file}; | ||
| 41 | die "'config_file', which is set to '$self->{data}->{config_file}', does not point to a valid file.\n" unless -f $self->{data}->{config_file}; | ||
| 42 | |||
| 43 | $self->{data}->{wait_for_uploads} = 1 unless defined $self->{data}->{wait_for_uploads}; | ||
| 44 | $self->{data}->{verbose} = 0 unless defined $self->{data}->{verbose}; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub run_phraseapp_cli { | ||
| 48 | my ($self, $action, $langs, $capture) = @_; | ||
| 49 | |||
| 50 | local $ENV{'PHRASEAPP_CONFIG'} = $self->{data}->{config_file}; | ||
| 51 | |||
| 52 | my $command = $action; | ||
| 53 | |||
| 54 | $command = 'phraseapp '.$command; | ||
| 55 | |||
| 56 | if ($self->{data}->{verbose}) { | ||
| 57 | $command .= ' --verbose '; | ||
| 58 | } | ||
| 59 | |||
| 60 | print "Running '$command'...\n"; | ||
| 61 | return $self->run_cmd($command, $capture); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub pull_ts { | ||
| 65 | my ($self, $langs) = @_; | ||
| 66 | |||
| 67 | return $self->run_phraseapp_cli('pull', $langs); | ||
| 68 | } | ||
| 69 | |||
| 70 | sub push_ts { | ||
| 71 | my ($self, $langs) = @_; | ||
| 72 | |||
| 73 | my $action = 'push'; | ||
| 74 | |||
| 75 | if ($self->{data}->{wait_for_uploads}) { | ||
| 76 | $action = $action.' --wait'; | ||
| 77 | } | ||
| 78 | |||
| 79 | $self->run_phraseapp_cli($action, $langs); | ||
| 80 | } | ||
| 81 | |||
| 82 | 1; | ||