From 849316b90fed5a15aa18f5155be6055f1b0f8cc3 Mon Sep 17 00:00:00 2001 From: Juan Vasquez Date: Sat, 18 May 2024 16:55:57 -0600 Subject: [PATCH] [FEATURE] Support compatibility for Ruby versions the `bundle_report compatibility` now supports the `--ruby-version` flag to ask it for incompatible gems with the given Ruby version. Closes https://github.com/fastruby/next_rails/issues/17 Closes https://github.com/fastruby/next_rails/issues/115 --- CHANGELOG.md | 2 + README.md | 8 ++- exe/bundle_report | 10 ++- lib/next_rails/bundle_report.rb | 6 +- .../ruby_version_compatibility.rb | 45 ++++++++++++ lib/next_rails/gem_info.rb | 4 ++ .../ruby_version_compatibility_spec.rb | 68 +++++++++++++++++++ 7 files changed, 138 insertions(+), 5 deletions(-) create mode 100644 lib/next_rails/bundle_report/ruby_version_compatibility.rb create mode 100644 spec/next_rails/bundle_report/ruby_version_compatibility_spec.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index 6f8eef1..dde37f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ * Your changes/patches go here. +- [FEATURE: Support compatibility for Ruby versions](https://github.com/fastruby/next_rails/pull/116) + # v1.3.0 / 2023-06-16 [(commits)](https://github.com/fastruby/next_rails/compare/v1.2.4...v1.3.0) - [FEATURE: Add NextRails.next? for application usage (e.g. Rails shims)](https://github.com/fastruby/next_rails/pull/97) diff --git a/README.md b/README.md index ebdd766..d6a4add 100644 --- a/README.md +++ b/README.md @@ -39,10 +39,14 @@ bundle_report outdated --json # Show gems that don't work with Rails 5.2.0 bundle_report compatibility --rails-version=5.2.0 -# Show the usual help message -bundle_report --help +# Show gems that don't work with Ruby 3.0 +bundle_report compatibility --ruby-version=3.0 + # Find minimum compatible ruby version with Rails 7.0.0 bundle_report ruby_check --rails-version=7.0.0 + +# Show the usual help message +bundle_report --help ``` ### Application usage diff --git a/exe/bundle_report b/exe/bundle_report index 8408433..c8bcf08 100755 --- a/exe/bundle_report +++ b/exe/bundle_report @@ -33,6 +33,10 @@ at_exit do options[:rails_version] = rails_version end + opts.on("--ruby-version [STRING]", "Ruby version to check compatibility against (defaults to 2.3)") do |ruby_version| + options[:ruby_version] = ruby_version + end + opts.on("--include-rails-gems", "Include Rails gems in compatibility report (defaults to false)") do options[:include_rails_gems] = true end @@ -61,7 +65,11 @@ at_exit do when "ruby_check" then NextRails::BundleReport.compatible_ruby_version(rails_version: options.fetch(:rails_version)) when "outdated" then NextRails::BundleReport.outdated(options.fetch(:format, nil)) else - NextRails::BundleReport.compatibility(rails_version: options.fetch(:rails_version, "5.0"), include_rails_gems: options.fetch(:include_rails_gems, false)) + if options[:ruby_version] + NextRails::BundleReport.compatibility(ruby_version: options.fetch(:ruby_version, "2.3")) + else + NextRails::BundleReport.compatibility(rails_version: options.fetch(:rails_version, "5.0"), include_rails_gems: options.fetch(:include_rails_gems, false)) + end end end diff --git a/lib/next_rails/bundle_report.rb b/lib/next_rails/bundle_report.rb index 41b7be4..d2e090e 100644 --- a/lib/next_rails/bundle_report.rb +++ b/lib/next_rails/bundle_report.rb @@ -8,7 +8,9 @@ module NextRails module BundleReport extend self - def compatibility(rails_version: nil, include_rails_gems: nil) + def compatibility(rails_version: nil, ruby_version: nil, include_rails_gems: nil) + puts NextRails::BundleReport::RubyVersionCompatibility.new(options: { ruby_version: ruby_version }).generate if ruby_version + incompatible_gems = NextRails::GemInfo.all.reject do |gem| gem.compatible_with_rails?(rails_version: rails_version) || (!include_rails_gems && gem.from_rails?) end.sort_by { |gem| gem.name } @@ -168,4 +170,4 @@ def output_to_stdout(out_of_date_gems, total_gem_count, sourced_from_git_count) puts footer end end -end \ No newline at end of file +end diff --git a/lib/next_rails/bundle_report/ruby_version_compatibility.rb b/lib/next_rails/bundle_report/ruby_version_compatibility.rb new file mode 100644 index 0000000..38211e9 --- /dev/null +++ b/lib/next_rails/bundle_report/ruby_version_compatibility.rb @@ -0,0 +1,45 @@ +require "colorize" + +class NextRails::BundleReport::RubyVersionCompatibility + attr_reader :gems, :options + + VALID_RUBY_VERSIONS = (1.9..3.4).step(0.1).to_a.freeze + + def initialize(gems: NextRails::GemInfo.all, options: nil) + @gems = gems + @options = options + end + + def generate + return invalid_message unless valid? + + message + end + + private + + def message + output = "=> Incompatible gems with Ruby #{ruby_version}:\n".white.bold + incompatible.each do |gem| + output += "\n#{gem.name} - required Ruby version: #{gem.gem_specification.required_ruby_version}".magenta + end + output += "\n\n#{incompatible.length} gems incompatible with Ruby #{ruby_version}".red + output + end + + def incompatible + gems.reject { |gem| gem.compatible_with_ruby?(ruby_version) } + end + + def ruby_version + options[:ruby_version].to_f + end + + def invalid_message + "=> Invalid Ruby version: #{ruby_version}. Valid versions are: #{VALID_RUBY_VERSIONS.join(', ')}".white.bold + end + + def valid? + VALID_RUBY_VERSIONS.include?(ruby_version) + end +end diff --git a/lib/next_rails/gem_info.rb b/lib/next_rails/gem_info.rb index 1b1a210..d6060f9 100644 --- a/lib/next_rails/gem_info.rb +++ b/lib/next_rails/gem_info.rb @@ -147,5 +147,9 @@ def spec_compatible_with_rails?(specification: nil, rails_version: nil) rails_dependency.requirement.satisfied_by?(Gem::Version.new(rails_version)) end end + + def compatible_with_ruby?(ruby_version) + gem_specification.required_ruby_version.satisfied_by?(Gem::Version.new(ruby_version)) + end end end diff --git a/spec/next_rails/bundle_report/ruby_version_compatibility_spec.rb b/spec/next_rails/bundle_report/ruby_version_compatibility_spec.rb new file mode 100644 index 0000000..53df51a --- /dev/null +++ b/spec/next_rails/bundle_report/ruby_version_compatibility_spec.rb @@ -0,0 +1,68 @@ +# frozen_string_literal: true + +require "spec_helper" +require "next_rails/bundle_report/ruby_version_compatibility" + +RSpec.describe NextRails::BundleReport::RubyVersionCompatibility do + let(:ruby_3_0_gem) do + Gem::Specification.new do |s| + s.name = "ruby_3_0_gem" + s.version = "1.0.0" + s.required_ruby_version = ">= 3.0" + end + end + + let(:ruby_2_5_gem) do + Gem::Specification.new do |s| + s.name = "ruby_2_5_gem" + s.version = "1.0.0" + s.required_ruby_version = ">= 2.5" + end + end + + let(:ruby_2_3_to_2_5_gem) do + Gem::Specification.new do |s| + s.name = "ruby_2_3_to_2_5_gem" + s.version = "1.0.0" + s.required_ruby_version = [">= 2.3", "< 2.5"] + end + end + + let(:no_ruby_version_gem) do + Gem::Specification.new do |s| + s.name = "no_ruby_version_gem" + s.version = "1.0.0" + end + end + + describe "#generate" do + let(:gems) do + [ + NextRails::GemInfo.new(ruby_3_0_gem), + NextRails::GemInfo.new(ruby_2_5_gem), + NextRails::GemInfo.new(ruby_2_3_to_2_5_gem), + NextRails::GemInfo.new(no_ruby_version_gem) + ] + end + + context "with invalid ruby version" do + it "returns invalid message" do + options = { ruby_version: "hola" } + + result = described_class.new(gems: gems, options: options).generate + expect(result).to include "Invalid Ruby version" + end + end + + context "with valid ruby version" do + it "returns 2 incompatible gems" do + options = { ruby_version: "2.7" } + + result = described_class.new(gems: gems, options: options).generate + + expect(result).to include "Incompatible gems with Ruby 2.7" + expect(result).to include "2 gems incompatible with Ruby 2.7" + end + end + end +end