class Pandan::DependencyGraph

Public Class Methods

new(argv) click to toggle source
Calls superclass method
# File lib/pandan/command/dependency_graph.rb, line 24
def initialize(argv)
  @xcworkspace = argv.option('xcworkspace')
  @xcworkspace ||= XCWorkspace.find_workspace
  @save_gv = argv.flag?('graphviz')
  @save_png = argv.flag?('image')
  @filter = argv.option('filter')
  @filter ||= '.*' # Match everything
  super
end
options() click to toggle source
Calls superclass method
# File lib/pandan/command/dependency_graph.rb, line 11
def self.options
  [
    ['--xcworkspace=path/to/workspace', 'If not set, Pandan will try to find a workspace'],
    ['--graphviz', 'Outputs the dependency graph in GraphViz format'],
    ['--image', 'Outputs the dependency graph as a PNG image'],
    ['--filter=expression', 'If set, pandan will select all targets whose name match the regular expression']
  ].concat(super)
end

Public Instance Methods

run() click to toggle source
# File lib/pandan/command/dependency_graph.rb, line 46
def run
  parser = Parser.new(@xcworkspace, @filter)
  targets = parser.all_targets
  graph = Graph.new(false)
  graph.add_target_info(targets)

  Dir.mktmpdir do |dir|
    tmpfile = File.join(dir, 'dependencies.gv')
    tmpfile_reduced = File.join(dir, 'dependencies_reduced.gv')

    save_gv(graphviz_data(graph), tmpfile)
    `tred #{tmpfile} > #{tmpfile_reduced}` # tred performs a transitive reduction on the graph
    reduced_graph = GraphViz.parse(tmpfile_reduced)

    FileUtils.mv(tmpfile_reduced, 'dependencies.gv') if @save_gv
    save_png(reduced_graph, 'dependencies.png') if @save_png
  end
end
validate!() click to toggle source
Calls superclass method
# File lib/pandan/command/dependency_graph.rb, line 34
def validate!
  super
  help! 'Could not find the workspace. Try setting it manually using the --xcworkspace option.' unless @xcworkspace

  if `which tred`.empty?
    help! 'Pandan requires GraphViz to generate the dependency graph. '\
          'Please install it, e.g. with Homebrew: `brew install graphviz`.'
  end

  help! 'Please use at least one of --graphviz and --image.' if @save_gv.nil? && @save_png.nil?
end

Private Instance Methods

graphviz_data(graph) click to toggle source
# File lib/pandan/command/dependency_graph.rb, line 67
def graphviz_data(graph)
  graphviz = GraphViz.new(type: :digraph)

  graph.nodes.each_value do |node|
    next unless node.name.match?(/#{@filter}/)

    target_node = graphviz.add_node(node.name)
    node.neighbors.each do |dependency|
      next unless dependency.name.match?(/#{@filter}/)

      dep_node = graphviz.add_node(dependency.name)
      graphviz.add_edge(target_node, dep_node)
    end
  end
  graphviz
end
save_gv(graphviz_data, filename) click to toggle source
# File lib/pandan/command/dependency_graph.rb, line 84
def save_gv(graphviz_data, filename)
  graphviz_data.output(dot: filename)
end
save_png(graphviz_data, filename) click to toggle source
# File lib/pandan/command/dependency_graph.rb, line 88
def save_png(graphviz_data, filename)
  graphviz_data.output(png: filename)
end