2016-02-01 15:43:26 +03:00
|
|
|
# frozen_string_literal: true
|
2023-03-17 12:36:42 +03:00
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
#--
|
|
|
|
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
|
|
|
|
# All rights reserved.
|
|
|
|
# See LICENSE.txt for permissions.
|
|
|
|
#++
|
|
|
|
|
2021-10-21 22:23:08 +03:00
|
|
|
require_relative "tsort"
|
2019-04-22 14:56:16 +03:00
|
|
|
require_relative "deprecate"
|
2007-11-10 10:48:56 +03:00
|
|
|
|
2010-02-22 05:52:35 +03:00
|
|
|
##
|
|
|
|
# Gem::DependencyList is used for installing and uninstalling gems in the
|
|
|
|
# correct order to avoid conflicts.
|
2012-11-29 10:52:18 +04:00
|
|
|
#--
|
|
|
|
# TODO: It appears that all but topo-sort functionality is being duplicated
|
|
|
|
# (or is planned to be duplicated) elsewhere in rubygems. Is the majority of
|
|
|
|
# this class necessary anymore? Especially #ok?, #why_not_ok?
|
2010-02-22 05:52:35 +03:00
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
class Gem::DependencyList
|
2011-03-01 12:41:32 +03:00
|
|
|
attr_reader :specs
|
2007-11-10 10:48:56 +03:00
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
include Enumerable
|
2021-10-21 22:23:08 +03:00
|
|
|
include Gem::TSort
|
2007-11-10 10:48:56 +03:00
|
|
|
|
2010-02-22 05:52:35 +03:00
|
|
|
##
|
|
|
|
# Allows enabling/disabling use of development dependencies
|
|
|
|
|
|
|
|
attr_accessor :development
|
2007-11-10 10:48:56 +03:00
|
|
|
|
2010-02-22 05:52:35 +03:00
|
|
|
##
|
2011-06-01 07:45:05 +04:00
|
|
|
# Creates a DependencyList from the current specs.
|
2010-02-22 05:52:35 +03:00
|
|
|
|
2011-06-01 07:45:05 +04:00
|
|
|
def self.from_specs
|
2010-02-22 05:52:35 +03:00
|
|
|
list = new
|
2012-11-29 10:52:18 +04:00
|
|
|
list.add(*Gem::Specification.to_a)
|
2011-06-01 07:45:05 +04:00
|
|
|
list
|
|
|
|
end
|
2010-02-22 05:52:35 +03:00
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new DependencyList. If +development+ is true, development
|
|
|
|
# dependencies will be included.
|
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
def initialize(development = false)
|
2007-11-10 10:48:56 +03:00
|
|
|
@specs = []
|
2010-02-22 05:52:35 +03:00
|
|
|
|
|
|
|
@development = development
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
##
|
2007-11-10 10:48:56 +03:00
|
|
|
# Adds +gemspecs+ to the dependency list.
|
2009-06-10 01:38:59 +04:00
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
def add(*gemspecs)
|
2013-02-28 03:20:57 +04:00
|
|
|
@specs.concat gemspecs
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
|
2011-03-01 12:41:32 +03:00
|
|
|
def clear
|
|
|
|
@specs.clear
|
|
|
|
end
|
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
##
|
2010-02-22 05:52:35 +03:00
|
|
|
# Return a list of the gem specifications in the dependency list, sorted in
|
|
|
|
# order so that no gemspec in the list depends on a gemspec earlier in the
|
|
|
|
# list.
|
2007-11-10 10:48:56 +03:00
|
|
|
#
|
2009-06-10 01:38:59 +04:00
|
|
|
# This is useful when removing gems from a set of installed gems. By
|
|
|
|
# removing them in the returned order, you don't get into as many dependency
|
|
|
|
# issues.
|
2007-11-10 10:48:56 +03:00
|
|
|
#
|
2009-06-10 01:38:59 +04:00
|
|
|
# If there are circular dependencies (yuck!), then gems will be returned in
|
|
|
|
# order until only the circular dependents and anything they reference are
|
|
|
|
# left. Then arbitrary gemspecs will be returned until the circular
|
|
|
|
# dependency is broken, after which gems will be returned in dependency
|
|
|
|
# order again.
|
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
def dependency_order
|
|
|
|
sorted = strongly_connected_components.flatten
|
|
|
|
|
|
|
|
result = []
|
|
|
|
seen = {}
|
|
|
|
|
|
|
|
sorted.each do |spec|
|
2018-11-21 13:20:47 +03:00
|
|
|
if index = seen[spec.name]
|
|
|
|
if result[index].version < spec.version
|
2007-11-10 10:48:56 +03:00
|
|
|
result[index] = spec
|
|
|
|
end
|
|
|
|
else
|
|
|
|
seen[spec.name] = result.length
|
|
|
|
result << spec
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result.reverse
|
|
|
|
end
|
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
##
|
|
|
|
# Iterator over dependency_order
|
|
|
|
|
|
|
|
def each(&block)
|
|
|
|
dependency_order.each(&block)
|
|
|
|
end
|
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
def find_name(full_name)
|
2020-06-10 20:46:05 +03:00
|
|
|
@specs.find {|spec| spec.full_name == full_name }
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
|
2010-02-22 05:52:35 +03:00
|
|
|
def inspect # :nodoc:
|
2023-04-07 03:15:17 +03:00
|
|
|
format("%s %p>", super[0..-2], map(&:full_name))
|
2010-02-22 05:52:35 +03:00
|
|
|
end
|
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
##
|
2007-11-10 10:48:56 +03:00
|
|
|
# Are all the dependencies in the list satisfied?
|
2009-06-10 01:38:59 +04:00
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
def ok?
|
2011-03-01 12:41:32 +03:00
|
|
|
why_not_ok?(:quick).empty?
|
|
|
|
end
|
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
def why_not_ok?(quick = false)
|
2020-06-10 20:46:05 +03:00
|
|
|
unsatisfied = Hash.new {|h,k| h[k] = [] }
|
2011-03-10 01:32:29 +03:00
|
|
|
each do |spec|
|
2011-03-01 12:41:32 +03:00
|
|
|
spec.runtime_dependencies.each do |dep|
|
2019-02-14 15:59:03 +03:00
|
|
|
inst = Gem::Specification.any? do |installed_spec|
|
2022-08-09 05:16:07 +03:00
|
|
|
dep.name == installed_spec.name &&
|
|
|
|
dep.requirement.satisfied_by?(installed_spec.version)
|
2019-02-14 15:59:03 +03:00
|
|
|
end
|
2011-03-01 12:41:32 +03:00
|
|
|
|
2022-08-09 05:16:07 +03:00
|
|
|
unless inst || @specs.find {|s| s.satisfies_requirement? dep }
|
2011-03-01 12:41:32 +03:00
|
|
|
unsatisfied[spec.name] << dep
|
|
|
|
return unsatisfied if quick
|
|
|
|
end
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
end
|
2011-06-01 07:45:05 +04:00
|
|
|
|
2011-03-01 12:41:32 +03:00
|
|
|
unsatisfied
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
##
|
2019-03-05 06:32:58 +03:00
|
|
|
# It is ok to remove a gemspec from the dependency list?
|
2007-11-10 10:48:56 +03:00
|
|
|
#
|
2009-06-10 01:38:59 +04:00
|
|
|
# If removing the gemspec creates breaks a currently ok dependency, then it
|
2010-02-22 05:52:35 +03:00
|
|
|
# is NOT ok to remove the gemspec.
|
2009-06-10 01:38:59 +04:00
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
def ok_to_remove?(full_name, check_dev=true)
|
2007-11-10 10:48:56 +03:00
|
|
|
gem_to_remove = find_name full_name
|
|
|
|
|
2015-07-02 00:50:14 +03:00
|
|
|
# If the state is inconsistent, at least don't crash
|
|
|
|
return true unless gem_to_remove
|
|
|
|
|
2019-02-14 15:59:03 +03:00
|
|
|
siblings = @specs.find_all do |s|
|
2007-11-10 10:48:56 +03:00
|
|
|
s.name == gem_to_remove.name &&
|
|
|
|
s.full_name != gem_to_remove.full_name
|
2019-02-14 15:59:03 +03:00
|
|
|
end
|
2007-11-10 10:48:56 +03:00
|
|
|
|
|
|
|
deps = []
|
|
|
|
|
|
|
|
@specs.each do |spec|
|
2012-11-29 10:52:18 +04:00
|
|
|
check = check_dev ? spec.dependencies : spec.runtime_dependencies
|
|
|
|
|
|
|
|
check.each do |dep|
|
2007-11-10 10:48:56 +03:00
|
|
|
deps << dep if gem_to_remove.satisfies_requirement?(dep)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-14 15:59:03 +03:00
|
|
|
deps.all? do |dep|
|
|
|
|
siblings.any? do |s|
|
2007-11-10 10:48:56 +03:00
|
|
|
s.satisfies_requirement? dep
|
2019-02-14 15:59:03 +03:00
|
|
|
end
|
|
|
|
end
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
|
2011-03-01 12:41:32 +03:00
|
|
|
##
|
|
|
|
# Remove everything in the DependencyList that matches but doesn't
|
|
|
|
# satisfy items in +dependencies+ (a hash of gem names to arrays of
|
|
|
|
# dependencies).
|
|
|
|
|
2018-11-21 13:20:47 +03:00
|
|
|
def remove_specs_unsatisfied_by(dependencies)
|
2019-02-14 15:59:03 +03:00
|
|
|
specs.reject! do |spec|
|
2011-03-01 12:41:32 +03:00
|
|
|
dep = dependencies[spec.name]
|
2022-08-09 05:16:07 +03:00
|
|
|
dep && !dep.requirement.satisfied_by?(spec.version)
|
2019-02-14 15:59:03 +03:00
|
|
|
end
|
2011-03-01 12:41:32 +03:00
|
|
|
end
|
|
|
|
|
2010-02-22 05:52:35 +03:00
|
|
|
##
|
|
|
|
# Removes the gemspec matching +full_name+ from the dependency list
|
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
def remove_by_name(full_name)
|
2020-06-10 20:46:05 +03:00
|
|
|
@specs.delete_if {|spec| spec.full_name == full_name }
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
##
|
|
|
|
# Return a hash of predecessors. <tt>result[spec]</tt> is an Array of
|
2010-02-22 05:52:35 +03:00
|
|
|
# gemspecs that have a dependency satisfied by the named gemspec.
|
2009-06-10 01:38:59 +04:00
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
def spec_predecessors
|
2020-06-10 20:46:05 +03:00
|
|
|
result = Hash.new {|h,k| h[k] = [] }
|
2007-11-10 10:48:56 +03:00
|
|
|
|
|
|
|
specs = @specs.sort.reverse
|
|
|
|
|
|
|
|
specs.each do |spec|
|
|
|
|
specs.each do |other|
|
|
|
|
next if spec == other
|
|
|
|
|
|
|
|
other.dependencies.each do |dep|
|
2018-11-21 13:20:47 +03:00
|
|
|
if spec.satisfies_requirement? dep
|
2007-11-10 10:48:56 +03:00
|
|
|
result[spec] << other
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def tsort_each_node(&block)
|
|
|
|
@specs.each(&block)
|
|
|
|
end
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
def tsort_each_child(node)
|
2007-11-10 10:48:56 +03:00
|
|
|
specs = @specs.sort.reverse
|
|
|
|
|
2010-02-22 05:52:35 +03:00
|
|
|
dependencies = node.runtime_dependencies
|
|
|
|
dependencies.push(*node.development_dependencies) if @development
|
|
|
|
|
|
|
|
dependencies.each do |dep|
|
2007-11-10 10:48:56 +03:00
|
|
|
specs.each do |spec|
|
2018-11-21 13:20:47 +03:00
|
|
|
if spec.satisfies_requirement? dep
|
2014-12-07 03:53:01 +03:00
|
|
|
yield spec
|
2007-11-10 10:48:56 +03:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
##
|
2007-11-10 10:48:56 +03:00
|
|
|
# Count the number of gemspecs in the list +specs+ that are not in
|
|
|
|
# +ignored+.
|
2009-06-10 01:38:59 +04:00
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
def active_count(specs, ignored)
|
2020-06-10 20:46:05 +03:00
|
|
|
specs.count {|spec| ignored[spec.full_name].nil? }
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
2011-06-01 07:45:05 +04:00
|
|
|
end
|