Skip to content

Instantly share code, notes, and snippets.

@JaSei
Created April 23, 2015 14:43
Show Gist options
  • Save JaSei/09d84f52d330977525c6 to your computer and use it in GitHub Desktop.
Save JaSei/09d84f52d330977525c6 to your computer and use it in GitHub Desktop.
find used modules and install via ppm
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw(say);
use Module::CoreList;
use File::Next;
use HTTP::Tiny;
use JSON;
use Path::Tiny;
local $| = 1;
my %modules;
my %distr;
my $iter = File::Next::files({file_filter => sub {/\.p[ml]$/}}, $ARGV[0]);
while (my $file_path = $iter->()) {
say $file_path;
my @modules;
my @content = path($file_path)->lines();
foreach my $line (@content) {
if ($line =~ /^\s*use ([\w:]+)/) {
push @modules, $1;
}
}
my $core = 0;
my $other = 0;
foreach my $module (@modules) {
if (!Module::CoreList->first_release($module)) {
my $distribution;
if ($distribution = find_distribution($module)) {
$distr{ $distribution } = 0;
$other++;
eval "use $module; 1"
or do {
if ($@ =~ /^Can't locate/) {
print "$module - $distribution install\n";
}
};
}
$modules{ $module } = $distribution;
}
$core++;
}
say "\tused modules: $core core, $other other";
}
foreach my $distribution (sort keys %distr) {
say "install $distribution";
system "ppm install $distribution";
}
sub find_distribution {
my ($module) = @_;
my $response = HTTP::Tiny->new->get("http://api.metacpan.org/v0/module/$module");
if ($response->{success}) {
my $module_info = JSON->new->decode($response->{content});
return $module_info->{distribution};
}
return;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment