Class: Terraspace::All::Grapher
Instance Method Summary
collapse
#logger
#cache_dirs, #dirs, #extract_stack_name, #local_paths, #mod_names, #select_stack?, #stack_names, #with_each_mod
Methods inherited from Base
#initialize
Instance Method Details
#build_graph ⇒ Object
24
25
26
27
28
29
30
31
|
# File 'lib/terraspace/all/grapher.rb', line 24
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
graph = Terraspace::Dependency::Graph.new(stack_names, dependencies, @options)
graph.build
graph
end
|
#build_tree_data(nodes) ⇒ Object
41
42
43
44
45
46
47
48
49
|
# File 'lib/terraspace/all/grapher.rb', line 41
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/terraspace/all/grapher.rb', line 70
def draw(nodes)
path, filename = nil, filename()
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
|
#run ⇒ Object
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# 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)
elsif @options[:format] == "json"
puts (graph.build.map do |batch|
batch.map { |stack| stack.name }
end).to_json
else
draw(graph.nodes)
end
end
|
#text(nodes) ⇒ Object
33
34
35
36
37
38
39
|
# File 'lib/terraspace/all/grapher.rb', line 33
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
66
67
68
|
# File 'lib/terraspace/all/grapher.rb', line 66
def text_name(node)
node.highlighted? ? node.name.bright : node.name
end
|
#tree_data(parent, data = {}) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/terraspace/all/grapher.rb', line 51
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?
data[parent_name] << child_name
else
next_data = { child_name => [] }
data[parent_name] << tree_data(child, next_data)
end
end
data
end
|