@@ -3,11 +3,12 @@ package Module::AutoInstall;
3
3
4
4
use strict;
5
5
use Cwd ();
6
+ use File::Spec ();
6
7
use ExtUtils::MakeMaker ();
7
8
8
9
use vars qw{ $VERSION} ;
9
10
BEGIN {
10
- $VERSION = ' 1.03 ' ;
11
+ $VERSION = ' 1.06 ' ;
11
12
}
12
13
13
14
# special map on pre-defined feature sets
@@ -17,11 +18,14 @@ my %FeatureMap = (
17
18
);
18
19
19
20
# various lexical flags
20
- my ( @Missing , @Existing , %DisabledTests , $UnderCPAN , $HasCPANPLUS );
21
+ my ( @Missing , @Existing , %DisabledTests , $UnderCPAN , $InstallDepsTarget , $HasCPANPLUS );
21
22
my (
22
- $Config , $CheckOnly , $SkipInstall , $AcceptDefault , $TestOnly , $AllDeps
23
+ $Config , $CheckOnly , $SkipInstall , $AcceptDefault , $TestOnly , $AllDeps ,
24
+ $UpgradeDeps
23
25
);
24
- my ( $PostambleActions , $PostambleUsed );
26
+ my ( $PostambleActions , $PostambleActionsNoTest , $PostambleActionsUpgradeDeps ,
27
+ $PostambleActionsUpgradeDepsNoTest , $PostambleActionsListDeps ,
28
+ $PostambleActionsListAllDeps , $PostambleUsed , $NoTest );
25
29
26
30
# See if it's a testing or non-interactive session
27
31
_accept_default( $ENV {AUTOMATED_TESTING } or ! -t STDIN );
@@ -31,6 +35,10 @@ sub _accept_default {
31
35
$AcceptDefault = shift ;
32
36
}
33
37
38
+ sub _installdeps_target {
39
+ $InstallDepsTarget = shift ;
40
+ }
41
+
34
42
sub missing_modules {
35
43
return @Missing ;
36
44
}
@@ -63,6 +71,11 @@ sub _init {
63
71
__PACKAGE__ -> install( $Config , @Missing = split ( / ,/ , $1 ) );
64
72
exit 0;
65
73
}
74
+ elsif ( $arg =~ / ^--upgradedeps=(.*)$ / ) {
75
+ $UpgradeDeps = 1;
76
+ __PACKAGE__ -> install( $Config , @Missing = split ( / ,/ , $1 ) );
77
+ exit 0;
78
+ }
66
79
elsif ( $arg =~ / ^--default(?:deps)?$ / ) {
67
80
$AcceptDefault = 1;
68
81
}
@@ -125,7 +138,7 @@ sub import {
125
138
# check entirely since we don't want to have to load (and configure)
126
139
# an old CPAN just for a cosmetic message
127
140
128
- $UnderCPAN = _check_lock(1) unless $SkipInstall ;
141
+ $UnderCPAN = _check_lock(1) unless $SkipInstall || $InstallDepsTarget ;
129
142
130
143
while ( my ( $feature , $modules ) = splice ( @args , 0, 2 ) ) {
131
144
my ( @required , @tests , @skiptests );
@@ -175,7 +188,7 @@ sub import {
175
188
}
176
189
177
190
# XXX: check for conflicts and uninstalls(!) them.
178
- my $cur = _load ($mod );
191
+ my $cur = _version_of ($mod );
179
192
if (_version_cmp ($cur , $arg ) >= 0)
180
193
{
181
194
print " loaded. ($cur " . ( $arg ? " >= $arg " : ' ' ) . " )\n " ;
@@ -207,6 +220,7 @@ sub import {
207
220
$CheckOnly
208
221
or ($mandatory and $UnderCPAN )
209
222
or $AllDeps
223
+ or $InstallDepsTarget
210
224
or _prompt(
211
225
qq{ ==> Auto-install the }
212
226
. ( @required / 2 )
@@ -237,10 +251,17 @@ sub import {
237
251
}
238
252
}
239
253
240
- if ( @Missing and not ( $CheckOnly or $UnderCPAN ) ) {
254
+ if ( @Missing and not ( $CheckOnly or $UnderCPAN ) ) {
241
255
require Config;
242
- print
243
- " *** Dependencies will be installed the next time you type '$Config::Config {make}'.\n " ;
256
+ my $make = $Config::Config {make };
257
+ if ($InstallDepsTarget ) {
258
+ print
259
+ " *** To install dependencies type '$make installdeps' or '$make installdeps_notest'.\n " ;
260
+ }
261
+ else {
262
+ print
263
+ " *** Dependencies will be installed the next time you type '$make '.\n " ;
264
+ }
244
265
245
266
# make an educated guess of whether we'll need root permission.
246
267
print " (You may need to do that as the 'root' user.)\n "
@@ -253,6 +274,8 @@ sub import {
253
274
# import to main::
254
275
no strict ' refs' ;
255
276
*{' main::WriteMakefile' } = \&Write if caller (0) eq ' main' ;
277
+
278
+ return (@Existing , @Missing );
256
279
}
257
280
258
281
sub _running_under {
@@ -269,6 +292,10 @@ END_MESSAGE
269
292
sub _check_lock {
270
293
return unless @Missing or @_ ;
271
294
295
+ if ($ENV {PERL5_CPANM_IS_RUNNING }) {
296
+ return _running_under(' cpanminus' );
297
+ }
298
+
272
299
my $cpan_env = $ENV {PERL5_CPAN_IS_RUNNING };
273
300
274
301
if ($ENV {PERL5_CPANPLUS_IS_RUNNING }) {
@@ -322,14 +349,19 @@ sub install {
322
349
while ( my ( $pkg , $ver ) = splice ( @_ , 0, 2 ) ) {
323
350
324
351
# grep out those already installed
325
- if ( _version_cmp( _load ($pkg ), $ver ) >= 0 ) {
352
+ if ( _version_cmp( _version_of ($pkg ), $ver ) >= 0 ) {
326
353
push @installed , $pkg ;
327
354
}
328
355
else {
329
356
push @modules , $pkg , $ver ;
330
357
}
331
358
}
332
359
360
+ if ($UpgradeDeps ) {
361
+ push @modules , @installed ;
362
+ @installed = ();
363
+ }
364
+
333
365
return @installed unless @modules ; # nothing to do
334
366
return @installed if _check_lock(); # defer to the CPAN shell
335
367
@@ -361,7 +393,7 @@ sub install {
361
393
362
394
# see if we have successfully installed them
363
395
while ( my ( $pkg , $ver ) = splice ( @modules , 0, 2 ) ) {
364
- if ( _version_cmp( _load ($pkg ), $ver ) >= 0 ) {
396
+ if ( _version_cmp( _version_of ($pkg ), $ver ) >= 0 ) {
365
397
push @installed , $pkg ;
366
398
}
367
399
elsif ( $args {do_once } and open ( FAILED, ' >> .#autoinstall.failed' ) ) {
@@ -461,6 +493,11 @@ sub _cpanplus_config {
461
493
} else {
462
494
die " *** Cannot convert option $key = '$value ' to CPANPLUS version.\n " ;
463
495
}
496
+ push @config , ' prereqs' , $value ;
497
+ } elsif ( $key eq ' force' ) {
498
+ push @config , $key , $value ;
499
+ } elsif ( $key eq ' notest' ) {
500
+ push @config , ' skiptest' , $value ;
464
501
} else {
465
502
die " *** Cannot convert option $key to CPANPLUS version.\n " ;
466
503
}
@@ -495,10 +532,14 @@ sub _install_cpan {
495
532
# set additional options
496
533
while ( my ( $opt , $arg ) = splice ( @config , 0, 2 ) ) {
497
534
( $args {$opt } = $arg , next )
498
- if $opt =~ / ^force$ / ; # pseudo-option
535
+ if $opt =~ / ^(?: force|notest) $ / ; # pseudo-option
499
536
$CPAN::Config -> {$opt } = $arg ;
500
537
}
501
538
539
+ if ($args {notest } && (not CPAN::Shell-> can(' notest' ))) {
540
+ die " Your version of CPAN is too old to support the 'notest' pragma" ;
541
+ }
542
+
502
543
local $CPAN::Config -> {prerequisites_policy } = ' follow' ;
503
544
504
545
while ( my ( $pkg , $ver ) = splice ( @modules , 0, 2 ) ) {
@@ -517,8 +558,16 @@ sub _install_cpan {
517
558
delete $INC {$inc };
518
559
}
519
560
520
- my $rv = $args {force } ? CPAN::Shell-> force( install => $pkg )
521
- : CPAN::Shell-> install($pkg );
561
+ my $rv = do {
562
+ if ($args {force }) {
563
+ CPAN::Shell-> force( install => $pkg )
564
+ } elsif ($args {notest }) {
565
+ CPAN::Shell-> notest( install => $pkg )
566
+ } else {
567
+ CPAN::Shell-> install($pkg )
568
+ }
569
+ };
570
+
522
571
$rv ||= eval {
523
572
$CPAN::META -> instance( ' CPAN::Distribution' , $obj -> cpan_file, )
524
573
-> {install }
@@ -573,7 +622,7 @@ sub _update_to {
573
622
my $ver = shift ;
574
623
575
624
return
576
- if _version_cmp( _load ($class ), $ver ) >= 0; # no need to upgrade
625
+ if _version_cmp( _version_of ($class ), $ver ) >= 0; # no need to upgrade
577
626
578
627
if (
579
628
_prompt( " ==> A newer version of $class ($ver ) is required. Install?" ,
@@ -658,21 +707,48 @@ sub _can_write {
658
707
659
708
# load a module and return the version it reports
660
709
sub _load {
661
- my $mod = pop ; # class/instance doesn't matter
710
+ my $mod = pop ; # method/function doesn't matter
662
711
my $file = $mod ;
663
-
664
712
$file =~ s | ::| /| g ;
665
713
$file .= ' .pm' ;
666
-
667
714
local $@ ;
668
715
return eval { require $file ; $mod -> VERSION } || ( $@ ? undef : 0 );
669
716
}
670
717
718
+ # report version without loading a module
719
+ sub _version_of {
720
+ my $mod = pop ; # method/function doesn't matter
721
+ my $file = $mod ;
722
+ $file =~ s | ::| /| g ;
723
+ $file .= ' .pm' ;
724
+ foreach my $dir ( @INC ) {
725
+ next if ref $dir ;
726
+ my $path = File::Spec-> catfile($dir , $file );
727
+ next unless -e $path ;
728
+ require ExtUtils::MM_Unix;
729
+ return ExtUtils::MM_Unix-> parse_version($path );
730
+ }
731
+ return undef ;
732
+ }
733
+
671
734
# Load CPAN.pm and it's configuration
672
735
sub _load_cpan {
673
736
return if $CPAN::VERSION and $CPAN::Config and not @_ ;
674
737
require CPAN;
675
- if ( $CPAN::HandleConfig::VERSION ) {
738
+
739
+ # CPAN-1.82+ adds CPAN::Config::AUTOLOAD to redirect to
740
+ # CPAN::HandleConfig->load. CPAN reports that the redirection
741
+ # is deprecated in a warning printed at the user.
742
+
743
+ # CPAN-1.81 expects CPAN::HandleConfig->load, does not have
744
+ # $CPAN::HandleConfig::VERSION but cannot handle
745
+ # CPAN::Config->load
746
+
747
+ # Which "versions expect CPAN::Config->load?
748
+
749
+ if ( $CPAN::HandleConfig::VERSION
750
+ || CPAN::HandleConfig-> can(' load' )
751
+ ) {
676
752
# Newer versions of CPAN have a HandleConfig module
677
753
CPAN::HandleConfig-> load;
678
754
} else {
@@ -748,6 +824,35 @@ sub _make_args {
748
824
: " \$ (NOECHO) \$ (NOOP)"
749
825
);
750
826
827
+ my $deps_list = join ( ' ,' , @Missing , @Existing );
828
+
829
+ $PostambleActionsUpgradeDeps =
830
+ " \$ (PERL) $0 --config=$config --upgradedeps=$deps_list " ;
831
+
832
+ my $config_notest =
833
+ join ( ' ,' , (UNIVERSAL::isa( $Config , ' HASH' ) ? %{$Config } : @{$Config }),
834
+ ' notest' , 1 )
835
+ if $Config ;
836
+
837
+ $PostambleActionsNoTest = (
838
+ ($missing and not $UnderCPAN )
839
+ ? " \$ (PERL) $0 --config=$config_notest --installdeps=$missing "
840
+ : " \$ (NOECHO) \$ (NOOP)"
841
+ );
842
+
843
+ $PostambleActionsUpgradeDepsNoTest =
844
+ " \$ (PERL) $0 --config=$config_notest --upgradedeps=$deps_list " ;
845
+
846
+ $PostambleActionsListDeps =
847
+ ' @$(PERL) -le "print for @ARGV" '
848
+ . join (' ' , map $Missing [$_ ], grep $_ % 2 == 0, 0..$#Missing );
849
+
850
+ my @all = (@Missing , @Existing );
851
+
852
+ $PostambleActionsListAllDeps =
853
+ ' @$(PERL) -le "print for @ARGV" '
854
+ . join (' ' , map $all [$_ ], grep $_ % 2 == 0, 0..$#all );
855
+
751
856
return %args ;
752
857
}
753
858
@@ -782,24 +887,44 @@ sub Write {
782
887
783
888
sub postamble {
784
889
$PostambleUsed = 1;
890
+ my $fragment ;
785
891
786
- return <<"END_MAKE" ;
892
+ $fragment .= <<"AUTO_INSTALL" if ! $InstallDepsTarget ;
787
893
788
894
config :: installdeps
789
895
\t\$ (NOECHO) \$ (NOOP)
896
+ AUTO_INSTALL
897
+
898
+ $fragment .= <<"END_MAKE" ;
790
899
791
900
checkdeps ::
792
901
\t\$ (PERL) $0 --checkdeps
793
902
794
903
installdeps ::
795
904
\t $PostambleActions
796
905
906
+ installdeps_notest ::
907
+ \t $PostambleActionsNoTest
908
+
909
+ upgradedeps ::
910
+ \t $PostambleActionsUpgradeDeps
911
+
912
+ upgradedeps_notest ::
913
+ \t $PostambleActionsUpgradeDepsNoTest
914
+
915
+ listdeps ::
916
+ \t $PostambleActionsListDeps
917
+
918
+ listalldeps ::
919
+ \t $PostambleActionsListAllDeps
920
+
797
921
END_MAKE
798
922
923
+ return $fragment ;
799
924
}
800
925
801
926
1;
802
927
803
928
__END__
804
929
805
- #line 1056
930
+ #line 1193
0 commit comments