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

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_allowedObject

Returns the value of attribute minor_allowed.



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

def minor_allowed
  @minor_allowed
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



37
38
39
40
41
# File 'lib/bundler/patch/conservative_resolver.rb', line 37

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



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/bundler/patch/conservative_resolver.rb', line 43

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_allowed ? [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

#search_for(dependency) ⇒ Object



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

def search_for(dependency)
  res = super(dependency)

  dep = dependency.dep unless dependency.is_a? Gem::Dependency
  @conservative_search_for ||= {}
  #@conservative_search_for[dep] ||= # TODO turning off caching allowed a real-world sample to work, dunno why yet.
  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|
      if ENV['DEBUG_PATCH_RESOLVER']
        # TODO: if we keep this, gotta go through Bundler.ui
        begin
          if res
            p debug_format_result(dep, res)
          else
            p "No res for #{dep.to_s}. Orig res: #{super(dependency)}"
          end
        rescue => e
          p [e.message, e.backtrace[0..5]]
        end
      end
    end
  end
end

#sort_specs(specs, locked_spec) ⇒ Object



65
66
67
68
69
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
# File 'lib/bundler/patch/conservative_resolver.rb', line 65

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
    case
    when a_ver.segments[0] != b_ver.segments[0]
      b_ver <=> a_ver
    when !@minor_allowed && (a_ver.segments[1] != b_ver.segments[1])
      b_ver <=> a_ver
    when @gems_to_update.patching_but_not_this_gem?(gem_name)
      b_ver <=> a_ver
    else
      a_ver <=> b_ver
    end
  end.tap do |result|
    if @gems_to_update.unlocking_gem?(gem_name)
      if @gems_to_update.patching_gem?(gem_name)
        # this logic will keep a gem from updating past the patched version
        # if a more recent release (or minor, if enabled) version exists.
        # TODO: not sure if we want this special logic to remain or not.
        new_version = @gems_to_update.gem_patch_for(gem_name).new_version
        swap_version_to_end(specs, new_version, result) if new_version
      end
    else
      # make sure the current locked version is last in list.
      swap_version_to_end(specs, locked_version, result)
    end
  end
end

#swap_version_to_end(specs, version, result) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/bundler/patch/conservative_resolver.rb', line 101

def swap_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