class Bundler::PubGrub::BasicPackageSource
类型
在可能的情况下,Bundler::PubGrub
将接受用户定义的类型,只要它们符合“鸭子类型”原则。
## “包”
此类将用于表示正在求解的各种包。.to_s 将在显示错误和调试信息时调用,它应该返回包的名称。它还必须对 == 和 hash 有合理的定义。
示例类:String(“rails”)
## “版本”
此类将用于表示单个版本号。
版本不需要存储其关联的包,但它们只会与同一包的其他版本进行比较。
它必须是可比较的(并合理地实现 <=>)
示例类:Gem::Version,Integer
## “依赖”
此类表示一个包对另一个包的要求。它由 dependencies_for
(package, version) 返回,并将传递给 parse_dependency
以将其转换为 Bundler::PubGrub
理解的格式。
它还必须对 == 有合理的定义。
示例类:String(“~> 1.0”),Gem::Requirement
公共类方法
# File bundler/vendor/pub_grub/lib/pub_grub/basic_package_source.rb, line 92 def initialize @root_package = Package.root @root_version = Package.root_version @cached_versions = Hash.new do |h,k| if k == @root_package h[k] = [@root_version] else h[k] = all_versions_for(k) end end @sorted_versions = Hash.new { |h,k| h[k] = @cached_versions[k].sort } @version_indexes = Hash.new { |h,k| h[k] = @cached_versions[k].each.with_index.to_h } @cached_dependencies = Hash.new do |packages, package| if package == @root_package packages[package] = { @root_version => root_dependencies } else packages[package] = Hash.new do |versions, version| versions[version] = dependencies_for(package, version) end end end end
公共实例方法
覆盖我!
为每个包调用此方法,以查找包的所有可能版本。
每个包最多调用一次
返回:包的版本数组,按选择的优先顺序排列
# File bundler/vendor/pub_grub/lib/pub_grub/basic_package_source.rb, line 49 def all_versions_for(package) raise NotImplementedError end
覆盖我!
返回:形式为 { package => requirement, … } 的哈希
# File bundler/vendor/pub_grub/lib/pub_grub/basic_package_source.rb, line 56 def dependencies_for(package, version) raise NotImplementedError end
# File bundler/vendor/pub_grub/lib/pub_grub/basic_package_source.rb, line 137 def incompatibilities_for(package, version) package_deps = @cached_dependencies[package] sorted_versions = @sorted_versions[package] package_deps[version].map do |dep_package, dep_constraint_name| low = high = sorted_versions.index(version) # find version low such that all >= low share the same dep while low > 0 && package_deps[sorted_versions[low - 1]][dep_package] == dep_constraint_name low -= 1 end low = if low == 0 nil else sorted_versions[low] end # find version high such that all < high share the same dep while high < sorted_versions.length && package_deps[sorted_versions[high]][dep_package] == dep_constraint_name high += 1 end high = if high == sorted_versions.length nil else sorted_versions[high] end range = VersionRange.new(min: low, max: high, include_min: true) self_constraint = VersionConstraint.new(package, range: range) if !@packages.include?(dep_package) # no such package -> this version is invalid end dep_constraint = parse_dependency(dep_package, dep_constraint_name) if !dep_constraint # falsey indicates this dependency was invalid cause = Bundler::PubGrub::Incompatibility::InvalidDependency.new(dep_package, dep_constraint_name) return [Incompatibility.new([Term.new(self_constraint, true)], cause: cause)] elsif !dep_constraint.is_a?(VersionConstraint) # Upgrade range/union to VersionConstraint dep_constraint = VersionConstraint.new(dep_package, range: dep_constraint) end Incompatibility.new([Term.new(self_constraint, true), Term.new(dep_constraint, false)], cause: :dependency) end end
# File bundler/vendor/pub_grub/lib/pub_grub/basic_package_source.rb, line 131 def no_versions_incompatibility_for(_package, unsatisfied_term) cause = Incompatibility::NoVersions.new(unsatisfied_term) Incompatibility.new([unsatisfied_term], cause: cause) end
覆盖我!
将(用户定义的)依赖项转换为 Bundler::PubGrub
理解的格式。
Package
传递给此方法,但对于许多实现来说不是必需的。
返回:Bundler::PubGrub::VersionRange
、Bundler::PubGrub::VersionUnion
或一个
Bundler::PubGrub::VersionConstraint
# File bundler/vendor/pub_grub/lib/pub_grub/basic_package_source.rb, line 69 def parse_dependency(package, dependency) raise NotImplementedError end
覆盖我!
如果未覆盖,这将使用根包调用 dependencies_for
。
返回:形式为 { package => requirement, … } 的哈希(请参阅 dependencies_for
)
# File bundler/vendor/pub_grub/lib/pub_grub/basic_package_source.rb, line 78 def root_dependencies dependencies_for(@root_package, @root_version) end
覆盖我(也许)
如果未覆盖,将使用 all_versions_for
返回的顺序
返回:按优先顺序排列的版本数组
# File bundler/vendor/pub_grub/lib/pub_grub/basic_package_source.rb, line 87 def sort_versions_by_preferred(package, sorted_versions) indexes = @version_indexes[package] sorted_versions.sort_by { |version| indexes[version] } end
# File bundler/vendor/pub_grub/lib/pub_grub/basic_package_source.rb, line 119 def versions_for(package, range=VersionRange.any) versions = range.select_versions(@sorted_versions[package]) # Conditional avoids (among other things) calling # sort_versions_by_preferred with the root package if versions.size > 1 sort_versions_by_preferred(package, versions) else versions end end