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

def initialize(index, source_requirements, base)
  # hack for 1.10 and 1.11 support
  case Bundler::Resolver.instance_method(:initialize).arity
  when 3
    super(index, source_requirements, base)
  when 4
    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

#debug_format_result(dep, res) ⇒ Object



41
42
43
44
45
# File 'lib/bundler/patch/conservative_resolver.rb', line 41

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

#filter_specs(specs, locked_spec) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/bundler/patch/conservative_resolver.rb', line 47

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(specs, version, result) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/bundler/patch/conservative_resolver.rb', line 108

def move_version_to_end(specs, version, result)
  spec_group = specs.detect { |s| s.first.version.to_s == version.to_s }
  if spec_group
    result.reject! { |s| s.first.version.to_s === version.to_s }
    result << spec_group
  end
end

#search_for(dependency) ⇒ Object



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

def search_for(dependency)
  res = super(dependency)

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

  @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 |res|
      STDERR.puts debug_format_result(dep, res).inspect if ENV['DEBUG_PATCH_RESOLVER']
    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

#sort_specs(specs, locked_spec) ⇒ Object

reminder: sort still filters anything older than locked version



70
71
72
73
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
104
105
106
# File 'lib/bundler/patch/conservative_resolver.rb', line 70

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 }

  filtered.sort do |a, b|
    a_ver = a.first.version
    b_ver = b.first.version
    gem_patch = @gems_to_update.gem_patch_for(gem_name)
    new_version = gem_patch ? gem_patch.new_version : nil
    case
    when a_ver.segments[0] != b_ver.segments[0]
      b_ver <=> a_ver
    when !@minor_preferred && (a_ver.segments[1] != b_ver.segments[1])
      b_ver <=> a_ver
    when @prefer_minimal && !@gems_to_update.unlocking_gem?(gem_name)
      b_ver <=> a_ver
    when @prefer_minimal && @gems_to_update.unlocking_gem?(gem_name) &&
      (![a_ver, b_ver].include?(locked_version) &&
        (!new_version || (new_version && a_ver >= new_version && b_ver >= new_version)))
      b_ver <=> a_ver
    else
      a_ver <=> b_ver
    end
  end.tap do |result|
    if @gems_to_update.unlocking_gem?(gem_name)
      gem_patch = @gems_to_update.gem_patch_for(gem_name)
      if gem_patch && gem_patch.new_version && @prefer_minimal
        move_version_to_end(specs, gem_patch.new_version, result)
      end
    else
      move_version_to_end(specs, locked_version, result)
    end
  end
end