diff --git a/bundler/lib/bundler/current_ruby.rb b/bundler/lib/bundler/current_ruby.rb index 93e0c401c056..9cd08f4d09fd 100644 --- a/bundler/lib/bundler/current_ruby.rb +++ b/bundler/lib/bundler/current_ruby.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require_relative "rubygems_ext" + module Bundler # Returns current version of Ruby # @@ -9,38 +11,25 @@ def self.current_ruby end class CurrentRuby - KNOWN_MINOR_VERSIONS = %w[ - 1.8 - 1.9 - 2.0 - 2.1 - 2.2 - 2.3 - 2.4 - 2.5 - 2.6 - 2.7 - 3.0 - 3.1 - 3.2 - 3.3 - ].freeze - - KNOWN_MAJOR_VERSIONS = KNOWN_MINOR_VERSIONS.map {|v| v.split(".", 2).first }.uniq.freeze - - KNOWN_PLATFORMS = %w[ - jruby - maglev - mingw - mri - mswin - mswin64 - rbx - ruby - truffleruby - windows - x64_mingw - ].freeze + ALL_RUBY_VERSIONS = (18..27).to_a.concat((30..35).to_a).freeze + KNOWN_MINOR_VERSIONS = ALL_RUBY_VERSIONS.map {|v| v.digits.reverse.join(".") }.freeze + KNOWN_MAJOR_VERSIONS = ALL_RUBY_VERSIONS.map {|v| v.digits.last.to_s }.uniq.freeze + PLATFORM_MAP = { + ruby: [Gem::Platform::RUBY, CurrentRuby::ALL_RUBY_VERSIONS], + mri: [Gem::Platform::RUBY, CurrentRuby::ALL_RUBY_VERSIONS], + rbx: [Gem::Platform::RUBY], + truffleruby: [Gem::Platform::RUBY], + jruby: [Gem::Platform::JAVA, [18, 19]], + windows: [Gem::Platform::WINDOWS, CurrentRuby::ALL_RUBY_VERSIONS], + # deprecated + mswin: [Gem::Platform::MSWIN, CurrentRuby::ALL_RUBY_VERSIONS], + mswin64: [Gem::Platform::MSWIN64, CurrentRuby::ALL_RUBY_VERSIONS - [18]], + mingw: [Gem::Platform::MINGW, CurrentRuby::ALL_RUBY_VERSIONS], + x64_mingw: [Gem::Platform::X64_MINGW, CurrentRuby::ALL_RUBY_VERSIONS - [18, 19]], + }.each_with_object({}) do |(platform, spec), hash| + hash[platform] = spec[0] + spec[1]&.each {|version| hash[:"#{platform}_#{version}"] = spec[0] } + end.freeze def ruby? return true if Bundler::GemHelpers.generic_local_platform_is_ruby? @@ -82,7 +71,8 @@ def windows? RUBY_VERSION.start_with?("#{version}.") end - KNOWN_PLATFORMS.each do |platform| + all_platforms = PLATFORM_MAP.keys << "maglev" + all_platforms.each do |platform| define_method(:"#{platform}_#{trimmed_version}?") do send(:"#{platform}?") && send(:"on_#{trimmed_version}?") end diff --git a/bundler/lib/bundler/dependency.rb b/bundler/lib/bundler/dependency.rb index 09a145b8c8eb..45d74c5ba71f 100644 --- a/bundler/lib/bundler/dependency.rb +++ b/bundler/lib/bundler/dependency.rb @@ -2,31 +2,12 @@ require "rubygems/dependency" require_relative "shared_helpers" -require_relative "rubygems_ext" module Bundler class Dependency < Gem::Dependency attr_reader :autorequire attr_reader :groups, :platforms, :gemfile, :path, :git, :github, :branch, :ref, :glob - ALL_RUBY_VERSIONS = (18..27).to_a.concat((30..35).to_a).freeze - PLATFORM_MAP = { - ruby: [Gem::Platform::RUBY, ALL_RUBY_VERSIONS], - mri: [Gem::Platform::RUBY, ALL_RUBY_VERSIONS], - rbx: [Gem::Platform::RUBY], - truffleruby: [Gem::Platform::RUBY], - jruby: [Gem::Platform::JAVA, [18, 19]], - windows: [Gem::Platform::WINDOWS, ALL_RUBY_VERSIONS], - # deprecated - mswin: [Gem::Platform::MSWIN, ALL_RUBY_VERSIONS], - mswin64: [Gem::Platform::MSWIN64, ALL_RUBY_VERSIONS - [18]], - mingw: [Gem::Platform::MINGW, ALL_RUBY_VERSIONS], - x64_mingw: [Gem::Platform::X64_MINGW, ALL_RUBY_VERSIONS - [18, 19]], - }.each_with_object({}) do |(platform, spec), hash| - hash[platform] = spec[0] - spec[1]&.each {|version| hash[:"#{platform}_#{version}"] = spec[0] } - end.freeze - def initialize(name, version, options = {}, &blk) type = options["type"] || :runtime super(name, version, type) @@ -62,7 +43,7 @@ def gem_platforms(valid_platforms) end def expanded_platforms - @expanded_platforms ||= @platforms.filter_map {|pl| PLATFORM_MAP[pl] }.flatten.uniq + @expanded_platforms ||= @platforms.filter_map {|pl| CurrentRuby::PLATFORM_MAP[pl] }.flatten.uniq end def should_include? diff --git a/bundler/lib/bundler/dsl.rb b/bundler/lib/bundler/dsl.rb index 05c60f2f1aab..08fdacb991ef 100644 --- a/bundler/lib/bundler/dsl.rb +++ b/bundler/lib/bundler/dsl.rb @@ -13,7 +13,7 @@ def self.evaluate(gemfile, lockfile, unlock) builder.to_definition(lockfile, unlock) end - VALID_PLATFORMS = Bundler::Dependency::PLATFORM_MAP.keys.freeze + VALID_PLATFORMS = Bundler::CurrentRuby::PLATFORM_MAP.keys.freeze VALID_KEYS = %w[group groups git path glob name branch ref tag require submodules platform platforms type source install_if gemfile force_ruby_platform].freeze diff --git a/bundler/spec/bundler/current_ruby_spec.rb b/bundler/spec/bundler/current_ruby_spec.rb new file mode 100644 index 000000000000..e0498d5a9e99 --- /dev/null +++ b/bundler/spec/bundler/current_ruby_spec.rb @@ -0,0 +1,140 @@ +# frozen_string_literal: true + +RSpec.describe Bundler::CurrentRuby do + describe "PLATFORM_MAP" do + subject { described_class::PLATFORM_MAP } + + # rubocop:disable Naming/VariableNumber + let(:platforms) do + { ruby: Gem::Platform::RUBY, + ruby_18: Gem::Platform::RUBY, + ruby_19: Gem::Platform::RUBY, + ruby_20: Gem::Platform::RUBY, + ruby_21: Gem::Platform::RUBY, + ruby_22: Gem::Platform::RUBY, + ruby_23: Gem::Platform::RUBY, + ruby_24: Gem::Platform::RUBY, + ruby_25: Gem::Platform::RUBY, + ruby_26: Gem::Platform::RUBY, + ruby_27: Gem::Platform::RUBY, + ruby_30: Gem::Platform::RUBY, + ruby_31: Gem::Platform::RUBY, + ruby_32: Gem::Platform::RUBY, + ruby_33: Gem::Platform::RUBY, + ruby_34: Gem::Platform::RUBY, + ruby_35: Gem::Platform::RUBY, + mri: Gem::Platform::RUBY, + mri_18: Gem::Platform::RUBY, + mri_19: Gem::Platform::RUBY, + mri_20: Gem::Platform::RUBY, + mri_21: Gem::Platform::RUBY, + mri_22: Gem::Platform::RUBY, + mri_23: Gem::Platform::RUBY, + mri_24: Gem::Platform::RUBY, + mri_25: Gem::Platform::RUBY, + mri_26: Gem::Platform::RUBY, + mri_27: Gem::Platform::RUBY, + mri_30: Gem::Platform::RUBY, + mri_31: Gem::Platform::RUBY, + mri_32: Gem::Platform::RUBY, + mri_33: Gem::Platform::RUBY, + mri_34: Gem::Platform::RUBY, + mri_35: Gem::Platform::RUBY, + rbx: Gem::Platform::RUBY, + truffleruby: Gem::Platform::RUBY, + jruby: Gem::Platform::JAVA, + jruby_18: Gem::Platform::JAVA, + jruby_19: Gem::Platform::JAVA, + windows: Gem::Platform::WINDOWS, + windows_18: Gem::Platform::WINDOWS, + windows_19: Gem::Platform::WINDOWS, + windows_20: Gem::Platform::WINDOWS, + windows_21: Gem::Platform::WINDOWS, + windows_22: Gem::Platform::WINDOWS, + windows_23: Gem::Platform::WINDOWS, + windows_24: Gem::Platform::WINDOWS, + windows_25: Gem::Platform::WINDOWS, + windows_26: Gem::Platform::WINDOWS, + windows_27: Gem::Platform::WINDOWS, + windows_30: Gem::Platform::WINDOWS, + windows_31: Gem::Platform::WINDOWS, + windows_32: Gem::Platform::WINDOWS, + windows_33: Gem::Platform::WINDOWS, + windows_34: Gem::Platform::WINDOWS, + windows_35: Gem::Platform::WINDOWS } + end + + let(:deprecated) do + { mswin: Gem::Platform::MSWIN, + mswin_18: Gem::Platform::MSWIN, + mswin_19: Gem::Platform::MSWIN, + mswin_20: Gem::Platform::MSWIN, + mswin_21: Gem::Platform::MSWIN, + mswin_22: Gem::Platform::MSWIN, + mswin_23: Gem::Platform::MSWIN, + mswin_24: Gem::Platform::MSWIN, + mswin_25: Gem::Platform::MSWIN, + mswin_26: Gem::Platform::MSWIN, + mswin_27: Gem::Platform::MSWIN, + mswin_30: Gem::Platform::MSWIN, + mswin_31: Gem::Platform::MSWIN, + mswin_32: Gem::Platform::MSWIN, + mswin_33: Gem::Platform::MSWIN, + mswin_34: Gem::Platform::MSWIN, + mswin_35: Gem::Platform::MSWIN, + mswin64: Gem::Platform::MSWIN64, + mswin64_19: Gem::Platform::MSWIN64, + mswin64_20: Gem::Platform::MSWIN64, + mswin64_21: Gem::Platform::MSWIN64, + mswin64_22: Gem::Platform::MSWIN64, + mswin64_23: Gem::Platform::MSWIN64, + mswin64_24: Gem::Platform::MSWIN64, + mswin64_25: Gem::Platform::MSWIN64, + mswin64_26: Gem::Platform::MSWIN64, + mswin64_27: Gem::Platform::MSWIN64, + mswin64_30: Gem::Platform::MSWIN64, + mswin64_31: Gem::Platform::MSWIN64, + mswin64_32: Gem::Platform::MSWIN64, + mswin64_33: Gem::Platform::MSWIN64, + mswin64_34: Gem::Platform::MSWIN64, + mswin64_35: Gem::Platform::MSWIN64, + mingw: Gem::Platform::MINGW, + mingw_18: Gem::Platform::MINGW, + mingw_19: Gem::Platform::MINGW, + mingw_20: Gem::Platform::MINGW, + mingw_21: Gem::Platform::MINGW, + mingw_22: Gem::Platform::MINGW, + mingw_23: Gem::Platform::MINGW, + mingw_24: Gem::Platform::MINGW, + mingw_25: Gem::Platform::MINGW, + mingw_26: Gem::Platform::MINGW, + mingw_27: Gem::Platform::MINGW, + mingw_30: Gem::Platform::MINGW, + mingw_31: Gem::Platform::MINGW, + mingw_32: Gem::Platform::MINGW, + mingw_33: Gem::Platform::MINGW, + mingw_34: Gem::Platform::MINGW, + mingw_35: Gem::Platform::MINGW, + x64_mingw: Gem::Platform::X64_MINGW, + x64_mingw_20: Gem::Platform::X64_MINGW, + x64_mingw_21: Gem::Platform::X64_MINGW, + x64_mingw_22: Gem::Platform::X64_MINGW, + x64_mingw_23: Gem::Platform::X64_MINGW, + x64_mingw_24: Gem::Platform::X64_MINGW, + x64_mingw_25: Gem::Platform::X64_MINGW, + x64_mingw_26: Gem::Platform::X64_MINGW, + x64_mingw_27: Gem::Platform::X64_MINGW, + x64_mingw_30: Gem::Platform::X64_MINGW, + x64_mingw_31: Gem::Platform::X64_MINGW, + x64_mingw_32: Gem::Platform::X64_MINGW, + x64_mingw_33: Gem::Platform::X64_MINGW, + x64_mingw_34: Gem::Platform::X64_MINGW, + x64_mingw_35: Gem::Platform::X64_MINGW } + end + # rubocop:enable Naming/VariableNumber + + it "includes all platforms" do + expect(subject).to eq(platforms.merge(deprecated)) + end + end +end diff --git a/bundler/spec/bundler/dependency_spec.rb b/bundler/spec/bundler/dependency_spec.rb index 1185c4e5dd04..f930459571da 100644 --- a/bundler/spec/bundler/dependency_spec.rb +++ b/bundler/spec/bundler/dependency_spec.rb @@ -35,140 +35,15 @@ end end - describe "PLATFORM_MAP" do - subject { described_class::PLATFORM_MAP } + it "is on the current platform" do + engine = Gem.win_platform? ? "windows" : RUBY_ENGINE - # rubocop:disable Naming/VariableNumber - let(:platforms) do - { ruby: Gem::Platform::RUBY, - ruby_18: Gem::Platform::RUBY, - ruby_19: Gem::Platform::RUBY, - ruby_20: Gem::Platform::RUBY, - ruby_21: Gem::Platform::RUBY, - ruby_22: Gem::Platform::RUBY, - ruby_23: Gem::Platform::RUBY, - ruby_24: Gem::Platform::RUBY, - ruby_25: Gem::Platform::RUBY, - ruby_26: Gem::Platform::RUBY, - ruby_27: Gem::Platform::RUBY, - ruby_30: Gem::Platform::RUBY, - ruby_31: Gem::Platform::RUBY, - ruby_32: Gem::Platform::RUBY, - ruby_33: Gem::Platform::RUBY, - ruby_34: Gem::Platform::RUBY, - ruby_35: Gem::Platform::RUBY, - mri: Gem::Platform::RUBY, - mri_18: Gem::Platform::RUBY, - mri_19: Gem::Platform::RUBY, - mri_20: Gem::Platform::RUBY, - mri_21: Gem::Platform::RUBY, - mri_22: Gem::Platform::RUBY, - mri_23: Gem::Platform::RUBY, - mri_24: Gem::Platform::RUBY, - mri_25: Gem::Platform::RUBY, - mri_26: Gem::Platform::RUBY, - mri_27: Gem::Platform::RUBY, - mri_30: Gem::Platform::RUBY, - mri_31: Gem::Platform::RUBY, - mri_32: Gem::Platform::RUBY, - mri_33: Gem::Platform::RUBY, - mri_34: Gem::Platform::RUBY, - mri_35: Gem::Platform::RUBY, - rbx: Gem::Platform::RUBY, - truffleruby: Gem::Platform::RUBY, - jruby: Gem::Platform::JAVA, - jruby_18: Gem::Platform::JAVA, - jruby_19: Gem::Platform::JAVA, - windows: Gem::Platform::WINDOWS, - windows_18: Gem::Platform::WINDOWS, - windows_19: Gem::Platform::WINDOWS, - windows_20: Gem::Platform::WINDOWS, - windows_21: Gem::Platform::WINDOWS, - windows_22: Gem::Platform::WINDOWS, - windows_23: Gem::Platform::WINDOWS, - windows_24: Gem::Platform::WINDOWS, - windows_25: Gem::Platform::WINDOWS, - windows_26: Gem::Platform::WINDOWS, - windows_27: Gem::Platform::WINDOWS, - windows_30: Gem::Platform::WINDOWS, - windows_31: Gem::Platform::WINDOWS, - windows_32: Gem::Platform::WINDOWS, - windows_33: Gem::Platform::WINDOWS, - windows_34: Gem::Platform::WINDOWS, - windows_35: Gem::Platform::WINDOWS } - end - - let(:deprecated) do - { mswin: Gem::Platform::MSWIN, - mswin_18: Gem::Platform::MSWIN, - mswin_19: Gem::Platform::MSWIN, - mswin_20: Gem::Platform::MSWIN, - mswin_21: Gem::Platform::MSWIN, - mswin_22: Gem::Platform::MSWIN, - mswin_23: Gem::Platform::MSWIN, - mswin_24: Gem::Platform::MSWIN, - mswin_25: Gem::Platform::MSWIN, - mswin_26: Gem::Platform::MSWIN, - mswin_27: Gem::Platform::MSWIN, - mswin_30: Gem::Platform::MSWIN, - mswin_31: Gem::Platform::MSWIN, - mswin_32: Gem::Platform::MSWIN, - mswin_33: Gem::Platform::MSWIN, - mswin_34: Gem::Platform::MSWIN, - mswin_35: Gem::Platform::MSWIN, - mswin64: Gem::Platform::MSWIN64, - mswin64_19: Gem::Platform::MSWIN64, - mswin64_20: Gem::Platform::MSWIN64, - mswin64_21: Gem::Platform::MSWIN64, - mswin64_22: Gem::Platform::MSWIN64, - mswin64_23: Gem::Platform::MSWIN64, - mswin64_24: Gem::Platform::MSWIN64, - mswin64_25: Gem::Platform::MSWIN64, - mswin64_26: Gem::Platform::MSWIN64, - mswin64_27: Gem::Platform::MSWIN64, - mswin64_30: Gem::Platform::MSWIN64, - mswin64_31: Gem::Platform::MSWIN64, - mswin64_32: Gem::Platform::MSWIN64, - mswin64_33: Gem::Platform::MSWIN64, - mswin64_34: Gem::Platform::MSWIN64, - mswin64_35: Gem::Platform::MSWIN64, - mingw: Gem::Platform::MINGW, - mingw_18: Gem::Platform::MINGW, - mingw_19: Gem::Platform::MINGW, - mingw_20: Gem::Platform::MINGW, - mingw_21: Gem::Platform::MINGW, - mingw_22: Gem::Platform::MINGW, - mingw_23: Gem::Platform::MINGW, - mingw_24: Gem::Platform::MINGW, - mingw_25: Gem::Platform::MINGW, - mingw_26: Gem::Platform::MINGW, - mingw_27: Gem::Platform::MINGW, - mingw_30: Gem::Platform::MINGW, - mingw_31: Gem::Platform::MINGW, - mingw_32: Gem::Platform::MINGW, - mingw_33: Gem::Platform::MINGW, - mingw_34: Gem::Platform::MINGW, - mingw_35: Gem::Platform::MINGW, - x64_mingw: Gem::Platform::X64_MINGW, - x64_mingw_20: Gem::Platform::X64_MINGW, - x64_mingw_21: Gem::Platform::X64_MINGW, - x64_mingw_22: Gem::Platform::X64_MINGW, - x64_mingw_23: Gem::Platform::X64_MINGW, - x64_mingw_24: Gem::Platform::X64_MINGW, - x64_mingw_25: Gem::Platform::X64_MINGW, - x64_mingw_26: Gem::Platform::X64_MINGW, - x64_mingw_27: Gem::Platform::X64_MINGW, - x64_mingw_30: Gem::Platform::X64_MINGW, - x64_mingw_31: Gem::Platform::X64_MINGW, - x64_mingw_32: Gem::Platform::X64_MINGW, - x64_mingw_33: Gem::Platform::X64_MINGW, - x64_mingw_34: Gem::Platform::X64_MINGW, - x64_mingw_35: Gem::Platform::X64_MINGW } - end - # rubocop:enable Naming/VariableNumber + dep = described_class.new( + "test_gem", + "1.0.0", + { "platforms" => "#{engine}_#{RbConfig::CONFIG["MAJOR"]}#{RbConfig::CONFIG["MINOR"]}" }, + ) - it "includes all platforms" do - expect(subject).to eq(platforms.merge(deprecated)) - end + expect(dep.current_platform?).to be_truthy end end diff --git a/bundler/spec/bundler/dsl_spec.rb b/bundler/spec/bundler/dsl_spec.rb index c4f9d0dbb5d6..f5fc0c7bbee4 100644 --- a/bundler/spec/bundler/dsl_spec.rb +++ b/bundler/spec/bundler/dsl_spec.rb @@ -201,8 +201,8 @@ describe "#gem" do # rubocop:disable Naming/VariableNumber [:ruby, :ruby_18, :ruby_19, :ruby_20, :ruby_21, :ruby_22, :ruby_23, :ruby_24, :ruby_25, :ruby_26, :ruby_27, - :ruby_30, :ruby_31, :ruby_32, :ruby_33, :mri, :mri_18, :mri_19, :mri_20, :mri_21, :mri_22, :mri_23, :mri_24, - :mri_25, :mri_26, :mri_27, :mri_30, :mri_31, :mri_32, :mri_33, :jruby, :rbx, :truffleruby].each do |platform| + :ruby_30, :ruby_31, :ruby_32, :ruby_33, :ruby_34, :ruby_35, :mri, :mri_18, :mri_19, :mri_20, :mri_21, :mri_22, :mri_23, :mri_24, + :mri_25, :mri_26, :mri_27, :mri_30, :mri_31, :mri_32, :mri_33, :mri_34, :mri_35, :jruby, :rbx, :truffleruby].each do |platform| it "allows #{platform} as a valid platform" do subject.gem("foo", platform: platform) end @@ -211,7 +211,9 @@ it "allows platforms matching the running Ruby version" do platform = "ruby_#{RbConfig::CONFIG["MAJOR"]}#{RbConfig::CONFIG["MINOR"]}" - subject.gem("foo", platform: platform) + + expect { subject.gem("foo", platform: platform) }.not_to raise_error + expect(Bundler.current_ruby.respond_to?("#{platform}?")).to be_truthy end it "rejects invalid platforms" do