Class: Terraspace::All::Grapher

Inherits:
Base
  • Object
show all
Includes:
Util::Logging
Defined in:
lib/terraspace/all/grapher.rb

Instance Method Summary collapse

Methods included from Util::Logging

#logger

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Terraspace::All::Base

Instance Method Details

#build_tree_data(nodes) ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'lib/terraspace/all/grapher.rb', line 29

def build_tree_data(nodes)
  if nodes.size == 1
    tree_data(nodes.first)
  else
    root = Terraspace::Dependency::Node.new('.')
    nodes.each { |node| node.parent!(root) }
    tree_data(root)
  end
end

#draw(nodes) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/terraspace/all/grapher.rb', line 58

def draw(nodes)
  path, filename = nil, filename() # outside block to capture value
  digraph do
    node_attribs << color('"#b6d7a8"') << filled << fontcolor("white")
    edge_attribs << color('"#999999"') << filled
    nodes.each do |parent|
      if parent.highlighted?
        node(parent.name)
      else
        node(parent.name).attributes << color('"#A4C2F4"')
      end
      parent.children.each do |child|
        edge(parent.name, child.name)
      end
    end
    FileUtils.mkdir_p(File.dirname(filename))
    save(filename, "png")
    path = "#{filename}.png"
  end

  logger.info "Graph saved to #{Terraspace::Util.pretty_path(path)}"
  open(path)
end

#runObject



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/terraspace/all/grapher.rb', line 8

def run
  check_graphviz!
  logger.info "Building graph..."
  builder = Terraspace::Builder.new(@options.merge(mod: "placeholder", quiet: true, draw_full_graph: draw_full_graph))
  builder.run
  graph = builder.graph
  if @options[:format] == "text"
    text(graph.top_nodes)
  else
    draw(graph.nodes)
  end
end

#text(nodes) ⇒ Object



21
22
23
24
25
26
27
# File 'lib/terraspace/all/grapher.rb', line 21

def text(nodes)
  Rainbow.enabled = false unless @options[:full]
  data = build_tree_data(nodes)
  Rainbow.enabled = true unless @options[:full]
  tree = TTY::Tree.new(data)
  logger.info tree.render
end

#text_name(node) ⇒ Object



54
55
56
# File 'lib/terraspace/all/grapher.rb', line 54

def text_name(node)
  node.highlighted? ? node.name.bright : node.name
end

#tree_data(parent, data = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/terraspace/all/grapher.rb', line 39

def tree_data(parent, data={})
  parent_name = text_name(parent)
  data[parent_name] ||= []
  parent.children.each do |child|
    child_name = text_name(child)
    if child.children.empty? # leaf node
      data[parent_name] << child_name
    else
      next_data = { child_name => [] }
      data[parent_name] << tree_data(child, next_data)
    end
  end
  data
end