Author: dagolden
Date: Wed Sep 16 19:25:30 2009
New Revision: 13353
Modified:
Module-Build/branches/inc-bundling/lib/inc/latest.pm
Module-Build/branches/inc-bundling/t/bundle_inc.t
Log:
[PATCH 1/3] refactor inc::latest guts into separate module
Modified: Module-Build/branches/inc-bundling/lib/inc/latest.pm
==============================================================================
--- Module-Build/branches/inc-bundling/lib/inc/latest.pm (original)
+++ Module-Build/branches/inc-bundling/lib/inc/latest.pm Wed Sep 16
19:25:30 2009
@@ -9,38 +9,32 @@
use IO::File ();
use File::Copy ();
+# track and return modules loaded by inc::latest
+my @loaded_modules;
+sub loaded_modules {...@loaded_modules}
+
+# must ultimately "goto" the import routine of the module to be loaded
+# so that the calling package is correct when $mod->import() runs.
sub import {
my ($package, $mod, @args) = @_;
return unless(defined $mod);
my $inc_path = './inc/latest.pm';
+ my $private_path = './inc/latest/private.pm';
if(-e $inc_path) {
# delete our methods
delete $inc::latest::{$_} for(keys %inc::latest::);
# load the bundled module
require $inc_path;
+ require $private_path;
my $import = inc::latest->can('import');
goto $import;
}
- # author mode - just load the modules
- $package->load_module($mod, @args);
-}
-
-my @loaded_modules;
-sub loaded_modules {...@loaded_modules}
-
-sub load_module {
- my $package = shift;
- my ($mod, @args) = @_;
-
+ # author mode - just record and load the modules
push(@loaded_modules, $mod);
- (my $pm = $mod) =~ s#::#/#g;
- $pm .= '.pm';
- require($pm);
- if(@args and my $import = $mod->can('import')) {
- goto $import;
- }
+ require inc::latest::private;
+ goto \&inc::latest::private::_load_module;
}
sub write {
@@ -48,9 +42,21 @@
my ($where) = @_;
warn "should really be writing in inc/" unless $where =~ /inc$/;
- File::Path::mkpath $where;
+
+ # write inc/latest.pm
+ File::Path::mkpath( $where );
my $fh = IO::File->new( File::Spec->catfile($where,'latest.pm'), "w" );
print {$fh} do {local $/; <DATA>};
+ close $fh;
+
+ # write inc/latest/private;
+ require inc::latest::private;
+ File::Path::mkpath( File::Spec->catdir( $where, 'latest' ) );
+ my $from = $INC{'inc/latest/private.pm'};
+ my $to = File::Spec->catfile($where,'latest','private.pm');
+ File::Copy::copy( $from, $to ) or die "Couldn't copy '$from' to '$to': $!";
+
+ return 1;
}
sub bundle_module {
@@ -223,94 +229,8 @@
__DATA__
package inc::latest;
-
use strict;
-use File::Spec;
-use IO::File;
-
-sub import {
- my ($pack, $mod, @args) = @_;
- my $file = $pack->_mod2path($mod);
-
- if ($INC{$file}) {
- # Already loaded
- return $pack->_load($mod, @args);
- }
-
- # A bundled copy must be present
- my ($bundled, $bundled_dir) = $pack->_search_bundled($file)
- or die "No bundled copy of $mod found";
-
- my $from_inc = $pack->_search_INC($file);
- unless ($from_inc) {
- # Only bundled is available
- unshift(@INC, $bundled_dir);
- return $pack->_load($mod, @args);
- }
-
- if (_version($from_inc) >= _version($bundled)) {
- # Ignore the bundled copy
- return $pack->_load($mod, @args);
- }
-
- # Load the bundled copy
- unshift(@INC, $bundled_dir);
- return $pack->_load($mod, @args);
-}
-
-sub _version {
- require ExtUtils::MakeMaker;
- return ExtUtils::MM->parse_version(shift);
-}
-
-sub _load {
- my ($self, $mod, @args) = @_;
- eval "require $mod";
- die $@ if $@;
- $mod->import(@args);
- return;
-}
-
-sub _search_bundled {
- my ($self, $file) = @_;
-
- my $mypath = 'inc';
-
- local *DH; # Maintain 5.005 compatibility
- opendir DH, $mypath or die "Can't open directory $mypath: $!";
-
- while (defined(my $e = readdir DH)) {
- next unless $e =~ /^inc_/;
- my $try = File::Spec->catfile($mypath, $e, $file);
-
- return($try, File::Spec->catdir($mypath, $e)) if -e $try;
- }
- return;
-}
-
-# Look for the given path in @INC.
-sub _search_INC {
- # TODO: doesn't handle coderefs or arrayrefs or objects in @INC, but
- # it probably should
- my ($self, $file) = @_;
-
- foreach my $dir (@INC) {
- next if ref $dir;
- my $try = File::Spec->catfile($dir, $file);
- return $try if -e $try;
- }
-
- return;
-}
-
-# Translate a module name into a directory/file.pm to search for in @INC
-sub _mod2path {
- my ($self, $mod) = @_;
- my @parts = split /::/, $mod;
- $parts[-1] .= '.pm';
- return $parts[0] if @parts == 1;
- return File::Spec->catfile(@parts);
-}
-
+require inc::latest::private;
+our @ISA = qw/inc::latest::private/;
1;
Modified: Module-Build/branches/inc-bundling/t/bundle_inc.t
==============================================================================
--- Module-Build/branches/inc-bundling/t/bundle_inc.t (original)
+++ Module-Build/branches/inc-bundling/t/bundle_inc.t Wed Sep 16 19:25:30 2009
@@ -25,8 +25,9 @@
);
# see what gets bundled
-my $dist_inc = File::Spec->catdir($mb->dist_dir, 'inc');
stdout_stderr_of( sub { $mb->dispatch('distdir') } );
+
+my $dist_inc = File::Spec->catdir($mb->dist_dir, 'inc');
ok( -e File::Spec->catfile( $dist_inc, 'latest.pm' ),
"./inc/latest.pm created"
);
@@ -48,7 +49,7 @@
# Force bundled M::B to a higher version so it gets loaded
-my $fh = IO::File->new($mb_file, "+<");
+my $fh = IO::File->new($mb_file, "+<") or die "Could not open $mb_file: $!";
my $mb_code = do { local $/; <$fh> };
$mb_code =~ s{\$VERSION\s+=\s+\S+}{\$VERSION = 9999;};
$fh->seek(0,0);