Class: Rwm::DependencyGraph
- Inherits:
-
Object
- Object
- Rwm::DependencyGraph
- Includes:
- TSort
- Defined in:
- lib/rwm/dependency_graph.rb
Instance Attribute Summary collapse
-
#edges ⇒ Object
readonly
Returns the value of attribute edges.
-
#packages ⇒ Object
readonly
Returns the value of attribute packages.
Class Method Summary collapse
-
.build(workspace) ⇒ Object
Build graph from a workspace by parsing all Gemfiles.
-
.load(workspace) ⇒ Object
Load graph from cached .rwm/graph.json, falling back to build.
Instance Method Summary collapse
- #add_edge(from_name, to_name) ⇒ Object
- #add_package(package) ⇒ Object
-
#dependencies(name) ⇒ Object
Dependencies of a package (what it depends on).
-
#direct_dependents(name) ⇒ Object
Direct dependents of a package (what depends on it).
-
#execution_levels ⇒ Object
Group packages into execution levels — packages at the same level have no interdependencies and can run in parallel.
-
#initialize ⇒ DependencyGraph
constructor
A new instance of DependencyGraph.
- #save(path, workspace_root) ⇒ Object
- #to_dot ⇒ Object
-
#to_json_data ⇒ Object
Serialize to JSON for .rwm/graph.json.
- #to_mermaid ⇒ Object
-
#topological_order ⇒ Object
Topological sort (dependencies before dependents).
-
#transitive_dependents(name) ⇒ Object
Walk the graph to find all transitive dependents.
Constructor Details
#initialize ⇒ DependencyGraph
Returns a new instance of DependencyGraph.
13 14 15 16 17 |
# File 'lib/rwm/dependency_graph.rb', line 13 def initialize @packages = {} # name => Package @edges = {} # name => [dep_name, ...] @dependents = {} # name => [dependent_name, ...] end |
Instance Attribute Details
#edges ⇒ Object (readonly)
Returns the value of attribute edges.
11 12 13 |
# File 'lib/rwm/dependency_graph.rb', line 11 def edges @edges end |
#packages ⇒ Object (readonly)
Returns the value of attribute packages.
11 12 13 |
# File 'lib/rwm/dependency_graph.rb', line 11 def packages @packages end |
Class Method Details
.build(workspace) ⇒ Object
Build graph from a workspace by parsing all Gemfiles
132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/rwm/dependency_graph.rb', line 132 def self.build(workspace) graph = new workspace.packages.each { |pkg| graph.add_package(pkg) } workspace.packages.each do |pkg| deps = GemfileParser.parse(pkg.gemfile_path, workspace.packages) deps.each { |dep| graph.add_edge(pkg.name, dep.name) } end graph end |
.load(workspace) ⇒ Object
Load graph from cached .rwm/graph.json, falling back to build. Auto-rebuilds when any package Gemfile is newer than the cache.
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/rwm/dependency_graph.rb', line 93 def self.load(workspace) path = workspace.graph_path unless File.exist?(path) Rwm.debug("graph: no cached graph found, building from scratch") return build_and_save(workspace) end if stale?(path, workspace.packages) Rwm.debug("graph: cached graph is stale, rebuilding") return build_and_save(workspace) end Rwm.debug("graph: loading from cache at #{path}") data = JSON.parse(File.read(path)) graph = new workspace.packages.each { |pkg| graph.add_package(pkg) } data["edges"]&.each do |name, deps| deps.each { |dep| graph.add_edge(name, dep) } end graph end |
Instance Method Details
#add_edge(from_name, to_name) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/rwm/dependency_graph.rb', line 25 def add_edge(from_name, to_name) @edges[from_name] ||= [] @edges[from_name] << to_name unless @edges[from_name].include?(to_name) @dependents[to_name] ||= [] @dependents[to_name] << from_name unless @dependents[to_name].include?(from_name) end |
#add_package(package) ⇒ Object
19 20 21 22 23 |
# File 'lib/rwm/dependency_graph.rb', line 19 def add_package(package) @packages[package.name] = package @edges[package.name] ||= [] @dependents[package.name] ||= [] end |
#dependencies(name) ⇒ Object
Dependencies of a package (what it depends on)
33 34 35 |
# File 'lib/rwm/dependency_graph.rb', line 33 def dependencies(name) @edges[name] || [] end |
#direct_dependents(name) ⇒ Object
Direct dependents of a package (what depends on it)
38 39 40 |
# File 'lib/rwm/dependency_graph.rb', line 38 def direct_dependents(name) @dependents[name] || [] end |
#execution_levels ⇒ Object
Group packages into execution levels — packages at the same level have no interdependencies and can run in parallel
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/rwm/dependency_graph.rb', line 69 def execution_levels return [] if @packages.empty? remaining = @packages.keys.dup levels = [] until remaining.empty? # Find packages whose deps are all already placed in earlier levels placed = levels.flatten level = remaining.select do |name| dependencies(name).all? { |dep| placed.include?(dep) } end raise CycleError, [["Unable to resolve execution levels — possible cycle"]] if level.empty? levels << level.sort remaining -= level end levels end |
#save(path, workspace_root) ⇒ Object
157 158 159 160 161 162 |
# File 'lib/rwm/dependency_graph.rb', line 157 def save(path, workspace_root) @workspace_root = workspace_root dir = File.dirname(path) FileUtils.mkdir_p(dir) File.write(path, JSON.pretty_generate(to_json_data) + "\n") end |
#to_dot ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/rwm/dependency_graph.rb', line 164 def to_dot lines = [] lines << "digraph rwm {" lines << " rankdir=LR;" lines << " node [shape=box];" @packages.each_value do |pkg| lines << " \"#{pkg.name}\" [label=\"#{pkg.name} (#{pkg.type})\"];" end @edges.each do |from, deps| deps.each do |to| lines << " \"#{from}\" -> \"#{to}\";" end end lines << "}" lines.join("\n") + "\n" end |
#to_json_data ⇒ Object
Serialize to JSON for .rwm/graph.json
146 147 148 149 150 151 152 153 154 155 |
# File 'lib/rwm/dependency_graph.rb', line 146 def to_json_data { "version" => 1, "generated_at" => Time.now.iso8601, "packages" => @packages.transform_values do |pkg| { "name" => pkg.name, "type" => pkg.type.to_s, "path" => pkg.relative_path(@workspace_root || "") } end, "edges" => @edges.transform_values(&:sort) } end |
#to_mermaid ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/rwm/dependency_graph.rb', line 184 def to_mermaid lines = [] lines << "graph LR" @packages.each_value do |pkg| lines << " #{pkg.name}[\"#{pkg.name} (#{pkg.type})\"]" end @edges.each do |from, deps| deps.each do |to| lines << " #{from} --> #{to}" end end lines.join("\n") + "\n" end |
#topological_order ⇒ Object
Topological sort (dependencies before dependents)
61 62 63 64 65 |
# File 'lib/rwm/dependency_graph.rb', line 61 def topological_order tsort rescue TSort::Cyclic => e raise CycleError, [[e.]] end |
#transitive_dependents(name) ⇒ Object
Walk the graph to find all transitive dependents
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/rwm/dependency_graph.rb', line 43 def transitive_dependents(name) visited = Set.new queue = [name] until queue.empty? current = queue.shift direct_dependents(current).each do |dep| next if visited.include?(dep) visited << dep queue << dep end end visited.to_a end |