Last active
December 18, 2015 08:59
-
-
Save moznion/5757889 to your computer and use it in GitHub Desktop.
Fetch dependencies of CPAN module by using metacpan api.
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
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use utf8; | |
use 5.008001; | |
use autodie; | |
use Furl; | |
use JSON; | |
use Module::CoreList; | |
use Getopt::Long qw/:config posix_default no_ignore_case bundling auto_help/; | |
GetOptions( | |
\my %opt, qw/ | |
timeout=i | |
perl=f | |
/ | |
); | |
my $furl = Furl->new( | |
agent => 'dependencies.pl', | |
timeout => $opt{timeout} || 10, | |
); | |
my $module = $ARGV[0]; | |
my $version = '"term": { "release.status": "latest" }'; | |
if ( $ARGV[1] ) { | |
$version = qq/"term": { "release.version": "$ARGV[1]" }/; | |
} | |
my $min_perl = $opt{perl} || "$]"; | |
die "[Error] Please specify module name as the first argment." unless $module; | |
$module =~ s/::/-/g; # replace | |
my $res = $furl->post( | |
'http://api.metacpan.org/v0/release/_search', | |
[ 'Content-Type' => 'application/json' ], | |
sprintf( <<'EOJ', $module, $version ) ); | |
{ | |
"query": { | |
"match_all": {} | |
}, | |
"fields": [ "dependency" ], | |
"filter": { | |
"and": [ | |
{ "term": { "release.distribution": "%s" } }, | |
{ "term": { "release.maturity": "released" } }, | |
{ %s } | |
] | |
} | |
} | |
EOJ | |
my $content_json = decode_json( $res->{content} ); | |
my $dependencies = $content_json->{hits}->{hits}[0]->{fields}->{dependency}; | |
my @cores; | |
my @non_cores; | |
for my $dependency (@$dependencies) { | |
my $module = $dependency->{module}; | |
next if $module eq 'perl'; | |
my $core_version = Module::CoreList->first_release($module); | |
if ( $core_version && $min_perl - $core_version > 0 ) { | |
push @cores, $module; | |
next; | |
} | |
push @non_cores, $module; | |
} | |
@cores = sort { $a cmp $b } unique(@cores); | |
@non_cores = sort { $a cmp $b } unique(@non_cores); | |
print "Target: perl-$min_perl\n"; | |
print 'Depends on ' . scalar(@cores) . " core modules: \n"; | |
print "\t$_\n" for (@cores); | |
print 'Depends on ' . scalar(@non_cores) . " non core modules: \n"; | |
print "\t$_\n" for (@non_cores); | |
sub unique { | |
my @array = @_; | |
my %hash = map { $_, 1 } @array; | |
return keys %hash; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment