Sha256: 233e79d27fcee15ba60654645fdfbad5b4dbd4a163345bf28274194d4db3706a

Contents?: true

Size: 1.03 KB

Versions: 2

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

require "graphviz"
require "cobra_commander/component_tree"

module CobraCommander
  # Generates graphs of components
  class Graph
    def initialize(app_path, format)
      @format = format
      @tree = CobraCommander.umbrella_tree(app_path).to_h
    end

    def generate!
      return unless valid_format?

      g = GraphViz.new(:G, type: :digraph, concentrate: true)

      app_node = g.add_nodes(@tree[:name])
      map_nodes(g, app_node, @tree)
      output(g)
    end

  private

    def map_nodes(g, parent_node, tree)
      tree[:dependencies].each do |dep|
        node = g.find_node(dep[:name]) || g.add_nodes(dep[:name])
        g.add_edges(parent_node, node)
        map_nodes(g, node, dep)
      end
    end

    def output(g)
      graph = "graph.#{@format}"
      g.output(@format => graph)
      puts "Graph generated at #{`pwd`.chomp}/#{graph}"
    end

    def valid_format?
      return true if @format == "png" || @format == "dot"
      puts "FORMAT must be 'png' or 'dot'"
      false
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cobra_commander-0.5.1 lib/cobra_commander/graph.rb
cobra_commander-0.5.0 lib/cobra_commander/graph.rb