Carton/CLI.pm

filter: Statementstmt 21.1% 41 / 194 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 26.5% 13 / 49 Podpod 0.0% 0 / 25 totaltotal 15.4% 54 / 350 CC 36 SCAR 27.2
CC
36
Cov
16%
cmd_check
56.0
run
30.0
print
30.0
CRAP 15.1
« Carton/Tree.pm ↑ Summary Carton/Util.pm »
1 package Carton::CLI;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Term::ANSIColor qw(colored);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Carton;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Carton::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Carton::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Carton::Tree;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use constant { SUCCESS => 0, INFO => 1, WARN => 2, ERROR => 3 };
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 our $Colors = {
19 SUCCESS, => 'green',
20 WARN, => 'yellow',
21 INFO, => 'cyan',
22 ERROR, => 'red',
23 };
24  
25 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
26 0 my $class = shift;
27 0 bless {
28 color => 1,
29 verbose => 0,
30 }, $class;
31 }
32  
33 0 ▶sub carton {
Subroutine carton: not called CC=2 SCAR=17.9
Pod undocumented
34 0 my $self = shift;
35 0 ▶ $self->{carton} ||= Carton->new;
Condition0%0/3
$self->{"carton"} ||= "Carton"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"carton"} ||= "Carton"->new
$self->{"carton"} "Carton"->new
Truth table0%0/3
A ||= B
A= $self->{"carton"}B= "Carton"->new
ABresult
1X1
011
000
36 }
37  
38 0 ▶sub work_file {
Subroutine work_file: not called CC=1 SCAR=6.9
Pod undocumented
39 0 my($self, $file) = @_;
40 0 return "$self->{work_dir}/$file";
41 }
42  
43 0 ▶sub run {
Subroutine run: not called CC=5 SCAR=34.0
Pod undocumented
44 0 my($self, @args) = @_;
45  
46 0 ▶ $self->{work_dir} = $ENV{PERL_CARTON_HOME} || (Cwd::cwd() . "/.carton");
Condition0%0/2
$ENV{'PERL_CARTON_HOME'} || Cwd::cwd() . '/.carton'
l!l
00
MC/DC0%0/1
$ENV{'PERL_CARTON_HOME'} || Cwd::cwd() . '/.carton'
$ENV{'PERL_CARTON_HOME'}
Truth table0%0/2
A
A= $ENV{'PERL_CARTON_HOME'}
Aresult
11
00
47 0 ▶ mkdir $self->{work_dir}, 0777 unless -e $self->{work_dir};
Branch: 1 branch
BranchTrueFalseTotal
unless -e $self->{"work_dir"} 0 0 0
48  
49 0 local @ARGV = @args;
50 0 my @commands;
51 0 my $p = Getopt::Long::Parser->new(
52 config => [ "no_ignore_case", "pass_through" ],
53 );
54 $p->getoptions(
55 0 ▶ "h|help" => sub { unshift @commands, 'help' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
56 0 ▶ "v|version" => sub { unshift @commands, 'version' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
57 "color!" => \$self->{color},
58 "verbose!" => \$self->{verbose},
59 0 );
60  
61 0 push @commands, @ARGV;
62  
63 0 ▶ my $cmd = shift @commands || 'install';
Condition0%0/2
shift @commands || 'install'
l!l
00
MC/DC0%0/1
shift @commands || 'install'
shift @commands
Truth table0%0/2
A
A= shift @commands
Aresult
11
00
64 0 my $call = $self->can("cmd_$cmd");
65  
66 0 ▶ if ($call) {
Branch: 1 branch
BranchTrueFalseTotal
if ($call) { } 0 0 0
67 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 0 $self->$call(@commands);
69 0 ▶ } catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
70 0 ▶ /Carton::Error::CommandExit/ and return;
Branch: 1 branch
BranchTrueFalseTotal
/Carton::Error::CommandExit/ and return 0 0 0
71 0 die $_;
72 }
73 0 } else {
74 0 $self->error("Could not find command '$cmd'\n");
75 }
76 }
77  
78 0 ▶sub commands {
Subroutine commands: not called CC=1 SCAR=2.2
Pod undocumented
79 0 my $self = shift;
80  
81 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
82 0 map { s/^cmd_//; $_ }
83 0 grep /^cmd_(.*)/, sort keys %{__PACKAGE__."::"};
84 }
85  
86 0 ▶sub cmd_usage {
Subroutine cmd_usage: not called CC=1 SCAR=6.9
Pod undocumented
87 0 my $self = shift;
88 0 $self->print(<<HELP);
89 Usage: carton <command>
90  
91 where <command> is one of:
92 0 @{[ join ", ", $self->commands ]}
93  
94 Run carton -h <command> for help.
95 HELP
96 }
97  
98 0 ▶sub parse_options {
Subroutine parse_options: not called CC=1 SCAR=6.9
Pod undocumented
99 0 my($self, $args, @spec) = @_;
100 0 Getopt::Long::GetOptionsFromArray($args, @spec);
101 }
102  
103 0 ▶sub printf {
Subroutine printf: not called CC=1 SCAR=6.9
Pod undocumented
104 0 my $self = shift;
105 0 my $type = pop;
106 0 my($temp, @args) = @_;
107 0 $self->print(sprintf($temp, @args), $type);
108 }
109  
110 0 ▶sub print {
Subroutine print: not called CC=5 SCAR=34.0
Pod undocumented
111 0 my($self, $msg, $type) = @_;
112 0 ▶ $msg = colored $msg, $Colors->{$type} if defined $type && $self->{color};
Branch: 1 branch
BranchTrueFalseTotal
if defined $type and $self->{"color"} 0 0 0
Condition0%0/3
defined $type and $self->{"color"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $type and $self->{"color"}
defined $type $self->{"color"}
Truth table0%0/3
A and B
A= defined $typeB= $self->{"color"}
ABresult
0X0
100
111
113 0 ▶ my $fh = $type && $type >= WARN ? *STDERR : *STDOUT;
Branch: 1 branch
BranchTrueFalseTotal
$type && $type >= 2 ? : 0 0 0
Condition0%0/3
$type and $type >= 2
!ll&&!rl&&r
000
MC/DC0%0/2
$type and $type >= 2
$type $type >= 2
Truth table0%0/3
A and B
A= $typeB= $type >= 2
ABresult
0X0
100
111
114 0 print {$fh} $msg;
115 }
116  
117 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
Pod undocumented
118 0 my($self, $msg) = @_;
119 0 $self->print($msg, ERROR);
120 0 Carton::Error::CommandExit->throw;
121 }
122  
123 0 ▶sub cmd_help {
Subroutine cmd_help: not called CC=2 SCAR=17.9
Pod undocumented
124 0 my $self = shift;
125 0 ▶ my $module = $_[0] ? ("Carton::Doc::" . ucfirst $_[0]) : "Carton";
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 0 0 0
126 0 system "perldoc", $module;
127 }
128  
129 0 ▶sub cmd_version {
Subroutine cmd_version: not called CC=1 SCAR=6.9
Pod undocumented
130 0 my $self = shift;
131 0 $self->print("carton $Carton::VERSION\n");
132 }
133  
134 0 ▶sub cmd_bundle {
Subroutine cmd_bundle: not called CC=3 SCAR=24.8
Pod undocumented
135 0 my($self, @args) = @_;
136  
137 0 ▶ $self->parse_options(\@args, "p|path=s" => sub { $self->carton->{path} = $_[1] });
Subroutine __ANON__: not called CC=1 SCAR=6.9
138  
139 0 my $lock = $self->find_lock;
140 0 my $local_mirror = $self->carton->local_mirror;
141  
142 0 $self->carton->configure(
143 lock => $lock,
144 mirror_file => $self->mirror_file,
145 );
146  
147 0 my $cpanfile = $self->has_cpanfile;
148  
149 0 ▶ if ($cpanfile && $lock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cpanfile and $lock) { } 0 0 0
Condition0%0/3
$cpanfile and $lock
!ll&&!rl&&r
000
MC/DC0%0/2
$cpanfile and $lock
$cpanfile $lock
Truth table0%0/3
A and B
A= $cpanfileB= $lock
ABresult
0X0
100
111
150 0 $self->print("Bundling modules using $cpanfile\n");
151 0 $self->carton->download_from_cpanfile($cpanfile, $local_mirror);
152 } else {
153 0 $self->error("Can't locate cpanfile and lock file. Run carton install first\n");
154 }
155  
156 0 $self->printf("Complete! Modules were bundled into %s\n", $local_mirror, SUCCESS);
157 }
158  
159 0 ▶sub cmd_install {
Subroutine cmd_install: not called CC=5 SCAR=34.0
Pod undocumented
160 0 my($self, @args) = @_;
161  
162 $self->parse_options(
163 \@args,
164 0 ▶ "p|path=s" => sub { $self->carton->{path} = $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
165 "deployment!" => \$self->{deployment},
166 "cached!" => \$self->{use_local_mirror},
167 0 );
168  
169 0 my $lock = $self->find_lock;
170 0 my $local_mirror = $self->carton->local_mirror;
171  
172 $self->carton->configure(
173 lock => $lock,
174 mirror_file => $self->mirror_file, # $lock object?
175 0 ▶ ( $self->{use_local_mirror} && -d $local_mirror ? (mirror => $local_mirror) : () ),
Branch: 1 branch
BranchTrueFalseTotal
$self->{'use_local_mirror'} && -d $local_mirror ? : 0 0 0
Condition0%0/3
$self->{"use_local_mirror"} and -d $local_mirror
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"use_local_mirror"} and -d $local_mirror
$self->{"use_local_mirror"} -d $local_mirror
Truth table0%0/3
A and B
A= $self->{"use_local_mirror"}B= -d $local_mirror
ABresult
0X0
100
111
176 );
177  
178 0 my $cpanfile = $self->has_cpanfile;
179  
180 0 ▶ if (!$cpanfile) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $cpanfile) { } 0 0 0
elsif ($self->{'deployment'}) { } 0 0 0
181 0 $self->error("Can't locate cpanfile.\n");
182 } elsif ($self->{deployment}) {
183 0 $self->print("Installing modules using $cpanfile (deployment mode)\n");
184 0 $self->carton->install_from_cpanfile($cpanfile);
185 } else {
186 0 $self->print("Installing modules using $cpanfile\n");
187 0 $self->carton->install_from_cpanfile($cpanfile, 1);
188 0 $self->carton->update_lock_file($self->lock_file);
189 }
190  
191 0 $self->printf("Complete! Modules were installed into %s\n", $self->carton->{path}, SUCCESS);
192 }
193  
194 0 ▶sub mirror_file {
Subroutine mirror_file: not called CC=1 SCAR=6.9
Pod undocumented
195 0 my $self = shift;
196 0 return $self->work_file("02packages.details.txt");
197 }
198  
199 0 ▶sub has_cpanfile {
Subroutine has_cpanfile: not called CC=2 SCAR=17.9
Pod undocumented
200 0 my $self = shift;
201  
202 0 ▶ return 'cpanfile' if -e 'cpanfile';
Branch: 1 branch
BranchTrueFalseTotal
if -e "cpanfile" 0 0 0
203 0 return;
204 }
205  
206 0 ▶sub cmd_show {
Subroutine cmd_show: not called CC=4 SCAR=30.0
Pod undocumented
207 0 my($self, @args) = @_;
208  
209 0 ▶ my $lock = $self->find_lock
Branch: 1 branch
BranchTrueFalseTotal
my $lock = $self->find_lock or $self->error("Can't find carton.lock: Run `carton install`\n") 0 0 0
210 or $self->error("Can't find carton.lock: Run `carton install`\n");
211 0 my $index = $self->carton->build_index($lock->{modules});
212  
213 0 for my $module (@args) {
214 my $meta = $index->{$module}{meta}
215 0 ▶ or $self->error("Couldn't locate $module in carton.lock\n");
Branch: 1 branch
BranchTrueFalseTotal
my $meta = $index->{$module}{"meta"} or $self->error("Couldn't locate $module in carton.lock\n") 0 0 0
216 0 $self->print( Carton::Util::to_json($meta) );
217 }
218 }
219  
220 0 ▶sub cmd_tree {
Subroutine cmd_tree: not called CC=1 SCAR=6.9
Pod undocumented
221 0 my $self = shift;
222 0 $self->cmd_list("--tree", @_);
223 }
224  
225 0 ▶sub cmd_list {
Subroutine cmd_list: not called CC=5 SCAR=34.0
Pod undocumented
226 0 my($self, @args) = @_;
227  
228 0 my $tree_mode;
229 0 $self->parse_options(\@args, "tree!" => \$tree_mode);
230  
231 0 ▶ my $lock = $self->find_lock
Branch: 1 branch
BranchTrueFalseTotal
my $lock = $self->find_lock or $self->error("Can't find carton.lock: Run `carton install` to rebuild the lock file.\n") 0 0 0
232 or $self->error("Can't find carton.lock: Run `carton install` to rebuild the lock file.\n");
233  
234 0 ▶ if ($tree_mode) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tree_mode) { } 0 0 0
235 0 my $tree = $self->carton->build_tree($lock->{modules});
236 0 ▶ $self->carton->walk_down_tree($tree, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
237 0 my($module, $depth) = @_;
238 0 my $line = " " x $depth . "$module->{dist}\n";
239 0 $self->print($line);
240 0 });
241 } else {
242 0 ▶ for my $module (values %{$lock->{modules} || {}}) {
Condition0%0/2
$lock->{'modules'} || {}
l!l
00
MC/DC0%0/1
$lock->{'modules'} || {}
$lock->{'modules'}
Truth table0%0/2
A
A= $lock->{'modules'}
Aresult
11
00
243 0 $self->print("$module->{dist}\n");
244 }
245 }
246 }
247  
248 0 ▶sub cmd_check {
Subroutine cmd_check: not called CC=7 SCAR=40.3
Pod undocumented
249 0 my($self, @args) = @_;
250  
251 0 ▶ my $file = $self->has_cpanfile
Branch: 1 branch
BranchTrueFalseTotal
my $file = $self->has_cpanfile or $self->error("Can't find a build file: nothing to check.\n") 0 0 0
252 or $self->error("Can't find a build file: nothing to check.\n");
253  
254 0 ▶ $self->parse_options(\@args, "p|path=s", sub { $self->carton->{path} = $_[1] });
Subroutine __ANON__: not called CC=1 SCAR=6.9
255  
256 0 my $lock = $self->carton->build_lock;
257 0 my @deps = $self->carton->list_dependencies;
258  
259 0 my $res = $self->carton->check_satisfies($lock, \@deps);
260  
261 0 my $ok = 1;
262 0 ▶ if (@{$res->{unsatisfied}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$res->{"unsatisfied"};}) 0 0 0
263 0 $self->print("Following dependencies are not satisfied. Run `carton install` to install them.\n", WARN);
264 0 for my $dep (@{$res->{unsatisfied}}) {
265 0 ▶ $self->print("$dep->{module} " . ($dep->{version} ? "($dep->{version})" : "") . "\n");
Branch: 1 branch
BranchTrueFalseTotal
$dep->{'version'} ? : 0 0 0
266 }
267 0 $ok = 0;
268 }
269  
270 0 ▶ if ($res->{superflous}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res->{"superflous"}) 0 0 0
271 $self->printf("Following modules are found in %s but couldn't be tracked from your $file\n",
272 0 $self->carton->{path}, WARN);
273 0 ▶ $self->carton->walk_down_tree($res->{superflous}, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
274 0 my($module, $depth) = @_;
275 0 my $line = " " x $depth . "$module->{dist}\n";
276 0 $self->print($line);
277 0 }, 1);
278 0 $ok = 0;
279 }
280  
281 0 ▶ if ($ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ok) 0 0 0
282 $self->printf("Dependencies specified in your $file are satisfied and matches with modules in %s.\n",
283 0 $self->carton->{path}, SUCCESS);
284 }
285 }
286  
287 0 ▶sub cmd_update {
Subroutine cmd_update: not called CC=1 SCAR=6.9
Pod undocumented
288 # "cleanly" update distributions in extlib
289 # rebuild the tree, update modules with DFS
290 0 die <<EOF;
291 carton update is not implemented yet.
292  
293 The command is supposed to update all the dependencies to the latest
294 version as if you don't have the current local environment doesn't
295 exist.
296  
297 For now, you can remove the local environment and re-run carton install
298 to get the similar functionality.
299  
300 EOF
301  
302 }
303  
304 0 ▶sub cmd_exec {
Subroutine cmd_exec: not called CC=3 SCAR=24.8
Pod undocumented
305 0 my($self, @args) = @_;
306  
307 # allows -Ilib
308 0 ▶ @args = map { /^(-[I])(.+)/ ? ($1,$2) : $_ } @args;
Branch: 1 branch
BranchTrueFalseTotal
/^(-[I])(.+)/ ? : 0 0 0
309  
310 0 my $system; # for unit testing
311 my @include;
312 0 $self->parse_options(\@args, 'I=s@', \@include, "system", \$system);
313  
314 0 my $path = $self->carton->{path};
315 0 my $lib = join ",", @include, "$path/lib/perl5", ".";
316  
317 0 local $ENV{PERL5OPT} = "-Mlib::core::only -Mlib=$lib";
318 0 local $ENV{PATH} = "$path/bin:$ENV{PATH}";
319  
320 0 ▶ $system ? system(@args) : exec(@args);
Branch: 1 branch
BranchTrueFalseTotal
$system ? : 0 0 0
321 }
322  
323 0 ▶sub find_lock {
Subroutine find_lock: not called CC=2 SCAR=17.9
Pod undocumented
324 0 my $self = shift;
325  
326 0 ▶ if (-e $self->lock_file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $self->lock_file) 0 0 0
327 0 return $self->lock_data; # TODO object
328 }
329  
330 0 return;
331 }
332  
333 0 ▶sub lock_data {
Subroutine lock_data: not called CC=1 SCAR=6.9
Pod undocumented
334 0 my $self = shift;
335  
336 0 my $lock;
337 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
338 0 $lock = Carton::Util::load_json($self->lock_file);
339 0 ▶ } catch {
Subroutine __ANON__: not called CC=2 SCAR=17.9
340 0 ▶ if (/No such file/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/No such file/) { } 0 0 0
341 0 $self->error("Can't locate carton.lock\n");
342 } else {
343 0 $self->error("Can't parse carton.lock: $_\n");
344 }
345 0 };
346  
347 0 return $lock;
348 }
349  
350 0 ▶sub lock_file {
Subroutine lock_file: not called CC=1 SCAR=6.9
Pod undocumented
351 0 my $self = shift;
352 0 return 'carton.lock';
353 }
354  
355  
356 1 1;
« Carton/Tree.pm ↑ Summary Carton/Util.pm »