| 1 | package Meta::Grapher::Moose::Renderer::Graphviz; | ||
| 2 | |||
| 3 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use namespace::autoclean; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use autodie; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | our $VERSION = '1.03'; | ||
| 9 | |||
| 10 | 1 | ▶ | use File::Temp qw( tempfile ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 0 | ▶ | use GraphViz2; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 12 | use Meta::Grapher::Moose::Constants qw( CLASS ROLE P_ROLE ANON_ROLE ); | ||
| 13 | |||
| 14 | use Moose; | ||
| 15 | |||
| 16 | with( | ||
| 17 | 'Meta::Grapher::Moose::Role::HasOutput', | ||
| 18 | 'Meta::Grapher::Moose::Role::Renderer', | ||
| 19 | ); | ||
| 20 | |||
| 21 | has _graph => ( | ||
| 22 | is => 'ro', | ||
| 23 | isa => 'GraphViz2', | ||
| 24 | lazy => 1, | ||
| 25 | builder => '_build_graph', | ||
| 26 | ); | ||
| 27 | |||
| 28 | has output => ( | ||
| 29 | is => 'ro', | ||
| 30 | isa => 'Str', | ||
| 31 | ); | ||
| 32 | |||
| 33 | # TODO: Make this configurable from the command line, either by accepting some | ||
| 34 | # sort of JSON-as-command-line-argument-flag setting, or by having multiple | ||
| 35 | # attributes that *are* individually settable and are lazily built into this | ||
| 36 | # formatting hashref if nothing is passed. | ||
| 37 | has formatting => ( | ||
| 38 | is => 'ro', | ||
| 39 | isa => 'HashRef[HashRef]', | ||
| 40 | builder => '_build_formatting', | ||
| 41 | ); | ||
| 42 | |||
| 43 | sub render { | ||
| 44 | my $self = shift; | ||
| 45 | |||
| 46 | # are we rendering to a named file or a temp file? | ||
| 47 | my $output = ( | ||
| 48 | $self->has_output ? $self->output : do { | ||
| 49 | my ( undef, $filename ) = tempfile(); | ||
| 50 | $filename; | ||
| 51 | } | ||
| 52 | ); | ||
| 53 | |||
| 54 | $self->_graph->run( | ||
| 55 | format => ( $self->format eq 'src' ? 'dot' : $self->format ), | ||
| 56 | output_file => $output, | ||
| 57 | ); | ||
| 58 | |||
| 59 | # If we were rendering to STDOUT, send to STDOUT | ||
| 60 | unless ( $self->has_output ) { | ||
| 61 | open my $fh, '<:raw', $output; | ||
| 62 | while (<$fh>) { | ||
| 63 | print or die $!; | ||
| 64 | } | ||
| 65 | close $fh; | ||
| 66 | unlink $output; | ||
| 67 | } | ||
| 68 | |||
| 69 | return; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub _build_graph { | ||
| 73 | return GraphViz2->new; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub add_package { | ||
| 77 | my $self = shift; | ||
| 78 | my %args = @_; | ||
| 79 | |||
| 80 | $self->_graph->add_node( | ||
| 81 | name => $args{id}, | ||
| 82 | label => $args{label}, | ||
| 83 | %{ $self->formatting->{ $args{type} } }, | ||
| 84 | ); | ||
| 85 | |||
| 86 | return; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub _build_formatting { | ||
| 90 | my @std = ( | ||
| 91 | fontname => 'Helvetica', | ||
| 92 | fontsize => 9, | ||
| 93 | shape => 'rect', | ||
| 94 | ); | ||
| 95 | |||
| 96 | return { | ||
| 97 | CLASS() => { @std, style => 'bold', }, | ||
| 98 | ROLE() => { @std, }, | ||
| 99 | P_ROLE() => { @std, style => 'dashed', }, | ||
| 100 | ANON_ROLE() => { @std, style => 'dotted', }, | ||
| 101 | }; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub add_edge { | ||
| 105 | my $self = shift; | ||
| 106 | my %p = @_; | ||
| 107 | |||
| 108 | $self->_graph->add_edge( | ||
| 109 | from => $p{from}, | ||
| 110 | to => $p{to}, | ||
| 111 | weight => $p{weight}, | ||
| 112 | ); | ||
| 113 | |||
| 114 | return; | ||
| 115 | } | ||
| 116 | |||
| 117 | __PACKAGE__->meta->make_immutable; | ||
| 118 | |||
| 119 | 1; | ||
| 120 | |||
| 121 | # ABSTRACT: Render a Meta::Grapher::Moose as a graph using GraphViz2 | ||
| 122 | |||
| 123 | __END__ | ||