perl: move the perl/Git/FromCPAN tree to perl/FromCPAN
[git] / perl / Git / LoadCPAN.pm
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<FromCPAN::Module::Name> shipped with Git itself.
20
21 Usually distributors will not ship with Git's Git::FromCPAN tree at
22 all, preferring to use their own packaging of CPAN modules instead.
23
24 This module is only intended to be used for code shipping in the
25 C<git.git> repository. Use it for anything else at your peril!
26
27 =cut
28
29 sub 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
73 1;