Class: Berkshelf::Visualizer
- Inherits:
-
Object
- Object
- Berkshelf::Visualizer
- Includes:
- Buff::ShellOut
- Defined in:
- lib/berkshelf/visualizer.rb
Class Method Summary collapse
Instance Method Summary collapse
- #adjacencies(object) ⇒ Object
- #each_node(&block) ⇒ Object
- #edge(a, b) ⇒ Object
-
#initialize ⇒ Visualizer
constructor
A new instance of Visualizer.
- #node(object) ⇒ Object
- #nodes ⇒ Object
-
#to_dot ⇒ String
Convert the current graph to a DOT.
-
#to_png(outfile = 'graph.png') ⇒ String
Save the graph visually as a PNG.
Constructor Details
#initialize ⇒ Visualizer
Returns a new instance of Visualizer.
23 24 25 |
# File 'lib/berkshelf/visualizer.rb', line 23 def initialize @nodes = {} end |
Class Method Details
.from_lockfile(lockfile) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/berkshelf/visualizer.rb', line 8 def from_lockfile(lockfile) new.tap do |instance| lockfile.graph.each do |item| instance.node(item.name) item.dependencies.each do |name, version| instance.edge(item.name, name) end end end end |
Instance Method Details
#adjacencies(object) ⇒ Object
47 48 49 |
# File 'lib/berkshelf/visualizer.rb', line 47 def adjacencies(object) @nodes[object] || Set.new end |
#each_node(&block) ⇒ Object
36 37 38 |
# File 'lib/berkshelf/visualizer.rb', line 36 def each_node(&block) nodes.each(&block) end |
#edge(a, b) ⇒ Object
40 41 42 43 44 45 |
# File 'lib/berkshelf/visualizer.rb', line 40 def edge(a, b) node(a) node(b) @nodes[a].add(b) end |
#node(object) ⇒ Object
27 28 29 30 |
# File 'lib/berkshelf/visualizer.rb', line 27 def node(object) @nodes[object] ||= Set.new self end |
#nodes ⇒ Object
32 33 34 |
# File 'lib/berkshelf/visualizer.rb', line 32 def nodes @nodes.keys end |
#to_dot ⇒ String
Convert the current graph to a DOT. This is an intermediate step in generating a PNG.
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/berkshelf/visualizer.rb', line 55 def to_dot out = %|digraph Solve__Graph {\n| nodes.each do |node| out << %| "#{node}" [ fontsize = 10, label = "#{node}" ]\n| end nodes.each do |node| adjacencies(node).each do |edge| out << %| "#{node}" -> "#{edge}" [ fontsize = 10 ]\n| end end out << %|}| out end |
#to_png(outfile = 'graph.png') ⇒ String
Save the graph visually as a PNG.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/berkshelf/visualizer.rb', line 79 def to_png(outfile = 'graph.png') tempfile = Tempfile.new('dotdotfile') tempfile.write(to_dot) tempfile.rewind unless Berkshelf.which('dot') raise GraphvizNotInstalled.new end command = "dot -T png #{tempfile.path} -o #{outfile}" response = shell_out(command) unless response.success? raise GraphvizCommandFailed.new(command, response.stderr) end File.(outfile) ensure if tempfile && File.exist?(tempfile.path) tempfile.close tempfile.unlink end end |