Last active
February 1, 2019 15:37
-
-
Save floriandejonckheere/60f3b46a03bc4b4afa96fdf52e755410 to your computer and use it in GitHub Desktop.
Find cyclical dependencies in Open Webslides' frontend code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
NODES = [] | |
`grep -r "from 'modules/" app/modules`.split(%r{$}).map(&:strip).each do |line| | |
next if line.empty? | |
split = line.split ':' | |
source = split.first.match(%r{^app/modules/([^/]*)})&.captures&.first | |
target = split.last.match(%r{^import .* from 'modules/([^']*)';$})&.captures&.first | |
puts "No source found in '#{split.first}'" unless source | |
puts "No target found in '#{split.last}'" unless target | |
next unless source && target | |
NODES << { :source => source, :target => target } | |
end | |
NODES.uniq! | |
def dfs(graph, traversed, node) | |
# The graph is cyclical if we have already visited a node | |
if traversed.any? node | |
traversed << node | |
puts "Cycle found! #{traversed.join ' -> '}" | |
return | |
end | |
traversed << node | |
# Visit all adjacent nodes | |
graph.select { |n| n[:source] == node }.each do |child| | |
dfs graph, traversed.dup, child[:target] | |
end | |
end | |
NODES.each do |start| | |
traversed = [] | |
traversed << start[:source] | |
dfs NODES, [], start[:target] | |
end | |
puts '---' | |
NODES.group_by { |n| n[:source] }.sort.each do |key, value| | |
puts "package \e[31;1m#{key}\e[0m imports \e[31;1m#{value.map {|n| n[:target] }.sort.join(', ')}\e[0m" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment