Skip to content

Instantly share code, notes, and snippets.

@alexevanczuk
Last active April 21, 2023 14:37
Show Gist options
  • Save alexevanczuk/3ba89e039aeb4faf6839a4b9e210341e to your computer and use it in GitHub Desktop.
Save alexevanczuk/3ba89e039aeb4faf6839a4b9e210341e to your computer and use it in GitHub Desktop.
# typed: true
require 'parse_packwerk'
ParsePackwerk.all.each do |package|
new_dependencies = package.violations.select(&:dependency?).map(&:to_package_name)
new_dependencies = [*package.dependencies, *new_dependencies].uniq.sort
new_package = package.with(dependencies: new_dependencies)
ParsePackwerk.write_package_yml!(new_package)
end
# typed: true
require 'parse_packwerk'
def print_cycles(cycles)
cycles.each do |cycle|
cycle_strings = cycle.map(&:to_s)
cycle_strings << cycle.first.to_s
puts " - #{cycle_strings.join(" → ")}"
end
end
def get_cycles
package_set = T.let(Packwerk::Cli.new.send(:package_set), Packwerk::PackageSet)
edges = package_set.flat_map do |package|
package.dependencies.map do |dependency|
[package.name, package_set.fetch(dependency)&.name]
end
end
dependency_graph = Packwerk.const_get(:Graph).new(edges)
Set.new(dependency_graph.cycles)
end
cycle_set = Set.new
ParsePackwerk.all.shuffle.each do |package|
puts "========== [#{cycle_set.count} total cycles, #{cycle_set.to_a.flatten.count} total TODOs] Analyzing #{package.name}"
all_dependencies_required_for_zero_dependency_violations = package.violations.select(&:dependency?).map(&:to_package_name).uniq.sort
puts "- #{all_dependencies_required_for_zero_dependency_violations.count} dependencies to add"
all_dependencies_required_for_zero_dependency_violations.each do |dependency|
ParsePackwerk.bust_cache!
package = T.must(ParsePackwerk.find(package.name))
puts "- Experimentally adding dependency #{dependency}"
new_dependencies = [*package.dependencies, dependency].uniq.sort
new_package = package.with(dependencies: new_dependencies)
ParsePackwerk.write_package_yml!(new_package)
all_cycles = get_cycles
new_cycles = all_cycles - cycle_set
if new_cycles.count == 0
puts " - No new cycles found!"
else
puts " - #{new_cycles.count} new cycles, with #{new_cycles.sum(&:count)} new TODOs:"
print_cycles(new_cycles)
end
cycle_set = Set.new(all_cycles)
# puts "Reverting change..."
# ParsePackwerk.write_package_yml!(package)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment