CPAN/Maker/Bootstrapper/Role/ExtraFiles.pm untested

filter: Statementstmt 0.0% 0 / 112 Branchbran 0.0% 0 / 50 Conditioncond 0.0% 0 / 33 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 225 CC 26 SCAR 62.6
CC
26
Cov
0%
cmd_extra_files
702.0
BEGIN
2.0
BEGIN
2.0
CRAP 522.0
1 package CPAN::Maker::Bootstrapper::Role::ExtraFiles;
2  
3 use strict;
4 use warnings;
5  
6 use CLI::Simple::Constants qw(:booleans);
7 use Data::Dumper;
8 use English qw(-no_match_vars);
9 use List::Util qw(any none);
10 use Scalar::Util qw(reftype);
11  
12 use Role::Tiny;
13  
14 our $VERSION = '2.5.0';
15  
16 ########################################################################
17 sub cmd_extra_files {
18 ########################################################################
19 my ($self) = @_;
20  
21 my ( $path, @extra_files ) = $self->get_args;
22  
23 require YAML::Tiny;
24  
25 my $buildspec = YAML::Tiny::LoadFile('buildspec.yml');
26 my $extra = $buildspec->{'extra-files'} // [];
27  
28 ######################################################################
29 # With no arguments, emit the source files represented by
30 # buildspec.yml. This form is used by make to regenerate extra-files.
31 ######################################################################
32 if ( !defined $path ) {
33 foreach my $entry ( @{$extra} ) {
34 if ( !ref $entry ) {
35 print "$entry\n";
36 next;
37 }
38  
39 next
40 if reftype($entry) ne 'HASH';
41  
42 foreach my $section ( keys %{$entry} ) {
43 my $files = $entry->{$section};
44  
45 if ( !ref $files ) {
46 print "$files\n"
47 if defined $files && $files ne q{};
48 next;
49 }
50  
51 next
52 if reftype($files) ne 'ARRAY';
53  
54 foreach my $file ( @{$files} ) {
55 print "$file\n"
56 if defined $file && !ref $file && $file ne q{};
57 }
58 }
59 }
60  
61 return $SUCCESS;
62 }
63  
64 die "ERROR: path must root, '.' or share\n"
65 if none { $path eq $_ } qw(root . share);
66  
67 die "ERROR: usage cmb extra-files [path file...]\n"
68 if !@extra_files;
69  
70 $path = q{.}
71 if $path eq 'root';
72  
73 ######################################################################
74 # Additions must exist. Removals are idempotent and need not exist on
75 # disk.
76 ######################################################################
77 foreach my $file (@extra_files) {
78 next
79 if $file =~ /^-/xsm;
80  
81 die "ERROR: file not found - make sure '$file' exists before adding to buildspec.yml\n"
82 if !-f $file;
83 }
84  
85 if ( $path eq q{.} ) {
86 my @updated;
87  
88 foreach my $entry ( @{$extra} ) {
89 if ( ref $entry ) {
90 push @updated, $entry;
91 next;
92 }
93  
94 my $remove = any {
95 my $file = $_;
96 $file =~ s/^-//xsm;
97 $_ =~ /^-/xsm && $file eq $entry;
98 } @extra_files;
99  
100 push @updated, $entry
101 if !$remove;
102 }
103  
104 foreach my $file (@extra_files) {
105 next
106 if $file =~ /^-/xsm;
107  
108 next
109 if any { !ref $_ && $_ eq $file } @updated;
110  
111 push @updated, $file;
112 }
113  
114 $extra = \@updated;
115 }
116 else {
117 my @updated;
118 my @share_files;
119 my $share_seen = $FALSE;
120  
121 ####################################################################
122 # Fold any duplicate share sections into one while preserving all
123 # other extra-files sections unchanged.
124 ####################################################################
125 foreach my $entry ( @{$extra} ) {
126 if ( ref $entry
127 && reftype($entry) eq 'HASH'
128 && exists $entry->{share} ) {
129 my $files = $entry->{share};
130  
131 if ( ref $files && reftype($files) eq 'ARRAY' ) {
132 foreach my $file ( @{$files} ) {
133 push @share_files, $file
134 if !any { $_ eq $file } @share_files;
135 }
136 }
137  
138 next
139 if $share_seen;
140  
141 $share_seen = $TRUE;
142 next;
143 }
144  
145 push @updated, $entry;
146 }
147  
148 foreach my $arg (@extra_files) {
149 if ( $arg =~ /^-(.+)$/xsm ) {
150 my $remove = $1;
151 my @kept;
152  
153 foreach my $file (@share_files) {
154 push @kept, $file
155 if $file ne $remove;
156 }
157  
158 @share_files = @kept;
159 next;
160 }
161  
162 push @share_files, $arg
163 if !any { $_ eq $arg } @share_files;
164 }
165  
166 push @updated, { share => \@share_files }
167 if @share_files;
168  
169 $extra = \@updated;
170 }
171  
172 $buildspec->{'extra-files'} = $extra;
173  
174 rename 'buildspec.yml', 'buildspec.yml.bak'
175 or die "ERROR: could not back up buildspec.yml\n$OS_ERROR";
176  
177 eval {
178 YAML::Tiny::DumpFile( 'buildspec.yml', $buildspec );
179 return 1;
180 } or do {
181 my $error = $EVAL_ERROR;
182  
183 rename 'buildspec.yml.bak', 'buildspec.yml'
184 or die "ERROR: could not restore buildspec.yml\n$OS_ERROR";
185  
186 die $error;
187 };
188  
189 return $SUCCESS;
190 }
191  
192 1;