Class: Bundler::Patch::ConservativeResolver

Inherits:
Resolver
  • Object
show all
Defined in:
lib/bundler/patch/conservative_resolver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(index, source_requirements, base) ⇒ ConservativeResolver

Returns a new instance of ConservativeResolver.



5
6
7
8
9
10
11
12
# File 'lib/bundler/patch/conservative_resolver.rb', line 5

def initialize(index, source_requirements, base)
  case Bundler::Resolver.instance_method(:initialize).arity
  when 3 # 1.9 1.10
    super(index, source_requirements, base)
  when 4 # 1.11 1.12
    super(index, source_requirements, base, nil)
  end
end

Instance Attribute Details

#gems_to_updateObject

Returns the value of attribute gems_to_update.



3
4
5
# File 'lib/bundler/patch/conservative_resolver.rb', line 3

def gems_to_update
  @gems_to_update
end

#locked_specsObject

Returns the value of attribute locked_specs.



3
4
5
# File 'lib/bundler/patch/conservative_resolver.rb', line 3

def locked_specs
  @locked_specs
end

#minor_preferredObject

Returns the value of attribute minor_preferred.



3
4
5
# File 'lib/bundler/patch/conservative_resolver.rb', line 3

def minor_preferred
  @minor_preferred
end

#prefer_minimalObject

Returns the value of attribute prefer_minimal.



3
4
5
# File 'lib/bundler/patch/conservative_resolver.rb', line 3

def prefer_minimal
  @prefer_minimal
end

#strictObject

Returns the value of attribute strict.



3
4
5
# File 'lib/bundler/patch/conservative_resolver.rb', line 3

def strict
  @strict
end

Instance Method Details

#both_versions_gt_or_equal_to_version(version) ⇒ Object



144
145
146
# File 'lib/bundler/patch/conservative_resolver.rb', line 144

def both_versions_gt_or_equal_to_version(version)
  version && @a_ver >= version && @b_ver >= version
end

#debug_format_result(dep, res) ⇒ Object



45
46
47
48
49
# File 'lib/bundler/patch/conservative_resolver.rb', line 45

def debug_format_result(dep, res)
  a = [dep.to_s,
       res.map { |sg| [sg.version, sg.dependencies_for_activated_platforms.map { |dp| [dp.name, dp.requirement.to_s] }] }]
  [a.first, a.last.map { |sg_data| [sg_data.first.version, sg_data.last.map { |aa| aa.join(' ') }] }]
end

#either_version_older_than_locked(locked_version) ⇒ Object



123
124
125
# File 'lib/bundler/patch/conservative_resolver.rb', line 123

def either_version_older_than_locked(locked_version)
  @a_ver < locked_version || @b_ver < locked_version
end

#filter_specs(specs, locked_spec) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/bundler/patch/conservative_resolver.rb', line 51

def filter_specs(specs, locked_spec)
  res = specs.select do |sg|
    # SpecGroup is grouped by name/version, multiple entries for multiple platforms.
    # We only need the name, which will be the same, so hard coding to first is ok.
    gem_spec = sg.first

    if locked_spec
      gsv = gem_spec.version
      lsv = locked_spec.version

      must_match = @minor_preferred ? [0] : [0, 1]

      matches = must_match.map { |idx| gsv.segments[idx] == lsv.segments[idx] }
      (matches.uniq == [true]) ? gsv.send(:>=, lsv) : false
    else
      true
    end
  end

  sort_specs(res, locked_spec)
end

#move_version_to_end(result, version) ⇒ Object



148
149
150
151
# File 'lib/bundler/patch/conservative_resolver.rb', line 148

def move_version_to_end(result, version)
  move, keep = result.partition { |s| s.first.version.to_s == version.to_s }
  keep.concat(move)
end

#neither_version_matches(match_version) ⇒ Object



136
137
138
# File 'lib/bundler/patch/conservative_resolver.rb', line 136

def neither_version_matches(match_version)
  !one_version_matches(match_version)
