2018-11-03 02:07:56 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Bundler
|
|
|
|
class RubyVersion
|
|
|
|
attr_reader :versions,
|
|
|
|
:patchlevel,
|
|
|
|
:engine,
|
|
|
|
:engine_versions,
|
|
|
|
:gem_version,
|
|
|
|
:engine_gem_version
|
|
|
|
|
|
|
|
def initialize(versions, patchlevel, engine, engine_version)
|
|
|
|
# The parameters to this method must satisfy the
|
|
|
|
# following constraints, which are verified in
|
|
|
|
# the DSL:
|
|
|
|
#
|
|
|
|
# * If an engine is specified, an engine version
|
|
|
|
# must also be specified
|
|
|
|
# * If an engine version is specified, an engine
|
|
|
|
# must also be specified
|
|
|
|
# * If the engine is "ruby", the engine version
|
|
|
|
# must not be specified, or the engine version
|
|
|
|
# specified must match the version.
|
|
|
|
|
|
|
|
@versions = Array(versions).map do |v|
|
|
|
|
op, v = Gem::Requirement.parse(v)
|
|
|
|
op == "=" ? v.to_s : "#{op} #{v}"
|
|
|
|
end
|
|
|
|
|
|
|
|
@gem_version = Gem::Requirement.create(@versions.first).requirements.first.last
|
2022-12-26 08:00:11 +03:00
|
|
|
@input_engine = engine&.to_s
|
|
|
|
@engine = engine&.to_s || "ruby"
|
2018-11-03 02:07:56 +03:00
|
|
|
@engine_versions = (engine_version && Array(engine_version)) || @versions
|
|
|
|
@engine_gem_version = Gem::Requirement.create(@engine_versions.first).requirements.first.last
|
2022-07-29 08:59:56 +03:00
|
|
|
@patchlevel = patchlevel || (@gem_version.prerelease? ? "-1" : nil)
|
2018-11-03 02:07:56 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_s(versions = self.versions)
|
|
|
|
output = String.new("ruby #{versions_string(versions)}")
|
2022-07-29 08:59:56 +03:00
|
|
|
output << "p#{patchlevel}" if patchlevel && patchlevel != "-1"
|
2018-11-03 02:07:56 +03:00
|
|
|
output << " (#{engine} #{versions_string(engine_versions)})" unless engine == "ruby"
|
|
|
|
|
|
|
|
output
|
|
|
|
end
|
|
|
|
|
|
|
|
# @private
|
|
|
|
PATTERN = /
|
|
|
|
ruby\s
|
2022-07-29 08:59:56 +03:00
|
|
|
(\d+\.\d+\.\d+(?:\.\S+)?) # ruby version
|
2018-11-03 02:07:56 +03:00
|
|
|
(?:p(-?\d+))? # optional patchlevel
|
|
|
|
(?:\s\((\S+)\s(.+)\))? # optional engine info
|
2019-04-14 09:01:35 +03:00
|
|
|
/xo.freeze
|
2018-11-03 02:07:56 +03:00
|
|
|
|
|
|
|
# Returns a RubyVersion from the given string.
|
|
|
|
# @param [String] the version string to match.
|
|
|
|
# @return [RubyVersion,Nil] The version if the string is a valid RubyVersion
|
|
|
|
# description, and nil otherwise.
|
|
|
|
def self.from_string(string)
|
|
|
|
new($1, $2, $3, $4) if string =~ PATTERN
|
|
|
|
end
|
|
|
|
|
|
|
|
def single_version_string
|
|
|
|
to_s(gem_version)
|
|
|
|
end
|
|
|
|
|
|
|
|
def ==(other)
|
|
|
|
versions == other.versions &&
|
|
|
|
engine == other.engine &&
|
|
|
|
engine_versions == other.engine_versions &&
|
|
|
|
patchlevel == other.patchlevel
|
|
|
|
end
|
|
|
|
|
|
|
|
def host
|
|
|
|
@host ||= [
|
|
|
|
RbConfig::CONFIG["host_cpu"],
|
|
|
|
RbConfig::CONFIG["host_vendor"],
|
2019-04-14 09:01:35 +03:00
|
|
|
RbConfig::CONFIG["host_os"],
|
2018-11-03 02:07:56 +03:00
|
|
|
].join("-")
|
|
|
|
end
|
|
|
|
|
|
|
|
# Returns a tuple of these things:
|
|
|
|
# [diff, this, other]
|
|
|
|
# The priority of attributes are
|
|
|
|
# 1. engine
|
|
|
|
# 2. ruby_version
|
|
|
|
# 3. engine_version
|
|
|
|
def diff(other)
|
|
|
|
raise ArgumentError, "Can only diff with a RubyVersion, not a #{other.class}" unless other.is_a?(RubyVersion)
|
|
|
|
if engine != other.engine && @input_engine
|
|
|
|
[:engine, engine, other.engine]
|
|
|
|
elsif versions.empty? || !matches?(versions, other.gem_version)
|
|
|
|
[:version, versions_string(versions), versions_string(other.versions)]
|
|
|
|
elsif @input_engine && !matches?(engine_versions, other.engine_gem_version)
|
|
|
|
[:engine_version, versions_string(engine_versions), versions_string(other.engine_versions)]
|
|
|
|
elsif patchlevel && (!patchlevel.is_a?(String) || !other.patchlevel.is_a?(String) || !matches?(patchlevel, other.patchlevel))
|
|
|
|
[:patchlevel, patchlevel, other.patchlevel]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def versions_string(versions)
|
|
|
|
Array(versions).join(", ")
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.system
|
2019-04-12 20:05:26 +03:00
|
|
|
ruby_engine = RUBY_ENGINE.dup
|
2022-07-29 08:59:56 +03:00
|
|
|
ruby_version = Gem.ruby_version.to_s
|
|
|
|
ruby_engine_version = RUBY_ENGINE == "ruby" ? ruby_version : RUBY_ENGINE_VERSION.dup
|
2018-11-03 02:07:56 +03:00
|
|
|
patchlevel = RUBY_PATCHLEVEL.to_s
|
|
|
|
|
2023-03-24 23:00:40 +03:00
|
|
|
@system ||= RubyVersion.new(ruby_version, patchlevel, ruby_engine, ruby_engine_version)
|
2018-11-03 02:07:56 +03:00
|
|
|
end
|
|
|
|
|
2020-10-15 07:20:25 +03:00
|
|
|
private
|
2018-11-03 02:07:56 +03:00
|
|
|
|
|
|
|
def matches?(requirements, version)
|
|
|
|
# Handles RUBY_PATCHLEVEL of -1 for instances like ruby-head
|
|
|
|
return requirements == version if requirements.to_s == "-1" || version.to_s == "-1"
|
|
|
|
|
|
|
|
Array(requirements).all? do |requirement|
|
|
|
|
Gem::Requirement.create(requirement).satisfied_by?(Gem::Version.create(version))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|