App/Command/pull.pm

filter: Statementstmt 27.1% 19 / 70 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 60.0% 6 / 10 Podpod 75.0% 3 / 4 totaltotal 23.7% 28 / 118 CC 16 SCAR 41.4
CC
16
Cov
19%
execute
132.0
validate_args
20.0
validate_mode
12.0
CRAP 63.0
« App/Command/push.pm ↑ Summary App/DocumentExportApi.pm »
1 # ABSTRACT: pull translation files from Smartcat
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Smartcat::App::Command::pull;
6 2 ▶use Smartcat::App -command;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Smartcat::App::Constants qw(COMPLETE);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Smartcat::App::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 0 ▶sub opt_spec {
Subroutine opt_spec: not called CC=1 SCAR=6.9
Pod documented
13 0 my ($self) = @_;
14  
15 0 my @opts = $self->SUPER::opt_spec();
16  
17 0 push @opts,
18 [ 'complete-documents' => 'Pull "complete" documents only' ],
19 [ 'complete-projects' => 'Pull "complete" projects only' ],
20 [ 'skip-missing' => 'Do not create (skip) missing files' ],
21 [ 'mode:s' => 'Unit export mode, available values: current, confirmed, complete; default value: current',
22 { default => "current" }
23 ],
24 $self->project_id_opt_spec,
25 $self->project_workdir_opt_spec,
26 $self->file_params_opt_spec,
27 ;
28  
29 0 return @opts;
30 }
31  
32 0 ▶sub validate_args {
Subroutine validate_args: not called CC=4 SCAR=30.0
Pod documented
33 0 my ( $self, $opt, $args ) = @_;
34  
35 0 $self->SUPER::validate_args( $opt, $args );
36 0 $self->validate_project_id( $opt, $args );
37 0 $self->validate_project_workdir( $opt, $args );
38 0 $self->validate_file_params( $opt, $args );
39 0 $self->validate_mode( $opt, $args );
40  
41 $self->app->{rundata}->{complete_projects} =
42 0 ▶ defined $opt->{complete_projects} ? $opt->{complete_projects} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $opt->{'complete_projects'} ? : 0 0 0
43 $self->app->{rundata}->{complete_documents} =
44 0 ▶ defined $opt->{complete_documents} ? $opt->{complete_documents} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $opt->{'complete_documents'} ? : 0 0 0
45 $self->app->{rundata}->{skip_missing} =
46 0 ▶ defined $opt->{skip_missing} ? $opt->{skip_missing} : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $opt->{'skip_missing'} ? : 0 0 0
47 }
48  
49 0 ▶sub validate_mode {
Subroutine validate_mode: not called CC=3 SCAR=24.8
Pod undocumented
50 0 my ( $self, $opt, $args ) = @_;
51 0 my $rundata = $self->app->{rundata};
52 $self->usage_error("Incorrect 'mode' value, 'current', 'confirmed', 'complete' are possible")
53 0 ▶ if defined $opt->{mode} && $opt->{mode} !~ /current|confirmed|complete/;
Branch: 1 branch
BranchTrueFalseTotal
if defined $opt->{"mode"} and not $opt->{"mode"} =~ /current|confirmed|complete/ 0 0 0
Condition0%0/3
defined $opt->{"mode"} and not $opt->{"mode"} =~ /current|confirmed|complete/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $opt->{"mode"} and not $opt->{"mode"} =~ /current|confirmed|complete/
defined $opt->{"mode"} not $opt->{"mode"} =~ /current|confirmed|complete/
Truth table0%0/3
A and not(B)
A= defined $opt->{"mode"}B= not $opt->{"mode"} =~ /current|confirmed|complete/
ABresult
0X0
100
111
54 0 $rundata->{mode} = $opt->{mode};
55 }
56  
57 0 ▶sub execute {
Subroutine execute: not called CC=11 SCAR=48.8
Pod documented
58 0 my ( $self, $opt, $args ) = @_;
59  
60 0 my $rundata = $self->app->{rundata};
61 $log->info(
62 sprintf(
63 "Running 'pull' command for project '%s' and translation files from '%s'...",
64 $rundata->{project_id},
65 $rundata->{project_workdir}
66 )
67 0 );
68  
69 0 my $project = $self->app->project_api->get_project;
70 0 ▶ exit 1 unless $project;
Branch: 1 branch
BranchTrueFalseTotal
unless $project 0 0 0
71  
72 0 ▶ unless ( !$rundata->{complete_projects} || $project->status eq COMPLETE ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (not $rundata->{"complete_projects"} or $project->status eq "completed") 0 0 0
Condition0%0/3
not $rundata->{"complete_projects"} or $project->status eq "completed"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $rundata->{"complete_projects"} or $project->status eq "completed"
not $rundata->{"complete_projects"} $project->status eq "completed"
Truth table0%0/3
not(A) or B
A= not $rundata->{"complete_projects"}B= $project->status eq "completed"
ABresult
1X1
011
000
73 0 $log->warn(
74 sprintf(
75 "Skip: project '%s' [%s] is not complete (status = %s)",
76 $project->name, $project->id, $project->status
77 )
78 );
79 0 exit 0;
80 }
81  
82 0 my $documents;
83 0 ▶ if ( $rundata->{complete_documents} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rundata->{'complete_documents'}) { } 0 0 0
84 0 $documents = [];
85 0 for ( @{ $project->documents } ) {
86 0 ▶ if ( $_->status eq COMPLETE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->status eq 'completed') { } 0 0 0
87 0 push @$documents, $_;
88 }
89 else {
90 0 $log->info(
91 sprintf(
92 "Skip: document '%s(%s)' [%s] is not complete (status = %s)",
93 $_->name, $_->target_language, $_->id, $_->status
94 )
95 );
96 }
97 }
98 $log->info(
99 0 sprintf(
100 "Found %d complete documents on the server",
101 scalar(@$documents)
102 )
103 );
104 }
105 else {
106 0 $documents = $project->documents;
107 0 $log->info(
108 sprintf(
109 "Found %d documents on the server",
110 scalar(@$documents)
111 )
112 );
113 }
114  
115 0 ▶ if ( $rundata->{skip_missing} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rundata->{"skip_missing"}) 0 0 0
116 my @existing_documents = grep {
117 0 my $filepath = get_file_path(
118 $rundata->{project_workdir},
119 $_->target_language,
120 0 $_->full_path, $rundata->{filetype});
121  
122 0 my $exists = -e $filepath;
123  
124 0 ▶ if ($rundata->{debug}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rundata->{"debug"}) 0 0 0
125 0 ▶ my $status = $exists ? 'exists' : 'does not exist';
Branch: 1 branch
BranchTrueFalseTotal
$exists ? : 0 0 0
126 0 $log->info(
127 sprintf("%s (%s)", $filepath, $status)
128 );
129 }
130  
131 # return the status
132 0 $exists;
133 } @$documents;
134 0 $documents = \@existing_documents;
135 }
136  
137 0 my $count = scalar(@$documents);
138 0 ▶ if ($count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count == 0) { } 0 0 0
139 0 $log->warn('List of documents to download is empty; nothing to do.');
140 } else {
141 0 $log->info(
142 sprintf(
143 "Will download %d documents",
144 $count
145 )
146 );
147 0 $self->app->document_export_api->export_files($documents);
148 }
149  
150 $log->info(
151 sprintf(
152 "Finished 'pull' command for project '%s' and translation files from '%s'.",
153 $rundata->{project_id},
154 $rundata->{project_workdir}
155 )
156 0 );
157 }
158  
159 2 1;
« App/Command/push.pm ↑ Summary App/DocumentExportApi.pm »