Skip to content

Instantly share code, notes, and snippets.

@tokubass
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tokubass/2dc55f41cc278c18c911 to your computer and use it in GitHub Desktop.
Save tokubass/2dc55f41cc278c18c911 to your computer and use it in GitHub Desktop.
PPIで特定ディレクトリ内だけの継承関係グラフ化(依存モジュールエラーは嫌だ)
#!/usr/bin/env perl
use strict;
use warnings;
use PPI;
use File::PackageIndexer;
use IO::All;
use Graph::Easy;
use Encode;
my $dir = shift;
my $isa_relationship = {};
my $indexer = File::PackageIndexer->new;
for my $io_file (io($dir)->deep->all_files) {
my $Document = PPI::Document->new($io_file->name);
my $pkgs = $indexer->parse($Document);
for my $pkg (keys %$pkgs) {
$pkgs->{$pkg} = $pkgs->{$pkg}->{isa};
}
$isa_relationship = { %$pkgs, %$isa_relationship };
}
my $graph = Graph::Easy->new;
for my $child_pkg (keys %$isa_relationship) {
if ( my @parent_pkgs = @{$isa_relationship->{$child_pkg}} ) {
for my $parent_pkg (@parent_pkgs) {
$graph->add_edge($child_pkg,$parent_pkg);
unless ($isa_relationship->{$parent_pkg}) {
set_color_attr_of_node($graph->node($parent_pkg), 'yellow');
}
}
}else{
set_color_attr_of_node($graph->node($child_pkg), 'red');
}
}
sub set_color_attr_of_node {
my ($node, $color) = @_;
return unless $node;
$node->set_attribute('fill', $color);
}
print encode_utf8($graph->as_html_file);
__END__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment