Skip to content

Instantly share code, notes, and snippets.

@p120ph37
Created July 20, 2016 19:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save p120ph37/fd612af731c3ac82ddd8643623201230 to your computer and use it in GitHub Desktop.
Save p120ph37/fd612af731c3ac82ddd8643623201230 to your computer and use it in GitHub Desktop.
Automatically download/build/install CPAN dependencies into a project-local folder.
###
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;
@p120ph37
Copy link
Author

Depends only on core Perl modules and the cpanm command. Additional build-system and c-library dependencies may be required by some CPAN modules. Bootstraps local::lib (if it is not available), builds the requested modules in ./local/ (if they have not been built already), and finally requires them (but does not use them - you need to do that yourself if you want any specific import() calls to be made)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment