Created
June 23, 2014 05:19
-
-
Save miyagawa/1cadd24f573d269675e3 to your computer and use it in GitHub Desktop.
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
diff --git a/lib/App/CPANGhq.pm b/lib/App/CPANGhq.pm | |
index bfdd431..0b0c785 100644 | |
--- a/lib/App/CPANGhq.pm | |
+++ b/lib/App/CPANGhq.pm | |
@@ -14,28 +14,15 @@ use Module::Metadata; | |
use version 0.77 (); | |
use Getopt::Long (); | |
use Pod::Usage (); | |
+use MetaCPAN::Client; | |
our @MIRRORS = qw/http%www.cpan.org http%cpan.metacpan.org/; | |
use Class::Accessor::Lite::Lazy 0.03 ( | |
new => 1, | |
ro_lazy => { | |
- packages_file => sub { | |
- max_by { +(stat($_))[9] } #mtime | |
- grep {-f $_} | |
- map { | |
- "$ENV{HOME}/.cpanm/sources/$_/02packages.details.txt"; | |
- } @MIRRORS; | |
- }, | |
- installed_base => sub { $Config{sitelibexp} }, | |
- search_inc => sub { | |
- my $d = shift->installed_base; | |
- [$d, "$d/$Config{archname}"]; | |
- }, | |
- meta_dir => sub { | |
- shift->installed_base . "/$Config{archname}/.meta"; | |
- } | |
- }, | |
+ client => sub { MetaCPAN::Client->new }, | |
+ } | |
); | |
## class methods | |
@@ -45,12 +32,7 @@ sub run { | |
my ($opt, $argv) = $class->parse_options(@argv); | |
my @modules = @$argv; | |
- if ($opt->{cpanfile}) { | |
- push @modules, $class->resolve_modules_from_cpanfile; | |
- } | |
- | |
my $self = $class->new; | |
- | |
$self->clone_modules(@modules); | |
} | |
@@ -92,22 +74,7 @@ sub clone_modules { | |
my ($self, @modules) = @_; | |
for my $module (@modules) { | |
- my $dist_path = $self->search_mirror_index($module); | |
- unless ($dist_path) { | |
- warn "skip $module: distribution is not found in packages file.\n"; | |
- next; | |
- } | |
- | |
- my $d = CPAN::DistnameInfo->new($dist_path); | |
- my $dist_name = $d->dist; | |
- | |
- unless (Module::Metadata->new_from_module($module, inc => $self->search_inc)) { | |
- warn "skip $module: not installed in site_perl.\n"; | |
- next; | |
- } | |
- | |
- my $repo = $self->resolve_repo($dist_name); | |
- | |
+ my $repo = $self->resolve_repo($module); | |
if ($repo) { | |
!system 'ghq', 'get', $repo or do { warn $! if $! }; | |
} | |
@@ -118,49 +85,18 @@ sub clone_modules { | |
} | |
sub resolve_repo { | |
- my ($self, $dist_name) = @_; | |
- | |
- my $base = $self->meta_dir; | |
- my @dirs = glob "$base/$dist_name*"; | |
- | |
- my @candidate_metas; | |
- for my $d (@dirs) { | |
- my $dirbase = basename $d; | |
- next unless $dirbase =~ m!\A\Q$dist_name\E-[^-]+\z!ms; | |
- | |
- my $meta_json = "$d/MYMETA.json"; | |
- next unless -f $meta_json && -r $meta_json; | |
- | |
- my $meta = decode_json(do { | |
- local $/; | |
- open my $fh, '<', $meta_json or die $!; | |
- <$fh> | |
- }); | |
- | |
- push @candidate_metas, $meta; | |
- } | |
- | |
- my $meta = max_by { version->parse($_->{version})->numify } @candidate_metas; | |
- | |
- $meta && $meta->{resources}{repository}{url}; | |
-} | |
- | |
-sub search_mirror_index { | |
- my ($self, $module) = @_; | |
- | |
- my $packages_file = $self->packages_file or die 'no packages file found'; | |
- open my $fh, '<', $packages_file or die $!; | |
- while (<$fh>) { | |
- if (my (undef, $tar_path) = $_ =~ m!^ | |
- \Q$module\E | |
- \s+ | |
- ([\w\.]+) # version | |
- \s+ | |
- (\S*) # tar path | |
- !mx) { | |
- return $tar_path; | |
+ my ($self, $name) = @_; | |
+ | |
+ my $repo; | |
+ eval { | |
+ my $module = $self->client->module($name); | |
+ my $release = $self->client->release($module->distribution); | |
+ if ($release->resources->{repository}) { | |
+ $repo = $release->resources->{repository}{url}; | |
} | |
- } | |
+ }; | |
+ | |
+ return $repo; | |
} | |
1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment