123456789_123456789_123456789_123456789_123456789_

Class: Bundler::PubGrub::Incompatibility

Relationships & Source Files
Namespace Children
Classes:
Extension / Inclusion / Inheritance Descendants
Subclasses:
Inherits: Object
Defined in: lib/bundler/vendor/pub_grub/lib/pub_grub/incompatibility.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(terms, cause:, custom_explanation: nil) ⇒ Incompatibility

[ GitHub ]

  
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/incompatibility.rb', line 16

def initialize(terms, cause:, custom_explanation: nil)
  @cause = cause
  @terms = cleanup_terms(terms)
  @custom_explanation = custom_explanation

  if cause == :dependency && @terms.length != 2
    raise ArgumentError, "a dependency Incompatibility must have exactly two terms. Got #{@terms.inspect}"
  end
end

Instance Attribute Details

#cause (readonly)

[ GitHub ]

  
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/incompatibility.rb', line 14

attr_reader :terms, :cause

#conflict?Boolean (readonly)

[ GitHub ]

  
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/incompatibility.rb', line 39

def conflict?
  ConflictCause === cause
end

#failure?Boolean (readonly)

[ GitHub ]

  
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/incompatibility.rb', line 35

def failure?
  terms.empty? || (terms.length == 1 && Package.root?(terms[0].package) && terms[0].positive?)
end

#terms (readonly)

[ GitHub ]

  
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/incompatibility.rb', line 14

attr_reader :terms, :cause

Instance Method Details

#cleanup_terms(terms) (private)

[ GitHub ]

  
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/incompatibility.rb', line 128

def cleanup_terms(terms)
  terms.each do |term|
    raise "#{term.inspect} must be a term" unless term.is_a?(Term)
  end

  if terms.length != 1 && ConflictCause === cause
    terms = terms.reject do |term|
      term.positive? && Package.root?(term.package)
    end
  end

  # Optimized simple cases
  return terms if terms.length <= 1
  return terms if terms.length == 2 && terms[0].package != terms[1].package

  terms.group_by(&:package).map do |package, common_terms|
    common_terms.inject do |acc, term|
      acc.intersect(term)
    end
  end
end

#eql?(other) ⇒ Boolean

[ GitHub ]

  
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/incompatibility.rb', line 30

def eql?(other)
  cause.eql?(other.cause) &&
    terms.eql?(other.terms)
end

#external_incompatibilities

Returns all external incompatibilities in this incompatibility’s derivation graph

[ GitHub ]

  
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/incompatibility.rb', line 45

def external_incompatibilities
  if conflict?
    [
      cause.conflict,
      cause.other
    ].flat_map(&:external_incompatibilities)
  else
    [this]
  end
end

#hash

[ GitHub ]

  
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/incompatibility.rb', line 26

def hash
  cause.hash ^ terms.hash
end

#inspect

[ GitHub ]

  
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/incompatibility.rb', line 111

def inspect
  "#<#{self.class} #{to_s}>"
end

#pretty_print(q)

[ GitHub ]

  
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/incompatibility.rb', line 115

def pretty_print(q)
  q.group 2, "#<#{self.class}", ">" do
    q.breakable
    q.text to_s

    q.breakable
    q.text " caused by "
    q.pp @cause
  end
end

#to_s

[ GitHub ]

  
# File 'lib/bundler/vendor/pub_grub/lib/pub_grub/incompatibility.rb', line 56

def to_s
  return @custom_explanation if @custom_explanation

  case cause
  when :root
    "(root dependency)"
  when :dependency
    "#{terms[0].to_s(allow_every: true)} depends on #{terms[1].invert}"
  when Bundler::PubGrub::Incompatibility::InvalidDependency
    "#{terms[0].to_s(allow_every: true)} depends on unknown package #{cause.package}"
  when Bundler::PubGrub::Incompatibility::NoVersions
    "no versions satisfy #{cause.constraint}"
  when Bundler::PubGrub::Incompatibility::ConflictCause
    if failure?
      "version solving has failed"
    elsif terms.length == 1
      term = terms[0]
      if term.positive?
        if term.constraint.any?
          "#{term.package} cannot be used"
        else
          "#{term.to_s(allow_every: true)} cannot be used"
        end
      else
        "#{term.invert} is required"
      end
    else
      if terms.all?(&:positive?)
        if terms.length == 2
          "#{terms[0].to_s(allow_every: true)} is incompatible with #{terms[1]}"
        else
          "one of #{terms.map(&:to_s).join(" or ")} must be false"
        end
      elsif terms.all?(&:negative?)
        if terms.length == 2
          "either #{terms[0].invert} or #{terms[1].invert}"
        else
          "one of #{terms.map(&:invert).join(" or ")} must be true";
        end
      else
        positive = terms.select(&:positive?)
        negative = terms.select(&:negative?).map(&:invert)

        if positive.length == 1
          "#{positive[0].to_s(allow_every: true)} requires #{negative.join(" or ")}"
        else
          "if #{positive.join(" and ")} then #{negative.join(" or ")}"
        end
      end
    end
  else
    raise "unhandled cause: #{cause.inspect}"
  end
end