blib/lib/Shipwright/Script/Update.pm

filter: Statementstmt 24.8% 33 / 133 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 28 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 71.4% 10 / 14 Podpod 0.0% 0 / 2 totaltotal 16.1% 43 / 267 CC 57 SCAR 72.7
CC
57
Cov
14%
run
2162.0
_update
90.0
__ANON__
20.0
CRAP 1433.6
« blib/lib/Shipwright/Script/Import.pm ↑ Summary blib/lib/Shipwright/Script/List.pm »
1 package Shipwright::Script::Update;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use base qw/App::CLI::Command Shipwright::Base Shipwright::Script/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 __PACKAGE__->mk_accessors(
8 qw/all follow builder utility inc version only_sources as add_deps delete_deps/
9 );
10  
11 1 ▶use Shipwright;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use File::Spec::Functions qw/catdir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Shipwright::Source;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Shipwright::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use File::Copy qw/copy move/;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use File::Temp qw/tempdir/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 0 ▶sub options {
Subroutine options: not called CC=1 SCAR=6.9
Pod undocumented
20 (
21 0 'a|all' => 'all',
22 'follow' => 'follow',
23 'builder' => 'builder',
24 'utility' => 'utility',
25 'inc' => 'inc',
26 'version=s' => 'version',
27 'only-sources' => 'only_sources',
28 'as=s' => 'as',
29 'add-deps=s' => 'add_deps',
30 'delete-deps=s' => 'delete_deps',
31 );
32 }
33  
34 1 my ( $shipwright, $map, $source, $branches );
35  
36 0 ▶sub run {
Subroutine run: not called CC=46 SCAR=76.8
Pod undocumented
37 0 my $self = shift;
38  
39 0 $shipwright = Shipwright->new(
40 repository => $self->repository,
41 log_level => $self->log_level,
42 log_file => $self->log_file
43 );
44  
45 0 ▶ if ( $self->builder ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($self->builder) { } 0 0 0
elsif ($self->utility) { } 0 0 0
elsif ($self->inc) { } 0 0 0
elsif ($self->add_deps) { } 0 0 0
elsif ($self->delete_deps) { } 0 0 0
46 0 $shipwright->backend->update( path => '/bin/shipwright-builder' );
47 }
48 elsif ( $self->utility ) {
49 0 $shipwright->backend->update( path => '/bin/shipwright-utility' );
50  
51 }
52 elsif ( $self->inc ) {
53 0 $shipwright->backend->update( path => '/inc/' );
54  
55 }
56 elsif ( $self->add_deps ) {
57 0 my @deps = split /\s*,\s*/, $self->add_deps;
58 0 ▶ my $name = shift or confess_or_die 'need name arg';
Branch: 1 branch
BranchTrueFalseTotal
my $name = shift() or confess_or_die("need name arg") 0 0 0
59 0 ▶ my $requires = $shipwright->backend->requires( name => $name ) || {};
Condition0%0/2
$shipwright->backend->requires('name', $name) || {}
l!l
00
MC/DC0%0/1
$shipwright->backend->requires('name', $name) || {}
$shipwright->backend->requires('name', $name)
Truth table0%0/2
A
A= $shipwright->backend->requires('name', $name)
Aresult
11
00
60 0 for my $dep ( @deps ) {
61 0 my $new_dep;
62 0 my $version = 0;
63 0 ▶ if ( $dep =~ /(.*)=(.*)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dep =~ /(.*)=(.*)/) 0 0 0
64 0 $dep = $1;
65 0 $version = $2;
66 }
67  
68 0 ▶ $new_dep = 1 unless $requires->{requires}{$dep};
Branch: 1 branch
BranchTrueFalseTotal
unless $requires->{"requires"}{$dep} 0 0 0
69 0 $requires->{requires}{$dep} = { version => $version };
70 0 $shipwright->backend->_yml( "/scripts/$name/require.yml", $requires );
71  
72 0 ▶ if ($new_dep) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_dep) 0 0 0
73  
74 # we need to update refs.yml
75 0 my $refs = $shipwright->backend->refs;
76 0 $refs->{$dep}++;
77 0 $shipwright->backend->refs($refs);
78 }
79 }
80 }
81 elsif ( $self->delete_deps ) {
82 0 my @deps = split /\s*,\s*/, $self->delete_deps;
83 0 ▶ my $name = shift or confess_or_die 'need name arg';
Branch: 1 branch
BranchTrueFalseTotal
my $name = shift() or confess_or_die("need name arg") 0 0 0
84 0 ▶ my $requires = $shipwright->backend->requires( name => $name ) || {};
Condition0%0/2
$shipwright->backend->requires('name', $name) || {}
l!l
00
MC/DC0%0/1
$shipwright->backend->requires('name', $name) || {}
$shipwright->backend->requires('name', $name)
Truth table0%0/2
A
A= $shipwright->backend->requires('name', $name)
Aresult
11
00
85 0 my $deleted;
86 0 for my $dep ( @deps ) {
87 0 for my $type ( qw/requires build_requires recommends test_requires/ ) {
88 0 ▶ if ( $requires->{$type} && exists $requires->{$type}{$dep} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($requires->{$type} and exists $requires->{$type}{$dep}) 0 0 0
Condition0%0/3
$requires->{$type} and exists $requires->{$type}{$dep}
!ll&&!rl&&r
000
MC/DC0%0/2
$requires->{$type} and exists $requires->{$type}{$dep}
$requires->{$type} exists $requires->{$type}{$dep}
Truth table0%0/3
A and B
A= $requires->{$type}B= exists $requires->{$type}{$dep}
ABresult
0X0
100
111
89 0 delete $requires->{$type}{$dep};
90 0 $deleted = 1;
91 }
92 }
93  
94 0 $shipwright->backend->_yml( "/scripts/$name/require.yml", $requires );
95 0 my $refs = $shipwright->backend->refs;
96 0 ▶ $refs->{$dep}-- if $refs->{$dep} > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $refs->{$dep} > 0 0 0 0
97 0 $shipwright->backend->refs($refs);
98 }
99 0 ▶ if ( $deleted ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deleted) { } 0 0 0
100 0 $self->log->fatal( 'successfully updated' );
101 }
102 else {
103 0 $self->log->fatal( "not updated: no such deps in $name" );
104 }
105 0 return;
106 }
107 else {
108 0 ▶ $map = $shipwright->backend->map || {};
Condition0%0/2
$shipwright->backend->map || {}
l!l
00
MC/DC0%0/1
$shipwright->backend->map || {}
$shipwright->backend->map
Truth table0%0/2
A
A= $shipwright->backend->map
Aresult
11
00
109 0 ▶ $source = $shipwright->backend->source || {};
Condition0%0/2
$shipwright->backend->source || {}
l!l
00
MC/DC0%0/1
$shipwright->backend->source || {}
$shipwright->backend->source
Truth table0%0/2
A
A= $shipwright->backend->source
Aresult
11
00
110 0 $branches = $shipwright->backend->branches;
111  
112 0 ▶ if ( $self->all ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->all) { } 0 0 0
113 0 ▶ confess_or_die '--all can not be specified with --as or NAME'
Branch: 1 branch
BranchTrueFalseTotal
if @_ or $self->as 0 0 0
Condition0%0/3
@_ or $self->as
l!l&&r!l&&!r
000
MC/DC0%0/2
@_ or $self->as
@_ $self->as
Truth table0%0/3
A or B
A= @_B= $self->as
ABresult
1X1
011
000
114 if @_ || $self->as;
115  
116 0 ▶ my $dists = $shipwright->backend->order || [];
Condition0%0/2
$shipwright->backend->order || []
l!l
00
MC/DC0%0/1
$shipwright->backend->order || []
$shipwright->backend->order
Truth table0%0/2
A
A= $shipwright->backend->order
Aresult
11
00
117 0 for (@$dists) {
118 0 $self->_update($_);
119 }
120 }
121 else {
122 0 my $name = shift;
123 0 ▶ confess_or_die "need name arg\n" unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
124  
125 # die if the specified branch doesn't exist
126 0 ▶ if ( $branches && $self->as ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($branches and $self->as) 0 0 0
Condition0%0/3
$branches and $self->as
!ll&&!rl&&r
000
MC/DC0%0/2
$branches and $self->as
$branches $self->as
Truth table0%0/3
A and B
A= $branchesB= $self->as
ABresult
0X0
100
111
127 confess_or_die "$name doesn't have branch "
128 . $self->as
129 . ". please use import cmd instead"
130 0 ▶ unless grep { $_ eq $self->as } @{ $branches->{$name} || [] };
Branch: 1 branch
BranchTrueFalseTotal
unless grep {$_ eq $self->as;} @{$branches->{$name} or [];} 0 0 0
Condition0%0/2
$branches->{$name} || []
l!l
00
MC/DC0%0/1
$branches->{$name} || []
$branches->{$name}
Truth table0%0/2
A
A= $branches->{$name}
Aresult
11
00
131 }
132  
133 0 my $new_source = shift;
134 0 ▶ if ($new_source) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_source) 0 0 0
135 0 ▶ system(
Branch: 4 branches
BranchTrueFalseTotal
system "$0 relocate -r " . $self->repository . ($self->log_level ? ' --log-level ' . $self->log_level : "") . ($self->log_file ? ' --log-file ' . $self->log_file : "") . ($self->as ? ' --as ' . $self->as : "") . " $name $new_source" and die "relocate $name to $new_source failed: $!" 0 0 0
$self->log_level ? : 0 0 0
$self->log_file ? : 0 0 0
$self->as ? : 0 0 0
136 "$0 relocate -r "
137 . $self->repository
138 . (
139 $self->log_level
140 ? ( " --log-level " . $self->log_level )
141 : ''
142 )
143 . (
144 $self->log_file ? ( " --log-file " . $self->log_file )
145 : ''
146 )
147 . (
148 $self->as ? ( " --as " . $self->as )
149 : ''
150 )
151 . " $name $new_source"
152 ) && die "relocate $name to $new_source failed: $!";
153 # renew our $source
154 0 ▶ $source = $shipwright->backend->source || {};
Condition0%0/2
$shipwright->backend->source || {}
l!l
00
MC/DC0%0/1
$shipwright->backend->source || {}
$shipwright->backend->source
Truth table0%0/2
A
A= $shipwright->backend->source
Aresult
11
00
155 }
156  
157 0 my @dists;
158 0 ▶ if ( $self->follow ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->follow) { } 0 0 0
159 0 my (%checked);
160 my $find_deps;
161 0 ▶ $find_deps = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
162 0 my $name = shift;
163 0 ▶ return if $checked{$name}++;
Branch: 1 branch
BranchTrueFalseTotal
if $checked{$name}++ 0 0 0
164  
165 0 my ($require) =
166 $shipwright->backend->requires( name => $name );
167 0 for my $type (
168 qw/requires build_requires recommends
169 test_requires/
170 )
171 {
172 0 for ( keys %{ $require->{$type} } ) {
173 0 $find_deps->($_);
174 }
175 }
176 0 };
177  
178 0 $find_deps->($name);
179 0 @dists = keys %checked;
180 }
181 else {
182 0 @dists = $name;
183 }
184  
185 0 for (@dists) {
186 0 ▶ if ( $self->only_sources ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->only_sources) { } 0 0 0
187 0 ▶ if ( $_ eq $name ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq $name) { } 0 0 0
188 0 $self->_update( $_, $self->version, $self->as );
189 }
190 else {
191 0 $self->_update($_);
192 }
193 }
194 else {
195 0 local $ENV{SHIPWRIGHT_SOURCE_ROOT} = tempdir(
196 'shipwright_source_XXXXXX',
197 CLEANUP => 1,
198 TMPDIR => 1
199 );
200 0 ▶ system(
Branch: 4 branches
BranchTrueFalseTotal
$self->log_level ? : 0 0 0
$self->log_file ? : 0 0 0
$self->as ? : 0 0 0
$self->version ? : 0 0 0
201 "$0 import -r "
202 . $self->repository
203 . (
204 $self->log_level
205 ? ( " --log-level " . $self->log_level )
206 : ''
207 )
208 . (
209 $self->log_file
210 ? ( " --log-file " . $self->log_file )
211 : ''
212 )
213 . (
214 $self->as ? ( " --as " . $self->as )
215 : ''
216 )
217 . (
218 $self->version ? ( " --version " . $self->version )
219 : ''
220 )
221 . " --name $_"
222 );
223 }
224 }
225 }
226 }
227 0 $self->log->fatal( 'successfully updated' );
228 }
229  
230 0 ▶sub _update {
Subroutine _update: not called CC=9 SCAR=45.0
231 0 my $self = shift;
232 0 my $name = shift;
233 0 my $version = shift;
234 0 my $as = shift;
235 0 ▶ if ( $source->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($source->{$name}) { } 0 0 0
236 $shipwright->source(
237 Shipwright::Source->new(
238 name => $name,
239 source => (
240 ref $source->{$name}
241 ? $source->{$name}{ $as || $branches->{$name}[0] }
242 0 ▶ : $source->{$name}
Branch: 1 branch
BranchTrueFalseTotal
ref $source->{$name} ? : 0 0 0
Condition0%0/3
$as || $branches->{$name}[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
$as || $branches->{$name}[0]
$as $branches->{$name}[0]
Truth table0%0/3
A || B
A= $asB= $branches->{$name}[0]
ABresult
1X1
011
000
243 ),
244 follow => 0,
245 version => $version,
246 )
247 );
248 }
249 else {
250  
251 # it's a cpan dist
252 0 my $s;
253  
254 0 ▶ if ( $name =~ /^cpan-/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name =~ /^cpan-/) { } 0 0 0
elsif ($map->{$name}) { } 0 0 0
255 0 $s = { reverse %$map }->{$name};
256 }
257 elsif ( $map->{$name} ) {
258 0 $s = $name;
259 0 $name = $map->{$name};
260 }
261 else {
262 0 confess_or_die 'invalid name ' . $name . "\n";
263 }
264  
265 0 ▶ unless ( $s ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($s) 0 0 0
266 0 warn "can't find the source name of $name, skipping";
267 0 next;
268 }
269  
270 $shipwright->source(
271 0 Shipwright::Source->new(
272 source => "cpan:$s",
273 follow => 0,
274 version => $version,
275 )
276 );
277 }
278  
279 0 $shipwright->source->run;
280  
281 0 $version = load_yaml_file( $shipwright->source->version_path );
282  
283 0 my $branches = $shipwright->backend->branches;
284 $shipwright->backend->import(
285 source => catdir( $shipwright->source->directory, $name ),
286 comment => "update $name",
287 overwrite => 1,
288 version => $version->{$name},
289 as => $as,
290 branches => $shipwright->source->isa('Shipwright::Source::Shipyard')
291 0 ▶ ? ( $branches->{$name} || [] )
Branch: 1 branch
BranchTrueFalseTotal
$shipwright->source->isa('Shipwright::Source::Shipyard') ? : 0 0 0
Condition0%0/2
$branches->{$name} or []
l!l
00
MC/DC0%0/1
$branches->{$name} or []
$branches->{$name}
Truth table0%0/2
A
A= $branches->{$name}
Aresult
11
00
292 : (undef),
293 );
294 }
295  
296 1 1;
297  
298 __END__
« blib/lib/Shipwright/Script/Import.pm ↑ Summary blib/lib/Shipwright/Script/List.pm »