forked from dependabot/dependabot-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequirement.rb
70 lines (54 loc) · 2.24 KB
/
requirement.rb
1
2
3
4
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# typed: true
# frozen_string_literal: true
require "sorbet-runtime"
require "dependabot/requirement"
require "dependabot/utils"
require "dependabot/hex/version"
module Dependabot
module Hex
class Requirement < Dependabot::Requirement
extend T::Sig
AND_SEPARATOR = /\s+and\s+/
OR_SEPARATOR = /\s+or\s+/
# Add the double-equality matcher to the list of allowed operations
OPS = OPS.merge("==" => ->(v, r) { v == r })
# Override the version pattern to allow local versions
quoted = OPS.keys.map { |k| Regexp.quote k }.join "|"
PATTERN_RAW = "\\s*(#{quoted})?\\s*(#{Hex::Version::VERSION_PATTERN})\\s*".freeze
PATTERN = /\A#{PATTERN_RAW}\z/
# Returns an array of requirements. At least one requirement from the
# returned array must be satisfied for a version to be valid.
sig { override.params(requirement_string: T.nilable(String)).returns(T::Array[Requirement]) }
def self.requirements_array(requirement_string)
T.must(requirement_string).strip.split(OR_SEPARATOR).map do |req_string|
requirements = req_string.strip.split(AND_SEPARATOR)
new(requirements)
end
end
# Patches Gem::Requirement to make it accept requirement strings like
# "~> 4.2.5, >= 4.2.5.1" without first needing to split them.
def initialize(*requirements)
requirements = requirements.flatten.flat_map do |req_string|
req_string.split(",").map(&:strip)
end
super(requirements)
end
# Override the parser to create Hex::Versions
def self.parse(obj)
return ["=", Hex::Version.new(obj.to_s)] if obj.is_a?(Gem::Version)
unless (matches = PATTERN.match(obj.to_s))
msg = "Illformed requirement [#{obj.inspect}]"
raise BadRequirementError, msg
end
return DefaultRequirement if matches[1] == ">=" && matches[2] == "0"
[matches[1] || "=", Hex::Version.new(T.must(matches[2]))]
end
def satisfied_by?(version)
version = Hex::Version.new(version.to_s)
requirements.all? { |op, rv| (OPS[op] || OPS["="]).call(version, rv) }
end
end
end
end
Dependabot::Utils
.register_requirement_class("hex", Dependabot::Hex::Requirement)