Carton/CLI.pm untested

filter: Statementstmt 0.0% 0 / 246 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 31 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 40 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 410 CC 27 SCAR 26.8
CC
27
Cov
0%
cmd_install
42.0
run
30.0
cmd_check
30.0
CRAP 14.5
1 package Carton::CLI;
2 use strict;
3 use warnings;
4 use Config;
5 use Getopt::Long;
6 use Path::Tiny;
7 use Try::Tiny;
8 use Module::CoreList;
9 use Scalar::Util qw(blessed);
10  
11 use Carton;
12 use Carton::Builder;
13 use Carton::Mirror;
14 use Carton::Snapshot;
15 use Carton::Util;
16 use Carton::Environment;
17 use Carton::Error;
18  
19 use constant { SUCCESS => 0, INFO => 1, WARN => 2, ERROR => 3 };
20  
21 our $UseSystem = 0; # 1 for unit testing
22  
23 use Class::Tiny {
24 verbose => undef,
25 carton => sub { $_[0]->_build_carton },
26 mirror => sub { $_[0]->_build_mirror },
27 };
28  
29 sub _build_mirror {
30 my $self = shift;
31 Carton::Mirror->new($ENV{PERL_CARTON_MIRROR} || $Carton::Mirror::DefaultMirror);
32 }
33  
34 sub run {
35 my($self, @args) = @_;
36  
37 my @commands;
38 my $p = Getopt::Long::Parser->new(
39 config => [ "no_ignore_case", "pass_through" ],
40 );
41 $p->getoptionsfromarray(
42 \@args,
43 "h|help" => sub { unshift @commands, 'help' },
44 "v|version" => sub { unshift @commands, 'version' },
45 "verbose!" => sub { $self->verbose($_[1]) },
46 );
47  
48 push @commands, @args;
49  
50 my $cmd = shift @commands || 'install';
51  
52 my $code = try {
53 my $call = $self->can("cmd_$cmd")
54 or Carton::Error::CommandNotFound->throw(error => "Could not find command '$cmd'");
55 $self->$call(@commands);
56 return 0;
57 } catch {
58 die $_ unless blessed $_ && $_->can('rethrow');
59  
60 if ($_->isa('Carton::Error::CommandExit')) {
61 return $_->code || 255;
62 } elsif ($_->isa('Carton::Error::CommandNotFound')) {
63 warn $_->error, "\n\n";
64 $self->cmd_usage;
65 return 255;
66 } elsif ($_->isa('Carton::Error')) {
67 warn $_->error, "\n";
68 return 255;
69 }
70 };
71  
72 return $code;
73 }
74  
75 sub commands {
76 my $self = shift;
77  
78 no strict 'refs';
79 map { s/^cmd_//; $_ }
80 grep { /^cmd_.*/ && $self->can($_) } sort keys %{__PACKAGE__."::"};
81 }
82  
83 sub cmd_usage {
84 my $self = shift;
85 $self->print(<<HELP);
86 Usage: carton <command>
87  
88 where <command> is one of:
89 @{[ join ", ", $self->commands ]}
90  
91 Run carton -h <command> for help.
92 HELP
93 }
94  
95 sub parse_options {
96 my($self, $args, @spec) = @_;
97 my $p = Getopt::Long::Parser->new(
98 config => [ "no_auto_abbrev", "no_ignore_case" ],
99 );
100 $p->getoptionsfromarray($args, @spec);
101 }
102  
103 sub parse_options_pass_through {
104 my($self, $args, @spec) = @_;
105  
106 my $p = Getopt::Long::Parser->new(
107 config => [ "no_auto_abbrev", "no_ignore_case", "pass_through" ],
108 );
109 $p->getoptionsfromarray($args, @spec);
110  
111 # with pass_through keeps -- in args
112 shift @$args if $args->[0] && $args->[0] eq '--';
113 }
114  
115 sub printf {
116 my $self = shift;
117 my $type = pop;
118 my($temp, @args) = @_;
119 $self->print(sprintf($temp, @args), $type);
120 }
121  
122 sub print {
123 my($self, $msg, $type) = @_;
124 my $fh = $type && $type >= WARN ? *STDERR : *STDOUT;
125 print {$fh} $msg;
126 }
127  
128 sub error {
129 my($self, $msg) = @_;
130 $self->print($msg, ERROR);
131 Carton::Error::CommandExit->throw;
132 }
133  
134 sub cmd_help {
135 my $self = shift;
136 my $module = $_[0] ? ("Carton::Doc::" . ucfirst $_[0]) : "Carton.pm";
137 system "perldoc", $module;
138 }
139  
140 sub cmd_version {
141 my $self = shift;
142 $self->print("carton $Carton::VERSION\n");
143 }
144  
145 sub cmd_bundle {
146 my($self, @args) = @_;
147  
148 my $env = Carton::Environment->build;
149 $env->snapshot->load;
150  
151 $self->print("Bundling modules using @{[$env->cpanfile]}\n");
152  
153 my $builder = Carton::Builder->new(
154 mirror => $self->mirror,
155 cpanfile => $env->cpanfile,
156 );
157 $builder->bundle($env->install_path, $env->vendor_cache, $env->snapshot);
158  
159 $self->printf("Complete! Modules were bundled into %s\n", $env->vendor_cache, SUCCESS);
160 }
161  
162 sub cmd_fatpack {
163 my($self, @args) = @_;
164  
165 my $env = Carton::Environment->build;
166 require Carton::Packer;
167 Carton::Packer->new->fatpack_carton($env->vendor_bin);
168 }
169  
170 sub cmd_install {
171 my($self, @args) = @_;
172  
173 my($install_path, $cpanfile_path, @without);
174  
175 $self->parse_options(
176 \@args,
177 "p|path=s" => \$install_path,
178 "cpanfile=s" => \$cpanfile_path,
179 "without=s" => sub { push @without, split /,/, $_[1] },
180 "deployment!" => \my $deployment,
181 "cached!" => \my $cached,
182 );
183  
184 my $env = Carton::Environment->build($cpanfile_path, $install_path);
185 $env->snapshot->load_if_exists;
186  
187 if ($deployment && !$env->snapshot->loaded) {
188 $self->error("--deployment requires cpanfile.snapshot: Run `carton install` and make sure cpanfile.snapshot is checked into your version control.\n");
189 }
190  
191 my $builder = Carton::Builder->new(
192 cascade => 1,
193 mirror => $self->mirror,
194 without => \@without,
195 cpanfile => $env->cpanfile,
196 );
197  
198 # TODO: --without with no .lock won't fetch the groups, resulting in insufficient requirements
199  
200 if ($deployment) {
201 $self->print("Installing modules using @{[$env->cpanfile]} (deployment mode)\n");
202 $builder->cascade(0);
203 } else {
204 $self->print("Installing modules using @{[$env->cpanfile]}\n");
205 }
206  
207 # TODO merge CPANfile git to mirror even if lock doesn't exist
208 if ($env->snapshot->loaded) {
209 my $index_file = $env->install_path->child("cache/modules/02packages.details.txt");
210 $index_file->parent->mkpath;
211  
212 $env->snapshot->write_index($index_file);
213 $builder->index($index_file);
214 }
215  
216 if ($cached) {
217 $builder->mirror(Carton::Mirror->new($env->vendor_cache));
218 }
219  
220 $builder->install($env->install_path);
221  
222 unless ($deployment) {
223 $env->cpanfile->load;
224 $env->snapshot->find_installs($env->install_path, $env->cpanfile->requirements);
225 $env->snapshot->save;
226 }
227  
228 $self->print("Complete! Modules were installed into @{[$env->install_path]}\n", SUCCESS);
229 }
230  
231 sub cmd_show {
232 my($self, @args) = @_;
233  
234 my $env = Carton::Environment->build;
235 $env->snapshot->load;
236  
237 for my $module (@args) {
238 my $dist = $env->snapshot->find($module)
239 or $self->error("Couldn't locate $module in cpanfile.snapshot\n");
240 $self->print( $dist->name . "\n" );
241 }
242 }
243  
244 sub cmd_list {
245 my($self, @args) = @_;
246  
247 my $format = 'name';
248  
249 $self->parse_options(
250 \@args,
251 "distfile" => sub { $format = 'distfile' },
252 );
253  
254 my $env = Carton::Environment->build;
255 $env->snapshot->load;
256  
257 for my $dist ($env->snapshot->distributions) {
258 $self->print($dist->$format . "\n");
259 }
260 }
261  
262 sub cmd_tree {
263 my($self, @args) = @_;
264  
265 my $env = Carton::Environment->build;
266 $env->snapshot->load;
267 $env->cpanfile->load;
268  
269 my %seen;
270 my $dumper = sub {
271 my($dependency, $reqs, $level) = @_;
272 return if $level == 0;
273 return Carton::Tree::STOP if $dependency->dist->is_core;
274 return Carton::Tree::STOP if $seen{$dependency->distname}++;
275 $self->printf( "%s%s (%s)\n", " " x ($level - 1), $dependency->module, $dependency->distname, INFO );
276 };
277  
278 $env->tree->walk_down($dumper);
279 }
280  
281 sub cmd_check {
282 my($self, @args) = @_;
283  
284 my $cpanfile_path;
285 $self->parse_options(
286 \@args,
287 "cpanfile=s" => \$cpanfile_path,
288 );
289  
290 my $env = Carton::Environment->build($cpanfile_path);
291 $env->snapshot->load;
292 $env->cpanfile->load;
293  
294 # TODO remove snapshot
295 # TODO pass git spec to Requirements?
296 my $merged_reqs = $env->tree->merged_requirements;
297  
298 my @missing;
299 for my $module ($merged_reqs->required_modules) {
300 my $install = $env->snapshot->find_or_core($module);
301 if ($install) {
302 unless ($merged_reqs->accepts_module($module => $install->version_for($module))) {
303 push @missing, [ $module, 1, $install->version_for($module) ];
304 }
305 } else {
306 push @missing, [ $module, 0 ];
307 }
308 }
309  
310 if (@missing) {
311 $self->print("Following dependencies are not satisfied.\n", INFO);
312 for my $missing (@missing) {
313 my($module, $unsatisfied, $version) = @$missing;
314 if ($unsatisfied) {
315 $self->printf(" %s has version %s. Needs %s\n",
316 $module, $version, $merged_reqs->requirements_for_module($module), INFO);
317 } else {
318 $self->printf(" %s is not installed. Needs %s\n",
319 $module, $merged_reqs->requirements_for_module($module), INFO);
320 }
321 }
322 $self->printf("Run `carton install` to install them.\n", INFO);
323 Carton::Error::CommandExit->throw;
324 } else {
325 $self->print("cpanfile's dependencies are satisfied.\n", INFO);
326 }
327 }
328  
329 sub cmd_update {
330 my($self, @args) = @_;
331  
332 my $env = Carton::Environment->build;
333 $env->cpanfile->load;
334  
335  
336 my $cpanfile = Module::CPANfile->load($env->cpanfile);
337 @args = grep { $_ ne 'perl' } $env->cpanfile->required_modules unless @args;
338  
339 $env->snapshot->load;
340  
341 my @modules;
342 for my $module (@args) {
343 my $dist = $env->snapshot->find_or_core($module)
344 or $self->error("Could not find module $module.\n");
345 next if $dist->is_core;
346 push @modules, "$module~" . $env->cpanfile->requirements_for_module($module);
347 }
348  
349 return unless @modules;
350  
351 my $builder = Carton::Builder->new(
352 mirror => $self->mirror,
353 cpanfile => $env->cpanfile,
354 );
355 $builder->update($env->install_path, @modules);
356  
357 $env->snapshot->find_installs($env->install_path, $env->cpanfile->requirements);
358 $env->snapshot->save;
359 }
360  
361 sub cmd_run {
362 my($self, @args) = @_;
363  
364 local $UseSystem = 1;
365 $self->cmd_exec(@args);
366 }
367  
368 sub cmd_exec {
369 my($self, @args) = @_;
370  
371 my $env = Carton::Environment->build;
372 $env->snapshot->load;
373  
374 # allows -Ilib
375 @args = map { /^(-[I])(.+)/ ? ($1,$2) : $_ } @args;
376  
377 while (@args) {
378 if ($args[0] eq '-I') {
379 warn "exec -Ilib is deprecated. You might want to run: carton exec perl -Ilib ...\n";
380 splice(@args, 0, 2);
381 } else {
382 last;
383 }
384 }
385  
386 $self->parse_options_pass_through(\@args); # to handle --
387  
388 unless (@args) {
389 $self->error("carton exec needs a command to run.\n");
390 }
391  
392 # PERL5LIB takes care of arch
393 my $path = $env->install_path;
394 local $ENV{PERL5LIB} = "$path/lib/perl5";
395 local $ENV{PATH} = "$path/bin:$ENV{PATH}";
396  
397 if ($UseSystem) {
398 system @args;
399 } else {
400 exec @args;
401 exit 127; # command not found
402 }
403 }
404  
405 1;