lib/App/Dest.pm

filter: Statementstmt 95.9% 407 / 424 Branchbran 74.4% 131 / 176 Conditioncond 74.0% 57 / 77 MC/DCmcdc 47.6% 20 / 42 Subroutinesub 100% 50 / 50 Podpod 100% 22 / 22 totaltotal 86.8% 687 / 791 CC 152 SCAR 20.4
CC
152
Cov
88%
_build_execute_stack
21.1
update
12.0
__ANON__
10.3
CRAP 7.7
↑ Summary bin/dest »
1 package App::Dest;
2 # ABSTRACT: Deployment State Manager
3  
4 7 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 7 ▶use exact;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 7 ▶use File::Basename qw( dirname basename );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use File::Copy 'copy';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use File::Copy::Recursive qw( dircopy rcopy );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use File::DirCompare ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 7 ▶use File::Find 'find';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 7 ▶use File::Path qw( mkpath rmtree );
Subroutine BEGIN: called CC=1 SCAR=0.0
13 7 ▶use IPC::Run 'run';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use Path::Tiny 'path';
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use POSIX 'strftime';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use Text::Diff ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 7 our $VERSION = '1.37'; # VERSION
19  
20 5 ▶sub init {
Subroutine init: called CC=7 SCAR=20.5
Pod documented
21 5 my $self = _new( shift, 'expect_no_root_dir' );
22  
23 4 ▶ mkdir('.dest') or die "Unable to create .dest directory\n";
Branch: 1 branch
BranchTrueFalseTotal
mkdir ".dest" or die "Unable to create .dest directory\n" 4 0 4
24 4 ▶ open( my $watch, '>', '.dest/watch' ) or die "Unable to create ~/.dest/watch file\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $watch, ">", ".dest/watch" or die "Unable to create ~/.dest/watch file\n" 4 0 4
25  
26 4 $self = _new();
27  
28 4 ▶ if ( -f 'dest.watch' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f "dest.watch") 4 0 4
29 4 ▶ open( my $watches, '<', 'dest.watch' ) or die "Unable to read dest.watch file\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $watches, "<", "dest.watch" or die "Unable to read dest.watch file\n" 4 0 4
30  
31 4 my @watches = map { chomp; $_ } <$watches>;
32  
33 4 my @errors;
34 4 for my $watch (@watches) {
35 4 try {
36 4 $self->add($watch);
37 }
38 catch ($e) {
39 0 push( @errors, $watch . ': ' . $e );
40 }
41 }
42  
43 warn
44 "Created new watch list based on dest.watch file:\n" .
45 4 ▶ join( "\n", map { ' ' . $_ } @watches ) . "\n" .
Branch: 1 branch
BranchTrueFalseTotal
@errors ? : 0 4 4
46 (
47 (@errors)
48 0 ? "With the following errors:\n" . join( '', map { ' ' . $_ } @errors )
49 : ''
50 );
51 }
52  
53 4 return 0;
54 }
55  
56 8 ▶sub add {
Subroutine add: called CC=7 SCAR=19.5
Pod documented
57 8 my $self = _new(shift);
58 8 ▶ die "No directory specified; usage: dest add [directory]\n" unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 8 8
59  
60 8 my @watches = $self->_watch_list;
61 my @adds = map {
62 8 my $dir = $_;
63 8 ▶ die "Directory specified does not exist\n" unless ( -d $dir );
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 1 7 8
64  
65 7 my $rel_dir = $self->_rel2root($dir);
66 7 ▶ die "Directory $dir already added\n" if ( grep { $rel_dir eq $_ } @watches );
Branch: 1 branch
BranchTrueFalseTotal
if grep {$rel_dir eq $_;} @watches 1 6 7
67 6 $rel_dir;
68 } @_;
69  
70 6 ▶ open( my $watch, '>', $self->_rel2dir('.dest/watch') ) or die "Unable to write ~/.dest/watch file\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $watch, ">", $self->_rel2dir(".dest/watch") or die "Unable to write ~/.dest/watch file\n" 6 0 6
71 6 print $watch $_, "\n" for ( sort @adds, map { $self->_rel2root($_) } @watches );
72 6 mkpath("$self->{root_dir}/.dest/$_") for (@adds);
73  
74 6 return 0;
75 }
76  
77 1 ▶sub rm {
Subroutine rm: called CC=7 SCAR=20.1
Pod documented
78 1 my $self = _new(shift);
79 1 ▶ die "No directory specified; usage: dest rm [directory]\n" unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 1 1
80  
81 1 my @watches = $self->_watch_list;
82 my @rms = map {
83 1 my $dir = $_;
84 1 ▶ $dir //= '';
Condition50%1/2
$dir //= ""
l!l
10
MC/DC0%0/1
$dir //= ""
$dir
Truth table50%1/2
A
A= $dir
Aresult
11
00
85 1 $dir =~ s|/$||;
86  
87 1 ▶ die "Directory $dir not currently tracked\n" unless ( grep { $dir eq $_ } @watches );
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$dir eq $_;} @watches 0 1 1
88  
89 1 $self->_rel2root($dir);
90 } @_;
91  
92 1 ▶ open( my $watch_file, '>', $self->_rel2dir('.dest/watch') ) or die "Unable to write ~/.dest/watch file\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $watch_file, ">", $self->_rel2dir(".dest/watch") or die "Unable to write ~/.dest/watch file\n" 1 0 1
93 1 for my $watch_dir ( map { $self->_rel2root($_) } @watches ) {
94 1 ▶ if ( grep { $watch_dir eq $_ } @rms ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$watch_dir eq $_;} @rms) { } 1 0 1
95 1 rmtree( $self->_rel2dir(".dest/$watch_dir") );
96 }
97 else {
98 0 print $watch_file $watch_dir, "\n";
99 }
100 }
101  
102 1 return 0;
103 }
104  
105 3 ▶sub watches {
Subroutine watches: called CC=2 SCAR=6.9
Pod documented
106 3 my $self = _new(shift);
107  
108 3 my @watches = $self->_watch_list;
109 3 ▶ print join( "\n", @watches ), "\n" if @watches;
Branch: 1 branch
BranchTrueFalseTotal
if @watches 2 1 3
110  
111 3 return 0;
112 }
113  
114 1 ▶sub putwatch {
Subroutine putwatch: called CC=7 SCAR=19.7
Pod documented
115 1 my $self = _new(shift);
116 1 my ($file) = @_;
117 1 ▶ die "File specified does not exist\n" unless ( -f $file );
Branch: 1 branch
BranchTrueFalseTotal
unless -f $file 0 1 1
118  
119 1 ▶ open( my $new_watches, '<', $file ) or die "Unable to read specified file\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $new_watches, "<", $file or die "Unable to read specified file\n" 1 0 1
120  
121 1 my @new = map { chomp; $_ } <$new_watches>;
122 1 my @old = $self->_watch_list;
123  
124 1 for my $old (@old) {
125 1 ▶ next if ( grep { $_ eq $old } @new );
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $old;} @new 1 0 1
126 0 $self->rm($old);
127 }
128 1 for my $new (@new) {
129 2 ▶ next if ( grep { $_ eq $new } @old );
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq $new;} @old 1 1 2
130 1 $self->add($new);
131 }
132  
133 1 return 0;
134 }
135  
136 1 ▶sub writewatch {
Subroutine writewatch: called CC=2 SCAR=7.1
Pod documented
137 1 my $self = _new(shift);
138  
139 1 ▶ copy( $self->_rel2dir('.dest/watch'), $self->_rel2dir('dest.watch') ) or die "$!\n";
Branch: 1 branch
BranchTrueFalseTotal
copy($self->_rel2dir(".dest/watch"), $self->_rel2dir("dest.watch")) or die "$!\n" 1 0 1
140  
141 1 return 0;
142 }
143  
144 2 ▶sub make {
Subroutine make: called CC=6 SCAR=18.1
Pod documented
145 2 my ( $self, $path, $ext ) = @_;
146 2 ▶ die "No name specified; usage: dest make [path]\n" unless ($path);
Branch: 1 branch
BranchTrueFalseTotal
unless $path 0 2 2
147  
148 2 $path = strftime( $path, localtime );
149  
150 2 ▶ $ext = '.' . $ext if ( defined $ext );
Branch: 1 branch
BranchTrueFalseTotal
if defined $ext 1 1 2
151 2 ▶ $ext //= '';
Condition100%2/2
$ext //= ""
l!l
11
MC/DC100%1/1
$ext //= ""
$ext
Truth table100%2/2
A
A= $ext
Aresult
11
00
152  
153 2 try {
154 2 mkpath($path);
155 2 for ( qw( deploy verify revert ) ) {
156 6 ▶ open( my $file, '>', "$path/$_$ext" ) or die;
Branch: 1 branch
BranchTrueFalseTotal
open my $file, ">", "$path/$_$ext" or die 6 0 6
157 6 print $file "\n";
158 }
159 }
160 catch ($e) {
161 0 die "Failed to fully make $path; check permissions or existing files\n";
162 }
163  
164 2 $self->expand($path);
165  
166 2 return 0;
167 }
168  
169 2 ▶sub expand {
Subroutine expand: called CC=1 SCAR=0.0
Pod documented
170 2 my ( $self, $path ) = @_;
171  
172 2 print join( ' ', map { <"$path/$_*"> } qw( deploy verify revert ) ), "\n";
173  
174 2 return 0;
175 }
176  
177 2 ▶sub list {
Subroutine list: called CC=4 SCAR=13.9
Pod documented
178 2 my $self = _new(shift);
179 2 my ($filter) = @_;
180  
181 2 my $tree = $self->_actions_tree($filter);
182 2 for my $path ( sort keys %$tree ) {
183 2 ▶ print $path, ( ( @{ $tree->{$path} } ) ? ' actions:' : ' has no actions' ), "\n";
Branch: 1 branch
BranchTrueFalseTotal
@{$tree->{$path};} ? : 2 0 2
184 2 print ' ', $_, "\n" for ( @{ $tree->{$path} } );
185 }
186  
187 2 return 0;
188 }
189  
190 1 ▶sub prereqs {
Subroutine prereqs: called CC=4 SCAR=13.9
Pod documented
191 1 my $self = _new(shift);
192 1 my ($filter) = @_;
193  
194 1 for my $action ( @{ $self->_prereq_tree($filter)->{actions} } ) {
195 3 ▶ print $action->{action}, ( ( @{ $action->{prereqs} } ) ? ' prereqs:' : ' has no prereqs' ), "\n";
Branch: 1 branch
BranchTrueFalseTotal
@{$action->{'prereqs'};} ? : 1 2 3
196 3 print ' ', $_, "\n" for ( @{ $action->{prereqs} } );
197 }
198  
199 1 return 0;
200 }
201  
202 5 ▶sub status {
Subroutine status: called CC=7 SCAR=19.5
Pod documented
203 5 my $self = _new(shift);
204  
205 5 ▶ if ( -f $self->_rel2dir('dest.watch') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $self->_rel2dir("dest.watch")) 5 0 5
206 5 my $diff = Text::Diff::diff( $self->_rel2dir('.dest/watch'), $self->_rel2dir('dest.watch') );
207 5 ▶ warn "Diff between current watch list and dest.watch file:\n" . $diff . "\n" if ($diff);
Branch: 1 branch
BranchTrueFalseTotal
if $diff 0 5 5
208 }
209  
210 5 for my $path ( @{ $self->_status_data->{paths} } ) {
211 5 print "$path->{state} - $path->{path}\n";
212  
213 5 for my $action ( @{ $path->{actions} } ) {
214 12 ▶ unless ( $action->{modified} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($action->{'modified'}) { } 2 10 12
215 10 ▶ print ' ' . ( ( $action->{deployed} ) ? '-' : '+' ) . ' ' . $action->{action} . "\n";
Branch: 1 branch
BranchTrueFalseTotal
$action->{'deployed'} ? : 3 7 10
216 }
217 else {
218 2 print " $action->{action}\n";
219 2 print " M $action->{file}\n";
220 }
221 }
222 }
223  
224 5 return 0;
225 }
226  
227 2 ▶sub diff {
Subroutine diff: called CC=3 SCAR=11.1
Pod documented
228 2 my $self = _new(shift);
229 2 my ($path) = @_;
230  
231 2 ▶ if ( not defined $path ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $path) 1 1 2
232 1 $self->diff($_) for ( $self->_watch_list );
233 1 return 0;
234 }
235  
236 3 ▶ File::DirCompare->compare( $self->_rel2dir( '.dest/' . $self->_rel2root($path) ), $path, sub {
Subroutine __ANON__: called CC=7 SCAR=19.9
237 3 my ( $a, $b ) = @_;
238 3 ▶ $a ||= '';
Condition100%2/2
$a ||= ""
l!l
21
MC/DC100%1/1
$a ||= ""
$a
Truth table100%2/2
A
A= $a
Aresult
11
00
239 3 ▶ $b ||= '';
Condition100%2/2
$b ||= ""
l!l
21
MC/DC100%1/1
$b ||= ""
$b
Truth table100%2/2
A
A= $b
Aresult
11
00
240  
241 3 ▶ return if ( $a =~ /\/dest.wrap$/ or $b =~ /\/dest.wrap$/ or not -f $a or not -f $b );
Branch: 1 branch
BranchTrueFalseTotal
if $a =~ m[/dest.wrap$]u or $b =~ m[/dest.wrap$]u or not -f $a or not -f $b 2 1 3
Condition55%5/9
$a =~ m[/dest.wrap$]u or $b =~ m[/dest.wrap$]u or not -f $a or not -f $b
l!l&&r!l&&!r
201
$a =~ m[/dest.wrap$]u or $b =~ m[/dest.wrap$]u or not -f $a
l!l&&r!l&&!r
021
$a =~ m[/dest.wrap$]u or $b =~ m[/dest.wrap$]u
l!l&&r!l&&!r
003
MC/DC25%1/4
$a =~ m[/dest.wrap$]u or $b =~ m[/dest.wrap$]u or not -f $a or not -f $b
$a =~ m[/dest.wrap$]u $b =~ m[/dest.wrap$]u not -f $a not -f $b
Truth table40%2/5
A or B or not(C) or not(D)
A= $a =~ m[/dest.wrap$]uB= $b =~ m[/dest.wrap$]uC= not -f $aD= not -f $b
ABCDresult
1XXX1
01XX1
001X1
00011
00000
242 1 print Text::Diff::diff( $a, $b );
243 1 return;
244 1 } );
245  
246 1 return 0;
247 }
248  
249 2 ▶sub clean {
Subroutine clean: called CC=4 SCAR=13.9
Pod documented
250 2 my $self = _new(shift);
251  
252 2 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1 1 2
253 1 for (@_) {
254 1 my $dest = $self->_rel2dir(".dest/$_");
255 1 rmtree($dest);
256 1 rcopy( $self->_rel2dir($_), $dest );
257 }
258 }
259 else {
260 1 for ( map { $self->_rel2root($_) } $self->_watch_list ) {
261 1 my $dest = $self->_rel2dir(".dest/$_");
262 1 rmtree($dest);
263 1 dircopy( $self->_rel2dir($_), $dest );
264 }
265 }
266  
267 2 return 0;
268 }
269  
270 1 ▶sub preinstall {
Subroutine preinstall: called CC=4 SCAR=15.0
Pod documented
271 1 my $self = _new(shift);
272  
273 1 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 1 1
274 0 for (@_) {
275 0 my $dest = $self->_rel2dir(".dest/$_");
276 0 rmtree($dest);
277 }
278 }
279 else {
280 1 for ( map { $self->_rel2root($_) } $self->_watch_list ) {
281 1 my $dest = $self->_rel2dir(".dest/$_");
282 1 rmtree($dest);
283 1 mkdir($dest);
284 }
285 }
286  
287 1 return 0;
288 }
289  
290 1 ▶sub nuke {
Subroutine nuke: called CC=1 SCAR=0.0
Pod documented
291 1 my $self = _new(shift);
292 1 rmtree( $self->_rel2dir('.dest') );
293 1 return 0;
294 }
295  
296 5 ▶sub deploy {
Subroutine deploy: called CC=7 SCAR=19.6
Pod documented
297 5 my $self = _new(shift);
298 5 my ( $dry_run, $action ) = _dry_check(@_);
299 5 ▶ die "File to deploy required; usage: dest deploy file\n" unless ($action);
Branch: 1 branch
BranchTrueFalseTotal
unless $action 0 5 5
300  
301 5 my $redeploy = delete $self->{redeploy};
302 5 my $execute_stack = $self->_build_execute_stack( $action, 'deploy', undef, $redeploy );
303 die "Action already deployed\n" if (
304 not @$execute_stack or
305 5 ▶ not scalar( grep { $_->{action} eq $action and $_->{type} eq 'deploy' } @$execute_stack )
Branch: 1 branch
BranchTrueFalseTotal
unless @$execute_stack and scalar grep({$_->{"action"} eq $action and $_->{"type"} eq "deploy";} @$execute_stack) 1 4 5
Condition66%4/6
@$execute_stack and scalar grep({$_->{"action"} eq $action and $_->{"type"} eq "deploy";} @$execute_stack)
!ll&&!rl&&r
104
$_->{'action'} eq $action && $_->{'type'} eq 'deploy'
!ll&&!rl&&r
044
MC/DC50%1/2
@$execute_stack and scalar grep({$_->{"action"} eq $action and $_->{"type"} eq "deploy";} @$execute_stack)
@$execute_stack scalar grep({$_->{"action"} eq $action and $_->{"type"} eq "deploy";} @$execute_stack)
MC/DC50%1/2
$_->{'action'} eq $action && $_->{'type'} eq 'deploy'
$_->{'action'} eq $action $_->{'type'} eq 'deploy'
Truth table66%2/3
A and B
A= @$execute_stackB= scalar grep({$_->{"action"} eq $action and $_->{"type"} eq "deploy";} @$execute_stack)
ABresult
0X0
100
111
Truth table66%2/3
A && B
A= $_->{'action'} eq $actionB= $_->{'type'} eq 'deploy'
ABresult
0X0
100
111
306 );
307  
308 4 ▶ unless ($dry_run) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dry_run) { } 1 3 4
309 3 $self->_execute_action($_) for (@$execute_stack);
310 }
311 else {
312 1 _dry_run_report($execute_stack);
313 }
314  
315 4 return 0;
316 }
317  
318 1 ▶sub redeploy {
Subroutine redeploy: called CC=1 SCAR=0.0
Pod documented
319 1 my $self = _new(shift);
320 1 $self->{redeploy} = 1;
321 1 return $self->deploy(@_);
322 }
323  
324 1 ▶sub revdeploy {
Subroutine revdeploy: called CC=1 SCAR=0.0
Pod documented
325 1 my $self = _new(shift);
326 1 my ($action) = @_;
327  
328 1 $self->revert($action);
329 1 return $self->deploy($action);
330 }
331  
332 1 ▶sub verify {
Subroutine verify: called CC=1 SCAR=0.0
Pod documented
333 1 my $self = _new(shift);
334 1 my ($action) = @_;
335  
336 1 return $self->_execute_action( $self->_build_execute_stack( $action, 'verify' )->[0] );
337 }
338  
339 3 ▶sub revert {
Subroutine revert: called CC=7 SCAR=20.2
Pod documented
340 3 my $self = _new(shift);
341 3 my ( $dry_run, $action ) = _dry_check(@_);
342 3 ▶ die "File to revert required; usage: dest revert file\n" unless ($action);
Branch: 1 branch
BranchTrueFalseTotal
unless $action 0 3 3
343  
344 3 my $execute_stack = $self->_build_execute_stack( $action, 'revert' );
345 die "Action not deployed\n" if (
346 not @$execute_stack or
347 3 ▶ not scalar( grep { $_->{action} eq $action and $_->{type} eq 'revert' } @$execute_stack )
Branch: 1 branch
BranchTrueFalseTotal
unless @$execute_stack and scalar grep({$_->{"action"} eq $action and $_->{"type"} eq "revert";} @$execute_stack) 0 3 3
Condition50%3/6
@$execute_stack and scalar grep({$_->{"action"} eq $action and $_->{"type"} eq "revert";} @$execute_stack)
!ll&&!rl&&r
003
$_->{'action'} eq $action && $_->{'type'} eq 'revert'
!ll&&!rl&&r
403
MC/DC0%0/2
@$execute_stack and scalar grep({$_->{"action"} eq $action and $_->{"type"} eq "revert";} @$execute_stack)
@$execute_stack scalar grep({$_->{"action"} eq $action and $_->{"type"} eq "revert";} @$execute_stack)
MC/DC50%1/2
$_->{'action'} eq $action && $_->{'type'} eq 'revert'
$_->{'action'} eq $action $_->{'type'} eq 'revert'
Truth table33%1/3
A and B
A= @$execute_stackB= scalar grep({$_->{"action"} eq $action and $_->{"type"} eq "revert";} @$execute_stack)
ABresult
0X0
100
111
Truth table66%2/3
A && B
A= $_->{'action'} eq $actionB= $_->{'type'} eq 'revert'
ABresult
0X0
100
111
348 );
349  
350 3 ▶ unless ($dry_run) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dry_run) { } 1 2 3
351 2 $self->_execute_action($_) for (@$execute_stack);
352 }
353 else {
354 1 _dry_run_report($execute_stack);
355 }
356  
357 3 return 0;
358 }
359  
360 3 ▶sub update {
Subroutine update: called CC=12 SCAR=24.9
Pod documented
361 3 my $self = _new(shift);
362 3 my ( $dry_run, @incs ) = _dry_check(@_);
363  
364 3 my $seen_action = {};
365 3 my $execute_stack;
366  
367 3 for (
368 sort {
369 ( $b->{modified} || 0 ) <=> ( $a->{modified} || 0 ) ||
370 $a->{action} cmp $b->{action}
371 19 ▶ }
Condition85%6/7
($App::Dest::b->{'modified'} || 0) <=> ($App::Dest::a->{'modified'} || 0) || $App::Dest::a->{'action'} cmp $App::Dest::b->{'action'}
l!l&&r!l&&!r
2170
$App::Dest::b->{'modified'} || 0
l!l
118
$App::Dest::a->{'modified'} || 0
l!l
118
MC/DC0%0/2
($App::Dest::b->{'modified'} || 0) <=> ($App::Dest::a->{'modified'} || 0) || $App::Dest::a->{'action'} cmp $App::Dest::b->{'action'}
$App::Dest::b->{'modified'} $App::Dest::a->{'action'} cmp $App::Dest::b->{'action'}
MC/DC100%1/1
$App::Dest::a->{'modified'} || 0
$App::Dest::a->{'modified'}
Truth table66%2/3
A || B
A= $App::Dest::b->{'modified'}B= $App::Dest::a->{'action'} cmp $App::Dest::b->{'action'}
ABresult
1X1
011
000
Truth table100%2/2
A
A= $App::Dest::a->{'modified'}
Aresult
11
00
372 15 grep { not $_->{matches} }
373 3 values %{ $self->_status_data->{actions} }
374 ) {
375 2 push( @$execute_stack, @{ $self->_build_execute_stack( $_->{action}, 'revert' ) } ) if (
376 $_->{modified} or $_->{deployed}
377 14 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"modified"} or $_->{"deployed"} 2 12 14
Condition100%3/3
$_->{"modified"} or $_->{"deployed"}
l!l&&r!l&&!r
1112
MC/DC100%2/2
$_->{"modified"} or $_->{"deployed"}
$_->{"modified"} $_->{"deployed"}
Truth table100%3/3
A or B
A= $_->{"modified"}B= $_->{"deployed"}
ABresult
1X1
011
000
378 14 push( @$execute_stack, @{ $self->_build_execute_stack( $_->{action}, 'deploy', $seen_action ) } );
379 }
380  
381 $execute_stack = [
382 grep {
383 3 ▶ my $execution = $_;
Branch: 1 branch
BranchTrueFalseTotal
if @incs 1 2 3
384 8 my $match = 0;
385  
386 8 for (@incs) {
387 8 ▶ if ( index( $execution->{file}, $_ ) > -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($execution->{"file"}, $_) != -1) 8 0 8
388 8 $match = 1;
389 8 last;
390 }
391 }
392  
393 8 $match;
394 }
395 @$execute_stack
396 ] if (@incs);
397  
398 3 ▶ unless ($dry_run) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dry_run) { } 1 2 3
399 2 $self->_execute_action($_) for (@$execute_stack);
400 }
401 else {
402 1 _dry_run_report($execute_stack);
403 }
404  
405 3 return 0;
406 }
407  
408 2 ▶sub version {
Subroutine version: called CC=2 SCAR=7.2
Pod documented
409 2 ▶ print 'dest version ', $__PACKAGE__::VERSION || 0, "\n";
Condition50%1/2
$__PACKAGE__::VERSION || 0
l!l
02
MC/DC0%0/1
$__PACKAGE__::VERSION || 0
$__PACKAGE__::VERSION
Truth table50%1/2
A
A= $__PACKAGE__::VERSION
Aresult
11
00
410 2 return 0;
411 }
412  
413 51 ▶sub _new {
Subroutine _new: called CC=7 SCAR=19.5
414 51 my ( $self, $expect_no_root_dir ) = @_;
415  
416 51 ▶ if ( not ref $self ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 42 9 51
417 42 $self = bless( {}, __PACKAGE__ );
418  
419 42 $self->{root_dir} = Path::Tiny->cwd;
420  
421 42 ▶ while ( not $self->{root_dir}->child('.dest')->is_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"root_dir"}->child(".dest")->is_dir) 32 38 70
422 32 ▶ if ( $self->{root_dir}->is_rootdir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"root_dir"}->is_rootdir) 4 28 32
423 4 $self->{root_dir} = '';
424 4 last;
425 }
426 28 $self->{root_dir} = $self->{root_dir}->parent;
427 28 $self->{dir_depth}++;
428 }
429  
430 42 ▶ if ( $expect_no_root_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expect_no_root_dir) { } 5 37 42
431 5 ▶ die "Project already initialized\n" if $self->{root_dir};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"root_dir"} 1 4 5
432 }
433 else {
434 37 ▶ die "Project not initialized\n" unless $self->{root_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"root_dir"} 0 37 37
435 }
436 }
437  
438 50 return $self;
439 }
440  
441 11 ▶sub _dry_check {
Subroutine _dry_check: called CC=2 SCAR=6.9
442 11 my @clean = grep { $_ ne '-d' } @_;
443 11 ▶ return ( @clean != @_ ) ? 1 : 0, @clean;
Branch: 1 branch
BranchTrueFalseTotal
@clean != @_ ? : 3 8 11
444 }
445  
446 131 ▶sub _rel2root {
Subroutine _rel2root: called CC=2 SCAR=7.0
447 131 my ( $self, $dir ) = @_;
448 131 ▶ my $path = path( $dir || '.' );
Condition100%2/2
$dir || '.'
l!l
1292
MC/DC100%1/1
$dir || '.'
$dir
Truth table100%2/2
A
A= $dir
Aresult
11
00
449  
450 131 try {
451 131 $path = $path->realpath;
452 }
453 catch ($e) {
454 0 $path = $path->absolute;
455 }
456  
457 131 return $path->relative( $self->{root_dir} )->stringify;
458 }
459  
460 450 ▶sub _rel2dir {
Subroutine _rel2dir: called CC=3 SCAR=12.0
461 450 my ( $self, $dir ) = @_;
462 450 ▶ return ( '../' x ( $self->{dir_depth} || 0 ) ) . ( $dir || '.' );
Condition50%2/4
$self->{'dir_depth'} || 0
l!l
0450
$dir || '.'
l!l
4500
MC/DC0%0/1
$self->{'dir_depth'} || 0
$self->{'dir_depth'}
MC/DC0%0/1
$dir || '.'
$dir
Truth table50%1/2
A
A= $self->{'dir_depth'}
Aresult
11
00
Truth table50%1/2
A
A= $dir
Aresult
11
00
463 }
464  
465 110 ▶sub _watch_list {
Subroutine _watch_list: called CC=2 SCAR=7.0
466 110 my ($self) = @_;
467 110 ▶ open( my $watch, '<', $self->_rel2dir('.dest/watch') ) or die "Unable to read ~/.dest/watch file\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $watch, "<", $self->_rel2dir(".dest/watch") or die "Unable to read ~/.dest/watch file\n" 110 0 110
468 110 return sort map { chomp; $self->_rel2dir($_) } <$watch>;
469 }
470  
471 61 ▶sub _actions_tree {
Subroutine _actions_tree: called CC=2 SCAR=7.0
472 61 my ( $self, $filter ) = @_;
473  
474 61 my $tree;
475 61 for my $path ( $self->_watch_list ) {
476 61 my @actions;
477  
478 find( {
479 follow => 1,
480 no_chdir => 1,
481 1160 ▶ wanted => sub {
Subroutine __ANON__: called CC=4 SCAR=14.2
482 1160 ▶ return unless ( m|/deploy(?:\.[^\/]+)?| );
Branch: 1 branch
BranchTrueFalseTotal
unless m[/deploy(?:\.[^\/]+)?]u 899 261 1160
483 261 ( my $action = $_ ) =~ s|/deploy(?:\.[^\/]+)?||;
484  
485 261 ▶ push( @actions, $action ) if (
Branch: 1 branch
BranchTrueFalseTotal
if not defined $filter or index($action, $filter) != -1 261 0 261
Condition66%2/3
not defined $filter or index($action, $filter) != -1
l!l&&r!l&&!r
25830
MC/DC0%0/2
not defined $filter or index($action, $filter) != -1
not defined $filter index($action, $filter) != -1
Truth table66%2/3
not(A) or B
A= not defined $filterB= index($action, $filter) != -1
ABresult
1X1
011
000
486 not defined $filter or
487 index( $action, $filter ) > -1
488 );
489 },
490 61 }, $path );
491  
492 61 $tree->{$path} = [ sort @actions ];
493 }
494  
495 61 return $tree;
496 }
497  
498 26 ▶sub _prereq_tree {
Subroutine _prereq_tree: called CC=5 SCAR=16.1
499 26 my ( $self, $filter ) = @_;
500 26 my $tree = $self->_actions_tree($filter);
501  
502 26 my @actions;
503 26 for my $path ( sort keys %$tree ) {
504 26 for my $action ( @{ $tree->{$path} } ) {
505 114 my ($file) = <"$action/deploy*">;
506 114 ▶ open( my $content, '<', $file ) or die "Unable to read $file\n";
Branch: 1 branch
BranchTrueFalseTotal
open my $content, "<", $file or die "Unable to read $file\n" 114 0 114
507  
508 my $prereqs = [
509 59 map { $self->_rel2dir($_) }
510 59 grep { defined }
511 59 ▶ map { /dest\.prereq\b[\s:=-]+(.+?)\s*$/; $1 || undef }
Condition50%1/2
$1 || undef
l!l
590
MC/DC0%0/1
$1 || undef
$1
Truth table50%1/2
A
A= $1
Aresult
11
00
512 114 grep { /dest\.prereq/ } <$content>
513 ];
514  
515 114 $action = {
516 action => $action,
517 prereqs => $prereqs,
518 };
519  
520 114 push( @actions, $action );
521 }
522 }
523  
524 26 return { tree => $tree, actions => \@actions };
525 }
526  
527 33 ▶sub _status_data {
Subroutine _status_data: called CC=8 SCAR=21.0
528 33 my ($self) = @_;
529  
530 33 my $actions;
531 33 my $tree = $self->_actions_tree;
532 33 for my $path ( keys %$tree ) {
533 33 for my $action ( @{ $tree->{$path} } ) {
534 141 $actions->{$action} = {
535 action => $action,
536 deployed => 1,
537 matches => 1,
538 };
539 }
540 }
541  
542 33 my @paths;
543 33 for my $path ( $self->_watch_list ) {
544 33 my $printed_path = 0;
545 33 my $data;
546  
547 33 try {
548 File::DirCompare->compare(
549 $self->_rel2dir( '.dest/' . $self->_rel2root($path) ),
550 $path,
551 157 ▶ sub {
Subroutine __ANON__: called CC=10 SCAR=23.3
552 157 my ( $a, $b ) = @_;
553 157 ▶ return if ( $a and $a =~ /\/dest.wrap$/ or $b and $b =~ /\/dest.wrap$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $a and $a =~ m[/dest.wrap$]u or $b and $b =~ m[/dest.wrap$]u 29 128 157
Condition77%7/9
$a and $a =~ m[/dest.wrap$]u or $b and $b =~ m[/dest.wrap$]u
l!l&&r!l&&!r
029128
$a and $a =~ m[/dest.wrap$]u
!ll&&!rl&&r
122350
$b and $b =~ m[/dest.wrap$]u
!ll&&!rl&&r
1811029
MC/DC25%1/4
$a and $a =~ m[/dest.wrap$]u or $b and $b =~ m[/dest.wrap$]u
$a $a =~ m[/dest.wrap$]u $b $b =~ m[/dest.wrap$]u
Truth table57%4/7
A and B or C and D
A= $aB= $a =~ m[/dest.wrap$]uC= $bD= $b =~ m[/dest.wrap$]u
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
554 128 ▶ $data->{state} = 'diff' unless ( $printed_path++ );
Branch: 1 branch
BranchTrueFalseTotal
unless $printed_path++ 31 97 128
555  
556 128 ▶ if ( not $b ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $b) { } 18 110 128
elsif (not $a) { } 93 17 110
elsif ($a and $b) { } 17 0 17
557 push(
558 18 @{ $data->{actions} },
559 {
560 action => $self->_rel2dir( substr( $self->_rel2root($a), 6 ) ),
561 deployed => 1,
562 },
563 );
564 }
565 elsif ( not $a ) {
566 push(
567 93 @{ $data->{actions} },
568 {
569 action => $b,
570 deployed => 0,
571 },
572 );
573 }
574 ▶ elsif ( $a and $b ) {
Condition33%1/3
$a and $b
!ll&&!rl&&r
0017
MC/DC0%0/2
$a and $b
$a $b
Truth table33%1/3
A and B
A= $aB= $b
ABresult
0X0
100
111
575 17 ( my $action = $b ) =~ s,/(?:deploy|verify|revert)(?:\.[^\/]+)?$,,;
576 push(
577 17 @{ $data->{actions} },
578 {
579 action => $action,
580 modified => 1,
581 file => $b,
582 },
583 );
584 }
585  
586 128 return;
587 },
588 )
589 33 }
590 catch ($e) {
591 0 ▶ $data->{state} = '?' if ( $e =~ /Not a directory/ );
Branch: 1 branch
BranchTrueFalseTotal
if $e =~ /Not a directory/u 0 0 0
592 }
593 finally {
594 33 ▶ $data->{state} = 'ok' unless $printed_path;
Branch: 1 branch
BranchTrueFalseTotal
unless $printed_path 2 31 33
595 }
596  
597 33 $data->{path} = $path;
598 33 push( @paths, $data );
599  
600 33 ▶ if ( ref $data->{actions} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data->{"actions"} eq "ARRAY") 31 2 33
601 31 $actions->{ $_->{action} } = $_ for ( @{ $data->{actions} } );
602 }
603 }
604  
605 33 return { paths => \@paths, actions => $actions };
606 }
607  
608 25 ▶sub _build_execute_stack {
Subroutine _build_execute_stack: called CC=21 SCAR=30.5
609 25 my ( $self, $name, $type, $seen_action, $redeploy ) = @_;
610  
611 25 my @actions_stack = ($name);
612 25 my $prereqs = { map { $_->{action} => $_->{prereqs} } @{ $self->_prereq_tree->{actions} } };
613 25 my $state = $self->_status_data->{actions};
614  
615 25 ▶ if ( $type eq 'revert' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "revert") 5 20 25
616 5 my $postreqs;
617 5 for my $action ( keys %$prereqs ) {
618 21 push( @{ $postreqs->{$_} }, $action ) for ( @{ $prereqs->{$action} } );
619 }
620 5 $prereqs = $postreqs;
621 }
622  
623 25 my ( @execute_stack, $wraps );
624 25 ▶ $seen_action //= {};
Condition100%2/2
$seen_action //= {}
l!l
1411
MC/DC100%1/1
$seen_action //= {}
$seen_action
Truth table100%2/2
A
A= $seen_action
Aresult
11
00
625  
626 25 ▶ while (@actions_stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@actions_stack) 39 25 64
627 39 my $action = shift @actions_stack;
628 39 ▶ next if ( $seen_action->{$action}++ );
Branch: 1 branch
BranchTrueFalseTotal
if $seen_action->{$action}++ 9 30 39
629 30 ▶ push( @actions_stack, @{ $prereqs->{$action} || [] } );
Condition100%2/2
$prereqs->{$action} || []
l!l
228
MC/DC100%1/1
$prereqs->{$action} || []
$prereqs->{$action}
Truth table100%2/2
A
A= $prereqs->{$action}
Aresult
11
00
630  
631 30 ▶ my $location = ( $type ne 'revert' )
Branch: 1 branch
BranchTrueFalseTotal
$type ne 'revert' ? : 20 10 30
632 ? $action
633 : $self->_rel2dir( '.dest/' . $self->_rel2root($action) );
634  
635 30 my $file = ( <"$location/$type*"> )[0];
636  
637 30 ▶ unless ( exists $wraps->{$action} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $wraps->{$action}) 30 0 30
638 30 $wraps->{$action} = undef;
639 30 my @nodes = split( '/', $self->_rel2root($file) );
640 30 pop @nodes;
641 30 ▶ shift @nodes if ( defined $nodes[0] and $nodes[0] eq '.dest' );
Branch: 1 branch
BranchTrueFalseTotal
if defined $nodes[0] and $nodes[0] eq ".dest" 9 21 30
Condition100%3/3
defined $nodes[0] and $nodes[0] eq ".dest"
!ll&&!rl&&r
2199
MC/DC100%2/2
defined $nodes[0] and $nodes[0] eq ".dest"
defined $nodes[0] $nodes[0] eq ".dest"
Truth table100%3/3
A and B
A= defined $nodes[0]B= $nodes[0] eq ".dest"
ABresult
0X0
100
111
642 30 ▶ while (@nodes) {
Branch: 1 branch
BranchTrueFalseTotal
if (@nodes) 56 2 58
643 56 my $path = $self->_rel2dir( join( '/', @nodes ) . '/dest.wrap' );
644 56 ▶ if ( -f $path ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $path) 28 28 56
645 28 $wraps->{$action} = $path;
646 28 last;
647 }
648 28 pop @nodes;
649 }
650 }
651  
652 27 ▶ my $add = sub {
Subroutine __ANON__: called CC=6 SCAR=18.6
653 27 ▶ die "Action file does not exist for: $type $action\n" unless ($file);
Branch: 1 branch
BranchTrueFalseTotal
unless $file 0 27 27
654  
655 27 ▶ my $executable = ( $wraps->{$action} ) ? $wraps->{$action} : $file;
Branch: 1 branch
BranchTrueFalseTotal
$wraps->{$action} ? : 27 0 27
656 27 ▶ die 'Execute permission denied for: ' . $executable . "\n" unless ( -x $executable );
Branch: 1 branch
BranchTrueFalseTotal
unless -x $executable 0 27 27
657  
658 unshift(
659 @execute_stack,
660 {
661 type => $type,
662 action => $action,
663 file => $file,
664 27 wrap => $wraps->{$action},
665 },
666 );
667  
668 27 ▶ if ( $type eq 'deploy' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "deploy") 17 10 27
669 17 my $verify_file = ( <"$action/verify*"> )[0];
670 17 ▶ die "Action file does not exist for: verify $action\n" unless ($verify_file);
Branch: 1 branch
BranchTrueFalseTotal
unless $verify_file 0 17 17
671  
672 splice(
673 @execute_stack,
674 1,
675 0,
676 {
677 type => 'verify',
678 action => $action,
679 file => $verify_file,
680 17 wrap => $wraps->{$action},
681 },
682 );
683 }
684 30 };
685  
686 30 ▶ if ( $type eq 'deploy' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'deploy') { } 19 11 30
elsif ($type eq 'revert') { } 10 1 11
elsif ($type eq 'verify') { } 1 0 1
687 19 ▶ $add->() unless ( $state->{$action}{deployed} and not $redeploy );
Branch: 1 branch
BranchTrueFalseTotal
unless $state->{$action}{"deployed"} and not $redeploy 17 2 19
Condition100%3/3
$state->{$action}{"deployed"} and not $redeploy
!ll&&!rl&&r
1612
MC/DC100%2/2
$state->{$action}{"deployed"} and not $redeploy
$state->{$action}{"deployed"} not $redeploy
Truth table100%3/3
A and not(B)
A= $state->{$action}{"deployed"}B= not $redeploy
ABresult
0X0
100
111
688 }
689 elsif ( $type eq 'revert' ) {
690 10 ▶ $add->() if ( $state->{$action}{deployed} or $state->{$action}{modified} );
Branch: 1 branch
BranchTrueFalseTotal
if $state->{$action}{"deployed"} or $state->{$action}{"modified"} 9 1 10
Condition100%3/3
$state->{$action}{"deployed"} or $state->{$action}{"modified"}
l!l&&r!l&&!r
811
MC/DC100%2/2
$state->{$action}{"deployed"} or $state->{$action}{"modified"}
$state->{$action}{"deployed"} $state->{$action}{"modified"}
Truth table100%3/3
A or B
A= $state->{$action}{"deployed"}B= $state->{$action}{"modified"}
ABresult
1X1
011
000
691 }
692 elsif ( $type eq 'verify' ) {
693 1 $add->();
694 }
695 }
696  
697 25 return \@execute_stack;
698 }
699  
700 3 ▶sub _dry_run_report {
Subroutine _dry_run_report: called CC=3 SCAR=11.4
701 3 my ($execute_stack) = @_;
702 3 ▶ print '' . ( ( $_->{wrap} ) ? $_->{wrap} . ' ' : '' ) . $_->{file}, "\n" for (@$execute_stack);
Branch: 1 branch
BranchTrueFalseTotal
$_->{'wrap'} ? : 17 0 17
703 }
704  
705 27 ▶sub _execute_action {
Subroutine _execute_action: called CC=6 SCAR=19.4
706 27 my ( $self, $input ) = @_;
707 27 my ( $type, $wrap, $action, $file, $location ) = @$input{ qw( type wrap action file location ) };
708  
709 27 my ( $out, $err, $died );
710 27 ▶ my $run = sub {
Subroutine __ANON__: called CC=4 SCAR=20.7
711 27 try {
712 run(
713 27 ▶ [ ( grep { defined } $wrap ), $file, $type ],
Branch: 1 branch
BranchTrueFalseTotal
run([grep({defined $_;} $wrap), $file, $type], \(undef), \$out, \$err) or $died = 1 27 0 27
714 \undef, \$out, \$err,
715 ) or $died = 1;
716 }
717 catch ($e) {
718 0 $err = $e;
719 }
720  
721 27 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 27 27
722 0 ( my $err_str = $err ) =~ s/\s*at\s+.*$//;
723 0 chomp($err_str);
724 0 ▶ if ($died) {
Branch: 1 branch
BranchTrueFalseTotal
if ($died) { } 0 0 0
725 0 die "Failed to execute $file: $err_str\n";
726 }
727 else {
728 0 warn "Warnings from executed $file: $err_str\n";
729 }
730 }
731 27 };
732  
733 27 ▶ if ( $type eq 'verify' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'verify') { } 12 15 27
734 12 $run->();
735 12 chomp($out);
736 12 ▶ die "$err\n" if ($err);
Branch: 1 branch
BranchTrueFalseTotal
if $err 0 12 12
737  
738 12 ▶ if ($out) {
Branch: 1 branch
BranchTrueFalseTotal
if ($out) { } 12 0 12
739 12 print "ok - verify: $action\n";
740 }
741 else {
742 0 die "not ok - verify: $action\n";
743 }
744 }
745 else {
746 15 print "begin - $type: $action\n";
747 15 $run->();
748 15 print "ok - $type: $action\n";
749 }
750  
751 27 my $dest_copy = $self->_rel2dir( '.dest/' . $self->_rel2root($action) );
752 27 ▶ rmtree($dest_copy) unless ( $type eq 'verify' );
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "verify" 15 12 27
753 27 ▶ dircopy( $action, $dest_copy ) if ( $type eq 'deploy' );
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "deploy" 11 16 27
754  
755 27 return 0;
756 }
757  
758 7 1;
759  
760 __END__
↑ Summary bin/dest »