Commit | Line | Data |
dbcf12a6 |
1 | #line 1 |
2 | package Module::AutoInstall; |
3 | |
4 | use strict; |
5 | use Cwd (); |
6 | use ExtUtils::MakeMaker (); |
7 | |
8 | use vars qw{$VERSION}; |
9 | BEGIN { |
10 | $VERSION = '1.03'; |
11 | } |
12 | |
13 | # special map on pre-defined feature sets |
14 | my %FeatureMap = ( |
15 | '' => 'Core Features', # XXX: deprecated |
16 | '-core' => 'Core Features', |
17 | ); |
18 | |
19 | # various lexical flags |
20 | my ( @Missing, @Existing, %DisabledTests, $UnderCPAN, $HasCPANPLUS ); |
21 | my ( |
22 | $Config, $CheckOnly, $SkipInstall, $AcceptDefault, $TestOnly, $AllDeps |
23 | ); |
24 | my ( $PostambleActions, $PostambleUsed ); |
25 | |
26 | # See if it's a testing or non-interactive session |
27 | _accept_default( $ENV{AUTOMATED_TESTING} or ! -t STDIN ); |
28 | _init(); |
29 | |
30 | sub _accept_default { |
31 | $AcceptDefault = shift; |
32 | } |
33 | |
34 | sub missing_modules { |
35 | return @Missing; |
36 | } |
37 | |
38 | sub do_install { |
39 | __PACKAGE__->install( |
40 | [ |
41 | $Config |
42 | ? ( UNIVERSAL::isa( $Config, 'HASH' ) ? %{$Config} : @{$Config} ) |
43 | : () |
44 | ], |
45 | @Missing, |
46 | ); |
47 | } |
48 | |
49 | # initialize various flags, and/or perform install |
50 | sub _init { |
51 | foreach my $arg ( |
52 | @ARGV, |
53 | split( |
54 | /[\s\t]+/, |
55 | $ENV{PERL_AUTOINSTALL} || $ENV{PERL_EXTUTILS_AUTOINSTALL} || '' |
56 | ) |
57 | ) |
58 | { |
59 | if ( $arg =~ /^--config=(.*)$/ ) { |
60 | $Config = [ split( ',', $1 ) ]; |
61 | } |
62 | elsif ( $arg =~ /^--installdeps=(.*)$/ ) { |
63 | __PACKAGE__->install( $Config, @Missing = split( /,/, $1 ) ); |
64 | exit 0; |
65 | } |
66 | elsif ( $arg =~ /^--default(?:deps)?$/ ) { |
67 | $AcceptDefault = 1; |
68 | } |
69 | elsif ( $arg =~ /^--check(?:deps)?$/ ) { |
70 | $CheckOnly = 1; |
71 | } |
72 | elsif ( $arg =~ /^--skip(?:deps)?$/ ) { |
73 | $SkipInstall = 1; |
74 | } |
75 | elsif ( $arg =~ /^--test(?:only)?$/ ) { |
76 | $TestOnly = 1; |
77 | } |
78 | elsif ( $arg =~ /^--all(?:deps)?$/ ) { |
79 | $AllDeps = 1; |
80 | } |
81 | } |
82 | } |
83 | |
84 | # overrides MakeMaker's prompt() to automatically accept the default choice |
85 | sub _prompt { |
86 | goto &ExtUtils::MakeMaker::prompt unless $AcceptDefault; |
87 | |
88 | my ( $prompt, $default ) = @_; |
89 | my $y = ( $default =~ /^[Yy]/ ); |
90 | |
91 | print $prompt, ' [', ( $y ? 'Y' : 'y' ), '/', ( $y ? 'n' : 'N' ), '] '; |
92 | print "$default\n"; |
93 | return $default; |
94 | } |
95 | |
96 | # the workhorse |
97 | sub import { |
98 | my $class = shift; |
99 | my @args = @_ or return; |
100 | my $core_all; |
101 | |
102 | print "*** $class version " . $class->VERSION . "\n"; |
103 | print "*** Checking for Perl dependencies...\n"; |
104 | |
105 | my $cwd = Cwd::cwd(); |
106 | |
107 | $Config = []; |
108 | |
109 | my $maxlen = length( |
110 | ( |
111 | sort { length($b) <=> length($a) } |
112 | grep { /^[^\-]/ } |
113 | map { |
114 | ref($_) |
115 | ? ( ( ref($_) eq 'HASH' ) ? keys(%$_) : @{$_} ) |
116 | : '' |
117 | } |
118 | map { +{@args}->{$_} } |
119 | grep { /^[^\-]/ or /^-core$/i } keys %{ +{@args} } |
120 | )[0] |
121 | ); |
122 | |
123 | # We want to know if we're under CPAN early to avoid prompting, but |
124 | # if we aren't going to try and install anything anyway then skip the |
125 | # check entirely since we don't want to have to load (and configure) |
126 | # an old CPAN just for a cosmetic message |
127 | |
128 | $UnderCPAN = _check_lock(1) unless $SkipInstall; |
129 | |
130 | while ( my ( $feature, $modules ) = splice( @args, 0, 2 ) ) { |
131 | my ( @required, @tests, @skiptests ); |
132 | my $default = 1; |
133 | my $conflict = 0; |
134 | |
135 | if ( $feature =~ m/^-(\w+)$/ ) { |
136 | my $option = lc($1); |
137 | |
138 | # check for a newer version of myself |
139 | _update_to( $modules, @_ ) and return if $option eq 'version'; |
140 | |
141 | # sets CPAN configuration options |
142 | $Config = $modules if $option eq 'config'; |
143 | |
144 | # promote every features to core status |
145 | $core_all = ( $modules =~ /^all$/i ) and next |
146 | if $option eq 'core'; |
147 | |
148 | next unless $option eq 'core'; |
149 | } |
150 | |
151 | print "[" . ( $FeatureMap{ lc($feature) } || $feature ) . "]\n"; |
152 | |
153 | $modules = [ %{$modules} ] if UNIVERSAL::isa( $modules, 'HASH' ); |
154 | |
155 | unshift @$modules, -default => &{ shift(@$modules) } |
156 | if ( ref( $modules->[0] ) eq 'CODE' ); # XXX: bugward combatability |
157 | |
158 | while ( my ( $mod, $arg ) = splice( @$modules, 0, 2 ) ) { |
159 | if ( $mod =~ m/^-(\w+)$/ ) { |
160 | my $option = lc($1); |
161 | |
162 | $default = $arg if ( $option eq 'default' ); |
163 | $conflict = $arg if ( $option eq 'conflict' ); |
164 | @tests = @{$arg} if ( $option eq 'tests' ); |
165 | @skiptests = @{$arg} if ( $option eq 'skiptests' ); |
166 | |
167 | next; |
168 | } |
169 | |
170 | printf( "- %-${maxlen}s ...", $mod ); |
171 | |
172 | if ( $arg and $arg =~ /^\D/ ) { |
173 | unshift @$modules, $arg; |
174 | $arg = 0; |
175 | } |
176 | |
177 | # XXX: check for conflicts and uninstalls(!) them. |
178 | my $cur = _load($mod); |
179 | if (_version_cmp ($cur, $arg) >= 0) |
180 | { |
181 | print "loaded. ($cur" . ( $arg ? " >= $arg" : '' ) . ")\n"; |
182 | push @Existing, $mod => $arg; |
183 | $DisabledTests{$_} = 1 for map { glob($_) } @skiptests; |
184 | } |
185 | else { |
186 | if (not defined $cur) # indeed missing |
187 | { |
188 | print "missing." . ( $arg ? " (would need $arg)" : '' ) . "\n"; |
189 | } |
190 | else |
191 | { |
192 | # no need to check $arg as _version_cmp ($cur, undef) would satisfy >= above |
193 | print "too old. ($cur < $arg)\n"; |
194 | } |
195 | |
196 | push @required, $mod => $arg; |
197 | } |
198 | } |
199 | |
200 | next unless @required; |
201 | |
202 | my $mandatory = ( $feature eq '-core' or $core_all ); |
203 | |
204 | if ( |
205 | !$SkipInstall |
206 | and ( |
207 | $CheckOnly |
208 | or ($mandatory and $UnderCPAN) |
209 | or $AllDeps |
210 | or _prompt( |
211 | qq{==> Auto-install the } |
212 | . ( @required / 2 ) |
213 | . ( $mandatory ? ' mandatory' : ' optional' ) |
214 | . qq{ module(s) from CPAN?}, |
215 | $default ? 'y' : 'n', |
216 | ) =~ /^[Yy]/ |
217 | ) |
218 | ) |
219 | { |
220 | push( @Missing, @required ); |
221 | $DisabledTests{$_} = 1 for map { glob($_) } @skiptests; |
222 | } |
223 | |
224 | elsif ( !$SkipInstall |
225 | and $default |
226 | and $mandatory |
227 | and |
228 | _prompt( qq{==> The module(s) are mandatory! Really skip?}, 'n', ) |
229 | =~ /^[Nn]/ ) |
230 | { |
231 | push( @Missing, @required ); |
232 | $DisabledTests{$_} = 1 for map { glob($_) } @skiptests; |
233 | } |
234 | |
235 | else { |
236 | $DisabledTests{$_} = 1 for map { glob($_) } @tests; |
237 | } |
238 | } |
239 | |
240 | if ( @Missing and not( $CheckOnly or $UnderCPAN ) ) { |
241 | require Config; |
242 | print |
243 | "*** Dependencies will be installed the next time you type '$Config::Config{make}'.\n"; |
244 | |
245 | # make an educated guess of whether we'll need root permission. |
246 | print " (You may need to do that as the 'root' user.)\n" |
247 | if eval '$>'; |
248 | } |
249 | print "*** $class configuration finished.\n"; |
250 | |
251 | chdir $cwd; |
252 | |
253 | # import to main:: |
254 | no strict 'refs'; |
255 | *{'main::WriteMakefile'} = \&Write if caller(0) eq 'main'; |
256 | |
257 | return (@Existing, @Missing); |
258 | } |
259 | |
260 | sub _running_under { |
261 | my $thing = shift; |
262 | print <<"END_MESSAGE"; |
263 | *** Since we're running under ${thing}, I'll just let it take care |
264 | of the dependency's installation later. |
265 | END_MESSAGE |
266 | return 1; |
267 | } |
268 | |
269 | # Check to see if we are currently running under CPAN.pm and/or CPANPLUS; |
270 | # if we are, then we simply let it taking care of our dependencies |
271 | sub _check_lock { |
272 | return unless @Missing or @_; |
273 | |
274 | my $cpan_env = $ENV{PERL5_CPAN_IS_RUNNING}; |
275 | |
276 | if ($ENV{PERL5_CPANPLUS_IS_RUNNING}) { |
277 | return _running_under($cpan_env ? 'CPAN' : 'CPANPLUS'); |
278 | } |
279 | |
280 | require CPAN; |
281 | |
282 | if ($CPAN::VERSION > '1.89') { |
283 | if ($cpan_env) { |
284 | return _running_under('CPAN'); |
285 | } |
286 | return; # CPAN.pm new enough, don't need to check further |
287 | } |
288 | |
289 | # last ditch attempt, this -will- configure CPAN, very sorry |
290 | |
291 | _load_cpan(1); # force initialize even though it's already loaded |
292 | |
293 | # Find the CPAN lock-file |
294 | my $lock = MM->catfile( $CPAN::Config->{cpan_home}, ".lock" ); |
295 | return unless -f $lock; |
296 | |
297 | # Check the lock |
298 | local *LOCK; |
299 | return unless open(LOCK, $lock); |
300 | |
301 | if ( |
302 | ( $^O eq 'MSWin32' ? _under_cpan() : <LOCK> == getppid() ) |
303 | and ( $CPAN::Config->{prerequisites_policy} || '' ) ne 'ignore' |
304 | ) { |
305 | print <<'END_MESSAGE'; |
306 | |
307 | *** Since we're running under CPAN, I'll just let it take care |
308 | of the dependency's installation later. |
309 | END_MESSAGE |
310 | return 1; |
311 | } |
312 | |
313 | close LOCK; |
314 | return; |
315 | } |
316 | |
317 | sub install { |
318 | my $class = shift; |
319 | |
320 | my $i; # used below to strip leading '-' from config keys |
321 | my @config = ( map { s/^-// if ++$i; $_ } @{ +shift } ); |
322 | |
323 | my ( @modules, @installed ); |
324 | while ( my ( $pkg, $ver ) = splice( @_, 0, 2 ) ) { |
325 | |
326 | # grep out those already installed |
327 | if ( _version_cmp( _load($pkg), $ver ) >= 0 ) { |
328 | push @installed, $pkg; |
329 | } |
330 | else { |
331 | push @modules, $pkg, $ver; |
332 | } |
333 | } |
334 | |
335 | return @installed unless @modules; # nothing to do |
336 | return @installed if _check_lock(); # defer to the CPAN shell |
337 | |
338 | print "*** Installing dependencies...\n"; |
339 | |
340 | return unless _connected_to('cpan.org'); |
341 | |
342 | my %args = @config; |
343 | my %failed; |
344 | local *FAILED; |
345 | if ( $args{do_once} and open( FAILED, '.#autoinstall.failed' ) ) { |
346 | while (<FAILED>) { chomp; $failed{$_}++ } |
347 | close FAILED; |
348 | |
349 | my @newmod; |
350 | while ( my ( $k, $v ) = splice( @modules, 0, 2 ) ) { |
351 | push @newmod, ( $k => $v ) unless $failed{$k}; |
352 | } |
353 | @modules = @newmod; |
354 | } |
355 | |
356 | if ( _has_cpanplus() and not $ENV{PERL_AUTOINSTALL_PREFER_CPAN} ) { |
357 | _install_cpanplus( \@modules, \@config ); |
358 | } else { |
359 | _install_cpan( \@modules, \@config ); |
360 | } |
361 | |
362 | print "*** $class installation finished.\n"; |
363 | |
364 | # see if we have successfully installed them |
365 | while ( my ( $pkg, $ver ) = splice( @modules, 0, 2 ) ) { |
366 | if ( _version_cmp( _load($pkg), $ver ) >= 0 ) { |
367 | push @installed, $pkg; |
368 | } |
369 | elsif ( $args{do_once} and open( FAILED, '>> .#autoinstall.failed' ) ) { |
370 | print FAILED "$pkg\n"; |
371 | } |
372 | } |
373 | |
374 | close FAILED if $args{do_once}; |
375 | |
376 | return @installed; |
377 | } |
378 | |
379 | sub _install_cpanplus { |
380 | my @modules = @{ +shift }; |
381 | my @config = _cpanplus_config( @{ +shift } ); |
382 | my $installed = 0; |
383 | |
384 | require CPANPLUS::Backend; |
385 | my $cp = CPANPLUS::Backend->new; |
386 | my $conf = $cp->configure_object; |
387 | |
388 | return unless $conf->can('conf') # 0.05x+ with "sudo" support |
389 | or _can_write($conf->_get_build('base')); # 0.04x |
390 | |
391 | # if we're root, set UNINST=1 to avoid trouble unless user asked for it. |
392 | my $makeflags = $conf->get_conf('makeflags') || ''; |
393 | if ( UNIVERSAL::isa( $makeflags, 'HASH' ) ) { |
394 | # 0.03+ uses a hashref here |
395 | $makeflags->{UNINST} = 1 unless exists $makeflags->{UNINST}; |
396 | |
397 | } else { |
398 | # 0.02 and below uses a scalar |
399 | $makeflags = join( ' ', split( ' ', $makeflags ), 'UNINST=1' ) |
400 | if ( $makeflags !~ /\bUNINST\b/ and eval qq{ $> eq '0' } ); |
401 | |
402 | } |
403 | $conf->set_conf( makeflags => $makeflags ); |
404 | $conf->set_conf( prereqs => 1 ); |
405 | |
406 | |
407 | |
408 | while ( my ( $key, $val ) = splice( @config, 0, 2 ) ) { |
409 | $conf->set_conf( $key, $val ); |
410 | } |
411 | |
412 | my $modtree = $cp->module_tree; |
413 | while ( my ( $pkg, $ver ) = splice( @modules, 0, 2 ) ) { |
414 | print "*** Installing $pkg...\n"; |
415 | |
416 | MY::preinstall( $pkg, $ver ) or next if defined &MY::preinstall; |
417 | |
418 | my $success; |
419 | my $obj = $modtree->{$pkg}; |
420 | |
421 | if ( $obj and _version_cmp( $obj->{version}, $ver ) >= 0 ) { |
422 | my $pathname = $pkg; |
423 | $pathname =~ s/::/\\W/; |
424 | |
425 | foreach my $inc ( grep { m/$pathname.pm/i } keys(%INC) ) { |
426 | delete $INC{$inc}; |
427 | } |
428 | |
429 | my $rv = $cp->install( modules => [ $obj->{module} ] ); |
430 | |
431 | if ( $rv and ( $rv->{ $obj->{module} } or $rv->{ok} ) ) { |
432 | print "*** $pkg successfully installed.\n"; |
433 | $success = 1; |
434 | } else { |
435 | print "*** $pkg installation cancelled.\n"; |
436 | $success = 0; |
437 | } |
438 | |
439 | $installed += $success; |
440 | } else { |
441 | print << "."; |
442 | *** Could not find a version $ver or above for $pkg; skipping. |
443 | . |
444 | } |
445 | |
446 | MY::postinstall( $pkg, $ver, $success ) if defined &MY::postinstall; |
447 | } |
448 | |
449 | return $installed; |
450 | } |
451 | |
452 | sub _cpanplus_config { |
453 | my @config = (); |
454 | while ( @_ ) { |
455 | my ($key, $value) = (shift(), shift()); |
456 | if ( $key eq 'prerequisites_policy' ) { |
457 | if ( $value eq 'follow' ) { |
458 | $value = CPANPLUS::Internals::Constants::PREREQ_INSTALL(); |
459 | } elsif ( $value eq 'ask' ) { |
460 | $value = CPANPLUS::Internals::Constants::PREREQ_ASK(); |
461 | } elsif ( $value eq 'ignore' ) { |
462 | $value = CPANPLUS::Internals::Constants::PREREQ_IGNORE(); |
463 | } else { |
464 | die "*** Cannot convert option $key = '$value' to CPANPLUS version.\n"; |
465 | } |
466 | } else { |
467 | die "*** Cannot convert option $key to CPANPLUS version.\n"; |
468 | } |
469 | } |
470 | return @config; |
471 | } |
472 | |
473 | sub _install_cpan { |
474 | my @modules = @{ +shift }; |
475 | my @config = @{ +shift }; |
476 | my $installed = 0; |
477 | my %args; |
478 | |
479 | _load_cpan(); |
480 | require Config; |
481 | |
482 | if (CPAN->VERSION < 1.80) { |
483 | # no "sudo" support, probe for writableness |
484 | return unless _can_write( MM->catfile( $CPAN::Config->{cpan_home}, 'sources' ) ) |
485 | and _can_write( $Config::Config{sitelib} ); |
486 | } |
487 | |
488 | # if we're root, set UNINST=1 to avoid trouble unless user asked for it. |
489 | my $makeflags = $CPAN::Config->{make_install_arg} || ''; |
490 | $CPAN::Config->{make_install_arg} = |
491 | join( ' ', split( ' ', $makeflags ), 'UNINST=1' ) |
492 | if ( $makeflags !~ /\bUNINST\b/ and eval qq{ $> eq '0' } ); |
493 | |
494 | # don't show start-up info |
495 | $CPAN::Config->{inhibit_startup_message} = 1; |
496 | |
497 | # set additional options |
498 | while ( my ( $opt, $arg ) = splice( @config, 0, 2 ) ) { |
499 | ( $args{$opt} = $arg, next ) |
500 | if $opt =~ /^force$/; # pseudo-option |
501 | $CPAN::Config->{$opt} = $arg; |
502 | } |
503 | |
504 | local $CPAN::Config->{prerequisites_policy} = 'follow'; |
505 | |
506 | while ( my ( $pkg, $ver ) = splice( @modules, 0, 2 ) ) { |
507 | MY::preinstall( $pkg, $ver ) or next if defined &MY::preinstall; |
508 | |
509 | print "*** Installing $pkg...\n"; |
510 | |
511 | my $obj = CPAN::Shell->expand( Module => $pkg ); |
512 | my $success = 0; |
513 | |
514 | if ( $obj and _version_cmp( $obj->cpan_version, $ver ) >= 0 ) { |
515 | my $pathname = $pkg; |
516 | $pathname =~ s/::/\\W/; |
517 | |
518 | foreach my $inc ( grep { m/$pathname.pm/i } keys(%INC) ) { |
519 | delete $INC{$inc}; |
520 | } |
521 | |
522 | my $rv = $args{force} ? CPAN::Shell->force( install => $pkg ) |
523 | : CPAN::Shell->install($pkg); |
524 | $rv ||= eval { |
525 | $CPAN::META->instance( 'CPAN::Distribution', $obj->cpan_file, ) |
526 | ->{install} |
527 | if $CPAN::META; |
528 | }; |
529 | |
530 | if ( $rv eq 'YES' ) { |
531 | print "*** $pkg successfully installed.\n"; |
532 | $success = 1; |
533 | } |
534 | else { |
535 | print "*** $pkg installation failed.\n"; |
536 | $success = 0; |
537 | } |
538 | |
539 | $installed += $success; |
540 | } |
541 | else { |
542 | print << "."; |
543 | *** Could not find a version $ver or above for $pkg; skipping. |
544 | . |
545 | } |
546 | |
547 | MY::postinstall( $pkg, $ver, $success ) if defined &MY::postinstall; |
548 | } |
549 | |
550 | return $installed; |
551 | } |
552 | |
553 | sub _has_cpanplus { |
554 | return ( |
555 | $HasCPANPLUS = ( |
556 | $INC{'CPANPLUS/Config.pm'} |
557 | or _load('CPANPLUS::Shell::Default') |
558 | ) |
559 | ); |
560 | } |
561 | |
562 | # make guesses on whether we're under the CPAN installation directory |
563 | sub _under_cpan { |
564 | require Cwd; |
565 | require File::Spec; |
566 | |
567 | my $cwd = File::Spec->canonpath( Cwd::cwd() ); |
568 | my $cpan = File::Spec->canonpath( $CPAN::Config->{cpan_home} ); |
569 | |
570 | return ( index( $cwd, $cpan ) > -1 ); |
571 | } |
572 | |
573 | sub _update_to { |
574 | my $class = __PACKAGE__; |
575 | my $ver = shift; |
576 | |
577 | return |
578 | if _version_cmp( _load($class), $ver ) >= 0; # no need to upgrade |
579 | |
580 | if ( |
581 | _prompt( "==> A newer version of $class ($ver) is required. Install?", |
582 | 'y' ) =~ /^[Nn]/ |
583 | ) |
584 | { |
585 | die "*** Please install $class $ver manually.\n"; |
586 | } |
587 | |
588 | print << "."; |
589 | *** Trying to fetch it from CPAN... |
590 | . |
591 | |
592 | # install ourselves |
593 | _load($class) and return $class->import(@_) |
594 | if $class->install( [], $class, $ver ); |
595 | |
596 | print << '.'; exit 1; |
597 | |
598 | *** Cannot bootstrap myself. :-( Installation terminated. |
599 | . |
600 | } |
601 | |
602 | # check if we're connected to some host, using inet_aton |
603 | sub _connected_to { |
604 | my $site = shift; |
605 | |
606 | return ( |
607 | ( _load('Socket') and Socket::inet_aton($site) ) or _prompt( |
608 | qq( |
609 | *** Your host cannot resolve the domain name '$site', which |
610 | probably means the Internet connections are unavailable. |
611 | ==> Should we try to install the required module(s) anyway?), 'n' |
612 | ) =~ /^[Yy]/ |
613 | ); |
614 | } |
615 | |
616 | # check if a directory is writable; may create it on demand |
617 | sub _can_write { |
618 | my $path = shift; |
619 | mkdir( $path, 0755 ) unless -e $path; |
620 | |
621 | return 1 if -w $path; |
622 | |
623 | print << "."; |
624 | *** You are not allowed to write to the directory '$path'; |
625 | the installation may fail due to insufficient permissions. |
626 | . |
627 | |
628 | if ( |
629 | eval '$>' and lc(`sudo -V`) =~ /version/ and _prompt( |
630 | qq( |
631 | ==> Should we try to re-execute the autoinstall process with 'sudo'?), |
632 | ((-t STDIN) ? 'y' : 'n') |
633 | ) =~ /^[Yy]/ |
634 | ) |
635 | { |
636 | |
637 | # try to bootstrap ourselves from sudo |
638 | print << "."; |
639 | *** Trying to re-execute the autoinstall process with 'sudo'... |
640 | . |
641 | my $missing = join( ',', @Missing ); |
642 | my $config = join( ',', |
643 | UNIVERSAL::isa( $Config, 'HASH' ) ? %{$Config} : @{$Config} ) |
644 | if $Config; |
645 | |
646 | return |
647 | unless system( 'sudo', $^X, $0, "--config=$config", |
648 | "--installdeps=$missing" ); |
649 | |
650 | print << "."; |
651 | *** The 'sudo' command exited with error! Resuming... |
652 | . |
653 | } |
654 | |
655 | return _prompt( |
656 | qq( |
657 | ==> Should we try to install the required module(s) anyway?), 'n' |
658 | ) =~ /^[Yy]/; |
659 | } |
660 | |
661 | # load a module and return the version it reports |
662 | sub _load { |
663 | my $mod = pop; # class/instance doesn't matter |
664 | my $file = $mod; |
665 | |
666 | $file =~ s|::|/|g; |
667 | $file .= '.pm'; |
668 | |
669 | local $@; |
670 | return eval { require $file; $mod->VERSION } || ( $@ ? undef: 0 ); |
671 | } |
672 | |
673 | # Load CPAN.pm and it's configuration |
674 | sub _load_cpan { |
675 | return if $CPAN::VERSION and $CPAN::Config and not @_; |
676 | require CPAN; |
677 | |
678 | # CPAN-1.82+ adds CPAN::Config::AUTOLOAD to redirect to |
679 | # CPAN::HandleConfig->load. CPAN reports that the redirection |
680 | # is deprecated in a warning printed at the user. |
681 | |
682 | # CPAN-1.81 expects CPAN::HandleConfig->load, does not have |
683 | # $CPAN::HandleConfig::VERSION but cannot handle |
684 | # CPAN::Config->load |
685 | |
686 | # Which "versions expect CPAN::Config->load? |
687 | |
688 | if ( $CPAN::HandleConfig::VERSION |
689 | || CPAN::HandleConfig->can('load') |
690 | ) { |
691 | # Newer versions of CPAN have a HandleConfig module |
692 | CPAN::HandleConfig->load; |
693 | } else { |
694 | # Older versions had the load method in Config directly |
695 | CPAN::Config->load; |
696 | } |
697 | } |
698 | |
699 | # compare two versions, either use Sort::Versions or plain comparison |
700 | # return values same as <=> |
701 | sub _version_cmp { |
702 | my ( $cur, $min ) = @_; |
703 | return -1 unless defined $cur; # if 0 keep comparing |
704 | return 1 unless $min; |
705 | |
706 | $cur =~ s/\s+$//; |
707 | |
708 | # check for version numbers that are not in decimal format |
709 | if ( ref($cur) or ref($min) or $cur =~ /v|\..*\./ or $min =~ /v|\..*\./ ) { |
710 | if ( ( $version::VERSION or defined( _load('version') )) and |
711 | version->can('new') |
712 | ) { |
713 | |
714 | # use version.pm if it is installed. |
715 | return version->new($cur) <=> version->new($min); |
716 | } |
717 | elsif ( $Sort::Versions::VERSION or defined( _load('Sort::Versions') ) ) |
718 | { |
719 | |
720 | # use Sort::Versions as the sorting algorithm for a.b.c versions |
721 | return Sort::Versions::versioncmp( $cur, $min ); |
722 | } |
723 | |
724 | warn "Cannot reliably compare non-decimal formatted versions.\n" |
725 | . "Please install version.pm or Sort::Versions.\n"; |
726 | } |
727 | |
728 | # plain comparison |
729 | local $^W = 0; # shuts off 'not numeric' bugs |
730 | return $cur <=> $min; |
731 | } |
732 | |
733 | # nothing; this usage is deprecated. |
734 | sub main::PREREQ_PM { return {}; } |
735 | |
736 | sub _make_args { |
737 | my %args = @_; |
738 | |
739 | $args{PREREQ_PM} = { %{ $args{PREREQ_PM} || {} }, @Existing, @Missing } |
740 | if $UnderCPAN or $TestOnly; |
741 | |
742 | if ( $args{EXE_FILES} and -e 'MANIFEST' ) { |
743 | require ExtUtils::Manifest; |
744 | my $manifest = ExtUtils::Manifest::maniread('MANIFEST'); |
745 | |
746 | $args{EXE_FILES} = |
747 | [ grep { exists $manifest->{$_} } @{ $args{EXE_FILES} } ]; |
748 | } |
749 | |
750 | $args{test}{TESTS} ||= 't/*.t'; |
751 | $args{test}{TESTS} = join( ' ', |
752 | grep { !exists( $DisabledTests{$_} ) } |
753 | map { glob($_) } split( /\s+/, $args{test}{TESTS} ) ); |
754 | |
755 | my $missing = join( ',', @Missing ); |
756 | my $config = |
757 | join( ',', UNIVERSAL::isa( $Config, 'HASH' ) ? %{$Config} : @{$Config} ) |
758 | if $Config; |
759 | |
760 | $PostambleActions = ( |
761 | ($missing and not $UnderCPAN) |
762 | ? "\$(PERL) $0 --config=$config --installdeps=$missing" |
763 | : "\$(NOECHO) \$(NOOP)" |
764 | ); |
765 | |
766 | return %args; |
767 | } |
768 | |
769 | # a wrapper to ExtUtils::MakeMaker::WriteMakefile |
770 | sub Write { |
771 | require Carp; |
772 | Carp::croak "WriteMakefile: Need even number of args" if @_ % 2; |
773 | |
774 | if ($CheckOnly) { |
775 | print << "."; |
776 | *** Makefile not written in check-only mode. |
777 | . |
778 | return; |
779 | } |
780 | |
781 | my %args = _make_args(@_); |
782 | |
783 | no strict 'refs'; |
784 | |
785 | $PostambleUsed = 0; |
786 | local *MY::postamble = \&postamble unless defined &MY::postamble; |
787 | ExtUtils::MakeMaker::WriteMakefile(%args); |
788 | |
789 | print << "." unless $PostambleUsed; |
790 | *** WARNING: Makefile written with customized MY::postamble() without |
791 | including contents from Module::AutoInstall::postamble() -- |
792 | auto installation features disabled. Please contact the author. |
793 | . |
794 | |
795 | return 1; |
796 | } |
797 | |
798 | sub postamble { |
799 | $PostambleUsed = 1; |
800 | |
801 | return <<"END_MAKE"; |
802 | |
803 | config :: installdeps |
804 | \t\$(NOECHO) \$(NOOP) |
805 | |
806 | checkdeps :: |
807 | \t\$(PERL) $0 --checkdeps |
808 | |
809 | installdeps :: |
810 | \t$PostambleActions |
811 | |
812 | END_MAKE |
813 | |
814 | } |
815 | |
816 | 1; |
817 | |
818 | __END__ |
819 | |
820 | #line 1071 |