Class: Terraspace::All::Grapher

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

Instance Method Summary collapse

Methods included from Util::Logging

#logger

Methods included from Compiler::DirsConcern

#cache_dirs, #dirs, #extract_stack_name, #local_paths, #mod_names, #select_stack?, #stack_names, #with_each_mod

Methods inherited from Base

#initialize

Constructor Details

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

Instance Method Details

#build_graphObject



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

def build_graph
  resolver = Terraspace::Dependency::Resolver.new(@options.merge(quiet: true, draw_full_graph: draw_full_graph))
  resolver.resolve
  dependencies = Terraspace::Dependency::Registry.data # populated after build_unresolved
  graph = Terraspace::Dependency::Graph.new(stack_names, dependencies, @options)
  graph.build
  graph
end

#build_tree_data(nodes) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/terraspace/all/grapher.rb', line 37

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/terraspace/all/grapher.rb', line 66

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



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

def run
  check_graphviz!
  logger.info "Building graph..."
  graph = build_graph
  if @options[:format] == "text"
    text(graph.top_nodes)
  else
    draw(graph.nodes)
  end
end

#text(nodes) ⇒ Object



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

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



62
63
64
# File 'lib/terraspace/all/grapher.rb', line 62

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

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/terraspace/all/grapher.rb', line 47

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