Skip to content

Commit

Permalink
Implemented an experimental feature to save META files and local.json…
Browse files Browse the repository at this point in the history
… into sitelib/auto/meta
  • Loading branch information
miyagawa committed Jun 24, 2011
1 parent 685447a commit 46ff731
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
65 changes: 62 additions & 3 deletions lib/App/cpanminus/script.pm
Expand Up @@ -1193,6 +1193,7 @@ sub build_stuff {
$self->diag_ok($configure_state->{configured_ok} ? "OK" : "N/A");

my @deps = $self->find_prereqs($dist);
my $module_name = $self->find_module_name($configure_state);

my $distname = $dist->{meta}{name} ? "$dist->{meta}{name}-$dist->{meta}{version}" : $stuff;

Expand Down Expand Up @@ -1262,6 +1263,7 @@ DIAG
$self->diag_ok;
$self->diag("$msg\n", 1);
$self->{installed_dists}++;
$self->save_meta($stuff, $dist, $module_name);
return 1;
} else {
my $msg = "Building $distname failed";
Expand Down Expand Up @@ -1350,6 +1352,66 @@ sub configure_this {
return $state;
}

sub find_module_name {
my($self, $state) = @_;

return unless $state->{configured_ok};

if ($state->{use_module_build} &&
-e "_build/build_params") {
my $params = do { open my $in, "_build/build_params"; $self->safe_eval(join "", <$in>) };
return $params->[2]{module_name};
} elsif (-e "Makefile") {
open my $mf, "Makefile";
while (<$mf>) {
if (/^\#\s+NAME\s+=>\s+(.*)/) {
return $self->safe_eval($1);
}
}
}

return;
}

sub save_meta {
my($self, $module, $dist, $module_name) = @_;

return unless $dist->{distvname} && $dist->{source} eq 'cpan';

my $base = ($ENV{PERL_MM_OPT} || '') =~ /INSTALL_BASE=/
? ($self->install_base($ENV{PERL_MM_OPT}) . "/lib/perl5") : $Config{sitelibexp};

my $dir = "$base/auto/meta/$dist->{distvname}";
File::Path::mkpath([ $dir ], 0, 0777);

# Existence of MYMETA.* Depends on EUMM/M::B/M::I versions *and* whether user
# has CPAN::Meta or YAML::Tiny/JSON installed
for my $file (qw( META.yml MYMETA.yml MYMETA.json )) {
if (-e $file) {
File::Copy::copy($file, "$dir/$file");
}
}

$module_name ||= "";

open my $fh, ">", "$dir/local.json" or die $!;
print $fh <<JSON;
{
"name": "$module_name",
"module": "$module",
"version": "$dist->{version}",
"dist": "$dist->{distvname}",
"pathname": "$dist->{pathname}"
}
JSON
}

sub install_base {
my($self, $mm_opt) = @_;
$mm_opt =~ /INSTALL_BASE=(\S+)/ and return $1;
die "Your PERL_MM_OPT doesn't contain INSTALL_BASE";
}

sub safe_eval {
my($self, $code) = @_;
eval $code;
Expand Down Expand Up @@ -1397,9 +1459,6 @@ sub find_prereqs {
push @deps, $self->bundle_deps($dist);
}

# No need to remove, but this gets in the way of signature testing :/
unlink $_ for qw(MYMETA.json MYMETA.yml);

return @deps;
}

Expand Down
Empty file modified script/cpanm.PL 100644 → 100755
Empty file.
19 changes: 19 additions & 0 deletions xt/meta.t
@@ -0,0 +1,19 @@
use strict;
use Test::More;
use JSON;
use xt::Run;

my $local_lib = "$ENV{PERL_CPANM_HOME}/perl5";
run "-L", $local_lib, "Hash::MultiValue";

my $dist = (last_build_log =~ /Configuring (\S+)/)[0];

my $file = "$local_lib/lib/perl5/auto/meta/$dist/local.json";
ok -e $file;

open my $in, "<", $file;
my $data = JSON::decode_json(join "", <$in>);
is $data->{name}, "Hash::MultiValue";

done_testing;

0 comments on commit 46ff731

Please sign in to comment.