class Depcheck::GraphOutput

Public Instance Methods

post(objs, include, dot) click to toggle source
# File lib/depcheck/output/graph_output.rb, line 4
def post(objs, include, dot)

  # installs graphviz if needed
  system 'brew install graphviz' unless graphviz_installed?

  # Check that this is in the user's PATH after installing
  unless graphviz_installed?
    fail "graphviz is not in the user's PATH, or it failed to install"
  end

  # generate graph description
  graph = generate_graph_description(objs, include)

  # create temporary graph dot file
  file_name = 'graph'
  File.open("#{file_name}.dot", 'w') do |f|
    f.write(graph)
  end

  # run dot command
  if dot
  `dot -T png #{file_name}.dot`
  elsif
    `dot -T png #{file_name}.dot > #{file_name}.png && open #{file_name}.png`
    File.delete("#{file_name}.dot")
  end
end

Private Instance Methods

generate_graph_description(objs, include) click to toggle source
# File lib/depcheck/output/graph_output.rb, line 32
def generate_graph_description(objs, include)
  nodes = []
  objs.each do |obj|
    obj.dependencies.each do |dep|
      if obj.name.match(/#{include}/) || dep.match(/#{include}/)
        nodes << { source: obj.name, dep: dep }
      end
    end
  end

  desc = "digraph dependencies {\n node [fontname=monospace, fontsize=9, shape=box, style=rounded]\n"
  nodes.each do |node|
    desc += " \"#{node[:source]}\" -> \"#{node[:dep]}\"\n"
  end
  desc += "}\n"

  desc
end
graphviz_installed?() click to toggle source
# File lib/depcheck/output/graph_output.rb, line 52
def graphviz_installed?
  `which dot`.strip.empty? == false
end