Module: Pray::Constraint

Defined in:
lib/pray/constraint.rb

Class Method Summary collapse

Class Method Details

.bare_semver?(text) ⇒ Boolean

Returns:

  • (Boolean)


74
75
76
77
78
# File 'lib/pray/constraint.rb', line 74

def bare_semver?(text)
  Gem::Version.correct?(text)
rescue
  false
end

.latest_constraint_for_package(current_constraint, latest_version) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/pray/constraint.rb', line 38

def latest_constraint_for_package(current_constraint, latest_version)
  normalized = normalize_version_constraint(current_constraint)
  return "*" if normalized == "*"
  return pessimistic_constraint_for_version(latest_version) if normalized.start_with?("~")

  if normalized.start_with?("^")
    parsed = Gem::Version.new(latest_version)
    return "^#{parsed.segments[0]}.#{parsed.segments[1]}"
  end

  if normalized.start_with?("=") || bare_semver?(current_constraint.strip)
    return "=#{latest_version}"
  end

  pessimistic_constraint_for_version(latest_version)
end

.normalize_version_constraint(constraint) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/pray/constraint.rb', line 7

def normalize_version_constraint(constraint)
  trimmed = constraint.strip
  return trimmed if trimmed.empty? || trimmed == "*"
  return trimmed if operator_prefixed?(trimmed)

  if bare_semver?(trimmed)
    "=#{trimmed}"
  else
    trimmed
  end
end

.operator_prefixed?(text) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/pray/constraint.rb', line 70

def operator_prefixed?(text)
  text.start_with?("~>", "~", "^", "=", ">", "<") || text.include?("*")
end

.pessimistic_constraint_for_version(version) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/pray/constraint.rb', line 29

def pessimistic_constraint_for_version(version)
  parsed = Gem::Version.new(version)
  if parsed.segments[1].to_i.zero? && parsed.segments[2].to_i.zero?
    "~> #{parsed.segments[0]}.0"
  else
    "~> #{parsed.segments[0]}.#{parsed.segments[1]}"
  end
end

.ruby_pessimistic_to_semver(constraint) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pray/constraint.rb', line 55

def ruby_pessimistic_to_semver(constraint)
  text = constraint.strip.sub(/\A~>\s*/, "")
  parts = text.split(".")
  raise Error.resolution("unsupported Ruby pessimistic constraint: #{constraint}") if parts.empty? || parts.length > 3

  numbers = [parts[0].to_i, (parts[1] || 0).to_i, (parts[2] || 0).to_i]
  lower = numbers.join(".")
  upper = case parts.length
  when 1 then "#{numbers[0] + 1}.0.0"
  when 2 then "#{numbers[0]}.#{numbers[1] + 1}.0"
  else "#{numbers[0]}.#{numbers[1] + 1}.0"
  end
  ">= #{lower}, < #{upper}"
end

.version_satisfies(version, constraint) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'lib/pray/constraint.rb', line 19

def version_satisfies(version, constraint)
  normalized = normalize_version_constraint(constraint)
  return true if normalized.empty? || normalized == "*"

  requirement = Gem::Requirement.new(normalized.strip)
  requirement.satisfied_by?(Gem::Version.new(version))
rescue ArgumentError => error
  raise Error.resolution(error.message)
end