2018-11-02 19:07:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-06-01 05:49:40 -04:00
|
|
|
require_relative "match_platform"
|
2018-11-02 19:07:56 -04:00
|
|
|
|
|
|
|
module Bundler
|
|
|
|
class LazySpecification
|
|
|
|
include MatchPlatform
|
|
|
|
|
|
|
|
attr_reader :name, :version, :dependencies, :platform
|
2022-07-26 00:43:48 -04:00
|
|
|
attr_accessor :source, :remote, :force_ruby_platform
|
2018-11-02 19:07:56 -04:00
|
|
|
|
|
|
|
def initialize(name, version, platform, source = nil)
|
|
|
|
@name = name
|
|
|
|
@version = version
|
|
|
|
@dependencies = []
|
|
|
|
@platform = platform || Gem::Platform::RUBY
|
|
|
|
@source = source
|
|
|
|
@specification = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def full_name
|
2022-07-26 00:43:48 -04:00
|
|
|
if platform == Gem::Platform::RUBY
|
2018-11-02 19:07:56 -04:00
|
|
|
"#{@name}-#{@version}"
|
|
|
|
else
|
|
|
|
"#{@name}-#{@version}-#{platform}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ==(other)
|
|
|
|
identifier == other.identifier
|
|
|
|
end
|
|
|
|
|
2020-05-08 01:19:04 -04:00
|
|
|
def eql?(other)
|
|
|
|
identifier.eql?(other.identifier)
|
|
|
|
end
|
|
|
|
|
|
|
|
def hash
|
|
|
|
identifier.hash
|
|
|
|
end
|
|
|
|
|
2021-11-30 12:12:54 -05:00
|
|
|
##
|
|
|
|
# Does this locked specification satisfy +dependency+?
|
|
|
|
#
|
|
|
|
# NOTE: Rubygems default requirement is ">= 0", which doesn't match
|
|
|
|
# prereleases of 0 versions, like "0.0.0.dev" or "0.0.0.SNAPSHOT". However,
|
|
|
|
# bundler users expect those to work. We need to make sure that Gemfile
|
|
|
|
# dependencies without explicit requirements (which use ">= 0" under the
|
|
|
|
# hood by default) are still valid for locked specs using this kind of
|
|
|
|
# versions. The method implements an ad-hoc fix for that. A better solution
|
|
|
|
# might be to change default rubygems requirement of dependencies to be ">=
|
|
|
|
# 0.A" but that's a major refactoring likely to break things. Hopefully we
|
|
|
|
# can attempt it in the future.
|
|
|
|
#
|
|
|
|
|
2018-11-02 19:07:56 -04:00
|
|
|
def satisfies?(dependency)
|
2021-11-30 12:07:32 -05:00
|
|
|
effective_requirement = dependency.requirement == Gem::Requirement.default ? Gem::Requirement.new(">= 0.A") : dependency.requirement
|
2021-11-30 12:03:14 -05:00
|
|
|
|
|
|
|
@name == dependency.name && effective_requirement.satisfied_by?(Gem::Version.new(@version))
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_lock
|
|
|
|
out = String.new
|
|
|
|
|
2022-07-26 00:43:48 -04:00
|
|
|
if platform == Gem::Platform::RUBY
|
2018-11-02 19:07:56 -04:00
|
|
|
out << " #{name} (#{version})\n"
|
|
|
|
else
|
|
|
|
out << " #{name} (#{version}-#{platform})\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
dependencies.sort_by(&:to_s).uniq.each do |dep|
|
|
|
|
next if dep.type == :development
|
|
|
|
out << " #{dep.to_lock}\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
out
|
|
|
|
end
|
|
|
|
|
2022-07-26 00:43:48 -04:00
|
|
|
def materialize_for_installation
|
2022-07-14 16:22:54 -04:00
|
|
|
source.local!
|
|
|
|
|
2022-07-26 00:43:48 -04:00
|
|
|
__materialize__(ruby_platform_materializes_to_ruby_platform? ? platform : Bundler.local_platform)
|
|
|
|
end
|
|
|
|
|
|
|
|
def materialize_for_resolution
|
|
|
|
return self unless Gem::Platform.match_spec?(self)
|
|
|
|
|
|
|
|
__materialize__(platform)
|
|
|
|
end
|
|
|
|
|
|
|
|
def __materialize__(platform)
|
2022-07-25 05:14:06 -04:00
|
|
|
@specification = begin
|
2022-07-23 14:56:17 -04:00
|
|
|
search_object = if source.is_a?(Source::Path) || !ruby_platform_materializes_to_ruby_platform?
|
2020-12-08 02:36:29 -05:00
|
|
|
Dependency.new(name, version)
|
|
|
|
else
|
2022-07-23 14:56:17 -04:00
|
|
|
self
|
2020-12-08 02:36:29 -05:00
|
|
|
end
|
2020-05-08 01:19:04 -04:00
|
|
|
candidates = source.specs.search(search_object)
|
|
|
|
same_platform_candidates = candidates.select do |spec|
|
2022-07-26 00:43:48 -04:00
|
|
|
MatchPlatform.platforms_match?(spec.platform, platform)
|
2020-05-08 01:19:04 -04:00
|
|
|
end
|
2021-04-14 23:47:04 -04:00
|
|
|
installable_candidates = same_platform_candidates.select do |spec|
|
2022-01-18 23:28:23 -05:00
|
|
|
spec.is_a?(StubSpecification) ||
|
2021-04-14 23:47:04 -04:00
|
|
|
(spec.required_ruby_version.satisfied_by?(Gem.ruby_version) &&
|
|
|
|
spec.required_rubygems_version.satisfied_by?(Gem.rubygems_version))
|
|
|
|
end
|
2022-04-06 03:22:49 -04:00
|
|
|
search = installable_candidates.last || same_platform_candidates.last
|
2018-11-02 19:07:56 -04:00
|
|
|
search.dependencies = dependencies if search && (search.is_a?(RemoteSpecification) || search.is_a?(EndpointSpecification))
|
|
|
|
search
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def respond_to?(*args)
|
|
|
|
super || @specification ? @specification.respond_to?(*args) : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_s
|
2022-07-26 00:43:48 -04:00
|
|
|
@__to_s ||= if platform == Gem::Platform::RUBY
|
2018-11-02 19:07:56 -04:00
|
|
|
"#{name} (#{version})"
|
|
|
|
else
|
|
|
|
"#{name} (#{version}-#{platform})"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def identifier
|
2021-02-01 10:17:16 -05:00
|
|
|
@__identifier ||= [name, version, platform_string]
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def git_version
|
|
|
|
return unless source.is_a?(Bundler::Source::Git)
|
|
|
|
" #{source.revision[0..6]}"
|
|
|
|
end
|
|
|
|
|
2021-02-01 10:17:16 -05:00
|
|
|
protected
|
|
|
|
|
|
|
|
def platform_string
|
|
|
|
platform_string = platform.to_s
|
|
|
|
platform_string == Index::RUBY ? Index::NULL : platform_string
|
|
|
|
end
|
|
|
|
|
2020-10-15 00:20:25 -04:00
|
|
|
private
|
2018-11-02 19:07:56 -04:00
|
|
|
|
|
|
|
def to_ary
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def method_missing(method, *args, &blk)
|
|
|
|
raise "LazySpecification has not been materialized yet (calling :#{method} #{args.inspect})" unless @specification
|
|
|
|
|
|
|
|
return super unless respond_to?(method)
|
|
|
|
|
|
|
|
@specification.send(method, *args, &blk)
|
|
|
|
end
|
2020-12-14 18:32:54 -05:00
|
|
|
|
|
|
|
#
|
2020-12-22 18:45:19 -05:00
|
|
|
# For backwards compatibility with existing lockfiles, if the most specific
|
|
|
|
# locked platform is RUBY, we keep the previous behaviour of resolving the
|
|
|
|
# best platform variant at materiliazation time. For previous bundler
|
|
|
|
# versions (before 2.2.0) this was always the case (except when the lockfile
|
|
|
|
# only included non-ruby platforms), but we're also keeping this behaviour
|
|
|
|
# on newer bundlers unless users generate the lockfile from scratch or
|
|
|
|
# explicitly add a more specific platform.
|
2020-12-14 18:32:54 -05:00
|
|
|
#
|
|
|
|
def ruby_platform_materializes_to_ruby_platform?
|
2022-07-12 23:56:36 -04:00
|
|
|
!Bundler.most_specific_locked_platform?(generic_local_platform) || force_ruby_platform || Bundler.settings[:force_ruby_platform]
|
2020-12-14 18:32:54 -05:00
|
|
|
end
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
end
|