Module: VersionRequirementComparator

Included in:
DependencyWorker
Defined in:
lib/cutting_edge/versions.rb

Constant Summary collapse

HIGHER_THAN =

Operators that require the checked version to be higher than the required version

['>', '>=']

Instance Method Summary collapse

Instance Method Details

#version_difference_type(position) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/cutting_edge/versions.rb', line 36

def version_difference_type(position)
  case position
  when 0
    :outdated_major
  when 1
    :outdated_minor
  else
    :outdated_patch
  end
end

#version_requirement_diff(requirement, latest_version) ⇒ Object



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
# File 'lib/cutting_edge/versions.rb', line 8

def version_requirement_diff(requirement, latest_version)
  segments = latest_version.canonical_segments

  # Since we know `latest_version` is higher than the requirement, we can and must ignore cases where the constraint fails because `check_version` is lower
  # Such cases would be caused by the fact that we are generating `check_version` below by chopping off the version decimals after `position`
  # For example: `latest_version` is 3.5. On the first pass of this loop, we will thus be checking version 3.0.
  # This would fail a ~> 3.2 requirement on the first pass, falsely yielding an `:outdated_major`.
  # Therefore, filter out the HIGHER_THAN operators
  constraints = requirement.requirements.map do |comparator, version|
    if comparator == '~>'
      Gem::Requirement.new("< #{version.bump.to_s}") # Use only the upper bound requirement of a ~> comparison
    elsif !HIGHER_THAN.include?(comparator)
      Gem::Requirement.new("#{comparator} #{version.to_s}")
    end
  end
  constraints.compact!

  segments.each_with_index do |_v, position|
    check_version = Gem::Version.new(segments[0..position].join('.'))
    constraints.each do |constraint|
      unless constraint.satisfied_by?(check_version)
        return version_difference_type(position)
      end
    end
  end
  return :unknown # This can occur if latest_version is actually lower than the requirement (e.g., when a newer version of a gem has been yanked)
end