PageRenderTime 52ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/MooseX-ConfigFromFile-0.04/inc/Module/Install.pm

#
Perl | 470 lines | 311 code | 82 blank | 77 comment | 34 complexity | de388685eb7b662ac27cb4baaaf677a5 MD5 | raw file
  1. #line 1
  2. package Module::Install;
  3. # For any maintainers:
  4. # The load order for Module::Install is a bit magic.
  5. # It goes something like this...
  6. #
  7. # IF ( host has Module::Install installed, creating author mode ) {
  8. # 1. Makefile.PL calls "use inc::Module::Install"
  9. # 2. $INC{inc/Module/Install.pm} set to installed version of inc::Module::Install
  10. # 3. The installed version of inc::Module::Install loads
  11. # 4. inc::Module::Install calls "require Module::Install"
  12. # 5. The ./inc/ version of Module::Install loads
  13. # } ELSE {
  14. # 1. Makefile.PL calls "use inc::Module::Install"
  15. # 2. $INC{inc/Module/Install.pm} set to ./inc/ version of Module::Install
  16. # 3. The ./inc/ version of Module::Install loads
  17. # }
  18. use 5.005;
  19. use strict 'vars';
  20. use Cwd ();
  21. use File::Find ();
  22. use File::Path ();
  23. use vars qw{$VERSION $MAIN};
  24. BEGIN {
  25. # All Module::Install core packages now require synchronised versions.
  26. # This will be used to ensure we don't accidentally load old or
  27. # different versions of modules.
  28. # This is not enforced yet, but will be some time in the next few
  29. # releases once we can make sure it won't clash with custom
  30. # Module::Install extensions.
  31. $VERSION = '1.02';
  32. # Storage for the pseudo-singleton
  33. $MAIN = undef;
  34. *inc::Module::Install::VERSION = *VERSION;
  35. @inc::Module::Install::ISA = __PACKAGE__;
  36. }
  37. sub import {
  38. my $class = shift;
  39. my $self = $class->new(@_);
  40. my $who = $self->_caller;
  41. #-------------------------------------------------------------
  42. # all of the following checks should be included in import(),
  43. # to allow "eval 'require Module::Install; 1' to test
  44. # installation of Module::Install. (RT #51267)
  45. #-------------------------------------------------------------
  46. # Whether or not inc::Module::Install is actually loaded, the
  47. # $INC{inc/Module/Install.pm} is what will still get set as long as
  48. # the caller loaded module this in the documented manner.
  49. # If not set, the caller may NOT have loaded the bundled version, and thus
  50. # they may not have a MI version that works with the Makefile.PL. This would
  51. # result in false errors or unexpected behaviour. And we don't want that.
  52. my $file = join( '/', 'inc', split /::/, __PACKAGE__ ) . '.pm';
  53. unless ( $INC{$file} ) { die <<"END_DIE" }
  54. Please invoke ${\__PACKAGE__} with:
  55. use inc::${\__PACKAGE__};
  56. not:
  57. use ${\__PACKAGE__};
  58. END_DIE
  59. # This reportedly fixes a rare Win32 UTC file time issue, but
  60. # as this is a non-cross-platform XS module not in the core,
  61. # we shouldn't really depend on it. See RT #24194 for detail.
  62. # (Also, this module only supports Perl 5.6 and above).
  63. eval "use Win32::UTCFileTime" if $^O eq 'MSWin32' && $] >= 5.006;
  64. # If the script that is loading Module::Install is from the future,
  65. # then make will detect this and cause it to re-run over and over
  66. # again. This is bad. Rather than taking action to touch it (which
  67. # is unreliable on some platforms and requires write permissions)
  68. # for now we should catch this and refuse to run.
  69. if ( -f $0 ) {
  70. my $s = (stat($0))[9];
  71. # If the modification time is only slightly in the future,
  72. # sleep briefly to remove the problem.
  73. my $a = $s - time;
  74. if ( $a > 0 and $a < 5 ) { sleep 5 }
  75. # Too far in the future, throw an error.
  76. my $t = time;
  77. if ( $s > $t ) { die <<"END_DIE" }
  78. Your installer $0 has a modification time in the future ($s > $t).
  79. This is known to create infinite loops in make.
  80. Please correct this, then run $0 again.
  81. END_DIE
  82. }
  83. # Build.PL was formerly supported, but no longer is due to excessive
  84. # difficulty in implementing every single feature twice.
  85. if ( $0 =~ /Build.PL$/i ) { die <<"END_DIE" }
  86. Module::Install no longer supports Build.PL.
  87. It was impossible to maintain duel backends, and has been deprecated.
  88. Please remove all Build.PL files and only use the Makefile.PL installer.
  89. END_DIE
  90. #-------------------------------------------------------------
  91. # To save some more typing in Module::Install installers, every...
  92. # use inc::Module::Install
  93. # ...also acts as an implicit use strict.
  94. $^H |= strict::bits(qw(refs subs vars));
  95. #-------------------------------------------------------------
  96. unless ( -f $self->{file} ) {
  97. foreach my $key (keys %INC) {
  98. delete $INC{$key} if $key =~ /Module\/Install/;
  99. }
  100. local $^W;
  101. require "$self->{path}/$self->{dispatch}.pm";
  102. File::Path::mkpath("$self->{prefix}/$self->{author}");
  103. $self->{admin} = "$self->{name}::$self->{dispatch}"->new( _top => $self );
  104. $self->{admin}->init;
  105. @_ = ($class, _self => $self);
  106. goto &{"$self->{name}::import"};
  107. }
  108. local $^W;
  109. *{"${who}::AUTOLOAD"} = $self->autoload;
  110. $self->preload;
  111. # Unregister loader and worker packages so subdirs can use them again
  112. delete $INC{'inc/Module/Install.pm'};
  113. delete $INC{'Module/Install.pm'};
  114. # Save to the singleton
  115. $MAIN = $self;
  116. return 1;
  117. }
  118. sub autoload {
  119. my $self = shift;
  120. my $who = $self->_caller;
  121. my $cwd = Cwd::cwd();
  122. my $sym = "${who}::AUTOLOAD";
  123. $sym->{$cwd} = sub {
  124. my $pwd = Cwd::cwd();
  125. if ( my $code = $sym->{$pwd} ) {
  126. # Delegate back to parent dirs
  127. goto &$code unless $cwd eq $pwd;
  128. }
  129. unless ($$sym =~ s/([^:]+)$//) {
  130. # XXX: it looks like we can't retrieve the missing function
  131. # via $$sym (usually $main::AUTOLOAD) in this case.
  132. # I'm still wondering if we should slurp Makefile.PL to
  133. # get some context or not ...
  134. my ($package, $file, $line) = caller;
  135. die <<"EOT";
  136. Unknown function is found at $file line $line.
  137. Execution of $file aborted due to runtime errors.
  138. If you're a contributor to a project, you may need to install
  139. some Module::Install extensions from CPAN (or other repository).
  140. If you're a user of a module, please contact the author.
  141. EOT
  142. }
  143. my $method = $1;
  144. if ( uc($method) eq $method ) {
  145. # Do nothing
  146. return;
  147. } elsif ( $method =~ /^_/ and $self->can($method) ) {
  148. # Dispatch to the root M:I class
  149. return $self->$method(@_);
  150. }
  151. # Dispatch to the appropriate plugin
  152. unshift @_, ( $self, $1 );
  153. goto &{$self->can('call')};
  154. };
  155. }
  156. sub preload {
  157. my $self = shift;
  158. unless ( $self->{extensions} ) {
  159. $self->load_extensions(
  160. "$self->{prefix}/$self->{path}", $self
  161. );
  162. }
  163. my @exts = @{$self->{extensions}};
  164. unless ( @exts ) {
  165. @exts = $self->{admin}->load_all_extensions;
  166. }
  167. my %seen;
  168. foreach my $obj ( @exts ) {
  169. while (my ($method, $glob) = each %{ref($obj) . '::'}) {
  170. next unless $obj->can($method);
  171. next if $method =~ /^_/;
  172. next if $method eq uc($method);
  173. $seen{$method}++;
  174. }
  175. }
  176. my $who = $self->_caller;
  177. foreach my $name ( sort keys %seen ) {
  178. local $^W;
  179. *{"${who}::$name"} = sub {
  180. ${"${who}::AUTOLOAD"} = "${who}::$name";
  181. goto &{"${who}::AUTOLOAD"};
  182. };
  183. }
  184. }
  185. sub new {
  186. my ($class, %args) = @_;
  187. delete $INC{'FindBin.pm'};
  188. {
  189. # to suppress the redefine warning
  190. local $SIG{__WARN__} = sub {};
  191. require FindBin;
  192. }
  193. # ignore the prefix on extension modules built from top level.
  194. my $base_path = Cwd::abs_path($FindBin::Bin);
  195. unless ( Cwd::abs_path(Cwd::cwd()) eq $base_path ) {
  196. delete $args{prefix};
  197. }
  198. return $args{_self} if $args{_self};
  199. $args{dispatch} ||= 'Admin';
  200. $args{prefix} ||= 'inc';
  201. $args{author} ||= ($^O eq 'VMS' ? '_author' : '.author');
  202. $args{bundle} ||= 'inc/BUNDLES';
  203. $args{base} ||= $base_path;
  204. $class =~ s/^\Q$args{prefix}\E:://;
  205. $args{name} ||= $class;
  206. $args{version} ||= $class->VERSION;
  207. unless ( $args{path} ) {
  208. $args{path} = $args{name};
  209. $args{path} =~ s!::!/!g;
  210. }
  211. $args{file} ||= "$args{base}/$args{prefix}/$args{path}.pm";
  212. $args{wrote} = 0;
  213. bless( \%args, $class );
  214. }
  215. sub call {
  216. my ($self, $method) = @_;
  217. my $obj = $self->load($method) or return;
  218. splice(@_, 0, 2, $obj);
  219. goto &{$obj->can($method)};
  220. }
  221. sub load {
  222. my ($self, $method) = @_;
  223. $self->load_extensions(
  224. "$self->{prefix}/$self->{path}", $self
  225. ) unless $self->{extensions};
  226. foreach my $obj (@{$self->{extensions}}) {
  227. return $obj if $obj->can($method);
  228. }
  229. my $admin = $self->{admin} or die <<"END_DIE";
  230. The '$method' method does not exist in the '$self->{prefix}' path!
  231. Please remove the '$self->{prefix}' directory and run $0 again to load it.
  232. END_DIE
  233. my $obj = $admin->load($method, 1);
  234. push @{$self->{extensions}}, $obj;
  235. $obj;
  236. }
  237. sub load_extensions {
  238. my ($self, $path, $top) = @_;
  239. my $should_reload = 0;
  240. unless ( grep { ! ref $_ and lc $_ eq lc $self->{prefix} } @INC ) {
  241. unshift @INC, $self->{prefix};
  242. $should_reload = 1;
  243. }
  244. foreach my $rv ( $self->find_extensions($path) ) {
  245. my ($file, $pkg) = @{$rv};
  246. next if $self->{pathnames}{$pkg};
  247. local $@;
  248. my $new = eval { local $^W; require $file; $pkg->can('new') };
  249. unless ( $new ) {
  250. warn $@ if $@;
  251. next;
  252. }
  253. $self->{pathnames}{$pkg} =
  254. $should_reload ? delete $INC{$file} : $INC{$file};
  255. push @{$self->{extensions}}, &{$new}($pkg, _top => $top );
  256. }
  257. $self->{extensions} ||= [];
  258. }
  259. sub find_extensions {
  260. my ($self, $path) = @_;
  261. my @found;
  262. File::Find::find( sub {
  263. my $file = $File::Find::name;
  264. return unless $file =~ m!^\Q$path\E/(.+)\.pm\Z!is;
  265. my $subpath = $1;
  266. return if lc($subpath) eq lc($self->{dispatch});
  267. $file = "$self->{path}/$subpath.pm";
  268. my $pkg = "$self->{name}::$subpath";
  269. $pkg =~ s!/!::!g;
  270. # If we have a mixed-case package name, assume case has been preserved
  271. # correctly. Otherwise, root through the file to locate the case-preserved
  272. # version of the package name.
  273. if ( $subpath eq lc($subpath) || $subpath eq uc($subpath) ) {
  274. my $content = Module::Install::_read($subpath . '.pm');
  275. my $in_pod = 0;
  276. foreach ( split //, $content ) {
  277. $in_pod = 1 if /^=\w/;
  278. $in_pod = 0 if /^=cut/;
  279. next if ($in_pod || /^=cut/); # skip pod text
  280. next if /^\s*#/; # and comments
  281. if ( m/^\s*package\s+($pkg)\s*;/i ) {
  282. $pkg = $1;
  283. last;
  284. }
  285. }
  286. }
  287. push @found, [ $file, $pkg ];
  288. }, $path ) if -d $path;
  289. @found;
  290. }
  291. #####################################################################
  292. # Common Utility Functions
  293. sub _caller {
  294. my $depth = 0;
  295. my $call = caller($depth);
  296. while ( $call eq __PACKAGE__ ) {
  297. $depth++;
  298. $call = caller($depth);
  299. }
  300. return $call;
  301. }
  302. # Done in evals to avoid confusing Perl::MinimumVersion
  303. eval( $] >= 5.006 ? <<'END_NEW' : <<'END_OLD' ); die $@ if $@;
  304. sub _read {
  305. local *FH;
  306. open( FH, '<', $_[0] ) or die "open($_[0]): $!";
  307. my $string = do { local $/; <FH> };
  308. close FH or die "close($_[0]): $!";
  309. return $string;
  310. }
  311. END_NEW
  312. sub _read {
  313. local *FH;
  314. open( FH, "< $_[0]" ) or die "open($_[0]): $!";
  315. my $string = do { local $/; <FH> };
  316. close FH or die "close($_[0]): $!";
  317. return $string;
  318. }
  319. END_OLD
  320. sub _readperl {
  321. my $string = Module::Install::_read($_[0]);
  322. $string =~ s/(?:\015{1,2}\012|\015|\012)/\n/sg;
  323. $string =~ s/(\n)\n*__(?:DATA|END)__\b.*\z/$1/s;
  324. $string =~ s/\n\n=\w+.+?\n\n=cut\b.+?\n+/\n\n/sg;
  325. return $string;
  326. }
  327. sub _readpod {
  328. my $string = Module::Install::_read($_[0]);
  329. $string =~ s/(?:\015{1,2}\012|\015|\012)/\n/sg;
  330. return $string if $_[0] =~ /\.pod\z/;
  331. $string =~ s/(^|\n=cut\b.+?\n+)[^=\s].+?\n(\n=\w+|\z)/$1$2/sg;
  332. $string =~ s/\n*=pod\b[^\n]*\n+/\n\n/sg;
  333. $string =~ s/\n*=cut\b[^\n]*\n+/\n\n/sg;
  334. $string =~ s/^\n+//s;
  335. return $string;
  336. }
  337. # Done in evals to avoid confusing Perl::MinimumVersion
  338. eval( $] >= 5.006 ? <<'END_NEW' : <<'END_OLD' ); die $@ if $@;
  339. sub _write {
  340. local *FH;
  341. open( FH, '>', $_[0] ) or die "open($_[0]): $!";
  342. foreach ( 1 .. $#_ ) {
  343. print FH $_[$_] or die "print($_[0]): $!";
  344. }
  345. close FH or die "close($_[0]): $!";
  346. }
  347. END_NEW
  348. sub _write {
  349. local *FH;
  350. open( FH, "> $_[0]" ) or die "open($_[0]): $!";
  351. foreach ( 1 .. $#_ ) {
  352. print FH $_[$_] or die "print($_[0]): $!";
  353. }
  354. close FH or die "close($_[0]): $!";
  355. }
  356. END_OLD
  357. # _version is for processing module versions (eg, 1.03_05) not
  358. # Perl versions (eg, 5.8.1).
  359. sub _version ($) {
  360. my $s = shift || 0;
  361. my $d =()= $s =~ /(\.)/g;
  362. if ( $d >= 2 ) {
  363. # Normalise multipart versions
  364. $s =~ s/(\.)(\d{1,3})/sprintf("$1%03d",$2)/eg;
  365. }
  366. $s =~ s/^(\d+)\.?//;
  367. my $l = $1 || 0;
  368. my @v = map {
  369. $_ . '0' x (3 - length $_)
  370. } $s =~ /(\d{1,3})\D?/g;
  371. $l = $l . '.' . join '', @v if @v;
  372. return $l + 0;
  373. }
  374. sub _cmp ($$) {
  375. _version($_[0]) <=> _version($_[1]);
  376. }
  377. # Cloned from Params::Util::_CLASS
  378. sub _CLASS ($) {
  379. (
  380. defined $_[0]
  381. and
  382. ! ref $_[0]
  383. and
  384. $_[0] =~ m/^[^\W\d]\w*(?:::\w+)*\z/s
  385. ) ? $_[0] : undef;
  386. }
  387. 1;
  388. # Copyright 2008 - 2011 Adam Kennedy.