Class: Bundler::Graph

Inherits:
Object show all
Defined in:
lib/bundler/graph.rb

Constant Summary collapse

USER_OPTIONS =
{:style => 'filled', :fillcolor => '#B9B9D5'}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Graph

Returns a new instance of Graph.



6
7
8
# File 'lib/bundler/graph.rb', line 6

def initialize(env)
  @env = env
end

Instance Method Details

#groupsObject



15
16
17
18
# File 'lib/bundler/graph.rb', line 15

def groups
  populate
  @groups
end

#nodesObject



10
11
12
13
# File 'lib/bundler/graph.rb', line 10

def nodes
  populate
  @nodes
end

#viz(output_file, show_gem_versions = false, show_dependency_requirements = false) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/bundler/graph.rb', line 20

def viz(output_file, show_gem_versions = false, show_dependency_requirements = false)
  require 'graphviz'
  populate

  graph_viz = GraphViz::new('Gemfile', {:concentrate => true, :normalize => true, :nodesep => 0.55})
  graph_viz.edge[:fontname] = graph_viz.node[:fontname] = 'Arial, Helvetica, SansSerif'
  graph_viz.edge[:fontsize] = 12

  viz_nodes = {}

  # populate all of the nodes
  nodes.each do |name, node|
    label = name.dup
    label << "\n#{node.version}" if show_gem_versions
    options = { :label => label }
    options.merge!( USER_OPTIONS ) if node.is_user
    viz_nodes[name] = graph_viz.add_node( name, options )
  end

  group_nodes = {}
  @groups.each do |name, dependencies|
    group_nodes[name] = graph_viz.add_node(name.to_s, { :shape => 'box3d', :fontsize => 16 }.merge(USER_OPTIONS))
    dependencies.each do |dependency|
      options = { :weight => 2 }
      if show_dependency_requirements && (dependency.requirement.to_s != ">= 0")
        options[:label] = dependency.requirement.to_s
      end
      graph_viz.add_edge( group_nodes[name], viz_nodes[dependency.name], options )
    end
  end

  @groups.keys.select { |group| group != :default }.each do |group|
    graph_viz.add_edge( group_nodes[group], group_nodes[:default], { :weight => 2 } )
  end

  viz_nodes.each do |name, node|
    nodes[name].dependencies.each do |dependency|
      options = { }
      if nodes[dependency.name].is_user
        options[:constraint] = false
      end
      if show_dependency_requirements && (dependency.requirement.to_s != ">= 0")
        options[:label] = dependency.requirement.to_s
      end

      graph_viz.add_edge( node, viz_nodes[dependency.name], options )
    end
  end

  graph_viz.output( :png => output_file )
end