Class: Berkshelf::Visualizer

Inherits:
Object
  • Object
show all
Includes:
ShellOut
Defined in:
lib/berkshelf/visualizer.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ShellOut

#shell_out, #shell_out!

Constructor Details

#initializeVisualizer

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, version)
      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, version) ⇒ Object



40
41
42
43
44
45
# File 'lib/berkshelf/visualizer.rb', line 40

def edge(a, b, version)
  node(a)
  node(b)

  @nodes[a].add(b => version)
end

#node(object) ⇒ Object



27
28
29
30
# File 'lib/berkshelf/visualizer.rb', line 27

def node(object)
  @nodes[object] ||= Set.new
  self
end

#nodesObject



32
33
34
# File 'lib/berkshelf/visualizer.rb', line 32

def nodes
  @nodes.keys
end

#to_dotString

Convert the current graph to a DOT. This is an intermediate step in generating a PNG.

Returns:



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# 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|
      edge.each do |name, version|
        if version == Semverse::DEFAULT_CONSTRAINT
          label = ""
        else
          label = " #{version}"
        end
        out << %{  "#{node}" -> "#{name}" [ fontsize = 10, label = "#{label}" ]\n}
      end
    end
  end

  out << %|}|
  out
end

#to_dot_file(outfile = "graph.dot") ⇒ Object



79
80
81
82
# File 'lib/berkshelf/visualizer.rb', line 79

def to_dot_file(outfile = "graph.dot")
  File.open(outfile, "w") { |f| f.write(to_dot) }
  File.expand_path(outfile)
end

#to_png(outfile = "graph.png") ⇒ String

Save the graph visually as a PNG.

Parameters:

  • outfile (String) (defaults to: "graph.png")

    the name/path of the file to output

Returns:

  • (String)

    the path where the file was written



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/berkshelf/visualizer.rb', line 91

def to_png(outfile = "graph.png")
  tempfile = Tempfile.new("dotdotfile")
  tempfile.write(to_dot)
  tempfile.rewind

  unless Berkshelf.which("dot") || Berkshelf.which("dot.exe")
    raise GraphvizNotInstalled.new
  end

  command = %{dot -T png #{tempfile.path} -o "#{outfile}"}
  response = shell_out(command)

  if response.error?
    raise GraphvizCommandFailed.new(command, response.stderr)
  end

  File.expand_path(outfile)
ensure
  if tempfile && File.exist?(tempfile.path)
    tempfile.close
    tempfile.unlink
  end
end