Skip to content

Commit 2dcde70

Browse files
committed
add hook script to run cpanm against shim
1 parent 655faad commit 2dcde70

File tree

3 files changed

+84
-0
lines changed

3 files changed

+84
-0
lines changed

bin/cpanm-shim

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env perl
2+
use strict;
3+
use warnings;
4+
5+
use Getopt::Long qw(:config gnu_getopt pass_through permute);
6+
7+
GetOptions(
8+
'cpanm=s' => \my $cpanm,
9+
'disable-metadb!' => \my $disable_metadb,
10+
'debug!' => \my $debug,
11+
) or die "Error in command line arguments.\n";
12+
13+
if (!defined $cpanm) {
14+
$cpanm = `which cpanm`;
15+
chomp $cpanm;
16+
}
17+
18+
die "can't find cpanm!\n"
19+
unless $cpanm && -e $cpanm;
20+
21+
my @libs = do {
22+
my @incs = `"$^X" -le"print for \@INC"`;
23+
chomp @incs;
24+
my %inc = map +($_ => 1), @incs;
25+
grep !$inc{$_}, 'lib', @INC;
26+
};
27+
28+
my @opts;
29+
push @opts, 'disable-metadb'
30+
if $disable_metadb;
31+
push @opts, 'debug'
32+
if $debug;
33+
34+
exec
35+
"$^X",
36+
(map "-I$_", @libs),
37+
"-MMetaCPAN::V0Shim::cpanm_hook".(@opts ? '='.join(',', @opts) : ''),
38+
$cpanm,
39+
@ARGV;
40+

cpanfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ requires 'Plack::Middleware::SimpleLogger';
1111
requires 'WWW::Form::UrlEncoded';
1212
requires 'HTTP::Request::Common';
1313
requires 'Plack::Test';
14+
requires 'LWP::Protocol::PSGI';

lib/MetaCPAN/V0Shim/cpanm_hook.pm

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package MetaCPAN::V0Shim::cpanm_hook;
2+
use strict;
3+
use warnings;
4+
5+
use LWP::Protocol::PSGI;
6+
use MetaCPAN::V0Shim;
7+
use Plack::Builder;
8+
9+
my @hooks;
10+
11+
sub import {
12+
my ($class, @opts) = @_;
13+
my %opts = map +($_ => 1), @opts;
14+
my $shim = MetaCPAN::V0Shim->new(
15+
($opts{debug} ? (debug => 1) : ()),
16+
)->to_app;
17+
18+
$shim = builder {
19+
if ($opts{debug}) {
20+
enable sub {
21+
my $app = shift;
22+
return sub {
23+
my ($env) = @_;
24+
$env->{'psgi.errors'} = \*STDERR;
25+
$app->($env);
26+
};
27+
};
28+
}
29+
enable 'SimpleLogger', level => 'debug';
30+
mount '/v0' => $shim;
31+
mount '/' => $shim;
32+
};
33+
push @hooks, LWP::Protocol::PSGI->register($shim, host => 'api.metacpan.org');
34+
if ($opts{'disable-metadb'}) {
35+
push @hooks, LWP::Protocol::PSGI->register(sub { [500, [], [''] ] }, host => 'cpanmetadb.plackperl.org');
36+
}
37+
}
38+
39+
sub unimport {
40+
@hooks = ();
41+
}
42+
43+
1;

0 commit comments

Comments
 (0)