Skip to content

Instantly share code, notes, and snippets.

@asterite
Created January 4, 2023 18:01
Show Gist options
  • Save asterite/c96cef663e4700d7512ad251b0e31494 to your computer and use it in GitHub Desktop.
Save asterite/c96cef663e4700d7512ad251b0e31494 to your computer and use it in GitHub Desktop.
Compute dependency sets
def affected_files(file, dependencies)
tracked_files = Set(String).new
track(file, dependencies, tracked_files)
tracked_files
end
def track(file, dependencies, tracked_files)
return unless tracked_files.add?(file)
dependencies[file].each do |to|
track(to, dependencies, tracked_files)
end
end
lines = File.read_lines(ARGV[0])
lines.shift # Remove the "digraph {" line
lines.pop # Remove the "}" ending line
edges = lines.map do |line|
to, from = line.split("->").map(&.strip.delete('"'))
{to, from}
end
dependencies = Hash(String, Set(String)).new { |h, k| h[k] = Set(String).new }
edges.each do |to, from|
dependencies[from].add(to)
end
all_files = (dependencies.keys + dependencies.values.flat_map(&.to_a)).uniq.sort
files_with_affected_files = all_files.map do |file|
{file, affected_files(file, dependencies)}
end.sort_by { |file, affected_files| affected_files.size }
files_with_affected_files.each do |file, affected_files|
next unless file.starts_with?("src")
puts "#{file}: #{affected_files.size}"
end
puts
puts "Total files: #{all_files.size}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment