summaryrefslogtreecommitdiff
path: root/lib/bundler/override.rb
blob: 0e0ec59fd73e8ead4e8088d507e8c257bf8ba4d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# frozen_string_literal: true

module Bundler
  class Override
    UPPER_BOUND_OPERATORS = ["<", "<="].freeze

    def self.find_for(overrides, name, field)
      overrides.find {|o| o.target == name && o.field == field } ||
        overrides.find {|o| o.target == :all && o.field == field }
    end

    # Attach the given overrides onto every LazySpecification in `specs` so
    # downstream consumers (LazySpecification#choose_compatible, the install-
    # time compatibility check, etc.) can read the override list off the spec
    # itself. Non-LazySpec entries (StubSpecification, Gem::Specification, ...)
    # are left untouched.
    def self.attach(specs, overrides)
      return if overrides.nil? || overrides.empty?
      specs.each {|s| s.overrides = overrides if s.is_a?(LazySpecification) }
    end

    attr_reader :target, :field, :operation, :source_location

    def initialize(target, field, operation, source_location: nil)
      @target = target
      @field = field
      @operation = operation
      @source_location = source_location
    end

    def source_location_label
      return nil unless @source_location
      "#{File.basename(@source_location.path)}:#{@source_location.lineno}"
    end

    def apply_to(requirement)
      case operation
      when nil
        Gem::Requirement.default
      when :ignore_upper
        remove_upper_bounds(requirement)
      when String
        Gem::Requirement.new(operation)
      else
        raise ArgumentError, "unsupported override operation: #{operation.inspect}"
      end
    end

    private

    def remove_upper_bounds(requirement)
      return Gem::Requirement.default if requirement.nil? || requirement.none?

      preserved = requirement.requirements.filter_map do |op, version|
        if UPPER_BOUND_OPERATORS.include?(op)
          nil
        elsif op == "~>"
          [">=", version]
        else
          [op, version]
        end
      end

      return Gem::Requirement.default if preserved.empty?

      Gem::Requirement.new(preserved.map {|op, v| "#{op} #{v}" })
    end
  end
end