class Gem::DependencyList
Gem::DependencyList 用于按正确的顺序安装和卸载 gem,以避免冲突。
属性
允许启用/禁用开发依赖项的使用
Public Class Methods
Source
# File lib/rubygems/dependency_list.rb, line 33 def self.from_specs list = new list.add(*Gem::Specification.to_a) list end
从当前 spec 创建一个 DependencyList。
Source
# File lib/rubygems/dependency_list.rb, line 43 def initialize(development = false) @specs = [] @development = development end
创建一个新的 DependencyList。如果 development 为 true,则会包含开发依赖项。
Public Instance Methods
Source
# File lib/rubygems/dependency_list.rb, line 52 def add(*gemspecs) @specs.concat gemspecs end
将 gemspecs 添加到依赖项列表中。
Source
# File lib/rubygems/dependency_list.rb, line 75 def dependency_order sorted = strongly_connected_components.flatten result = [] seen = {} sorted.each do |spec| if index = seen[spec.name] if result[index].version < spec.version result[index] = spec end else seen[spec.name] = result.length result << spec end end result.reverse end
返回依赖项列表中 gem spec 的列表,按顺序排序,以便列表中的任何 gemspec 都不依赖于列表前面的 gemspec。
这在从一组已安装的 gem 中删除 gem 时非常有用。通过按返回的顺序删除它们,可以减少很多依赖项问题。
如果存在循环依赖(令人讨厌!),则会按顺序返回 gem,直到只剩下循环依赖项以及它们引用的任何内容。然后将返回任意 gemspec,直到打破循环依赖,之后将再次按依赖顺序返回 gem。
Source
# File lib/rubygems/dependency_list.rb, line 98 def each(&block) dependency_order.each(&block) end
Source
# File lib/rubygems/dependency_list.rb, line 102 def find_name(full_name) @specs.find {|spec| spec.full_name == full_name } end
Source
# File lib/rubygems/dependency_list.rb, line 113 def ok? why_not_ok?(:quick).empty? end
列表中的所有依赖项都已满足吗?
Source
# File lib/rubygems/dependency_list.rb, line 142 def ok_to_remove?(full_name, check_dev = true) gem_to_remove = find_name full_name # If the state is inconsistent, at least don't crash return true unless gem_to_remove siblings = @specs.find_all do |s| s.name == gem_to_remove.name && s.full_name != gem_to_remove.full_name end deps = [] @specs.each do |spec| check = check_dev ? spec.dependencies : spec.runtime_dependencies check.each do |dep| deps << dep if gem_to_remove.satisfies_requirement?(dep) end end deps.all? do |dep| siblings.any? do |s| s.satisfies_requirement? dep end end end
可以从依赖项列表中删除 gemspec 吗?
如果删除 gemspec 会破坏当前已满足的依赖项,那么就不能删除该 gemspec。
Source
# File lib/rubygems/dependency_list.rb, line 185 def remove_by_name(full_name) @specs.delete_if {|spec| spec.full_name == full_name } end
从依赖项列表中删除与 full_name 匹配的 gemspec
Source
# File lib/rubygems/dependency_list.rb, line 175 def remove_specs_unsatisfied_by(dependencies) specs.reject! do |spec| dep = dependencies[spec.name] dep && !dep.requirement.satisfied_by?(spec.version) end end
移除 DependencyList 中匹配但未满足 dependencies(一个 gem 名称到依赖项数组的哈希)的项。
Source
# File lib/rubygems/dependency_list.rb, line 193 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 result[spec] << other end end end end result end
返回一个前驱哈希。result[spec] 是一个 Array,其中包含由命名 gemspec 满足的依赖项的 gemspec。
Source
# File lib/rubygems/dependency_list.rb, line 217 def tsort_each_child(node) specs = @specs.sort.reverse dependencies = node.runtime_dependencies dependencies.push(*node.development_dependencies) if @development dependencies.each do |dep| specs.each do |spec| if spec.satisfies_requirement? dep yield spec break end end end end
Source
# File lib/rubygems/dependency_list.rb, line 213 def tsort_each_node(&block) @specs.each(&block) end
Source
# File lib/rubygems/dependency_list.rb, line 117 def why_not_ok?(quick = false) unsatisfied = Hash.new {|h,k| h[k] = [] } each do |spec| spec.runtime_dependencies.each do |dep| inst = Gem::Specification.any? do |installed_spec| dep.name == installed_spec.name && dep.requirement.satisfied_by?(installed_spec.version) end unless inst || @specs.find {|s| s.satisfies_requirement? dep } unsatisfied[spec.name] << dep return unsatisfied if quick end end end unsatisfied end
私有实例方法
Source
# File lib/rubygems/dependency_list.rb, line 239 def active_count(specs, ignored) specs.count {|spec| ignored[spec.full_name].nil? } end
计算列表 specs 中不在 ignored 中的 gemspec 的数量。