Sha256: c4e1f889a31af1a6e6bfa0869849e45e9e97ad2b2a6e64d2585a1036e24a1731

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

module Depcheck
  class GraphOutput

    def post(objs, include)

      # 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
      `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, 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
    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.4.0 lib/depcheck/output/graph_output.rb