forked from clio/ten_years_rails
-
Notifications
You must be signed in to change notification settings - Fork 41
Move bundle_report rails compatibility logic into a class #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
JuanVqz
merged 4 commits into
main
from
move-rails-version-compatibility-into-its-own-class
Mar 7, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
84 changes: 84 additions & 0 deletions
84
lib/next_rails/bundle_report/rails_version_compatibility.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
class NextRails::BundleReport::RailsVersionCompatibility | ||
def initialize(gems: NextRails::GemInfo.all, options: {}) | ||
@gems = gems | ||
@options = options | ||
end | ||
|
||
def generate | ||
erb_output | ||
end | ||
|
||
def incompatible_gems_by_state | ||
@incompatible_gems_by_state ||= begin | ||
incompatible_gems.each { |gem| gem.find_latest_compatible(rails_version: rails_version) } | ||
incompatible_gems.group_by { |gem| gem.state(rails_version) } | ||
end | ||
end | ||
|
||
private | ||
|
||
def erb_output | ||
template = <<-ERB | ||
<% if incompatible_gems_by_state[:found_compatible] -%> | ||
<%= Rainbow("=> Incompatible with Rails #{rails_version} (with new versions that are compatible):").white.bold %> | ||
<%= Rainbow("These gems will need to be upgraded before upgrading to Rails #{rails_version}.").italic %> | ||
|
||
<% incompatible_gems_by_state[:found_compatible].each do |gem| -%> | ||
<%= gem_header(gem) %> - upgrade to <%= gem.latest_compatible_version.version %> | ||
<% end -%> | ||
|
||
<% end -%> | ||
<% if incompatible_gems_by_state[:incompatible] -%> | ||
<%= Rainbow("=> Incompatible with Rails #{rails_version} (with no new compatible versions):").white.bold %> | ||
<%= Rainbow("These gems will need to be removed or replaced before upgrading to Rails #{rails_version}.").italic %> | ||
|
||
<% incompatible_gems_by_state[:incompatible].each do |gem| -%> | ||
<%= gem_header(gem) %> - new version, <%= gem.latest_version.version %>, is not compatible with Rails #{rails_version} | ||
<% end -%> | ||
|
||
<% end -%> | ||
<% if incompatible_gems_by_state[:no_new_version] -%> | ||
<%= Rainbow("=> Incompatible with Rails #{rails_version} (with no new versions):").white.bold %> | ||
<%= Rainbow("These gems will need to be upgraded by us or removed before upgrading to Rails #{rails_version}.").italic %> | ||
<%= Rainbow("This list is likely to contain internal gems, like Cuddlefish.").italic %> | ||
|
||
<% incompatible_gems_by_state[:no_new_version].each do |gem| -%> | ||
<%= gem_header(gem) %> - new version not found | ||
<% end -%> | ||
|
||
<% end -%> | ||
<%= Rainbow(incompatible_gems.length.to_s).red %> gems incompatible with Rails <%= rails_version %> | ||
ERB | ||
|
||
erb_version = ERB.version | ||
if erb_version =~ /erb.rb \[([\d\.]+) .*\]/ | ||
erb_version = $1 | ||
end | ||
|
||
if Gem::Version.new(erb_version) < Gem::Version.new("2.2") | ||
ERB.new(template, nil, "-").result(binding) | ||
else | ||
ERB.new(template, trim_mode: "-").result(binding) | ||
end | ||
end | ||
|
||
def gem_header(_gem) | ||
header = Rainbow("#{_gem.name} #{_gem.version}").bold | ||
header << Rainbow(" (loaded from git)").magenta if _gem.sourced_from_git? | ||
header | ||
end | ||
|
||
def incompatible_gems | ||
@incompatible_gems ||= @gems.reject do |gem| | ||
gem.compatible_with_rails?(rails_version: rails_version) || (!include_rails_gems && gem.from_rails?) | ||
end.sort_by { |gem| gem.name } | ||
end | ||
|
||
def rails_version | ||
@options[:rails_version] | ||
end | ||
|
||
def include_rails_gems | ||
@options[:include_rails_gems] | ||
end | ||
end |
40 changes: 40 additions & 0 deletions
40
spec/next_rails/bundle_report/rails_version_compatibility_spec.rb
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
RSpec.describe NextRails::BundleReport::RailsVersionCompatibility do | ||
describe "#generate" do | ||
it "returns non incompatible gems" do | ||
output = NextRails::BundleReport::RailsVersionCompatibility.new(options: { rails_version: 7.0 }).generate | ||
expect(output).to match "gems incompatible with Rails 7.0" | ||
end | ||
|
||
it "returns incompatible with compatible versions" do | ||
next_rails_version = 7.1 | ||
specification = Gem::Specification.new do |s| | ||
s.name = "audited" | ||
s.version = "5.1.0" | ||
s.add_dependency "rails", ">= 5.0", "< 7.1" | ||
end | ||
audited = NextRails::GemInfo.new(specification) | ||
gems = [audited] | ||
|
||
allow_any_instance_of(described_class).to receive(:incompatible_gems_by_state) | ||
.and_return({ found_compatible: gems }) | ||
|
||
allow(audited).to receive(:latest_compatible_version).and_return(Gem::Version.new("5.8.0")) | ||
|
||
output = | ||
NextRails::BundleReport::RailsVersionCompatibility.new( | ||
gems: gems, | ||
options: { rails_version: next_rails_version, include_rails_gems: false } | ||
).generate | ||
|
||
expect(output).to include("Incompatible with Rails 7.1 (with new versions that are compatible):") | ||
expect(output).to include("These gems will need to be upgraded before upgrading to Rails 7.1.") | ||
expect(output).to include("- upgrade to 5.8.0") | ||
expect(output).to include("gems incompatible with Rails 7.1") | ||
end | ||
end | ||
end | ||
|
1 change: 0 additions & 1 deletion
1
spec/next_rails/bundle_report/ruby_version_compatibility_spec.rb
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
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.