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