Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Get installation order of interdependent module list
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;
use Capture::Tiny qw(capture_stdout);
use List::MoreUtils qw(uniq);
sub prereqs;
my %modules;
for my $module (@ARGV) {
my $deps = capture_stdout {
system qw/cpanm -q --showdeps/, $module;
};
my %deps = map {;$_=>1} split /\n/, $deps;
# The subset of %deps that are in @ARGV.
$modules{$module} = +{ map { $deps{$_} ? ($_ => 1) : ()} @ARGV };
}
my @installation_order = map { prereqs $_ } keys %modules;
sub prereqs {
my $module = shift;
my $prereqs = $modules{$module};
return +( map { prereqs $_ } keys %$prereqs ), $module;
}
say join " ", uniq @installation_order;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment