Created
July 20, 2016 19:52
-
-
Save p120ph37/fd612af731c3ac82ddd8643623201230 to your computer and use it in GitHub Desktop.
Automatically download/build/install CPAN dependencies into a project-local folder.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
package autodeps; | |
### | |
# | |
# Example usage: | |
# | |
# #!/usr/bin/perl | |
# use warnings; | |
# use strict; | |
# | |
# # Core modules: | |
# use Storable; | |
# use Time::HiRes; | |
# | |
# # Non-core modules to install in "local" if not available: | |
# use autodeps qw/Geo::Calc::XS Geo::ShapeFile Inline::C Text::CSV Text::CSV_XS/; | |
# use Geo::Calc::XS; | |
# use Geo::ShapeFile; | |
# use Geo::ShapeFile::Point; | |
# use Inline Config => DIRECTORY => $autodeps::local; | |
# | |
### | |
use strict; | |
use warnings; | |
use Cwd qw/cwd abs_path/; | |
use File::Basename qw/dirname/; | |
use File::Spec::Functions qw/catdir/; | |
our $local = catdir(abs_path(dirname(__FILE__)), 'local'); | |
sub import { | |
my @r = map{ my $r = "$_.pm"; $r =~ s/::/\//g; $r; }(@_[1..$#_]); | |
eval { | |
require(catdir($local, qw/lib perl5 local lib.pm/)); | |
local::lib->import($local); | |
require $_ for @r; | |
}; | |
if($@) { | |
open my $oldout, '>&', \*STDOUT; | |
open \*STDOUT, '>&', \*STDERR; | |
my $cwd = cwd(); | |
chdir catdir($local, '..'); | |
system(qw/cpanm -l local local::lib/, @_[1..$#_]); | |
warn "CWD: $cwd\n"; | |
chdir $cwd; | |
require local::lib; | |
local::lib->import($local); | |
require $_ for @r; | |
open \*STDOUT, '>&', $oldout; | |
} | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Depends only on core Perl modules and the
cpanm
command. Additional build-system and c-library dependencies may be required by some CPAN modules. Bootstrapslocal::lib
(if it is not available), builds the requested modules in./local/
(if they have not been built already), and finallyrequire
s them (but does notuse
them - you need to do that yourself if you want any specificimport()
calls to be made)