Module: RGL::Graph

Defined in:
lib/cocoapods-binary-cache/dependencies_graph/graph_visualizer.rb

Instance Method Summary collapse

Instance Method Details

#to_dot_graph(params = {}, highlight_nodes) ⇒ Object



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
# File 'lib/cocoapods-binary-cache/dependencies_graph/graph_visualizer.rb', line 9

def to_dot_graph(params={}, highlight_nodes)
  params['name'] ||= self.class.name.gsub(/:/, '_')
  fontsize       = params['fontsize'] ? params['fontsize'] : '12'
  graph          = (directed? ? DOT::Digraph : DOT::Graph).new(params)
  edge_class     = directed? ? DOT::DirectedEdge : DOT::Edge
  vertex_options = params['vertex'] || {}
  edge_options   = params['edge'] || {}

  each_vertex do |v|
    default_vertex_options =  {
      'name'     => vertex_id(v),
      'fontsize' => fontsize,
      'label'    => vertex_label(v),
      'style'    => 'filled',
    }
    if highlight_nodes.include?(v)
      default_vertex_options = default_vertex_options.merge({
        'color' => 'red',
        'fillcolor' => 'red'
      })
    else
      default_vertex_options = default_vertex_options.merge({
        'color' => 'blue',
        'fillcolor' => 'blue'
      })
    end

    each_vertex_options = default_vertex_options.merge(vertex_options)
    vertex_options.each{|option, val| each_vertex_options[option] = val.call(v) if val.is_a?(Proc)}
    graph << DOT::Node.new(each_vertex_options)
  end

  each_edge do |u, v|
    default_edge_options = {
      'from'     => vertex_id(u),
      'to'       => vertex_id(v),
      'fontsize' => fontsize
    }
    each_edge_options = default_edge_options.merge(edge_options)
    edge_options.each{|option, val| each_edge_options[option] = val.call(u, v) if val.is_a?(Proc)}
    graph << edge_class.new(each_edge_options)
  end

  graph
end

#write_to_graphic_file(fmt = 'png', dotfile = "graph", options = {}, highlight_nodes) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cocoapods-binary-cache/dependencies_graph/graph_visualizer.rb', line 55

def write_to_graphic_file(fmt='png', dotfile="graph", options={}, highlight_nodes)
  src = dotfile + ".dot"
  dot = dotfile + "." + fmt

  File.open(src, 'w') do |f|
    f << self.to_dot_graph(params=options, highlight_nodes=highlight_nodes).to_s << "\n"
  end

  unless system("dot -T#{fmt} #{src} -o #{dot}")
    message = "      Error executing dot. Did you install GraphViz?\n      Try installing it via Homebrew: `brew install graphviz`.\n      Visit https://graphviz.org/download/ for more installation instructions.\n    HEREDOC\n    raise message\n  end\n  dot\nend\n" # Use <<- to indent End of String terminator