2007-11-10 02:48:56 -05:00
|
|
|
#--
|
|
|
|
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
|
|
|
|
# All rights reserved.
|
|
|
|
# See LICENSE.txt for permissions.
|
|
|
|
#++
|
|
|
|
|
|
|
|
require 'tsort'
|
2011-05-31 23:45:05 -04:00
|
|
|
require 'rubygems/deprecate'
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
##
|
|
|
|
# Gem::DependencyList is used for installing and uninstalling gems in the
|
|
|
|
# correct order to avoid conflicts.
|
2012-11-29 01:52:18 -05: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-21 21:52:35 -05:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
class Gem::DependencyList
|
2011-03-01 04:41:32 -05:00
|
|
|
attr_reader :specs
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
include Enumerable
|
2007-11-10 02:48:56 -05:00
|
|
|
include TSort
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
##
|
|
|
|
# Allows enabling/disabling use of development dependencies
|
|
|
|
|
|
|
|
attr_accessor :development
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
##
|
2011-05-31 23:45:05 -04:00
|
|
|
# Creates a DependencyList from the current specs.
|
2010-02-21 21:52:35 -05:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
def self.from_specs
|
2010-02-21 21:52:35 -05:00
|
|
|
list = new
|
2012-11-29 01:52:18 -05:00
|
|
|
list.add(*Gem::Specification.to_a)
|
2011-05-31 23:45:05 -04:00
|
|
|
list
|
|
|
|
end
|
2010-02-21 21:52:35 -05:00
|
|
|
|
|
|
|
##
|
|
|
|
# Creates a new DependencyList. If +development+ is true, development
|
|
|
|
# dependencies will be included.
|
|
|
|
|
|
|
|
def initialize development = false
|
2007-11-10 02:48:56 -05:00
|
|
|
@specs = []
|
2010-02-21 21:52:35 -05:00
|
|
|
|
|
|
|
@development = development
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
2007-11-10 02:48:56 -05:00
|
|
|
# Adds +gemspecs+ to the dependency list.
|
2009-06-09 17:38:59 -04:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def add(*gemspecs)
|
2013-02-27 18:20:57 -05:00
|
|
|
@specs.concat gemspecs
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2011-03-01 04:41:32 -05:00
|
|
|
def clear
|
|
|
|
@specs.clear
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
2010-02-21 21:52:35 -05: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 02:48:56 -05:00
|
|
|
#
|
2009-06-09 17: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 02:48:56 -05:00
|
|
|
#
|
2009-06-09 17: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 02:48:56 -05:00
|
|
|
def dependency_order
|
|
|
|
sorted = strongly_connected_components.flatten
|
|
|
|
|
|
|
|
result = []
|
|
|
|
seen = {}
|
|
|
|
|
|
|
|
sorted.each do |spec|
|
|
|
|
if index = seen[spec.name] then
|
|
|
|
if result[index].version < spec.version then
|
|
|
|
result[index] = spec
|
|
|
|
end
|
|
|
|
else
|
|
|
|
seen[spec.name] = result.length
|
|
|
|
result << spec
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result.reverse
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Iterator over dependency_order
|
|
|
|
|
|
|
|
def each(&block)
|
|
|
|
dependency_order.each(&block)
|
|
|
|
end
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def find_name(full_name)
|
|
|
|
@specs.find { |spec| spec.full_name == full_name }
|
|
|
|
end
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
def inspect # :nodoc:
|
|
|
|
"#<%s:0x%x %p>" % [self.class, object_id, map { |s| s.full_name }]
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
2007-11-10 02:48:56 -05:00
|
|
|
# Are all the dependencies in the list satisfied?
|
2009-06-09 17:38:59 -04:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def ok?
|
2011-03-01 04:41:32 -05:00
|
|
|
why_not_ok?(:quick).empty?
|
|
|
|
end
|
|
|
|
|
|
|
|
def why_not_ok? quick = false
|
|
|
|
unsatisfied = Hash.new { |h,k| h[k] = [] }
|
2011-03-09 17:32:29 -05:00
|
|
|
each do |spec|
|
2011-03-01 04:41:32 -05:00
|
|
|
spec.runtime_dependencies.each do |dep|
|
2011-05-31 23:45:05 -04:00
|
|
|
inst = Gem::Specification.any? { |installed_spec|
|
2011-03-01 04:41:32 -05:00
|
|
|
dep.name == installed_spec.name and
|
|
|
|
dep.requirement.satisfied_by? installed_spec.version
|
|
|
|
}
|
|
|
|
|
|
|
|
unless inst or @specs.find { |s| s.satisfies_requirement? dep } then
|
|
|
|
unsatisfied[spec.name] << dep
|
|
|
|
return unsatisfied if quick
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
end
|
2011-05-31 23:45:05 -04:00
|
|
|
|
2011-03-01 04:41:32 -05:00
|
|
|
unsatisfied
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
2010-02-21 21:52:35 -05:00
|
|
|
# Is is ok to remove a gemspec from the dependency list?
|
2007-11-10 02:48:56 -05:00
|
|
|
#
|
2009-06-09 17:38:59 -04:00
|
|
|
# If removing the gemspec creates breaks a currently ok dependency, then it
|
2010-02-21 21:52:35 -05:00
|
|
|
# is NOT ok to remove the gemspec.
|
2009-06-09 17:38:59 -04:00
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def ok_to_remove?(full_name, check_dev=true)
|
2007-11-10 02:48:56 -05:00
|
|
|
gem_to_remove = find_name full_name
|
|
|
|
|
|
|
|
siblings = @specs.find_all { |s|
|
|
|
|
s.name == gem_to_remove.name &&
|
|
|
|
s.full_name != gem_to_remove.full_name
|
|
|
|
}
|
|
|
|
|
|
|
|
deps = []
|
|
|
|
|
|
|
|
@specs.each do |spec|
|
2012-11-29 01:52:18 -05:00
|
|
|
check = check_dev ? spec.dependencies : spec.runtime_dependencies
|
|
|
|
|
|
|
|
check.each do |dep|
|
2007-11-10 02:48:56 -05:00
|
|
|
deps << dep if gem_to_remove.satisfies_requirement?(dep)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
deps.all? { |dep|
|
|
|
|
siblings.any? { |s|
|
|
|
|
s.satisfies_requirement? dep
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2011-03-01 04:41:32 -05:00
|
|
|
##
|
|
|
|
# Remove everything in the DependencyList that matches but doesn't
|
|
|
|
# satisfy items in +dependencies+ (a hash of gem names to arrays of
|
|
|
|
# dependencies).
|
|
|
|
|
|
|
|
def remove_specs_unsatisfied_by dependencies
|
|
|
|
specs.reject! { |spec|
|
|
|
|
dep = dependencies[spec.name]
|
|
|
|
dep and not dep.requirement.satisfied_by? spec.version
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
##
|
|
|
|
# Removes the gemspec matching +full_name+ from the dependency list
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def remove_by_name(full_name)
|
|
|
|
@specs.delete_if { |spec| spec.full_name == full_name }
|
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# Return a hash of predecessors. <tt>result[spec]</tt> is an Array of
|
2010-02-21 21:52:35 -05:00
|
|
|
# gemspecs that have a dependency satisfied by the named gemspec.
|
2009-06-09 17:38:59 -04:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def spec_predecessors
|
|
|
|
result = Hash.new { |h,k| h[k] = [] }
|
|
|
|
|
|
|
|
specs = @specs.sort.reverse
|
|
|
|
|
|
|
|
specs.each do |spec|
|
|
|
|
specs.each do |other|
|
|
|
|
next if spec == other
|
|
|
|
|
|
|
|
other.dependencies.each do |dep|
|
|
|
|
if spec.satisfies_requirement? dep then
|
|
|
|
result[spec] << other
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
def tsort_each_node(&block)
|
|
|
|
@specs.each(&block)
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
def tsort_each_child(node)
|
2007-11-10 02:48:56 -05:00
|
|
|
specs = @specs.sort.reverse
|
|
|
|
|
2010-02-21 21:52:35 -05:00
|
|
|
dependencies = node.runtime_dependencies
|
|
|
|
dependencies.push(*node.development_dependencies) if @development
|
|
|
|
|
|
|
|
dependencies.each do |dep|
|
2007-11-10 02:48:56 -05:00
|
|
|
specs.each do |spec|
|
|
|
|
if spec.satisfies_requirement? dep then
|
|
|
|
begin
|
|
|
|
yield spec
|
|
|
|
rescue TSort::Cyclic
|
2011-01-18 19:08:49 -05:00
|
|
|
# do nothing
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
2007-11-10 02:48:56 -05:00
|
|
|
# Count the number of gemspecs in the list +specs+ that are not in
|
|
|
|
# +ignored+.
|
2009-06-09 17:38:59 -04:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def active_count(specs, ignored)
|
2011-01-18 19:08:49 -05:00
|
|
|
specs.count { |spec| ignored[spec.full_name].nil? }
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
end
|
2012-11-29 01:52:18 -05:00
|
|
|
|