Install.pm 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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.004;
  19. use strict 'vars';
  20. use vars qw{$VERSION};
  21. BEGIN {
  22. # All Module::Install core packages now require synchronised versions.
  23. # This will be used to ensure we don't accidentally load old or
  24. # different versions of modules.
  25. # This is not enforced yet, but will be some time in the next few
  26. # releases once we can make sure it won't clash with custom
  27. # Module::Install extensions.
  28. $VERSION = '0.68';
  29. }
  30. # Whether or not inc::Module::Install is actually loaded, the
  31. # $INC{inc/Module/Install.pm} is what will still get set as long as
  32. # the caller loaded module this in the documented manner.
  33. # If not set, the caller may NOT have loaded the bundled version, and thus
  34. # they may not have a MI version that works with the Makefile.PL. This would
  35. # result in false errors or unexpected behaviour. And we don't want that.
  36. my $file = join( '/', 'inc', split /::/, __PACKAGE__ ) . '.pm';
  37. unless ( $INC{$file} ) {
  38. die <<"END_DIE";
  39. Please invoke ${\__PACKAGE__} with:
  40. use inc::${\__PACKAGE__};
  41. not:
  42. use ${\__PACKAGE__};
  43. END_DIE
  44. }
  45. # If the script that is loading Module::Install is from the future,
  46. # then make will detect this and cause it to re-run over and over
  47. # again. This is bad. Rather than taking action to touch it (which
  48. # is unreliable on some platforms and requires write permissions)
  49. # for now we should catch this and refuse to run.
  50. if ( -f $0 and (stat($0))[9] > time ) {
  51. die << "END_DIE";
  52. Your installer $0 has a modification time in the future.
  53. This is known to create infinite loops in make.
  54. Please correct this, then run $0 again.
  55. END_DIE
  56. }
  57. use Cwd ();
  58. use File::Find ();
  59. use File::Path ();
  60. use FindBin;
  61. *inc::Module::Install::VERSION = *VERSION;
  62. @inc::Module::Install::ISA = __PACKAGE__;
  63. sub autoload {
  64. my $self = shift;
  65. my $who = $self->_caller;
  66. my $cwd = Cwd::cwd();
  67. my $sym = "${who}::AUTOLOAD";
  68. $sym->{$cwd} = sub {
  69. my $pwd = Cwd::cwd();
  70. if ( my $code = $sym->{$pwd} ) {
  71. # delegate back to parent dirs
  72. goto &$code unless $cwd eq $pwd;
  73. }
  74. $$sym =~ /([^:]+)$/ or die "Cannot autoload $who - $sym";
  75. unshift @_, ($self, $1);
  76. goto &{$self->can('call')} unless uc($1) eq $1;
  77. };
  78. }
  79. sub import {
  80. my $class = shift;
  81. my $self = $class->new(@_);
  82. my $who = $self->_caller;
  83. unless ( -f $self->{file} ) {
  84. require "$self->{path}/$self->{dispatch}.pm";
  85. File::Path::mkpath("$self->{prefix}/$self->{author}");
  86. $self->{admin} = "$self->{name}::$self->{dispatch}"->new( _top => $self );
  87. $self->{admin}->init;
  88. @_ = ($class, _self => $self);
  89. goto &{"$self->{name}::import"};
  90. }
  91. *{"${who}::AUTOLOAD"} = $self->autoload;
  92. $self->preload;
  93. # Unregister loader and worker packages so subdirs can use them again
  94. delete $INC{"$self->{file}"};
  95. delete $INC{"$self->{path}.pm"};
  96. }
  97. sub preload {
  98. my ($self) = @_;
  99. unless ( $self->{extensions} ) {
  100. $self->load_extensions(
  101. "$self->{prefix}/$self->{path}", $self
  102. );
  103. }
  104. my @exts = @{$self->{extensions}};
  105. unless ( @exts ) {
  106. my $admin = $self->{admin};
  107. @exts = $admin->load_all_extensions;
  108. }
  109. my %seen;
  110. foreach my $obj ( @exts ) {
  111. while (my ($method, $glob) = each %{ref($obj) . '::'}) {
  112. next unless $obj->can($method);
  113. next if $method =~ /^_/;
  114. next if $method eq uc($method);
  115. $seen{$method}++;
  116. }
  117. }
  118. my $who = $self->_caller;
  119. foreach my $name ( sort keys %seen ) {
  120. *{"${who}::$name"} = sub {
  121. ${"${who}::AUTOLOAD"} = "${who}::$name";
  122. goto &{"${who}::AUTOLOAD"};
  123. };
  124. }
  125. }
  126. sub new {
  127. my ($class, %args) = @_;
  128. # ignore the prefix on extension modules built from top level.
  129. my $base_path = Cwd::abs_path($FindBin::Bin);
  130. unless ( Cwd::abs_path(Cwd::cwd()) eq $base_path ) {
  131. delete $args{prefix};
  132. }
  133. return $args{_self} if $args{_self};
  134. $args{dispatch} ||= 'Admin';
  135. $args{prefix} ||= 'inc';
  136. $args{author} ||= ($^O eq 'VMS' ? '_author' : '.author');
  137. $args{bundle} ||= 'inc/BUNDLES';
  138. $args{base} ||= $base_path;
  139. $class =~ s/^\Q$args{prefix}\E:://;
  140. $args{name} ||= $class;
  141. $args{version} ||= $class->VERSION;
  142. unless ( $args{path} ) {
  143. $args{path} = $args{name};
  144. $args{path} =~ s!::!/!g;
  145. }
  146. $args{file} ||= "$args{base}/$args{prefix}/$args{path}.pm";
  147. bless( \%args, $class );
  148. }
  149. sub call {
  150. my ($self, $method) = @_;
  151. my $obj = $self->load($method) or return;
  152. splice(@_, 0, 2, $obj);
  153. goto &{$obj->can($method)};
  154. }
  155. sub load {
  156. my ($self, $method) = @_;
  157. $self->load_extensions(
  158. "$self->{prefix}/$self->{path}", $self
  159. ) unless $self->{extensions};
  160. foreach my $obj (@{$self->{extensions}}) {
  161. return $obj if $obj->can($method);
  162. }
  163. my $admin = $self->{admin} or die <<"END_DIE";
  164. The '$method' method does not exist in the '$self->{prefix}' path!
  165. Please remove the '$self->{prefix}' directory and run $0 again to load it.
  166. END_DIE
  167. my $obj = $admin->load($method, 1);
  168. push @{$self->{extensions}}, $obj;
  169. $obj;
  170. }
  171. sub load_extensions {
  172. my ($self, $path, $top) = @_;
  173. unless ( grep { lc $_ eq lc $self->{prefix} } @INC ) {
  174. unshift @INC, $self->{prefix};
  175. }
  176. foreach my $rv ( $self->find_extensions($path) ) {
  177. my ($file, $pkg) = @{$rv};
  178. next if $self->{pathnames}{$pkg};
  179. local $@;
  180. my $new = eval { require $file; $pkg->can('new') };
  181. unless ( $new ) {
  182. warn $@ if $@;
  183. next;
  184. }
  185. $self->{pathnames}{$pkg} = delete $INC{$file};
  186. push @{$self->{extensions}}, &{$new}($pkg, _top => $top );
  187. }
  188. $self->{extensions} ||= [];
  189. }
  190. sub find_extensions {
  191. my ($self, $path) = @_;
  192. my @found;
  193. File::Find::find( sub {
  194. my $file = $File::Find::name;
  195. return unless $file =~ m!^\Q$path\E/(.+)\.pm\Z!is;
  196. my $subpath = $1;
  197. return if lc($subpath) eq lc($self->{dispatch});
  198. $file = "$self->{path}/$subpath.pm";
  199. my $pkg = "$self->{name}::$subpath";
  200. $pkg =~ s!/!::!g;
  201. # If we have a mixed-case package name, assume case has been preserved
  202. # correctly. Otherwise, root through the file to locate the case-preserved
  203. # version of the package name.
  204. if ( $subpath eq lc($subpath) || $subpath eq uc($subpath) ) {
  205. open PKGFILE, "<$subpath.pm" or die "find_extensions: Can't open $subpath.pm: $!";
  206. my $in_pod = 0;
  207. while ( <PKGFILE> ) {
  208. $in_pod = 1 if /^=\w/;
  209. $in_pod = 0 if /^=cut/;
  210. next if ($in_pod || /^=cut/); # skip pod text
  211. next if /^\s*#/; # and comments
  212. if ( m/^\s*package\s+($pkg)\s*;/i ) {
  213. $pkg = $1;
  214. last;
  215. }
  216. }
  217. close PKGFILE;
  218. }
  219. push @found, [ $file, $pkg ];
  220. }, $path ) if -d $path;
  221. @found;
  222. }
  223. sub _caller {
  224. my $depth = 0;
  225. my $call = caller($depth);
  226. while ( $call eq __PACKAGE__ ) {
  227. $depth++;
  228. $call = caller($depth);
  229. }
  230. return $call;
  231. }
  232. 1;