| 1 | package Stepford::Grapher::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 qw(:all); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | use Moose; | ||
| 9 | |||
| 10 | use File::Temp qw( tempfile ); | ||
| 11 | use GraphViz2; | ||
| 12 | use Stepford::Grapher::Types qw( HashRef Str ); | ||
| 13 | |||
| 14 | our $VERSION = '1.01'; | ||
| 15 | |||
| 16 | has output => ( | ||
| 17 | is => 'ro', | ||
| 18 | isa => Str, | ||
| 19 | predicate => 'has_output', | ||
| 20 | ); | ||
| 21 | |||
| 22 | has format => ( | ||
| 23 | is => 'ro', | ||
| 24 | isa => Str, | ||
| 25 | lazy => 1, | ||
| 26 | builder => '_build_format', | ||
| 27 | ); | ||
| 28 | |||
| 29 | sub _build_format { | ||
| 30 | my $self = shift; | ||
| 31 | |||
| 32 | # attempt to use the file extension as the format, if there is a usable | ||
| 33 | # extension that is... | ||
| 34 | my $ext; | ||
| 35 | unless ( $self->has_output | ||
| 36 | && ( ($ext) = $self->output =~ /[.]([^.]+)\z/ ) ) { | ||
| 37 | return 'src'; | ||
| 38 | } | ||
| 39 | |||
| 40 | return $ext; | ||
| 41 | } | ||
| 42 | |||
| 43 | # TODO: Make this configurable from the command line, either by accepting some | ||
| 44 | # sort of JSON-as-command-line-argument-flag setting, or by having multiple | ||
| 45 | # attributes that *are* individually settable and are lazily built into this | ||
| 46 | # formatting hashref if nothing is passed. | ||
| 47 | has node_formatting => ( | ||
| 48 | is => 'ro', | ||
| 49 | isa => HashRef, | ||
| 50 | builder => '_build_node_formatting', | ||
| 51 | ); | ||
| 52 | |||
| 53 | sub _build_node_formatting { | ||
| 54 | return { | ||
| 55 | fontname => 'Helvetica', | ||
| 56 | fontsize => 9, | ||
| 57 | shape => 'rect', | ||
| 58 | }; | ||
| 59 | } | ||
| 60 | |||
| 61 | has edge_formatting => ( | ||
| 62 | is => 'ro', | ||
| 63 | isa => HashRef, | ||
| 64 | builder => '_build_edge_formatting', | ||
| 65 | ); | ||
| 66 | |||
| 67 | sub _build_edge_formatting { | ||
| 68 | return { | ||
| 69 | fontname => 'Helvetica', | ||
| 70 | fontsize => 7, | ||
| 71 | }; | ||
| 72 | } | ||
| 73 | |||
| 74 | with 'Stepford::Grapher::Role::Renderer'; | ||
| 75 | |||
| 76 | sub _create_blank_graph { | ||
| 77 | return GraphViz2->new; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub _populate_graph { | ||
| 81 | my $self = shift; | ||
| 82 | my $graph = shift; | ||
| 83 | my $data = shift; | ||
| 84 | |||
| 85 | $graph->add_node( | ||
| 86 | name => $_, | ||
| 87 | %{ $self->node_formatting } | ||
| 88 | ) for keys %{$data}; | ||
| 89 | |||
| 90 | for my $from ( keys %{$data} ) { | ||
| 91 | for my $label ( keys %{ $data->{$from} } ) { | ||
| 92 | $graph->add_edge( | ||
| 93 | from => $from, | ||
| 94 | to => $data->{$from}{$label}, | ||
| 95 | label => $label, | ||
| 96 | %{ $self->edge_formatting } | ||
| 97 | ); | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | return; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub _output_graph { | ||
| 105 | my $self = shift; | ||
| 106 | my $graph = shift; | ||
| 107 | |||
| 108 | # are we rendering to a named file or a temp file? | ||
| 109 | my $output = ( | ||
| 110 | $self->has_output ? $self->output : do { | ||
| 111 | my ( undef, $filename ) = tempfile(); | ||
| 112 | $filename; | ||
| 113 | } | ||
| 114 | ); | ||
| 115 | |||
| 116 | $graph->run( | ||
| 117 | format => ( $self->format eq 'src' ? 'dot' : $self->format ), | ||
| 118 | output_file => $output, | ||
| 119 | ); | ||
| 120 | |||
| 121 | # If we were rendering to STDOUT, send to STDOUT | ||
| 122 | unless ( $self->has_output ) { | ||
| 123 | open my $fh, '<:raw', $output; | ||
| 124 | while (<$fh>) { | ||
| 125 | print or die "Can't print: $!"; | ||
| 126 | } | ||
| 127 | close $fh; | ||
| 128 | unlink $output; | ||
| 129 | } | ||
| 130 | |||
| 131 | return; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub render { | ||
| 135 | my $self = shift; | ||
| 136 | my $data = shift; | ||
| 137 | |||
| 138 | my $graph = $self->_create_blank_graph; | ||
| 139 | $self->_populate_graph( $graph, $data ); | ||
| 140 | $self->_output_graph($graph); | ||
| 141 | } | ||
| 142 | |||
| 143 | __PACKAGE__->meta->make_immutable; | ||
| 144 | 1; | ||
| 145 | |||
| 146 | =pod | ||
| 147 | |||
| 148 | =encoding UTF-8 | ||
| 149 | |||
| 150 | =head1 NAME | ||
| 151 | |||
| 152 | Stepford::Grapher::Renderer::Graphviz - Render to a graph using GraphViz | ||
| 153 | |||
| 154 | =head1 VERSION | ||
| 155 | |||
| 156 | version 1.01 | ||
| 157 | |||
| 158 | =head1 SYNOPSIS | ||
| 159 | |||
| 160 | my $grapher = Stepford::Grapher->new( | ||
| 161 | step => 'My::Step::ExampleStep', | ||
| 162 | step_namespaces => ['My::Steps'], | ||
| 163 | renderer => Stepford::Grapher::Renderer::Graphiz->new( | ||
| 164 | output => 'diagram.png', | ||
| 165 | ), | ||
| 166 | ); | ||
| 167 | $grapher->run; | ||
| 168 | |||
| 169 | =head1 DESCRIPTION | ||
| 170 | |||
| 171 | Renders the graph using GraphViz. | ||
| 172 | |||
| 173 | =head1 ATTRIBUTES | ||
| 174 | |||
| 175 | =head2 output | ||
| 176 | |||
| 177 | A string containing the filename that the rendered graph should be written to. | ||
| 178 | By default this is undef, rendering the output to C<STDOUT>. | ||
| 179 | |||
| 180 | =head2 format | ||
| 181 | |||
| 182 | The format the graph should be written in (C<jpg>, C<png>, C<pdf>, etc.) You | ||
| 183 | may use the special format C<src> or C<dot> to indicate that you want simple dot | ||
| 184 | source code (for manually feeding into Graphviz) output rather than having the | ||
| 185 | rendering done for you. | ||
| 186 | |||
| 187 | By default the format is determined from the output file extension if there | ||
| 188 | is one. If one cannot be determined (either because the C<output> attribute | ||
| 189 | is undefined or the filename does not have an extension) then format will | ||
| 190 | default to C<src>. | ||
| 191 | |||
| 192 | =head1 METHOD | ||
| 193 | |||
| 194 | =head2 $renderer->render() | ||
| 195 | |||
| 196 | Renders the output. | ||
| 197 | |||
| 198 | =head1 SUPPORT | ||
| 199 | |||
| 200 | Bugs may be submitted through L<https://github.com/maxmind/Stepford-Grapher/issues>. | ||
| 201 | |||
| 202 | =head1 AUTHOR | ||
| 203 | |||
| 204 | Mark Fowler <mfowler@maxmind.com> | ||
| 205 | |||
| 206 | =head1 COPYRIGHT AND LICENSE | ||
| 207 | |||
| 208 | This software is copyright (c) 2016 - 2017 by MaxMind, Inc.. | ||
| 209 | |||
| 210 | This is free software; you can redistribute it and/or modify it under | ||
| 211 | the same terms as the Perl 5 programming language system itself. | ||
| 212 | |||
| 213 | =cut | ||
| 214 | |||
| 215 | __END__ | ||