Created
May 10, 2023 16:59
-
-
Save alexevanczuk/8eedba2adf45c227f9543159fa7036c5 to your computer and use it in GitHub Desktop.
Add packwerk dependencies that do not produce cycles
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
# 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