class Berkshelf::Visualizer

Public Class Methods

from_lockfile(lockfile) click to toggle source
# 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
new() click to toggle source
# File lib/berkshelf/visualizer.rb, line 23
def initialize
  @nodes = {}
end

Public Instance Methods

adjacencies(object) click to toggle source
# File lib/berkshelf/visualizer.rb, line 47
def adjacencies(object)
  @nodes[object] || Set.new
end
each_node(&block) click to toggle source
# File lib/berkshelf/visualizer.rb, line 36
def each_node(&block)
  nodes.each(&block)
end
edge(a, b, version) click to toggle source
# File lib/berkshelf/visualizer.rb, line 40
def edge(a, b, version)
  node(a)
  node(b)

  @nodes[a].add(b => version)
end
node(object) click to toggle source
# File lib/berkshelf/visualizer.rb, line 27
def node(object)
  @nodes[object] ||= Set.new
  self
end
nodes() click to toggle source
# File lib/berkshelf/visualizer.rb, line 32
def nodes
  @nodes.keys
end
to_dot() click to toggle source

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

@return [String]

# 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") click to toggle source
# 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") click to toggle source

Save the graph visually as a PNG.

@param [String] outfile

the name/path of the file to output

@return [String]

the path where the file was written
# 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