Last active
August 28, 2022 17:50
-
-
Save mriddle/b9d871afb20babc537bc69ee4b4790ec to your computer and use it in GitHub Desktop.
Find dependencies that need to be bumped for a Rails upgrade: check_rails_dependancies.rb -v 6.1.0
This file contains hidden or 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 | |
require 'bundler/setup' | |
require 'optparse' | |
require 'rainbow' | |
require 'set' | |
class RailsBumpHelper | |
RAILS_DEPENDENCIES = ["actioncable", "actionmailbox", "actionmailer", "actionpack", "actiontext", "actionview", "activejob", "activemodel", "activerecord", "activestorage", "activesupport", "railties", "rails"] | |
def initialize(verbose: false, version:) | |
@target_version = ::Gem::Version.create(version) | |
@verbose = verbose | |
@gems_using_rails = Set.new | |
@application_dependencies = Bundler.locked_gems.dependencies | |
end | |
def run! | |
@gems_using_rails = Set.new | |
application_dependencies.each do |name, dependency| | |
puts "Gem: #{name}" if verbose? | |
dependency.to_spec.traverse do |current_spec, dep, dep_spec, trail| | |
# If this gem uses something that's part of rails then add it (and its parents) to the list | |
puts "Tree: #{trail.map(&:name)}" if verbose? | |
if dep_spec.runtime_dependencies.map(&:name).any? { |dep_name| part_of_rails?(dep_name) } | |
trail.each { |d| gems_using_rails << d unless part_of_rails?(d.name) } | |
end | |
end | |
end | |
self | |
end | |
def what_needs_upgrading? | |
gems_with_runtime_rails_dependencies = {} | |
gems_using_rails.sort.each do |gem| | |
rails_deps = gem.runtime_dependencies.select { |dep| part_of_rails?(dep.name) } | |
gems_with_runtime_rails_dependencies[gem] = rails_deps if rails_deps.any? | |
end | |
puts "-"*30 | |
puts "#{gems_with_runtime_rails_dependencies.size} dependencies using Rails" | |
puts "-"*30 | |
gems_with_runtime_rails_dependencies.each do |gem, rails_deps| | |
puts Rainbow("#{gem.name} v#{gem.version}").white.bold | |
rails_deps.each do |dep| | |
if dep.requirement.satisfied_by?(target_version) | |
puts Rainbow("\t\u2713 #{dep.name}").green | |
else | |
puts Rainbow("\t\u2717 #{dep.name}").red | |
end | |
end | |
end | |
end | |
private | |
attr_reader :application_dependencies, :gems_using_rails, :target_version | |
def part_of_rails?(name) | |
RAILS_DEPENDENCIES.include?(name) | |
end | |
def verbose? | |
@verbose | |
end | |
end | |
options = {} | |
OptionParser.new do |opts| | |
opts.banner = "Usage: check_all_dependencies.rb [options]" | |
opts.on("--verbose", "Run verbosely") do |v| | |
options[:verbose] = v | |
end | |
opts.on("-v", "--version [VERSION]", "Verify dependencies satisfy given Rails version") do |version| | |
options[:version] = version | |
end | |
end.parse! | |
RailsBumpHelper.new(options).run!.what_needs_upgrading? |
This file contains hidden or 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
# Example usage | |
$ ./check_rails_dependencies.rb --version 6.1.0 | |
------------------------------ | |
93 dependencies using Rails | |
------------------------------ | |
action_mailer-logged_smtp_delivery v2.1.0 | |
✗ actionmailer | |
actionpack-action_caching v1.2.0 | |
✓ actionpack | |
✓ activerecord | |
actionpack-page_caching v1.1.0 | |
✓ actionpack | |
actionpack-xml_parser v1.0.2 | |
✗ actionpack | |
active_hash v1.5.2 | |
✓ activesupport | |
active_model_serializers v0.9.3 | |
✓ activemodel | |
✓ rails | |
active_record-comments v0.1.3 | |
✓ activerecord | |
active_record_host_pool v0.10.1 | |
✗ activerecord | |
active_record_inherit_assoc v2.5.0 | |
✗ activerecord | |
✓ rails | |
active_record_shards v3.14.0 | |
✓ activerecord | |
✓ activesupport | |
activerecord-delay_touching v1.0.1 | |
✓ activerecord | |
activeresource v4.0.0 | |
✗ activesupport | |
✗ activemodel | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment