summaryrefslogtreecommitdiff
path: root/utils/spore2dot.pl
blob: 034e8c72e569739644186af1ceda5902992b89b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/usr/bin/env perl

use strict;
use warnings;

use JSON;
use IO::All;
use Pod::Usage;

pod2usage(1) unless scalar(@ARGV) > 0;

my @specs;
foreach (@ARGV) {
    my $content < io $_;
    push @specs, JSON::decode_json($content);
}

print << 'DOT';
digraph {

    node [shape=record];
    edge [arrowhead = odot, arrowtail = none];
DOT

my $has_interface = scalar(@specs) > 1;
if ($has_interface) {
    my $top = $specs[0]->{name};
    print "    \"", $top, "\"\n";
    print "        [label=\"{\\N}\"];\n\n";
    foreach my $spec (@specs) {
        my $name = $spec->{meta}->{module} || $spec->{name};
        print "    \"", $top, "\" -> \"", $name, "\"\n\n";
    }

}

my %meth;
foreach my $spec (@specs) {
    my $name = $spec->{meta}->{module} || $spec->{name};
    print "    \"", $name, "\"\n";
    print "        [label=\"{";
    print "&laquo;interface&raquo;\\n" if ($has_interface);
    print "\\N|";
    for my $name (sort keys %{$spec->{methods}}) {
        die "duplicated $name" if exists $meth{$name};
        $meth{$name} = 1;
        my $desc = $spec->{methods}->{$name};
        print $name, "(";
        my $first = 1;
        if ($desc->{required_payload}) {
            print "payload";
            $first = 0;
        }
        for (@{$desc->{required_params}}) {
            print ", " unless $first;
            print $_;
            $first = 0;
        }
        if ($desc->{optional_params}) {
            print " " unless $first;
        }
        for (@{$desc->{optional_params}}) {
            print "\\[";
            print ", " unless $first;
            print $_, "\\]";
            $first = 0;
        }
        print ")";
        print " &otimes;" if $desc->{authentication} || $spec->{authentication};
        print "\\l";
        print "&nbsp;&nbsp;&nbsp;", $desc->{method}, " ", $desc->{path}, "\\l" if $ENV{SPORE_PATH};
    }
    print "}\"];\n\n";
}
print "}\n";

__END__

=head1 NAME

spore2dot

=head1 SYNOPSIS

spore2dot.pl api1.json [api2.json, ...] > api.dot

=cut