| 1 |
# frozen_string_literal: true |
| 2 |
|
| 3 |
require_relative "spec_group" |
| 4 |
|
| 5 |
module Bundler |
| 6 |
class Resolver |
| 7 |
# |
| 8 |
# This class is a PubGrub compatible "Version" class that takes Bundler |
| 9 |
# resolution complexities into account. |
| 10 |
# |
| 11 |
# Each Resolver::Candidate has a underlying `Gem::Version` plus a set of |
| 12 |
# platforms. For example, 1.1.0-x86_64-linux is a different resolution candidate |
| 13 |
# from 1.1.0 (generic). This is because different platform variants of the |
| 14 |
# same gem version can bring different dependencies, so they need to be |
| 15 |
# considered separately. |
| 16 |
# |
| 17 |
# Some candidates may also keep some information explicitly about the |
| 18 |
# package they refer to. These candidates are referred to as "canonical" and |
| 19 |
# are used when materializing resolution results back into RubyGems |
| 20 |
# specifications that can be installed, written to lock files, and so on. |
| 21 |
# |
| 22 |
class Candidate |
| 23 |
include Comparable |
| 24 |
|
| 25 |
attr_reader :version |
| 26 |
|
| 27 |
def initialize(version, group: nil, priority: -1) |
| 28 |
@spec_group = group || SpecGroup.new([]) |
| 29 |
@version = Gem::Version.new(version) |
| 30 |
@priority = priority |
| 31 |
end |
| 32 |
|
| 33 |
def dependencies |
| 34 |
@spec_group.dependencies |
| 35 |
end |
| 36 |
|
| 37 |
def to_specs(package) |
| 38 |
return [] if package.meta? |
| 39 |
|
| 40 |
@spec_group.to_specs(package.force_ruby_platform?) |
| 41 |
end |
| 42 |
|
| 43 |
def prerelease? |
| 44 |
@version.prerelease? |
| 45 |
end |
| 46 |
|
| 47 |
def segments |
| 48 |
@version.segments |
| 49 |
end |
| 50 |
|
| 51 |
def sort_obj |
| 52 |
[@version, @priority] |
| 53 |
end |
| 54 |
|
| 55 |
def <=>(other) |
| 56 |
return unless other.is_a?(self.class) |
| 57 |
|
| 58 |
sort_obj <=> other.sort_obj |
| 59 |
end |
| 60 |
|
| 61 |
def ==(other) |
| 62 |
return unless other.is_a?(self.class) |
| 63 |
|
| 64 |
sort_obj == other.sort_obj |
| 65 |
end |
| 66 |
|
| 67 |
def eql?(other) |
| 68 |
return unless other.is_a?(self.class) |
| 69 |
|
| 70 |
sort_obj.eql?(other.sort_obj) |
| 71 |
end |
| 72 |
|
| 73 |
def hash |
| 74 |
sort_obj.hash |
| 75 |
end |
| 76 |
|
| 77 |
def to_s |
| 78 |
@version.to_s |
| 79 |
end |
| 80 |
end |
| 81 |
end |
| 82 |
end |