2016-02-01 15:43:26 +03:00
|
|
|
# frozen_string_literal: true
|
2012-11-29 10:52:18 +04:00
|
|
|
require 'rubygems/dependency'
|
|
|
|
require 'rubygems/exceptions'
|
2015-07-02 04:30:07 +03:00
|
|
|
require 'rubygems/util'
|
2013-09-14 12:59:02 +04:00
|
|
|
require 'rubygems/util/list'
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
##
|
|
|
|
# Given a set of Gem::Dependency objects as +needed+ and a way to query the
|
|
|
|
# set of available specs via +set+, calculates a set of ActivationRequest
|
|
|
|
# objects which indicate all the specs that should be activated to meet the
|
|
|
|
# all the requirements.
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-11-19 04:34:13 +04:00
|
|
|
class Gem::Resolver
|
2015-07-02 00:50:14 +03:00
|
|
|
require 'rubygems/resolver/molinillo'
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-11-25 23:14:49 +04:00
|
|
|
##
|
|
|
|
# If the DEBUG_RESOLVER environment variable is set then debugging mode is
|
|
|
|
# enabled for the resolver. This will display information about the state
|
|
|
|
# of the resolver while a set of dependencies is being resolved.
|
|
|
|
|
|
|
|
DEBUG_RESOLVER = !ENV['DEBUG_RESOLVER'].nil?
|
|
|
|
|
|
|
|
##
|
2014-09-14 07:30:02 +04:00
|
|
|
# Set to true if all development dependencies should be considered.
|
2013-11-25 23:14:49 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
attr_accessor :development
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
##
|
|
|
|
# Set to true if immediate development dependencies should be considered.
|
|
|
|
|
|
|
|
attr_accessor :development_shallow
|
|
|
|
|
2013-12-18 05:02:58 +04:00
|
|
|
##
|
|
|
|
# When true, no dependencies are looked up for requested gems.
|
|
|
|
|
|
|
|
attr_accessor :ignore_dependencies
|
|
|
|
|
2013-11-25 23:14:49 +04:00
|
|
|
##
|
|
|
|
# List of dependencies that could not be found in the configured sources.
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
attr_reader :missing
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-12-08 05:22:39 +04:00
|
|
|
attr_reader :stats
|
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
##
|
|
|
|
# Hash of gems to skip resolution. Keyed by gem name, with arrays of
|
|
|
|
# gem specifications as values.
|
|
|
|
|
|
|
|
attr_accessor :skip_gems
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
##
|
|
|
|
# When a missing dependency, don't stop. Just go on and record what was
|
|
|
|
# missing.
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
attr_accessor :soft_missing
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-11-22 03:27:30 +04:00
|
|
|
##
|
|
|
|
# Combines +sets+ into a ComposedSet that allows specification lookup in a
|
|
|
|
# uniform manner. If one of the +sets+ is itself a ComposedSet its sets are
|
|
|
|
# flattened into the result ComposedSet.
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
def self.compose_sets *sets
|
2013-11-10 21:51:40 +04:00
|
|
|
sets.compact!
|
|
|
|
|
2013-11-22 03:27:30 +04:00
|
|
|
sets = sets.map do |set|
|
|
|
|
case set
|
2014-02-04 04:48:31 +04:00
|
|
|
when Gem::Resolver::BestSet then
|
|
|
|
set
|
2013-11-22 03:27:30 +04:00
|
|
|
when Gem::Resolver::ComposedSet then
|
|
|
|
set.sets
|
|
|
|
else
|
|
|
|
set
|
|
|
|
end
|
|
|
|
end.flatten
|
|
|
|
|
2013-11-10 21:51:40 +04:00
|
|
|
case sets.length
|
|
|
|
when 0 then
|
|
|
|
raise ArgumentError, 'one set in the composition must be non-nil'
|
|
|
|
when 1 then
|
|
|
|
sets.first
|
|
|
|
else
|
2013-11-19 04:34:13 +04:00
|
|
|
Gem::Resolver::ComposedSet.new(*sets)
|
2013-11-10 21:51:40 +04:00
|
|
|
end
|
2013-09-14 12:59:02 +04:00
|
|
|
end
|
2013-09-13 23:58:57 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
##
|
2013-11-25 23:14:49 +04:00
|
|
|
# Creates a Resolver that queries only against the already installed gems
|
|
|
|
# for the +needed+ dependencies.
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
def self.for_current_gems needed
|
2013-11-19 04:34:13 +04:00
|
|
|
new needed, Gem::Resolver::CurrentSet.new
|
2012-11-29 10:52:18 +04:00
|
|
|
end
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
##
|
2013-11-19 04:34:13 +04:00
|
|
|
# Create Resolver object which will resolve the tree starting
|
2013-10-18 04:41:36 +04:00
|
|
|
# with +needed+ Dependency objects.
|
2012-11-29 10:52:18 +04:00
|
|
|
#
|
2013-09-14 12:59:02 +04:00
|
|
|
# +set+ is an object that provides where to look for specifications to
|
2013-10-18 04:41:36 +04:00
|
|
|
# satisfy the Dependencies. This defaults to IndexSet, which will query
|
2013-09-14 12:59:02 +04:00
|
|
|
# rubygems.org.
|
|
|
|
|
|
|
|
def initialize needed, set = nil
|
2013-11-19 04:34:13 +04:00
|
|
|
@set = set || Gem::Resolver::IndexSet.new
|
2013-09-14 12:59:02 +04:00
|
|
|
@needed = needed
|
|
|
|
|
2013-12-18 05:02:58 +04:00
|
|
|
@development = false
|
2014-09-14 07:30:02 +04:00
|
|
|
@development_shallow = false
|
2013-12-18 05:02:58 +04:00
|
|
|
@ignore_dependencies = false
|
|
|
|
@missing = []
|
2014-09-14 07:30:02 +04:00
|
|
|
@skip_gems = {}
|
2013-12-18 05:02:58 +04:00
|
|
|
@soft_missing = false
|
|
|
|
@stats = Gem::Resolver::Stats.new
|
2013-09-14 12:59:02 +04:00
|
|
|
end
|
2013-09-13 23:58:57 +04:00
|
|
|
|
2013-11-25 23:14:49 +04:00
|
|
|
def explain stage, *data # :nodoc:
|
2014-09-14 07:30:02 +04:00
|
|
|
return unless DEBUG_RESOLVER
|
|
|
|
|
|
|
|
d = data.map { |x| x.pretty_inspect }.join(", ")
|
|
|
|
$stderr.printf "%10s %s\n", stage.to_s.upcase, d
|
2013-11-19 04:34:13 +04:00
|
|
|
end
|
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
def explain_list stage # :nodoc:
|
|
|
|
return unless DEBUG_RESOLVER
|
|
|
|
|
|
|
|
data = yield
|
|
|
|
$stderr.printf "%10s (%d entries)\n", stage.to_s.upcase, data.size
|
|
|
|
PP.pp data, $stderr unless data.empty?
|
2013-11-22 03:27:30 +04:00
|
|
|
end
|
|
|
|
|
2013-11-10 21:51:40 +04:00
|
|
|
##
|
|
|
|
# Creates an ActivationRequest for the given +dep+ and the last +possible+
|
|
|
|
# specification.
|
|
|
|
#
|
|
|
|
# Returns the Specification and the ActivationRequest
|
|
|
|
|
|
|
|
def activation_request dep, possible # :nodoc:
|
|
|
|
spec = possible.pop
|
|
|
|
|
2013-11-19 04:34:13 +04:00
|
|
|
explain :activate, [spec.full_name, possible.size]
|
2014-09-14 07:30:02 +04:00
|
|
|
explain :possible, possible
|
2013-11-19 04:34:13 +04:00
|
|
|
|
2013-11-10 21:51:40 +04:00
|
|
|
activation_request =
|
2013-11-19 04:34:13 +04:00
|
|
|
Gem::Resolver::ActivationRequest.new spec, dep, possible
|
2013-11-10 21:51:40 +04:00
|
|
|
|
|
|
|
return spec, activation_request
|
|
|
|
end
|
|
|
|
|
2015-07-02 00:50:14 +03:00
|
|
|
def requests s, act, reqs=[] # :nodoc:
|
2013-12-18 05:02:58 +04:00
|
|
|
return reqs if @ignore_dependencies
|
|
|
|
|
2014-09-14 07:30:02 +04:00
|
|
|
s.fetch_development_dependencies if @development
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
s.dependencies.reverse_each do |d|
|
|
|
|
next if d.type == :development and not @development
|
2014-09-14 07:30:02 +04:00
|
|
|
next if d.type == :development and @development_shallow and
|
|
|
|
act.development?
|
|
|
|
next if d.type == :development and @development_shallow and
|
|
|
|
act.parent
|
|
|
|
|
2015-07-02 00:50:14 +03:00
|
|
|
reqs << Gem::Resolver::DependencyRequest.new(d, act)
|
2013-12-08 05:22:39 +04:00
|
|
|
@stats.requirement!
|
2013-09-13 23:58:57 +04:00
|
|
|
end
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
@set.prefetch reqs
|
2013-09-13 23:58:57 +04:00
|
|
|
|
2013-12-08 05:22:39 +04:00
|
|
|
@stats.record_requirements reqs
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
reqs
|
|
|
|
end
|
2013-09-13 23:58:57 +04:00
|
|
|
|
2015-07-02 00:50:14 +03:00
|
|
|
include Molinillo::UI
|
2013-09-13 23:58:57 +04:00
|
|
|
|
2015-07-02 00:50:14 +03:00
|
|
|
def output
|
2018-05-30 16:01:35 +03:00
|
|
|
@output ||= debug? ? $stdout : File.open(IO::NULL, 'w')
|
2015-07-02 00:50:14 +03:00
|
|
|
end
|
2013-09-13 23:58:57 +04:00
|
|
|
|
2015-07-02 00:50:14 +03:00
|
|
|
def debug?
|
|
|
|
DEBUG_RESOLVER
|
|
|
|
end
|
2013-12-08 05:22:39 +04:00
|
|
|
|
2015-07-02 00:50:14 +03:00
|
|
|
include Molinillo::SpecificationProvider
|
2013-09-13 23:58:57 +04:00
|
|
|
|
2015-07-02 00:50:14 +03:00
|
|
|
##
|
|
|
|
# Proceed with resolution! Returns an array of ActivationRequest objects.
|
2013-09-13 23:58:57 +04:00
|
|
|
|
2015-07-02 00:50:14 +03:00
|
|
|
def resolve
|
|
|
|
locking_dg = Molinillo::DependencyGraph.new
|
2015-11-19 09:16:19 +03:00
|
|
|
Molinillo::Resolver.new(self, self).resolve(@needed.map { |d| DependencyRequest.new d, nil }, locking_dg).tsort.map(&:payload).compact
|
2015-07-02 00:50:14 +03:00
|
|
|
rescue Molinillo::VersionConflict => e
|
|
|
|
conflict = e.conflicts.values.first
|
|
|
|
raise Gem::DependencyResolutionError, Conflict.new(conflict.requirement_trees.first.first, conflict.existing, conflict.requirement)
|
2015-07-08 09:09:09 +03:00
|
|
|
ensure
|
2016-03-28 05:26:39 +03:00
|
|
|
@output.close if defined?(@output) and !debug?
|
2013-09-14 12:59:02 +04:00
|
|
|
end
|
2013-09-13 23:58:57 +04:00
|
|
|
|
2013-09-25 04:53:19 +04:00
|
|
|
##
|
2013-11-10 21:51:40 +04:00
|
|
|
# Extracts the specifications that may be able to fulfill +dependency+ and
|
|
|
|
# returns those that match the local platform and all those that match.
|
2013-09-25 04:53:19 +04:00
|
|
|
|
|
|
|
def find_possible dependency # :nodoc:
|
2013-11-10 21:51:40 +04:00
|
|
|
all = @set.find_all dependency
|
2014-09-14 07:30:02 +04:00
|
|
|
|
|
|
|
if (skip_dep_gems = skip_gems[dependency.name]) && !skip_dep_gems.empty?
|
|
|
|
matching = all.select do |api_spec|
|
|
|
|
skip_dep_gems.any? { |s| api_spec.version == s.version }
|
|
|
|
end
|
|
|
|
|
|
|
|
all = matching unless matching.empty?
|
|
|
|
end
|
|
|
|
|
2013-11-10 21:51:40 +04:00
|
|
|
matching_platform = select_local_platforms all
|
|
|
|
|
|
|
|
return matching_platform, all
|
2013-09-25 04:53:19 +04:00
|
|
|
end
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
##
|
2015-07-02 00:50:14 +03:00
|
|
|
# Returns the gems in +specs+ that match the local platform.
|
2013-09-19 01:39:43 +04:00
|
|
|
|
2015-07-02 00:50:14 +03:00
|
|
|
def select_local_platforms specs # :nodoc:
|
|
|
|
specs.select do |spec|
|
|
|
|
Gem::Platform.installable? spec
|
2013-09-25 04:53:19 +04:00
|
|
|
end
|
|
|
|
end
|
2013-09-14 12:59:02 +04:00
|
|
|
|
2015-07-02 00:50:14 +03:00
|
|
|
def search_for(dependency)
|
|
|
|
possibles, all = find_possible(dependency)
|
|
|
|
if !@soft_missing && possibles.empty?
|
|
|
|
@missing << dependency
|
|
|
|
exc = Gem::UnsatisfiableDependencyError.new dependency, all
|
|
|
|
exc.errors = @set.errors
|
|
|
|
raise exc
|
2013-09-25 04:53:19 +04:00
|
|
|
end
|
2017-10-08 04:32:18 +03:00
|
|
|
|
|
|
|
sources = []
|
|
|
|
|
|
|
|
groups = Hash.new { |hash, key| hash[key] = [] }
|
|
|
|
|
|
|
|
# create groups & sources in the same loop
|
|
|
|
sources = possibles.map { |spec|
|
|
|
|
source = spec.source
|
|
|
|
groups[source] << spec
|
|
|
|
source
|
|
|
|
}.uniq.reverse
|
|
|
|
|
|
|
|
activation_requests = []
|
|
|
|
|
|
|
|
sources.each do |source|
|
|
|
|
groups[source].
|
|
|
|
sort_by { |spec| [spec.version, Gem::Platform.local =~ spec.platform ? 1 : 0] }.
|
|
|
|
map { |spec| ActivationRequest.new spec, dependency, [] }.
|
|
|
|
each { |activation_request| activation_requests << activation_request }
|
|
|
|
end
|
|
|
|
|
|
|
|
activation_requests
|
2013-09-25 04:53:19 +04:00
|
|
|
end
|
|
|
|
|
2015-07-02 00:50:14 +03:00
|
|
|
def dependencies_for(specification)
|
|
|
|
return [] if @ignore_dependencies
|
|
|
|
spec = specification.spec
|
|
|
|
requests(spec, specification)
|
2013-09-25 04:53:19 +04:00
|
|
|
end
|
|
|
|
|
2015-07-02 00:50:14 +03:00
|
|
|
def requirement_satisfied_by?(requirement, activated, spec)
|
|
|
|
requirement.matches_spec? spec
|
2013-09-14 12:59:02 +04:00
|
|
|
end
|
2013-09-13 23:58:57 +04:00
|
|
|
|
2015-07-02 00:50:14 +03:00
|
|
|
def name_for(dependency)
|
|
|
|
dependency.name
|
|
|
|
end
|
2013-09-13 23:58:57 +04:00
|
|
|
|
2015-07-02 00:50:14 +03:00
|
|
|
def allow_missing?(dependency)
|
|
|
|
@missing << dependency
|
|
|
|
@soft_missing
|
2013-09-13 23:58:57 +04:00
|
|
|
end
|
2013-09-14 12:59:02 +04:00
|
|
|
|
2017-03-17 04:29:22 +03:00
|
|
|
def sort_dependencies(dependencies, activated, conflicts)
|
2017-08-04 17:57:31 +03:00
|
|
|
dependencies.sort_by.with_index do |dependency, i|
|
2017-03-17 04:29:22 +03:00
|
|
|
name = name_for(dependency)
|
|
|
|
[
|
|
|
|
activated.vertex_named(name).payload ? 0 : 1,
|
|
|
|
amount_constrained(dependency),
|
|
|
|
conflicts[name] ? 0 : 1,
|
|
|
|
activated.vertex_named(name).payload ? 0 : search_for(dependency).count,
|
2017-08-04 17:57:31 +03:00
|
|
|
i # for stable sort
|
2017-03-17 04:29:22 +03:00
|
|
|
]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
SINGLE_POSSIBILITY_CONSTRAINT_PENALTY = 1_000_000
|
|
|
|
private_constant :SINGLE_POSSIBILITY_CONSTRAINT_PENALTY if defined?(private_constant)
|
|
|
|
|
|
|
|
# returns an integer \in (-\infty, 0]
|
|
|
|
# a number closer to 0 means the dependency is less constraining
|
|
|
|
#
|
|
|
|
# dependencies w/ 0 or 1 possibilities (ignoring version requirements)
|
|
|
|
# are given very negative values, so they _always_ sort first,
|
|
|
|
# before dependencies that are unconstrained
|
|
|
|
def amount_constrained(dependency)
|
|
|
|
@amount_constrained ||= {}
|
|
|
|
@amount_constrained[dependency.name] ||= begin
|
|
|
|
name_dependency = Gem::Dependency.new(dependency.name)
|
|
|
|
dependency_request_for_name = Gem::Resolver::DependencyRequest.new(name_dependency, dependency.requester)
|
|
|
|
all = @set.find_all(dependency_request_for_name).size
|
|
|
|
|
|
|
|
if all <= 1
|
|
|
|
all - SINGLE_POSSIBILITY_CONSTRAINT_PENALTY
|
|
|
|
else
|
|
|
|
search = search_for(dependency).size
|
|
|
|
search - all
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
private :amount_constrained
|
|
|
|
|
2013-09-13 23:58:57 +04:00
|
|
|
end
|
2013-09-14 12:59:02 +04:00
|
|
|
|
2013-11-19 04:34:13 +04:00
|
|
|
require 'rubygems/resolver/activation_request'
|
|
|
|
require 'rubygems/resolver/conflict'
|
|
|
|
require 'rubygems/resolver/dependency_request'
|
|
|
|
require 'rubygems/resolver/requirement_list'
|
2013-12-08 05:22:39 +04:00
|
|
|
require 'rubygems/resolver/stats'
|
2013-11-19 04:34:13 +04:00
|
|
|
|
|
|
|
require 'rubygems/resolver/set'
|
|
|
|
require 'rubygems/resolver/api_set'
|
|
|
|
require 'rubygems/resolver/composed_set'
|
|
|
|
require 'rubygems/resolver/best_set'
|
|
|
|
require 'rubygems/resolver/current_set'
|
|
|
|
require 'rubygems/resolver/git_set'
|
|
|
|
require 'rubygems/resolver/index_set'
|
|
|
|
require 'rubygems/resolver/installer_set'
|
|
|
|
require 'rubygems/resolver/lock_set'
|
|
|
|
require 'rubygems/resolver/vendor_set'
|
2016-02-01 15:43:26 +03:00
|
|
|
require 'rubygems/resolver/source_set'
|
2013-11-19 04:34:13 +04:00
|
|
|
|
|
|
|
require 'rubygems/resolver/specification'
|
|
|
|
require 'rubygems/resolver/spec_specification'
|
|
|
|
require 'rubygems/resolver/api_specification'
|
|
|
|
require 'rubygems/resolver/git_specification'
|
|
|
|
require 'rubygems/resolver/index_specification'
|
|
|
|
require 'rubygems/resolver/installed_specification'
|
2013-11-25 23:14:49 +04:00
|
|
|
require 'rubygems/resolver/local_specification'
|
2013-12-08 05:22:39 +04:00
|
|
|
require 'rubygems/resolver/lock_specification'
|
2013-11-19 04:34:13 +04:00
|
|
|
require 'rubygems/resolver/vendor_specification'
|