1 package Autofetch; |
1 package Autofetch; |
2 |
2 |
3 use strict; |
3 use strict; |
4 use warnings; |
4 use warnings; |
|
5 use File::Spec::Functions qw(catfile rel2abs); |
5 |
6 |
6 sub fetch { |
7 sub fetch { |
7 my ($path, $module) = @_; |
8 my ($path, $module) = @_; |
8 system 'cpanm', -nq, -l => $path, $module; |
9 system 'cpanm', -nq, -l => $path, $module; |
9 } |
10 } |
10 |
11 |
11 sub cachepath { "$ENV{HOME}/.cache/lib/" . ($_[0] =~ y|/ |-_|r) } |
12 sub cachepath { |
|
13 catfile($ENV{HOME}, '.cache', 'lib', rel2abs($_[0]) =~ y|/ |-_|r); |
|
14 } |
12 |
15 |
13 sub modulefy { $_[0] =~ s/\.pm$//r =~ s|/|::|gr } |
16 sub modulefy { $_[0] =~ s/\.pm$//r =~ s|/|::|gr } |
14 |
17 |
15 my $path = cachepath($0); |
18 my $path = cachepath($0); |
|
19 my $incpath = catfile($path, 'lib', 'perl5'); |
16 |
20 |
17 push @INC, "$path/lib/perl5", sub { |
21 push @INC, $incpath, sub { |
18 my (undef, $file) = @_; |
22 my (undef, $file) = @_; |
19 |
23 |
20 fetch($path, modulefy($file)); |
24 fetch($path, modulefy($file)); |
21 |
25 |
22 return IO::File->new($path . '/lib/perl5/' . $file); |
26 return IO::File->new(catfile($incpath, $file)); |
23 }; |
27 }; |
24 |
28 |
25 1; |
29 1; |