class Dependabot::Maven::Version

Constants

ANCHORED_VERSION_PATTERN
NAMED_QUALIFIERS_HIERARCHY
NULL_VALUES
PREFIXED_TOKEN_HIERARCHY
VERSION_PATTERN

Public Class Methods

correct?(version) click to toggle source
# File lib/dependabot/maven/version.rb, line 35
def self.correct?(version)
  return false if version.nil?

  version.to_s.match?(ANCHORED_VERSION_PATTERN)
end
new(version) click to toggle source
Calls superclass method
# File lib/dependabot/maven/version.rb, line 41
def initialize(version)
  @version_string = version.to_s
  super(version.to_s.tr("_", "-"))
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/dependabot/maven/version.rb, line 59
def <=>(other)
  version = stringify_version(@version_string)
  version = fill_tokens(version)
  version = trim_version(version)

  other_version = stringify_version(other)
  other_version = fill_tokens(other_version)
  other_version = trim_version(other_version)

  version, other_version = convert_dates(version, other_version)

  prefixed_tokens = split_into_prefixed_tokens(version)
  other_prefixed_tokens = split_into_prefixed_tokens(other_version)

  prefixed_tokens, other_prefixed_tokens =
    pad_for_comparison(prefixed_tokens, other_prefixed_tokens)

  prefixed_tokens.count.times.each do |index|
    comp = compare_prefixed_token(
      prefix: prefixed_tokens[index][0],
      token: prefixed_tokens[index][1..-1] || "",
      other_prefix: other_prefixed_tokens[index][0],
      other_token: other_prefixed_tokens[index][1..-1] || ""
    )
    return comp unless comp.zero?
  end

  0
end
prerelease?() click to toggle source
# File lib/dependabot/maven/version.rb, line 50
def prerelease?
  tokens.any? do |token|
    next true if token == "eap"
    next false unless NAMED_QUALIFIERS_HIERARCHY[token]

    NAMED_QUALIFIERS_HIERARCHY[token] < 6
  end
end
to_s() click to toggle source
# File lib/dependabot/maven/version.rb, line 46
def to_s
  @version_string
end

Private Instance Methods

compare_prefixed_token(prefix:, token:, other_prefix:, other_token:) click to toggle source
# File lib/dependabot/maven/version.rb, line 155
def compare_prefixed_token(prefix:, token:, other_prefix:, other_token:)
  token_type = token.match?(/^\d+$/) ? :number : :qualifier
  other_token_type = other_token.match?(/^\d+$/) ? :number : :qualifier

  hierarchy = PREFIXED_TOKEN_HIERARCHY.fetch(prefix).fetch(token_type)
  other_hierarchy =
    PREFIXED_TOKEN_HIERARCHY.fetch(other_prefix).fetch(other_token_type)

  hierarchy_comparison = hierarchy <=> other_hierarchy
  return hierarchy_comparison unless hierarchy_comparison.zero?

  compare_token(token: token, other_token: other_token)
end
compare_token(token:, other_token:) click to toggle source
# File lib/dependabot/maven/version.rb, line 169
def compare_token(token:, other_token:)
  if (token_hierarchy = NAMED_QUALIFIERS_HIERARCHY[token])
    return -1 unless NAMED_QUALIFIERS_HIERARCHY[other_token]

    return token_hierarchy <=> NAMED_QUALIFIERS_HIERARCHY[other_token]
  end

  return 1 if NAMED_QUALIFIERS_HIERARCHY[other_token]

  if token.match?(/\A\d+\z/) && other_token.match?(/\A\d+\z/)
    token = token.to_i
    other_token = other_token.to_i
  end

  token <=> other_token
end
convert_dates(version, other_version) click to toggle source
# File lib/dependabot/maven/version.rb, line 127
def convert_dates(version, other_version)
  default = [version, other_version]
  return default unless version.match?(/^\d{4}-?\d{2}-?\d{2}$/)
  return default unless other_version.match?(/^\d{4}-?\d{2}-?\d{2}$/)

  [version.delete("-"), other_version.delete("-")]
end
fill_tokens(version) click to toggle source
# File lib/dependabot/maven/version.rb, line 108
def fill_tokens(version)
  # Add separators when transitioning from digits to characters
  version = version.gsub(/(\d)([A-Za-z])/, '\1-\2')
  version = version.gsub(/([A-Za-z])(\d)/, '\1-\2')

  # Replace empty tokens with 0
  version = version.gsub(/([\.\-])([\.\-])/, '\10\2')
  version = version.gsub(/^([\.\-])/, '0\1')
  version.gsub(/([\.\-])$/, '\10')
end
pad_for_comparison(prefixed_tokens, other_prefixed_tokens) click to toggle source
# File lib/dependabot/maven/version.rb, line 139
def pad_for_comparison(prefixed_tokens, other_prefixed_tokens)
  prefixed_tokens = prefixed_tokens.dup
  other_prefixed_tokens = other_prefixed_tokens.dup

  longest = [prefixed_tokens, other_prefixed_tokens].max_by(&:count)
  shortest = [prefixed_tokens, other_prefixed_tokens].min_by(&:count)

  longest.count.times do |index|
    next unless shortest[index].nil?

    shortest[index] = longest[index].start_with?(".") ? ".0" : "-"
  end

  [prefixed_tokens, other_prefixed_tokens]
end
split_into_prefixed_tokens(version) click to toggle source
# File lib/dependabot/maven/version.rb, line 135
def split_into_prefixed_tokens(version)
  ".#{version}".split(/(?=[\-\.\+])/)
end
stringify_version(version) click to toggle source
# File lib/dependabot/maven/version.rb, line 101
def stringify_version(version)
  version = version.to_s.downcase

  # Not technically correct, but pragmatic
  version.gsub(/^v(?=\d)/, "")
end
tokens() click to toggle source
# File lib/dependabot/maven/version.rb, line 91
def tokens
  @tokens ||=
    begin
      version = @version_string.to_s.downcase
      version = fill_tokens(version)
      version = trim_version(version)
      split_into_prefixed_tokens(version).map { |t| t[1..-1] }
    end
end
trim_version(version) click to toggle source
# File lib/dependabot/maven/version.rb, line 119
def trim_version(version)
  version.split("-").map do |v|
    parts = v.split(".")
    parts = parts[0..-2] while NULL_VALUES.include?(parts&.last)
    parts&.join(".")
  end.compact.reject(&:empty?).join("-")
end