cpm/CLI.pm untested

filter: Statementstmt 0.0% 0 / 490 Branchbran 0.0% 0 / 270 Conditioncond 0.0% 0 / 80 MC/DCmcdc 0.0% 0 / 45 Subroutinesub 0.0% 0 / 63 Podpod 0.0% 0 / 27 totaltotal 0.0% 0 / 975 CC 136 SCAR 49.9
CC
136
Cov
0%
parse_options
462.0
cmd_install
342.0
initial_task
240.0
CRAP 146.6
1 package App::cpm::CLI;
2 use v5.24;
3 use warnings;
4 use experimental qw(lexical_subs signatures);
5  
6 use App::cpm::Context;
7 use App::cpm::DistNotation;
8 use App::cpm::Distribution;
9 use App::cpm::Logger::File;
10 use App::cpm::Logger;
11 use App::cpm::Master;
12 use App::cpm::Requirement;
13 use App::cpm::Resolver::Cascade;
14 use App::cpm::Resolver::MetaCPAN;
15 use App::cpm::Resolver::MetaDB;
16 use App::cpm::Util qw(WIN32 determine_home maybe_abs terminal_width);
17 use App::cpm::Worker;
18 use App::cpm::version;
19 use App::cpm;
20 use CPAN::Meta;
21 use Command::Runner;
22 use Config;
23 use Cwd ();
24 use Darwin::InitObjC;
25 use ExtUtils::Helpers ();
26 use File::Copy ();
27 use File::Path ();
28 use File::Spec;
29 use Getopt::Long qw(:config no_auto_abbrev no_ignore_case bundling);
30 use Module::CPANfile;
31 use Module::cpmfile;
32 use Parallel::Pipes::App;
33  
34 my @TOP_LEVEL_RELATIONSHIP = qw(requires recommends suggests);
35 my @TOP_LEVEL_PHASE = qw(configure build test runtime develop);
36  
37 sub new ($class, %argv) {
38 my $prebuilt = exists $ENV{PERL_CPM_PREBUILT} && !$ENV{PERL_CPM_PREBUILT} ? 0 : 1;
39 bless {
40 argv => undef,
41 home => determine_home,
42 cwd => Cwd::cwd(),
43 workers => WIN32 ? 1 : 5,
44 snapshot => "cpanfile.snapshot",
45 dependency_file => undef,
46 local_lib => "local",
47 cpanmetadb => "https://cpanmetadb.plackperl.org/v1.0/",
48 _default_mirror => 'https://cpan.metacpan.org/',
49 progress => "auto",
50 final_install => "runtime",
51 configure_timeout => 60,
52 build_timeout => 3600,
53 test_timeout => 1800,
54 top_level_relationship => [qw(requires)],
55 top_level_phase => [qw(configure build test runtime develop)],
56 feature => [],
57 notest => 1,
58 prebuilt => $prebuilt,
59 pureperl_only => 0,
60 static_install => 1,
61 use_install_command => 0,
62 default_resolvers => 1,
63 report_perl_version => !$class->maybe_ci,
64 config_path => undef,
65 http_backend => "auto",
66 %argv
67 }, $class;
68 }
69  
70 sub maybe_ci ($class) {
71 !!grep $ENV{$_}, qw(CI AUTOMATED_TESTING AUTHOR_TESTING);
72 }
73  
74 sub _normalize_progress ($self, $progress) {
75 return if !defined $progress;
76 return $progress if $progress =~ /\A(?:auto|tty|plain)\z/;
77 die "Unknown --progress mode '$progress' (expected: auto, tty, plain)\n";
78 }
79  
80 sub _normalize_final_install ($self, $final_install) {
81 return if !defined $final_install;
82 return $final_install if $final_install =~ /\A(?:runtime|all)\z/;
83 die "Unknown --final-install mode '$final_install' (expected: runtime, all)\n";
84 }
85  
86 sub _normalize_top_level_phase ($self, $phase) {
87 return $phase if grep { $phase eq $_ } @TOP_LEVEL_PHASE;
88 die "Unknown --top-level-phase '$phase' (expected: configure, build, test, runtime, develop)\n";
89 }
90  
91 sub _normalize_top_level_relationship ($self, $relationship) {
92 return $relationship if grep { $relationship eq $_ } @TOP_LEVEL_RELATIONSHIP;
93 die "Unknown --top-level-relationship '$relationship' (expected: requires, recommends, suggests)\n";
94 }
95  
96 sub _set_top_level_phase ($self, @phase) {
97 my %selected;
98 for my $value (@phase) {
99 $selected{$self->_normalize_top_level_phase($_)} = 1 for split /,/, $value;
100 }
101 $self->{top_level_phase} = [grep { $selected{$_} } @TOP_LEVEL_PHASE];
102 }
103  
104 sub _set_top_level_relationship ($self, @relationship) {
105 my %selected;
106 for my $value (@relationship) {
107 $selected{$self->_normalize_top_level_relationship($_)} = 1 for split /,/, $value;
108 }
109 $self->{top_level_relationship} = [grep { $selected{$_} } @TOP_LEVEL_RELATIONSHIP];
110 }
111  
112 sub _warn_deprecated_top_level_option ($self, $option) {
113 warn "$option is deprecated; use --top-level-phase and/or --top-level-relationship instead.\n";
114 }
115  
116 sub _progress_default ($self) {
117 !WIN32 && !$self->maybe_ci && !$self->{verbose} && -t STDERR && terminal_width > 60 ? "tty" : "plain";
118 }
119  
120 sub parse_options ($self, @argv) {
121 local @ARGV = @argv;
122 my ($mirror, @resolver, @feature, $top_level_phase, $top_level_relationship);
123 my $with_relationship_option = sub ($relationship) {
124 (
125 "with-$relationship",
126 sub (@) {
127 $self->_warn_deprecated_top_level_option("--with-$relationship");
128 $self->_set_top_level_relationship($self->{top_level_relationship}->@*, $relationship);
129 },
130 "without-$relationship",
131 sub (@) {
132 $self->_warn_deprecated_top_level_option("--without-$relationship");
133 $self->_set_top_level_relationship(grep { $_ ne $relationship } $self->{top_level_relationship}->@*);
134 },
135 );
136 };
137 my $with_phase_option = sub ($phase) {
138 (
139 "with-$phase",
140 sub (@) {
141 $self->_warn_deprecated_top_level_option("--with-$phase");
142 $self->_set_top_level_phase($self->{top_level_phase}->@*, $phase);
143 },
144 "without-$phase",
145 sub (@) {
146 $self->_warn_deprecated_top_level_option("--without-$phase");
147 $self->_set_top_level_phase(grep { $_ ne $phase } $self->{top_level_phase}->@*);
148 },
149 );
150 };
151  
152 GetOptions
153 "L|local-lib-contained=s" => \($self->{local_lib}),
154 "color!" => \($self->{color}),
155 "g|global" => \($self->{global}),
156 "mirror=s" => \$mirror,
157 "v|verbose" => \($self->{verbose}),
158 "w|workers=i" => \($self->{workers}),
159 "target-perl=s" => \my $target_perl,
160 "test!" => sub ($, $value, @) { $self->{notest} = $value ? 0 : 1 },
161 "cpanfile=s" => sub ($, $value, @) { $self->{dependency_file} = { type => "cpanfile", path => $value } },
162 "cpmfile=s" => sub ($, $value, @) { $self->{dependency_file} = { type => "cpmfile", path => $value } },
163 "metafile=s" => sub ($, $value, @) { $self->{dependency_file} = { type => "metafile", path => $value } },
164 "snapshot=s" => \($self->{snapshot}),
165 "r|resolver=s@" => \@resolver,
166 "default-resolvers!" => \($self->{default_resolvers}),
167 "mirror-only" => \($self->{mirror_only}),
168 "dev" => \($self->{dev}),
169 "man-pages" => \($self->{man_pages}),
170 "home=s" => \($self->{home}),
171 "retry!" => sub (@) {},
172 "exclude-vendor!" => \($self->{exclude_vendor}),
173 "configure-timeout=i" => \($self->{configure_timeout}),
174 "build-timeout=i" => \($self->{build_timeout}),
175 "test-timeout=i" => \($self->{test_timeout}),
176 "final-install=s" => sub ($, $value, @) { $self->{final_install} = $self->_normalize_final_install($value) },
177 "progress=s" => sub ($, $value, @) { $self->{progress} = $self->_normalize_progress($value) },
178 "show-progress!" => sub (@) {},
179 "prebuilt!" => \($self->{prebuilt}),
180 "reinstall" => \($self->{reinstall}),
181 "pp|pureperl|pureperl-only" => \($self->{pureperl_only}),
182 "static-install!" => \($self->{static_install}),
183 "use-install-command!" => \($self->{use_install_command}),
184 "report-perl-version!" => \($self->{report_perl_version}),
185 "top-level-phase=s" => \$top_level_phase,
186 "top-level-relationship=s" => \$top_level_relationship,
187 "with-all" => sub (@) {
188 $self->_warn_deprecated_top_level_option("--with-all");
189 $self->{top_level_relationship} = [@TOP_LEVEL_RELATIONSHIP];
190 $self->{top_level_phase} = [@TOP_LEVEL_PHASE];
191 },
192 (map $with_relationship_option->($_), @TOP_LEVEL_RELATIONSHIP),
193 (map $with_phase_option->($_), @TOP_LEVEL_PHASE),
194 "feature=s@" => \@feature,
195 "show-build-log-on-failure" => \($self->{show_build_log_on_failure}),
196 "experimental-config=s" => \($self->{config_path}),
197 "http-backend=s" => \($self->{http_backend}),
198 or return 0;
199  
200 $self->{local_lib} = maybe_abs($self->{local_lib}, $self->{cwd}) if !$self->{global};
201 $self->{home} = maybe_abs($self->{home}, $self->{cwd});
202 $self->{resolver} = \@resolver;
203 $self->{feature} = \@feature if @feature;
204 $self->{mirror} = $self->normalize_mirror($mirror) if $mirror;
205 $self->{color} = 1 if !defined $self->{color} && -t STDOUT;
206 $self->{progress} = $self->_progress_default if $self->{progress} eq "auto";
207 if (defined $top_level_phase) {
208 $self->_set_top_level_phase($top_level_phase);
209 }
210 if (defined $top_level_relationship) {
211 $self->_set_top_level_relationship($top_level_relationship);
212 }
213 if ($target_perl) {
214 die "--target-perl option conflicts with --global option\n" if $self->{global};
215 # 5.8 is interpreted as 5.800, fix it
216 $target_perl = "v$target_perl" if $target_perl =~ /^5\.[1-9]\d*$/;
217 $target_perl = sprintf '%0.6f', App::cpm::version->parse($target_perl)->numify;
218 $target_perl = '5.008' if $target_perl eq '5.008000';
219 $self->{target_perl} = $target_perl;
220 }
221 if (WIN32 and $self->{workers} != 1) {
222 die "The number of workers must be 1 under WIN32 environment.\n";
223 }
224 if ($self->{pureperl_only} or !$self->{notest} or $self->{man_pages} or $self->{use_install_command}) {
225 $self->{prebuilt} = 0;
226 }
227  
228 $App::cpm::Logger::COLOR = 1 if $self->{color};
229 $App::cpm::Logger::VERBOSE = 1 if $self->{verbose};
230  
231 if (@ARGV) {
232 if ($ARGV[0] eq "-") {
233 my $argv = $self->read_argv_from_stdin;
234 return -1 if $argv->@* == 0;
235 $self->{argv} = $argv;
236 } else {
237 $self->{argv} = \@ARGV;
238 }
239 } elsif (!$self->{dependency_file}) {
240 $self->{dependency_file} = $self->locate_dependency_file;
241 }
242 return 1;
243 }
244  
245 sub read_argv_from_stdin ($self) {
246 my @argv;
247 while (my $line = <STDIN>) {
248 next if $line !~ /\S/;
249 next if $line =~ /^\s*#/;
250 $line =~ s/^\s*//;
251 $line =~ s/\s*$//;
252 push @argv, split /\s+/, $line;
253 }
254 return \@argv;
255 }
256  
257 sub _core_inc ($self) {
258 [
259 (!$self->{exclude_vendor} ? grep {$_} @Config{qw(vendorarch vendorlibexp)} : ()),
260 @Config{qw(archlibexp privlibexp)},
261 ];
262 }
263  
264 sub _search_inc ($self) {
265 return \@INC if $self->{global};
266  
267 my $base = $self->{local_lib};
268 my @local_lib = (
269 App::cpm::Util::maybe_abs(File::Spec->catdir($base, "lib", "perl5", $Config{archname})),
270 App::cpm::Util::maybe_abs(File::Spec->catdir($base, "lib", "perl5")),
271 );
272 if ($self->{target_perl}) {
273 return [@local_lib];
274 } else {
275 return [@local_lib, $self->_core_inc->@*];
276 }
277 }
278  
279 sub normalize_mirror ($self, $mirror) {
280 $mirror =~ s{/*$}{/};
281 return $mirror if $mirror =~ m{^https?://};
282 $mirror =~ s{^file://}{};
283 die "$mirror: No such directory.\n" if !-d $mirror;
284 "file://" . maybe_abs($mirror, $self->{cwd});
285 }
286  
287 sub run ($self, @argv) {
288 my $cmd = shift @argv or die "Need subcommand, try `cpm --help`\n";
289 $cmd = "help" if $cmd =~ /^(-h|--help)$/;
290 $cmd = "version" if $cmd =~ /^(-V|--version)$/;
291 if (my $sub = $self->can("cmd_$cmd")) {
292 return $self->$sub(@argv) if $cmd eq "exec";
293 my $ok = $self->parse_options(@argv);
294 return 1 if !$ok;
295 return 0 if $ok == -1;
296 return $self->$sub;
297 } else {
298 my $message = $cmd =~ /^-/ ? "Missing subcommand" : "Unknown subcommand '$cmd'";
299 die "$message, try `cpm --help`\n";
300 }
301 }
302  
303 sub cmd_version ($self) {
304 my $trial = $App::cpm::TRIAL ? '-TRIAL' : '';
305 print "cpm $App::cpm::VERSION$trial ($0)\n";
306 if ($App::cpm::GIT_DESCRIBE) {
307 print "This is a self-contained version, $App::cpm::GIT_DESCRIBE ($App::cpm::GIT_URL)\n";
308 }
309  
310 printf "perl version v%vd ($^X)\n\n", $^V;
311  
312 print " \%Config:\n";
313 for my $key (qw( archname installsitelib installsitebin installman1dir installman3dir
314 sitearchexp sitelibexp vendorarch vendorlibexp archlibexp privlibexp )) {
315 print " $key=$Config{$key}\n" if $Config{$key};
316 }
317  
318 print " \%ENV:\n";
319 for my $key (grep /^PERL/, sort keys %ENV) {
320 print " $key=$ENV{$key}\n";
321 }
322  
323 print " \@INC:\n";
324 for my $inc (@INC) {
325 print " $inc\n" if ref($inc) ne 'CODE';
326 }
327  
328 return 0;
329 }
330  
331 sub cmd_install ($self) {
332 die "Need arguments or cpm.yml/cpanfile/Build.PL/Makefile.PL\n" if !$self->{argv} && !$self->{dependency_file};
333  
334 local %ENV = %ENV;
335  
336 File::Path::mkpath($self->{home}) if !-d $self->{home};
337 my $now = time;
338 my $log_file = File::Spec->catfile($self->{home}, "build.log.$now");
339 my $work_dir = File::Spec->catdir($self->{home}, "work", "$now.$$");
340 my $cache_dir = File::Spec->catdir($self->{home}, "cache");
341  
342 my $ctx = App::cpm::Context->new(
343 http_backend => $self->{http_backend},
344 log_file => $log_file,
345 report_perl_version => $self->{report_perl_version},
346 );
347 $ctx->{logger}->symlink_to("$self->{home}/build.log");
348 my $trial = $App::cpm::TRIAL ? '-TRIAL' : '';
349 $ctx->log("Running cpm $App::cpm::VERSION$trial ($0) on perl $Config{version} built for $Config{archname} ($^X)");
350 $ctx->log("This is a self-contained version, $App::cpm::GIT_DESCRIBE ($App::cpm::GIT_URL)") if $App::cpm::GIT_DESCRIBE;
351 $ctx->log("Command line arguments are: @ARGV");
352 $ctx->log("Experimental config file path is $self->{config_path}") if $self->{config_path};
353 $ctx->log("Work directory is $work_dir");
354  
355 $ctx->log("You have make $ctx->{make}") if $ctx->{make};
356 $ctx->log("You have $ctx->{http_description}");
357 my $unpacker_desc = $ctx->{unpacker}->describe;
358 for my $key (sort keys $unpacker_desc->%*) {
359 $ctx->log("You have $key $unpacker_desc->{$key}");
360 }
361  
362 my ($implicit_install_base, $eumm_argv, $mb_argv) = $self->_parse_builder_env;
363 if ($implicit_install_base or $eumm_argv->@* or $mb_argv->@*) {
364 $ctx->log("Loading configuration from PERL_MM_OPT and PERL_MB_OPT:");
365 $ctx->log(" install_base: $implicit_install_base") if $implicit_install_base;
366 $ctx->log(" ExtUtils::MakeMaker options: $eumm_argv->@*") if $eumm_argv->@*;
367 $ctx->log(" Module::Build options: $mb_argv->@*") if $mb_argv->@*;
368 }
369  
370 $ctx->log("--", `$ctx->{perl} -V`, "--");
371  
372 my $master = App::cpm::Master->new(
373 core_inc => $self->_core_inc,
374 search_inc => $self->_search_inc,
375 global => $self->{global},
376 notest => $self->{notest},
377 progress => $self->{progress},
378 final_install => $self->{final_install},
379 (exists $self->{target_perl} ? (target_perl => $self->{target_perl}) : ()),
380 );
381  
382 my ($packages, $dists, $resolver) = $self->initial_task($ctx, $master);
383 return 0 if !$packages;
384  
385 my $worker = App::cpm::Worker->new(
386 $ctx,
387 verbose => $self->{verbose},
388 home => $self->{home},
389 work_dir => $work_dir,
390 cache_dir => $cache_dir,
391 resolver => $self->generate_resolver($ctx, $resolver),
392 man_pages => $self->{man_pages},
393 prebuilt => $self->{prebuilt},
394 pureperl_only => $self->{pureperl_only},
395 static_install => $self->{static_install},
396 use_install_command => $self->{use_install_command},
397 configure_timeout => $self->{configure_timeout},
398 build_timeout => $self->{build_timeout},
399 test_timeout => $self->{test_timeout},
400 ($self->{global} ? () : (local_lib => $self->{local_lib})),
401 implicit_install_base => $implicit_install_base,
402 eumm_argv => $eumm_argv,
403 mb_argv => $mb_argv,
404 config_path => $self->{config_path},
405 );
406  
407 $master->add_task($ctx, type => "resolve", final_target => 1, $_->%*) for $packages->@*;
408 $_->final_target(1) for $dists->@*;
409 $master->add_distribution($_) for $dists->@*;
410 $self->install($ctx, $master, $worker, $self->{workers});
411 $master->install_distributions($ctx);
412 my $fail = $master->fail($ctx);
413 if ($fail) {
414 local $App::cpm::Logger::VERBOSE = 0;
415 for my $type (qw(install resolve)) {
416 App::cpm::Logger->log(result => "FAIL", type => $type, message => $_) for $fail->{$type}->@*;
417 }
418 }
419 my $installed = $master->installed_distributions;
420 warn sprintf("%d distribution%s installed.\n", $installed, $installed > 1 ? "s" : "");
421 $self->cleanup;
422  
423 if ($fail) {
424 if ($self->{show_build_log_on_failure}) {
425 File::Copy::copy($ctx->{logger}->file, \*STDERR);
426 } else {
427 warn "See $self->{home}/build.log for details.\n";
428 warn "You may want to execute cpm with --show-build-log-on-failure,\n";
429 warn "so that the build.log is automatically dumped on failure.\n";
430 }
431 return 1;
432 } else {
433 return 0;
434 }
435 }
436  
437 sub _parse_builder_env ($class) {
438 my ($install_base, @eumm_argv, @mb_argv);
439 if ($ENV{PERL_MM_OPT}) {
440 my @argv = ExtUtils::Helpers::split_like_shell($ENV{PERL_MM_OPT});
441 while (@argv) {
442 my $arg = shift @argv;
443 if ($arg =~ /^INSTALL_BASE=(.+)/) {
444 $install_base = $1;
445 } else {
446 push @eumm_argv, $arg;
447 }
448 }
449 delete $ENV{PERL_MM_OPT};
450 }
451 if ($ENV{PERL_MB_OPT}) {
452 my @argv = ExtUtils::Helpers::split_like_shell($ENV{PERL_MB_OPT});
453 while (@argv) {
454 my $arg = shift @argv;
455 if ($arg eq "--install_base") {
456 $install_base = shift @argv;
457 } elsif ($arg =~ /^--install_base=(.+)/) {
458 $install_base = $1;
459 } else {
460 push @mb_argv, $arg;
461 }
462 }
463 delete $ENV{PERL_MB_OPT};
464 }
465 ($install_base, \@eumm_argv, \@mb_argv);
466 }
467  
468 sub install ($self, $ctx, $master, $worker, $num) {
469 Darwin::InitObjC::maybe_init();
470  
471 my $tty_progress = $self->{progress} eq "tty";
472 local $App::cpm::Logger::SILENT = $tty_progress ? 1 : 0;
473 my @task = $master->get_task($ctx);
474 Parallel::Pipes::App->run(
475 num => $num,
476 init_work => sub ($pipes) {
477 my @pid = sort { $a <=> $b } keys $pipes->{pipes}->%*;
478 $master->enable_terminal_logger(pids => \@pid, width => terminal_width) if $tty_progress;
479 $master->{terminal_logger}->use_color($self->{color}) if $tty_progress;
480 },
481 before_work => sub ($task, $pipe, @) {
482 $task->in_charge($pipe->{pid});
483 $master->log_task if $tty_progress;
484 },
485 work => sub ($task, @) {
486 return $worker->work($ctx, $task);
487 },
488 after_work => sub ($result, @) {
489 $master->register_result($ctx, $result);
490 @task = $master->get_task($ctx);
491 },
492 idle_tick => $tty_progress ? 0.5 : undef,
493 ($tty_progress
494 ? (idle_work => sub (@) { $master->log_task })
495 : ()),
496 tasks => \@task,
497 );
498 $master->finalize_terminal_logger if $tty_progress;
499 }
500  
501 sub cleanup ($self) {
502 my $week = time - 7*24*60*60;
503 my @entry = glob "$self->{home}/build.log.*";
504 if (opendir my $dh, "$self->{home}/work") {
505 push @entry,
506 map File::Spec->catdir("$self->{home}/work", $_),
507 grep !/^\.{1,2}$/,
508 readdir $dh;
509 }
510 for my $entry (@entry) {
511 my $mtime = (stat $entry)[9];
512 if ($mtime < $week) {
513 if (-d $entry) {
514 File::Path::rmtree($entry);
515 } else {
516 unlink $entry;
517 }
518 }
519 }
520 }
521  
522 sub initial_task ($self, $ctx, $master) {
523 if (!$self->{argv}) {
524 my ($requirement, $reinstall, $resolver) = $self->load_dependency_file($ctx);
525 my ($is_satisfied, @need_resolve) = $master->is_satisfied($requirement);
526 if (!$reinstall->@* and $is_satisfied) {
527 warn "All requirements are satisfied.\n";
528 return;
529 } elsif (!defined $is_satisfied) {
530 my ($req) = grep { $_->{package} eq "perl" } $requirement->@*;
531 die sprintf "%s requires perl %s, but you have only %s\n",
532 $self->{dependency_file}{path}, $req->{version_range}, $self->{target_perl} || $];
533 }
534 my @package = (@need_resolve, $reinstall->@*);
535 return (\@package, [], $resolver);
536 }
537  
538 $self->{mirror} ||= $self->{_default_mirror};
539  
540 my (@package, @dist);
541 for ($self->{argv}->@*) {
542 my $arg = $_; # copy
543 my ($package, $dist);
544 if (-d $arg || -f $arg || $arg =~ s{^file://}{}) {
545 $arg = maybe_abs($arg, $self->{cwd});
546 $dist = App::cpm::Distribution->new(source => "local", uri => "file://$arg", provides => []);
547 } elsif ($arg =~ /(?:^git:|\.git(?:@.+)?$)/) {
548 my %ref = $arg =~ s/(?<=\.git)@(.+)$// ? (ref => $1) : ();
549 $dist = App::cpm::Distribution->new(source => "git", uri => $arg, provides => [], %ref);
550 } elsif ($arg =~ m{^(https?|file)://}) {
551 my ($source, $distfile) = ($1 eq "file" ? "local" : "http", undef);
552 if (my $d = App::cpm::DistNotation->new_from_uri($arg)) {
553 ($source, $distfile) = ("cpan", $d->distfile);
554 }
555 $dist = App::cpm::Distribution->new(
556 source => $source,
557 uri => $arg,
558 $distfile ? (distfile => $distfile) : (),
559 provides => [],
560 );
561 } elsif (my $d = App::cpm::DistNotation->new_from_dist($arg)) {
562 $dist = App::cpm::Distribution->new(
563 source => "cpan",
564 uri => $d->cpan_uri($self->{mirror}),
565 distfile => $d->distfile,
566 provides => [],
567 );
568 } else {
569 my ($name, $version_range, $dev);
570 # copy from Menlo
571 # Plack@1.2 -> Plack~"==1.2"
572 $arg =~ s/^([A-Za-z0-9_:]+)@([v\d\._]+)$/$1~== $2/;
573 # support Plack~1.20, DBI~"> 1.0, <= 2.0"
574 if ($arg =~ /\~[v\d\._,\!<>= ]+$/) {
575 ($name, $version_range) = split '~', $arg, 2;
576 } else {
577 $arg =~ s/[~@]dev$// and $dev++;
578 $name = $arg;
579 }
580 $package = +{
581 package => $name,
582 version_range => $version_range || 0,
583 dev => $dev,
584 reinstall => $self->{reinstall},
585 };
586 }
587 push @package, $package if $package;
588 push @dist, $dist if $dist;
589 }
590  
591 return (\@package, \@dist, undef);
592 }
593  
594 sub locate_dependency_file ($self) {
595 if (-f "cpm.yml") {
596 return { type => "cpmfile", path => "cpm.yml" };
597 }
598 if (-f "cpanfile") {
599 return { type => "cpanfile", path => "cpanfile" };
600 }
601 if (-f 'META.json') {
602 my $meta = CPAN::Meta->load_file('META.json');
603 if (!$meta->dynamic_config) {
604 return { type => 'metafile', path => 'META.json' };
605 }
606 }
607 if (-f 'Build.PL' || -f 'Makefile.PL') {
608 my $build_file = -f 'Build.PL' ? 'Build.PL' : 'Makefile.PL';
609 my @cmd = ($^X, $build_file);
610 warn "Executing $build_file to generate MYMETA.json and to determine requirements...\n";
611 local %ENV = (
612 PERL5_CPAN_IS_RUNNING => 1,
613 PERL5_CPANPLUS_IS_RUNNING => 1,
614 PERL5_CPANM_IS_RUNNING => 1,
615 PERL_MM_USE_DEFAULT => 1,
616 %ENV,
617 );
618 if (!$self->{global}) {
619 my $base = App::cpm::Util::maybe_abs($self->{local_lib});
620 $ENV{PATH} = join $Config{path_sep}, File::Spec->catdir($base, "bin"), ( $ENV{PATH} ? $ENV{PATH} : () );
621 $ENV{PERL5LIB} = join $Config{path_sep}, File::Spec->catdir($base, "lib", "perl5"), ( $ENV{PERL5LIB} ? $ENV{PERL5LIB} : ());
622 if ($self->{use_install_command} && $build_file eq "Makefile.PL") {
623 push @cmd, "INSTALL_BASE=$base";
624 } elsif ($self->{use_install_command}) {
625 push @cmd, "--install_base", $base;
626 }
627 }
628 my $runner = Command::Runner->new(
629 command => \@cmd,
630 timeout => 60,
631 redirect => 1,
632 );
633 my $res = $runner->run;
634 if ($res->{timeout}) {
635 die "Error: timed out (>60s).\n$res->{stdout}";
636 }
637 if ($res->{result} != 0) {
638 die "Error: failed to execute $build_file.\n$res->{stdout}";
639 }
640 if (!-f 'MYMETA.json') {
641 die "Error: No MYMETA.json after executing $build_file\n";
642 }
643 return { type => 'metafile', path => 'MYMETA.json' };
644 }
645 return;
646 }
647  
648 sub load_dependency_file ($self, $ctx) {
649 my $cpmfile = do {
650 my ($type, $path) = $self->{dependency_file}->@{qw(type path)};
651 warn "Loading requirements from $path...\n";
652 if ($type eq "cpmfile") {
653 Module::cpmfile->load($path);
654 } elsif ($type eq "cpanfile") {
655 Module::cpmfile->from_cpanfile(Module::CPANfile->load($path));
656 } elsif ($type eq "metafile") {
657 Module::cpmfile->from_cpanmeta(CPAN::Meta->load_file($path));
658 } else {
659 die;
660 }
661 };
662 if (!$self->{mirror}) {
663 my $mirrors = $cpmfile->{_mirrors} || [];
664 if ($mirrors->@*) {
665 $self->{mirror} = $self->normalize_mirror($mirrors->[0]);
666 } else {
667 $self->{mirror} = $self->{_default_mirror};
668 }
669 }
670 my @phase = $self->{top_level_phase}->@*;
671 my @type = $self->{top_level_relationship}->@*;
672 my $reqs = $cpmfile->effective_requirements($self->{feature}, \@phase, \@type);
673  
674 my (@package, @reinstall);
675 for my $package (sort keys $reqs->%*) {
676 my $options = $reqs->{$package};
677 my $req = {
678 package => $package,
679 version_range => $options->{version},
680 dev => $options->{dev},
681 reinstall => $package ne "perl" && ($self->{reinstall} || $options->{git}) ? 1 : 0,
682 };
683 if ($req->{reinstall}) {
684 push @reinstall, $req;
685 } else {
686 push @package, $req;
687 }
688 }
689  
690 require App::cpm::Resolver::Custom;
691 my $resolver = App::cpm::Resolver::Custom->new(
692 $ctx,
693 requirements => $reqs,
694 mirror => $self->{mirror},
695 from => $self->{dependency_file}{type},
696 );
697 return (\@package, \@reinstall, $resolver->effective ? $resolver : undef);
698 }
699  
700 sub generate_resolver ($self, $ctx, $initial) {
701 my $cascade = App::cpm::Resolver::Cascade->new($ctx);
702 $cascade->add($initial) if $initial;
703 if ($self->{resolver}->@*) {
704 for my $r ($self->{resolver}->@*) {
705 my ($klass, @argv) = split /,/, $r;
706 my $resolver = $self->_generate_resolver($ctx, $klass, @argv);
707 $cascade->add($resolver);
708 }
709 }
710 return $cascade if !$self->{default_resolvers};
711  
712 if ($self->{mirror_only}) {
713 require App::cpm::Resolver::02Packages;
714 my $resolver = App::cpm::Resolver::02Packages->new(
715 $ctx,
716 mirror => $self->{mirror},
717 cache => "$self->{home}/sources",
718 );
719 $cascade->add($resolver);
720 return $cascade;
721 }
722  
723 if (!$self->{argv} and -f $self->{snapshot}) {
724 if (!eval { require App::cpm::Resolver::Snapshot }) {
725 die "To load $self->{snapshot}, you need to install Carton::Snapshot.\n";
726 }
727 warn "Loading distributions from $self->{snapshot}...\n";
728 my $resolver = App::cpm::Resolver::Snapshot->new(
729 $ctx,
730 path => $self->{snapshot},
731 mirror => $self->{mirror},
732 );
733 $cascade->add($resolver);
734 }
735  
736 my $resolver = App::cpm::Resolver::MetaCPAN->new(
737 $ctx,
738 $self->{dev} ? (dev => 1) : (only_dev => 1)
739 );
740 $cascade->add($resolver);
741 $resolver = App::cpm::Resolver::MetaDB->new(
742 $ctx,
743 uri => $self->{cpanmetadb},
744 mirror => $self->{mirror},
745 );
746 $cascade->add($resolver);
747 if (!$self->{dev}) {
748 $resolver = App::cpm::Resolver::MetaCPAN->new($ctx);
749 $cascade->add($resolver);
750 }
751  
752 $cascade;
753 }
754  
755 sub _generate_resolver ($self, $ctx, $klass, @argv) {
756 if ($klass =~ /^metadb$/i) {
757 my ($uri, $mirror);
758 if (@argv > 1) {
759 ($uri, $mirror) = @argv;
760 } elsif (@argv == 1) {
761 $mirror = $argv[0];
762 } else {
763 $mirror = $self->{mirror};
764 }
765 return App::cpm::Resolver::MetaDB->new(
766 $ctx,
767 $uri ? (uri => $uri) : (),
768 mirror => $self->normalize_mirror($mirror),
769 );
770 } elsif ($klass =~ /^metacpan$/i) {
771 return App::cpm::Resolver::MetaCPAN->new($ctx, dev => $self->{dev});
772 } elsif ($klass =~ /^02packages?$/i) {
773 require App::cpm::Resolver::02Packages;
774 my ($path, $mirror);
775 if (@argv > 1) {
776 ($path, $mirror) = @argv;
777 } elsif (@argv == 1) {
778 $mirror = $argv[0];
779 } else {
780 $mirror = $self->{mirror};
781 }
782 return App::cpm::Resolver::02Packages->new(
783 $ctx,
784 $path ? (path => $path) : (),
785 cache => "$self->{home}/sources",
786 mirror => $self->normalize_mirror($mirror),
787 );
788 } elsif ($klass =~ /^snapshot$/i) {
789 require App::cpm::Resolver::Snapshot;
790 return App::cpm::Resolver::Snapshot->new(
791 $ctx,
792 path => $self->{snapshot},
793 mirror => @argv ? $self->normalize_mirror($argv[0]) : $self->{mirror},
794 );
795 }
796 my $full_klass = $klass =~ s/^\+// ? $klass : "App::cpm::Resolver::$klass";
797 (my $file = $full_klass) =~ s{::}{/}g;
798 require "$file.pm"; # may die
799 return $full_klass->new($ctx, @argv);
800 }
801  
802 my $HELP = <<'EOF';
803 Usage: cpm install [OPTIONS...] ARGV...
804  
805 Examples:
806 # install modules into local/
807 > cpm install Module1 Module2 ...
808  
809 # install modules from one of
810 # * cpm.yml
811 # * cpanfile
812 # * META.json (with dynamic_config false)
813 # * Build.PL
814 # * Makefile.PL
815 > cpm install
816  
817 # install module into current @INC instead of local/
818 > cpm install -g Module
819  
820 # read modules from STDIN by specifying "-" as an argument
821 > cat module-list.txt | cpm install -
822  
823 # prefer TRIAL release
824 > cpm install --dev Moose
825  
826 # install modules as if version of your perl is 5.8.5
827 # so that modules which are not core in 5.8.5 will be installed
828 > cpm install --target-perl 5.8.5
829  
830 # resolve distribution names from DARKPAN/modules/02packages.details.txt.gz
831 # and fetch distributions from DARKPAN/authors/id/...
832 > cpm install --resolver 02packages,http://example.com/darkpan Your::Module
833 > cpm install --resolver 02packages,file:///path/to/darkpan Your::Module
834  
835 # read requires and recommends from the top-level cpanfile/cpmfile/metafile input
836 > cpm install --top-level-relationship requires,recommends
837  
838 # read only runtime requirements from the top-level cpanfile/cpmfile/metafile input
839 > cpm install --top-level-phase runtime
840  
841 Options:
842 -w, --workers=N
843 number of workers, default: 5
844 -L, --local-lib-contained=DIR
845 directory to install modules into, default: local/
846 -g, --global
847 install modules into current @INC instead of local/
848 -v, --verbose
849 verbose mode; you can see what is going on
850 --prebuilt, --no-prebuilt
851 save builds for CPAN distributions; and later, install the prebuilts if available
852 default: on; you can also set $ENV{PERL_CPM_PREBUILT} false to disable this option.
853 usage of --test, --man-pages, and/or --use-install-command disables this option.
854 --target-perl=VERSION (EXPERIMENTAL)
855 install modules as if version is your perl is VERSION
856 --mirror=URL
857 base url for the CPAN mirror to use, cannot be used multiple times. Use --resolver instead.
858 default: https://cpan.metacpan.org
859 --pp, --pureperl-only
860 prefer pureperl only build
861 --static-install, --no-static-install
862 enable/disable the static install, default: enable
863 --final-install=runtime|all
864 select which dependency-ready distributions are finally installed:
865 runtime install the requested targets and their runtime dependencies
866 all install every dependency-ready distribution
867 default: runtime
868 --use-install-command, --no-use-install-command
869 use make install or ./Build install for final installation when available.
870 default: off
871 -r, --resolver=class,args (EXPERIMENTAL, will be removed or renamed)
872 specify resolvers, you can use --resolver multiple times
873 available classes: metadb/metacpan/02packages/snapshot
874 --no-default-resolvers
875 even if you specify --resolver, cpm continues using the default resolvers.
876 if you just want to use your resolvers specified by --resolver,
877 you should specify --no-default-resolvers too
878 --reinstall
879 reinstall the distribution even if you already have the latest version installed
880 --dev (EXPERIMENTAL)
881 resolve TRIAL distributions too
882 --color, --no-color
883 turn on/off color output, default: on
884 --test, --no-test
885 run test cases, default: no
886 --man-pages
887 generate man pages
888 --retry, --no-retry
889 no-op compatibility option, will be removed in a future release
890 --show-build-log-on-failure
891 show build.log on failure, default: off
892 --report-perl-version, --no-report-perl-version
893 report your local perl version as part of User-Agent, default: on unless CI is enabled
894 --configure-timeout=sec, --build-timeout=sec, --test-timeout=sec
895 specify configure/build/test timeout second, default: 60sec, 3600sec, 1800sec
896 --progress=auto|tty|plain
897 select the progress mode:
898 auto choose tty for interactive non-Windows non-CI non-verbose terminals, otherwise plain
899 tty use the terminal progress UI
900 plain use plain line-by-line progress output
901 default: auto
902 --show-progress, --no-show-progress
903 no-op compatibility options; use --progress instead
904 --cpmfile=path
905 specify cpmfile path, default: ./cpm.yml
906 --cpanfile=path
907 specify cpanfile path, default: ./cpanfile
908 --metafile=path
909 specify META file path, default: N/A
910 --snapshot=path
911 specify cpanfile.snapshot path, default: ./cpanfile.snapshot
912 -V, --version
913 show version
914 -h, --help
915 show this help
916 --feature=identifier
917 specify the feature to enable in cpmfile/cpanfile/metafile; you can use --feature multiple times
918 --top-level-phase=phase
919 select phases from the top-level cpmfile/cpanfile/metafile input;
920 specifying this option replaces the default phase selection;
921 use a comma-separated list like configure,test
922 default: configure,build,test,runtime,develop
923 --top-level-relationship=relationship
924 select relationships from the top-level cpmfile/cpanfile/metafile input;
925 specifying this option replaces the default relationship selection;
926 use a comma-separated list like requires,recommends
927 default: requires
928 --with-requires, --without-requires (default: with)
929 --with-recommends, --without-recommends (default: without)
930 --with-suggests, --without-suggests (default: without)
931 --with-configure, --without-configure (default: with)
932 --with-build, --without-build (default: with)
933 --with-test, --without-test (default: with)
934 --with-runtime, --without-runtime (default: with)
935 --with-develop, --without-develop (default: without)
936 deprecated compatibility options for top-level dependency selection
937 --with-all
938 shortcut for --with-requires, --with-recommends, --with-suggests,
939 --with-configure, --with-build, --with-test, --with-runtime and --with-develop
940 you can also use --top-level-phase/--top-level-relationship instead
941 --http-backend=auto|httptiny|lwp|curl|wget
942 select the HTTP backend, default: auto
943 --experimental-config=path
944 load distribution-specific installation settings from a JSON file (experimental)
945 EOF
946  
947 sub cmd_help ($self) {
948 print $HELP;
949 return 0;
950 }
951  
952 1;