Last active
December 17, 2015 10:59
-
-
Save nanis/5599226 to your computer and use it in GitHub Desktop.
Walk through a directory, extract modules referenced in use/require statements for each Perl file, and output information in JSON format so it can be fed to create a nice graph using d3js.
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 5.014; | |
use strict; | |
use warnings; | |
use open qw(:std utf8); | |
# This is where a patched version of Module::Extract::Use lives | |
# The patched version adds a 'content' accessor to M::E::U::Item objects | |
use lib '/Users/.../src/Module-Extract-Use-1.03/lib'; | |
use Module::Extract::Use; | |
use Carp qw( croak ); | |
use File::Basename qw(basename); | |
use File::Find; | |
use File::MMagic; | |
use JSON qw( encode_json ); | |
use Module::CoreList; | |
use Module::Extract::Namespaces; | |
use YAML; | |
my $EXTOR = Module::Extract::Use->new; | |
my @NODES; | |
my %PREREQS; | |
my $MM = File::MMagic->new; | |
my $CORELIST = $Module::CoreList::version{5.008005}; | |
run(\@ARGV); | |
sub run { | |
my $argv = shift; | |
my %opts; | |
my ($top) = @$argv; | |
-d $top or croak 'Need top directory'; | |
find({wanted => \&report_use, no_chdir => 1}, $top); | |
for my $ns (keys %PREREQS) { | |
unless (grep $_->{name} eq $ns, @NODES) { | |
push @NODES, {name => $ns, prereqs => [] }; | |
} | |
} | |
say encode_json(\@NODES); | |
return; | |
} | |
sub report_use { | |
my $file = $File::Find::name; | |
return unless defined $file; | |
unless ($file =~ / [.]p[lm] \z/xi) { | |
my $type = $MM->checktype_filename($file); | |
$type =~ /executable|perl/ or return; | |
} | |
return unless -f $file; | |
my $ns = Module::Extract::Namespaces->from_file( $file ); | |
$ns ||= basename $file; | |
$ns =~ s/::/-/g; | |
my $modules = $EXTOR->get_modules_with_details($file) || []; | |
my $prereqs = [ | |
map s/::/-/gr, | |
grep !exists($CORELIST->{$_}), | |
map $_->pragma || $_->module || $_->content, @$modules | |
]; | |
push @NODES, {name => $ns, prereqs => $prereqs}; | |
$PREREQS{$_} = undef for @$prereqs; | |
return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment