2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2012-11-29 01:52:18 -05:00
|
|
|
require 'rubygems/dependency'
|
|
|
|
require 'rubygems/exceptions'
|
2015-07-01 21:30:07 -04:00
|
|
|
require 'rubygems/util'
|
2013-09-14 04:59:02 -04:00
|
|
|
require 'rubygems/util/list'
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
require 'uri'
|
|
|
|
require 'net/http'
|
|
|
|
|
2013-09-14 04: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 01:52:18 -05:00
|
|
|
|
2013-11-18 19:34:13 -05:00
|
|
|
class Gem::Resolver
|
2015-07-01 17:50:14 -04:00
|
|
|
require 'rubygems/resolver/molinillo'
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-11-25 14:14:49 -05: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-13 23:30:02 -04:00
|
|
|
# Set to true if all development dependencies should be considered.
|
2013-11-25 14:14:49 -05:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
attr_accessor :development
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
##
|
|
|
|
# Set to true if immediate development dependencies should be considered.
|
|
|
|
|
|
|
|
attr_accessor :development_shallow
|
|
|
|
|
2013-12-17 20:02:58 -05:00
|
|
|
##
|
|
|
|
# When true, no dependencies are looked up for requested gems.
|
|
|
|
|
|
|
|
attr_accessor :ignore_dependencies
|
|
|
|
|
2013-11-25 14:14:49 -05:00
|
|
|
##
|
|
|
|
# List of dependencies that could not be found in the configured sources.
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
attr_reader :missing
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-12-07 20:22:39 -05:00
|
|
|
attr_reader :stats
|
|
|
|
|
2014-09-13 23: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 04:59:02 -04:00
|
|
|
##
|
|
|
|
# When a missing dependency, don't stop. Just go on and record what was
|
|
|
|
# missing.
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
attr_accessor :soft_missing
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-11-21 18:27:30 -05: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 04:59:02 -04:00
|
|
|
def self.compose_sets *sets
|
2013-11-10 12:51:40 -05:00
|
|
|
sets.compact!
|
|
|
|
|
2013-11-21 18:27:30 -05:00
|
|
|
sets = sets.map do |set|
|
|
|
|
case set
|
2014-02-03 19:48:31 -05:00
|
|
|
when Gem::Resolver::BestSet then
|
|
|
|
set
|
2013-11-21 18:27:30 -05:00
|
|
|
when Gem::Resolver::ComposedSet then
|
|
|
|
set.sets
|
|
|
|
else
|
|
|
|
set
|
|
|
|
end
|
|
|
|
end.flatten
|
|
|
|
|
2013-11-10 12:51:40 -05: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-18 19:34:13 -05:00
|
|
|
Gem::Resolver::ComposedSet.new(*sets)
|
2013-11-10 12:51:40 -05:00
|
|
|
end
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
2013-09-13 15:58:57 -04:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
##
|
2013-11-25 14:14:49 -05:00
|
|
|
# Creates a Resolver that queries only against the already installed gems
|
|
|
|
# for the +needed+ dependencies.
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
def self.for_current_gems needed
|
2013-11-18 19:34:13 -05:00
|
|
|
new needed, Gem::Resolver::CurrentSet.new
|
2012-11-29 01:52:18 -05:00
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
##
|
2013-11-18 19:34:13 -05:00
|
|
|
# Create Resolver object which will resolve the tree starting
|
2013-10-17 20:41:36 -04:00
|
|
|
# with +needed+ Dependency objects.
|
2012-11-29 01:52:18 -05:00
|
|
|
#
|
2013-09-14 04:59:02 -04:00
|
|
|
# +set+ is an object that provides where to look for specifications to
|
2013-10-17 20:41:36 -04:00
|
|
|
# satisfy the Dependencies. This defaults to IndexSet, which will query
|
2013-09-14 04:59:02 -04:00
|
|
|
# rubygems.org.
|
|
|
|
|
|
|
|
def initialize needed, set = nil
|
2013-11-18 19:34:13 -05:00
|
|
|
@set = set || Gem::Resolver::IndexSet.new
|
2013-09-14 04:59:02 -04:00
|
|
|
@needed = needed
|
|
|
|
|
2013-12-17 20:02:58 -05:00
|
|
|
@development = false
|
2014-09-13 23:30:02 -04:00
|
|
|
@development_shallow = false
|
2013-12-17 20:02:58 -05:00
|
|
|
@ignore_dependencies = false
|
|
|
|
@missing = []
|
2014-09-13 23:30:02 -04:00
|
|
|
@skip_gems = {}
|
2013-12-17 20:02:58 -05:00
|
|
|
@soft_missing = false
|
|
|
|
@stats = Gem::Resolver::Stats.new
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
2013-09-13 15:58:57 -04:00
|
|
|
|
2013-11-25 14:14:49 -05:00
|
|
|
def explain stage, *data # :nodoc:
|
2014-09-13 23: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-18 19:34:13 -05:00
|
|
|
end
|
|
|
|
|
2014-09-13 23: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-21 18:27:30 -05:00
|
|
|
end
|
|
|
|
|
2013-11-10 12:51:40 -05: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-18 19:34:13 -05:00
|
|
|
explain :activate, [spec.full_name, possible.size]
|
2014-09-13 23:30:02 -04:00
|
|
|
explain :possible, possible
|
2013-11-18 19:34:13 -05:00
|
|
|
|
2013-11-10 12:51:40 -05:00
|
|
|
activation_request =
|
2013-11-18 19:34:13 -05:00
|
|
|
Gem::Resolver::ActivationRequest.new spec, dep, possible
|
2013-11-10 12:51:40 -05:00
|
|
|
|
|
|
|
return spec, activation_request
|
|
|
|
end
|
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
def requests s, act, reqs=[] # :nodoc:
|
2013-12-17 20:02:58 -05:00
|
|
|
return reqs if @ignore_dependencies
|
|
|
|
|
2014-09-13 23:30:02 -04:00
|
|
|
s.fetch_development_dependencies if @development
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
s.dependencies.reverse_each do |d|
|
|
|
|
next if d.type == :development and not @development
|
2014-09-13 23: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-01 17:50:14 -04:00
|
|
|
reqs << Gem::Resolver::DependencyRequest.new(d, act)
|
2013-12-07 20:22:39 -05:00
|
|
|
@stats.requirement!
|
2013-09-13 15:58:57 -04:00
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
@set.prefetch reqs
|
2013-09-13 15:58:57 -04:00
|
|
|
|
2013-12-07 20:22:39 -05:00
|
|
|
@stats.record_requirements reqs
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
reqs
|
|
|
|
end
|
2013-09-13 15:58:57 -04:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
include Molinillo::UI
|
2013-09-13 15:58:57 -04:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
def output
|
2015-07-01 20:51:50 -04:00
|
|
|
@output ||= debug? ? $stdout : File.open(Gem::Util::NULL_DEVICE, 'w')
|
2015-07-01 17:50:14 -04:00
|
|
|
end
|
2013-09-13 15:58:57 -04:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
def debug?
|
|
|
|
DEBUG_RESOLVER
|
|
|
|
end
|
2013-12-07 20:22:39 -05:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
include Molinillo::SpecificationProvider
|
2013-09-13 15:58:57 -04:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
##
|
|
|
|
# Proceed with resolution! Returns an array of ActivationRequest objects.
|
2013-09-13 15:58:57 -04:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
def resolve
|
|
|
|
locking_dg = Molinillo::DependencyGraph.new
|
2015-11-19 01:16:19 -05:00
|
|
|
Molinillo::Resolver.new(self, self).resolve(@needed.map { |d| DependencyRequest.new d, nil }, locking_dg).tsort.map(&:payload).compact
|
2015-07-01 17:50:14 -04: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 02:09:09 -04:00
|
|
|
ensure
|
2016-03-27 22:26:39 -04:00
|
|
|
@output.close if defined?(@output) and !debug?
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
2013-09-13 15:58:57 -04:00
|
|
|
|
2013-09-24 20:53:19 -04:00
|
|
|
##
|
2013-11-10 12:51:40 -05: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-24 20:53:19 -04:00
|
|
|
|
|
|
|
def find_possible dependency # :nodoc:
|
2013-11-10 12:51:40 -05:00
|
|
|
all = @set.find_all dependency
|
2014-09-13 23: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 12:51:40 -05:00
|
|
|
matching_platform = select_local_platforms all
|
|
|
|
|
|
|
|
return matching_platform, all
|
2013-09-24 20:53:19 -04:00
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
##
|
2015-07-01 17:50:14 -04:00
|
|
|
# Returns the gems in +specs+ that match the local platform.
|
2013-09-18 17:39:43 -04:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
def select_local_platforms specs # :nodoc:
|
|
|
|
specs.select do |spec|
|
|
|
|
Gem::Platform.installable? spec
|
2013-09-24 20:53:19 -04:00
|
|
|
end
|
|
|
|
end
|
2013-09-14 04:59:02 -04:00
|
|
|
|
2015-07-01 17:50:14 -04: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-24 20:53:19 -04:00
|
|
|
end
|
2016-09-27 21:16:43 -04:00
|
|
|
possibles.sort_by { |s| [s.source, s.version, Gem::Platform.local =~ s.platform ? 1 : 0] }.
|
|
|
|
map { |s| ActivationRequest.new s, dependency, [] }
|
2013-09-24 20:53:19 -04:00
|
|
|
end
|
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
def dependencies_for(specification)
|
|
|
|
return [] if @ignore_dependencies
|
|
|
|
spec = specification.spec
|
|
|
|
requests(spec, specification)
|
2013-09-24 20:53:19 -04:00
|
|
|
end
|
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
def requirement_satisfied_by?(requirement, activated, spec)
|
|
|
|
requirement.matches_spec? spec
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
2013-09-13 15:58:57 -04:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
def name_for(dependency)
|
|
|
|
dependency.name
|
|
|
|
end
|
2013-09-13 15:58:57 -04:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
def allow_missing?(dependency)
|
|
|
|
@missing << dependency
|
|
|
|
@soft_missing
|
2013-09-13 15:58:57 -04:00
|
|
|
end
|
2013-09-14 04:59:02 -04:00
|
|
|
|
2013-09-13 15:58:57 -04:00
|
|
|
end
|
2013-09-14 04:59:02 -04:00
|
|
|
|
2013-11-18 19:34:13 -05:00
|
|
|
##
|
|
|
|
# TODO remove in RubyGems 3
|
|
|
|
|
|
|
|
Gem::DependencyResolver = Gem::Resolver # :nodoc:
|
|
|
|
|
|
|
|
require 'rubygems/resolver/activation_request'
|
|
|
|
require 'rubygems/resolver/conflict'
|
|
|
|
require 'rubygems/resolver/dependency_request'
|
|
|
|
require 'rubygems/resolver/requirement_list'
|
2013-12-07 20:22:39 -05:00
|
|
|
require 'rubygems/resolver/stats'
|
2013-11-18 19:34:13 -05: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 07:43:26 -05:00
|
|
|
require 'rubygems/resolver/source_set'
|
2013-11-18 19:34:13 -05: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 14:14:49 -05:00
|
|
|
require 'rubygems/resolver/local_specification'
|
2013-12-07 20:22:39 -05:00
|
|
|
require 'rubygems/resolver/lock_specification'
|
2013-11-18 19:34:13 -05:00
|
|
|
require 'rubygems/resolver/vendor_specification'
|