-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathversion_resolver.rb
98 lines (79 loc) · 3.79 KB
/
version_resolver.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
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
# frozen_string_literal: true
# Cloud Foundry Java Buildpack
# Copyright 2013-2020 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
require 'java_buildpack/repository'
require 'java_buildpack/util/tokenized_version'
require 'java_buildpack/logging/logger_factory'
module JavaBuildpack
module Repository
# A resolver that selects values from a collection based on a set of rules governing wildcards
class VersionResolver
private_class_method :new
class << self
# Resolves a version from a collection of versions. The +candidate_version+ must be structured like:
# * up to three numeric components, followed by an optional string component
# * the final component may be a +
# The resolution returns the maximum of the versions that match the candidate version
#
# @param [TokenizedVersion] candidate_version the version, possibly containing a wildcard, to resolve. If
# +nil+, substituted with +.
# @param [Array<String>] versions the collection of versions to resolve against
# @return [TokenizedVersion] the resolved version or nil if no matching version is found
def resolve(candidate_version, versions)
tokenized_candidate_version = safe_candidate_version candidate_version
tokenized_versions = versions.map { |version| create_token(version) }.compact
tokenized_versions
.select { |tokenized_version| matches? tokenized_candidate_version, tokenized_version }
.max { |a, b| a <=> b }
end
private
TOKENIZED_WILDCARD = JavaBuildpack::Util::TokenizedVersion.new('+').freeze
private_constant :TOKENIZED_WILDCARD
def create_token(version)
JavaBuildpack::Util::TokenizedVersion.new(version, false)
rescue StandardError => e
logger = JavaBuildpack::Logging::LoggerFactory.instance.get_logger VersionResolver
logger.warn { "Discarding illegal version #{version}: #{e.message}" }
nil
end
def safe_candidate_version(candidate_version)
if candidate_version.nil?
TOKENIZED_WILDCARD
else
unless candidate_version.is_a?(JavaBuildpack::Util::TokenizedVersion)
raise "Invalid TokenizedVersion '#{candidate_version}'"
end
candidate_version
end
end
def matches?(tokenized_candidate_version, tokenized_version)
wildcard_matched = false
(0..3).all? do |i|
next true if wildcard_matched || (tokenized_candidate_version[i].nil? && tokenized_version[i].nil?)
next false if tokenized_candidate_version[i].nil? && !tokenized_version[i].nil?
if tokenized_candidate_version[i] == JavaBuildpack::Util::TokenizedVersion::WILDCARD
wildcard_matched = true
next true
end
if tokenized_candidate_version[i].end_with?(JavaBuildpack::Util::TokenizedVersion::WILDCARD)
next !tokenized_version[i].nil? && tokenized_version[i].start_with?(tokenized_candidate_version[i][0..-2])
end
tokenized_candidate_version[i] == tokenized_version[i]
end
end
end
end
end
end