1 |
dpavlin |
970 |
package Frey::Class::Graph; |
2 |
|
|
use Moose; |
3 |
|
|
|
4 |
|
|
extends 'Frey'; |
5 |
|
|
with 'Frey::Web'; |
6 |
dpavlin |
972 |
with 'Frey::File'; |
7 |
|
|
with 'Frey::Storage'; |
8 |
dpavlin |
970 |
|
9 |
|
|
use GraphViz; |
10 |
|
|
|
11 |
dpavlin |
972 |
has filter => ( |
12 |
|
|
documentation => 'Regex to select classes', |
13 |
dpavlin |
970 |
is => 'rw', |
14 |
|
|
isa => 'Str', |
15 |
dpavlin |
972 |
default => 'Frey', |
16 |
|
|
); |
17 |
|
|
|
18 |
|
|
has path => ( |
19 |
|
|
is => 'rw', |
20 |
|
|
isa => 'Str', |
21 |
dpavlin |
970 |
required => 1, |
22 |
dpavlin |
972 |
default => 'var/introspect/', |
23 |
dpavlin |
970 |
); |
24 |
|
|
|
25 |
dpavlin |
972 |
has produce_dot => ( |
26 |
|
|
is => 'rw', |
27 |
|
|
isa => 'Bool', |
28 |
|
|
); |
29 |
|
|
|
30 |
dpavlin |
970 |
sub as_markup { |
31 |
|
|
my ($self) = @_; |
32 |
|
|
|
33 |
dpavlin |
972 |
my $g = GraphViz->new( |
34 |
|
|
rankdir => 1, # horizontal |
35 |
|
|
# layout => 'neato', # grabs too much memory |
36 |
|
|
# layout => 'twopi', # grabs too much memory |
37 |
|
|
# overlap => 'compress', |
38 |
|
|
# no_overlap => 1, |
39 |
|
|
); |
40 |
dpavlin |
970 |
|
41 |
dpavlin |
972 |
our $count = {}; |
42 |
|
|
my $filter = $self->filter; |
43 |
dpavlin |
970 |
|
44 |
dpavlin |
972 |
foreach my $path ( $self->dir_extension( $self->path, qr{\.(ya?ml)$}) ) { |
45 |
dpavlin |
970 |
|
46 |
dpavlin |
972 |
my $class = $self->strip_path_extension( $path ) || die "can't strip $path"; |
47 |
|
|
my $data = $self->load( $path ); |
48 |
|
|
# warn "## $class $path ", $self->dump( $data ); # if $self->debug; |
49 |
|
|
|
50 |
|
|
sub count_label { |
51 |
|
|
my ($self,$package) = @_; |
52 |
|
|
my $label = $package; |
53 |
|
|
$label .= $self->dump( $count->{$package} ); |
54 |
|
|
return $label; |
55 |
|
|
} |
56 |
|
|
|
57 |
|
|
next if $filter && $class !~ m{$filter}; |
58 |
|
|
|
59 |
|
|
if ( my $includes = $data->{includes} ) { |
60 |
|
|
foreach my $type ( keys %$includes ) { |
61 |
|
|
foreach my $package ( @{ $includes->{$type} } ) { |
62 |
|
|
|
63 |
|
|
my $usage = $count->{$package}->{$type}->{$class}++; |
64 |
|
|
|
65 |
|
|
my $label = $self->count_label($package); |
66 |
|
|
|
67 |
|
|
next if $filter && $package !~ m{$filter}; |
68 |
|
|
|
69 |
|
|
warn "# $class\t$type\t$package\n$label\n"; |
70 |
|
|
|
71 |
|
|
$g->add_edge( $class => $package, label => $type ); |
72 |
|
|
} |
73 |
|
|
} |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
} |
77 |
|
|
|
78 |
|
|
warn "# count ",$self->dump( $count ); |
79 |
|
|
|
80 |
|
|
$self->store( 'var/classes.dot', $g->as_text ); |
81 |
|
|
|
82 |
|
|
if ( $self->produce_dot ) { |
83 |
|
|
$self->content_type( 'text/plain' ); |
84 |
|
|
return $g->as_canon; |
85 |
|
|
} |
86 |
|
|
|
87 |
dpavlin |
970 |
$self->content_type( 'image/png' ); |
88 |
|
|
return $g->as_png; |
89 |
dpavlin |
972 |
|
90 |
dpavlin |
970 |
} |
91 |
|
|
|
92 |
|
|
1; |