end

#one_version_matches(match_version) ⇒ Object



140
141
142
# File 'lib/bundler/patch/conservative_resolver.rb', line 140

def one_version_matches(match_version)
  [@a_ver, @b_ver].include?(match_version)
end

#post_sort(result) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/bundler/patch/conservative_resolver.rb', line 105

def post_sort(result)
  unless unlocking_gem?
    result = move_version_to_end(result, @locked_version)
  end

  if @new_version && unlocking_gem? && segments_match(:major, @new_version, @locked_version)
    if @prefer_minimal || (!@prefer_minimal && (result.last.first.version < @new_version))
      result = move_version_to_end(result, @new_version)
    end
  end

  result
end

#search_for(dependency) ⇒ Object



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
# File 'lib/bundler/patch/conservative_resolver.rb', line 14

def search_for(dependency)
  res = super(dependency)

  dep = dependency.dep unless dependency.is_a? Gem::Dependency

  super_result = "super search_for: #{debug_format_result(dep, res).inspect}"

  @conservative_search_for ||= {}
  res = @conservative_search_for[dep] ||= begin
    gem_name = dep.name

    # An Array per version returned, different entries for different platforms.
    # We just need the version here so it's ok to hard code this to the first instance.
    locked_spec = @locked_specs[gem_name].first

    (@strict ?
      filter_specs(res, locked_spec) :
      sort_specs(res, locked_spec)).tap do |result|
      if ENV['DEBUG_PATCH_RESOLVER']
        STDERR.puts super_result
        STDERR.puts "after search_for: #{debug_format_result(dep, result).inspect}"
      end
    end
  end

  # dup is important, in weird (large) cases Bundler will empty the result array corrupting the cache.
  # Bundler itself doesn't have this problem because the super search_for does a select on its cached
  # search results, effectively duping it.
  res.dup
end

#segments_do_not_match(level, a_ver = @a_ver, b_ver = @b_ver) ⇒ Object



131
132
133
134
# File 'lib/bundler/patch/conservative_resolver.rb', line 131

def segments_do_not_match(level, a_ver=@a_ver, b_ver=@b_ver)
  index = [:major, :minor].index(level)
  a_ver.segments[index] != b_ver.segments[index]
end

#segments_match(level, a_ver = @a_ver, b_ver = @b_ver) ⇒ Object



127
128
129
# File 'lib/bundler/patch/conservative_resolver.rb', line 127

def segments_match(level, a_ver=@a_ver, b_ver=@b_ver)
  !segments_do_not_match(level, a_ver, b_ver)
end

#sort_specs(specs, locked_spec) ⇒ Object

reminder: sort still filters anything older than locked version



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/bundler/patch/conservative_resolver.rb', line 74

def sort_specs(specs, locked_spec)
  return specs unless locked_spec
  @gem_name = locked_spec.name
  @locked_version = locked_spec.version

  filtered = specs.select { |s| s.first.version >= @locked_version }

  @gem_patch = @gems_to_update.gem_patch_for(@gem_name)
  @new_version = @gem_patch ? @gem_patch.new_version : nil

  result = filtered.sort do |a, b|
    @a_ver = a.first.version
    @b_ver = b.first.version
    case
    when segments_do_not_match(:major)
      @b_ver <=> @a_ver
    when !@minor_preferred && segments_do_not_match(:minor)
      @b_ver <=> @a_ver
    when @prefer_minimal && !unlocking_gem?
      @b_ver <=> @a_ver
    when @prefer_minimal && unlocking_gem? &&
      (neither_version_matches(@locked_version) &&
        (!@new_version || both_versions_gt_or_equal_to_version(@new_version)))
      @b_ver <=> @a_ver
    else
      @a_ver <=> @b_ver
    end
  end
  post_sort(result)
end

#unlocking_gem?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/bundler/patch/conservative_resolver.rb', line 119

def unlocking_gem?
  @gems_to_update.unlocking_gem?(@gem_name)
end