|
| 1 | +package Git::LoadCPAN; |
| 2 | +use 5.008; |
| 3 | +use strict; |
| 4 | +use warnings; |
| 5 | + |
| 6 | +=head1 NAME |
| 7 | +
|
| 8 | +Git::LoadCPAN - Wrapper for loading modules from the CPAN (OS) or Git's own copy |
| 9 | +
|
| 10 | +=head1 DESCRIPTION |
| 11 | +
|
| 12 | +The Perl code in Git depends on some modules from the CPAN, but we |
| 13 | +don't want to make those a hard requirement for anyone building from |
| 14 | +source. |
| 15 | +
|
| 16 | +Therefore the L<Git::LoadCPAN> namespace shipped with Git contains |
| 17 | +wrapper modules like C<Git::LoadCPAN::Module::Name> that will first |
| 18 | +attempt to load C<Module::Name> from the OS, and if that doesn't work |
| 19 | +will fall back on C<Git::FromCPAN::Module::Name> shipped with Git |
| 20 | +itself. |
| 21 | +
|
| 22 | +Usually distributors will not ship with Git's Git::FromCPAN tree at |
| 23 | +all, preferring to use their own packaging of CPAN modules instead. |
| 24 | +
|
| 25 | +This module is only intended to be used for code shipping in the |
| 26 | +C<git.git> repository. Use it for anything else at your peril! |
| 27 | +
|
| 28 | +=cut |
| 29 | + |
| 30 | +sub import { |
| 31 | + shift; |
| 32 | + my $caller = caller; |
| 33 | + my %args = @_; |
| 34 | + my $module = exists $args{module} ? delete $args{module} : die "BUG: Expected 'module' parameter!"; |
| 35 | + my $import = exists $args{import} ? delete $args{import} : die "BUG: Expected 'import' parameter!"; |
| 36 | + die "BUG: Too many arguments!" if keys %args; |
| 37 | + |
| 38 | + # Foo::Bar to Foo/Bar.pm |
| 39 | + my $package_pm = $module; |
| 40 | + $package_pm =~ s[::][/]g; |
| 41 | + $package_pm .= '.pm'; |
| 42 | + |
| 43 | + eval { |
| 44 | + require $package_pm; |
| 45 | + 1; |
| 46 | + } or do { |
| 47 | + my $error = $@ || "Zombie Error"; |
| 48 | + |
| 49 | + my $Git_LoadCPAN_pm_path = $INC{"Git/LoadCPAN.pm"} || die "BUG: Should have our own path from %INC!"; |
| 50 | + |
| 51 | + require File::Basename; |
| 52 | + my $Git_LoadCPAN_pm_root = File::Basename::dirname($Git_LoadCPAN_pm_path) || die "BUG: Can't figure out lib/Git dirname from '$Git_LoadCPAN_pm_path'!"; |
| 53 | + |
| 54 | + require File::Spec; |
| 55 | + my $Git_pm_FromCPAN_root = File::Spec->catdir($Git_LoadCPAN_pm_root, 'FromCPAN'); |
| 56 | + die "BUG: '$Git_pm_FromCPAN_root' should be a directory!" unless -d $Git_pm_FromCPAN_root; |
| 57 | + |
| 58 | + local @INC = ($Git_pm_FromCPAN_root, @INC); |
| 59 | + require $package_pm; |
| 60 | + }; |
| 61 | + |
| 62 | + if ($import) { |
| 63 | + no strict 'refs'; |
| 64 | + *{"${caller}::import"} = sub { |
| 65 | + shift; |
| 66 | + use strict 'refs'; |
| 67 | + unshift @_, $module; |
| 68 | + goto &{"${module}::import"}; |
| 69 | + }; |
| 70 | + use strict 'refs'; |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +1; |
0 commit comments