Sha256: 1cc828cf03f3b3475d3a4847f68e820e06c3cbb0c608ef85b26774d3f96558ff

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

module Depcheck
  class GraphOutput

    def post(objs)

      # 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)

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

      # run dot command
      `dot -T png #{file_name}.dot > #{file_name}.png && open #{file_name}.png`

      # remove temporary file
      File.delete("#{file_name}.dot")
    end

    def generate_graph_description(objs)
      nodes = []
      objs.each do |obj|
        obj.dependencies.each do |dep|
          nodes << { source: obj.name, dep: dep }
        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
    private :generate_graph_description

    def graphviz_installed?
      `which dot`.strip.empty? == false
    end
    private :graphviz_installed?

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
depcheck-0.3.0 lib/depcheck/output/graph_output.rb