| 1 | package Meta::Grapher::Moose::Renderer::Plantuml; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use namespace::autoclean; | ||
| 6 | use autodie; | ||
| 7 | |||
| 8 | our $VERSION = '1.03'; | ||
| 9 | |||
| 10 | use IPC::Run3; | ||
| 11 | use Meta::Grapher::Moose::Constants qw( CLASS ROLE P_ROLE ANON_ROLE ); | ||
| 12 | use Meta::Grapher::Moose::Renderer::Plantuml::Class; | ||
| 13 | use Meta::Grapher::Moose::Renderer::Plantuml::Link; | ||
| 14 | |||
| 15 | use Moose; | ||
| 16 | |||
| 17 | with( | ||
| 18 | 'Meta::Grapher::Moose::Role::HasOutput', | ||
| 19 | 'Meta::Grapher::Moose::Role::Renderer', | ||
| 20 | ); | ||
| 21 | |||
| 22 | has java_command => ( | ||
| 23 | is => 'ro', | ||
| 24 | isa => 'Str', | ||
| 25 | default => 'java', | ||
| 26 | documentation => 'The command to run the java binary. Defaults to "java"', | ||
| 27 | ); | ||
| 28 | |||
| 29 | has plantuml_jar => ( | ||
| 30 | is => 'ro', | ||
| 31 | isa => 'Str', | ||
| 32 | default => 'plantuml.jar', | ||
| 33 | documentation => | ||
| 34 | 'Path to the plantuml.jar. Defaults to plantuml.jar in the current directory', | ||
| 35 | ); | ||
| 36 | |||
| 37 | has formatting => ( | ||
| 38 | is => 'ro', | ||
| 39 | isa => 'HashRef[Str]', | ||
| 40 | builder => '_build_formatting', | ||
| 41 | documentation => 'Set specific spot markup for classes', | ||
| 42 | ); | ||
| 43 | |||
| 44 | sub _build_formatting { | ||
| 45 | return { | ||
| 46 | CLASS() => q{}, | ||
| 47 | ROLE() => '<<R,#FF7700>>', | ||
| 48 | P_ROLE() => '<<P,orchid>>', | ||
| 49 | ANON_ROLE() => '<<?,white>>', | ||
| 50 | }; | ||
| 51 | } | ||
| 52 | |||
| 53 | # Keep internal state of what we've seen until we're ready to render and | ||
| 54 | # create the source code. | ||
| 55 | has _plantuml_classes => ( | ||
| 56 | is => 'ro', | ||
| 57 | isa => 'HashRef[Meta::Grapher::Moose::Renderer::Plantuml::Class]', | ||
| 58 | default => sub { return {} }, | ||
| 59 | traits => ['Hash'], | ||
| 60 | handles => { | ||
| 61 | _add_plantuml_class => 'set', | ||
| 62 | _get_plantuml_class => 'get', | ||
| 63 | _all_plantuml_classes => 'values', | ||
| 64 | }, | ||
| 65 | ); | ||
| 66 | |||
| 67 | has _plantuml_links => ( | ||
| 68 | is => 'ro', | ||
| 69 | isa => 'ArrayRef[Meta::Grapher::Moose::Renderer::Plantuml::Link]', | ||
| 70 | default => sub { return [] }, | ||
| 71 | traits => ['Array'], | ||
| 72 | handles => { | ||
| 73 | _add_plantuml_link => 'push', | ||
| 74 | _all_plantuml_links => 'elements', | ||
| 75 | }, | ||
| 76 | ); | ||
| 77 | |||
| 78 | sub render { | ||
| 79 | my $self = shift; | ||
| 80 | |||
| 81 | my $src = $self->_calculate_source; | ||
| 82 | |||
| 83 | my $fh; | ||
| 84 | if ( $self->has_output ) { | ||
| 85 | ## no critic (InputOutput::RequireBriefOpen) | ||
| 86 | open $fh, '>:raw', $self->output; | ||
| 87 | ## use critic | ||
| 88 | } | ||
| 89 | else { | ||
| 90 | $fh = \*STDOUT; | ||
| 91 | } | ||
| 92 | |||
| 93 | if ( $self->format =~ /\A(?:src|plantuml)\z/ ) { | ||
| 94 | print {$fh} $src or die $!; | ||
| 95 | return; | ||
| 96 | } | ||
| 97 | |||
| 98 | return $self->_render_with_run3( $src, $fh ); | ||
| 99 | } | ||
| 100 | |||
| 101 | sub _full_java_command { | ||
| 102 | my $self = shift; | ||
| 103 | |||
| 104 | # Run plantuml while accepting source on STDIN, outputing to STDOUT | ||
| 105 | return $self->java_command, '-jar', $self->plantuml_jar, '-pipe', | ||
| 106 | '-t' . $self->format; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub _render_with_run3 { | ||
| 110 | my $self = shift; | ||
| 111 | my $src = shift; | ||
| 112 | my $fh = shift; | ||
| 113 | |||
| 114 | # note that errors just go to STDERR. This should be probably altered | ||
| 115 | # to be something more user friendly at some point in the future. | ||
| 116 | run3( [ $self->_full_java_command ], \$src, $fh ); | ||
| 117 | |||
| 118 | return; | ||
| 119 | } | ||
| 120 | |||
| 121 | sub add_package { | ||
| 122 | my $self = shift; | ||
| 123 | my %args = @_; | ||
| 124 | |||
| 125 | $self->_add_plantuml_class( | ||
| 126 | $args{id}, | ||
| 127 | Meta::Grapher::Moose::Renderer::Plantuml::Class->new( | ||
| 128 | id => $args{id}, | ||
| 129 | label => $args{label}, | ||
| 130 | class_attributes => $args{attributes}, | ||
| 131 | class_methods => $args{methods}, | ||
| 132 | class_type => $args{type}, | ||
| 133 | formatting => $self->formatting, | ||
| 134 | ) | ||
| 135 | ) unless $self->_get_plantuml_class( $args{id} ); | ||
| 136 | |||
| 137 | return; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub add_edge { | ||
| 141 | my $self = shift; | ||
| 142 | my %p = @_; | ||
| 143 | |||
| 144 | $self->_add_plantuml_link( | ||
| 145 | Meta::Grapher::Moose::Renderer::Plantuml::Link->new( | ||
| 146 | from => $p{from}, | ||
| 147 | to => $p{to}, | ||
| 148 | ) | ||
| 149 | ); | ||
| 150 | |||
| 151 | return; | ||
| 152 | } | ||
| 153 | |||
| 154 | sub _calculate_source { | ||
| 155 | my $self = shift; | ||
| 156 | |||
| 157 | return '@startuml' . "\n" . join( | ||
| 158 | q{}, | ||
| 159 | map { $_->to_plantuml } | ||
| 160 | sort { $a->id cmp $b->id } $self->_all_plantuml_classes | ||
| 161 | ) | ||
| 162 | . join( q{}, sort map { $_->to_plantuml } $self->_all_plantuml_links ) | ||
| 163 | . '@enduml' . "\n"; | ||
| 164 | } | ||
| 165 | |||
| 166 | __PACKAGE__->meta->make_immutable; | ||
| 167 | |||
| 168 | 1; | ||
| 169 | |||
| 170 | # ABSTRACT: Render a Meta::Grapher::Moose as a graph using PlantUML | ||
| 171 | |||
| 172 | __END__ |