Module: Vers::MavenVersion

Defined in:
lib/vers/maven_version.rb

Defined Under Namespace

Classes: MavenComponent

Constant Summary collapse

QUALIFIER_ORDER =
{
  "alpha" => 1,
  "beta" => 2,
  "milestone" => 3,
  "rc" => 4,
  "snapshot" => 5,
  "" => 6,
  "sp" => 7
}.freeze
UNKNOWN_QUALIFIER_ORDER =
8

Class Method Summary collapse

Class Method Details

.compare(a, b) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/vers/maven_version.rb', line 25

def compare(a, b)
  return 0 if a == b

  parts_a = parse_maven_version(a)
  parts_b = parse_maven_version(b)

  max_len = [parts_a.length, parts_b.length].max

  max_len.times do |i|
    comp_a = i < parts_a.length ? parts_a[i] : MavenComponent.new(is_null: true)
    comp_b = i < parts_b.length ? parts_b[i] : MavenComponent.new(is_null: true)

    cmp = compare_components(comp_a, comp_b)
    return cmp unless cmp == 0
  end

  0
end

.compare_components(a, b) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/vers/maven_version.rb', line 154

def compare_components(a, b)
  return 0 if a.is_null && b.is_null

  if a.is_null
    return compare_to_null(b) * -1
  end
  if b.is_null
    return compare_to_null(a)
  end

  if a.after_dash != b.after_dash
    if a.after_dash
      return b.is_numeric ? -1 : 1
    else
      return a.is_numeric ? 1 : -1
    end
  end

  if a.is_numeric && b.is_numeric
    return a.numeric <=> b.numeric
  end

  if a.is_numeric && !b.is_numeric
    return 1
  end
  if !a.is_numeric && b.is_numeric
    return -1
  end

  order_a = qualifier_order(a.qualifier)
  order_b = qualifier_order(b.qualifier)

  if order_a != order_b
    return order_a <=> order_b
  end

  known_a = QUALIFIER_ORDER.key?(a.qualifier)
  known_b = QUALIFIER_ORDER.key?(b.qualifier)

  if !known_a && !known_b
    return a.qualifier <=> b.qualifier
  end

  0
end

.compare_to_null(comp) ⇒ Object



200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/vers/maven_version.rb', line 200

def compare_to_null(comp)
  if comp.is_numeric
    if comp.numeric == 0
      0
    else
      1
    end
  else
    order_comp = qualifier_order(comp.qualifier)
    order_null = QUALIFIER_ORDER[""]
    order_comp <=> order_null
  end
end

.normalize_components(components) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/vers/maven_version.rb', line 132

def normalize_components(components)
  return components if components.empty?

  first_sublist_idx = components.index { |c| c.after_dash }

  if first_sublist_idx && first_sublist_idx > 0
    base_end = first_sublist_idx
    while base_end > 1 && components[base_end - 1].is_numeric && components[base_end - 1].numeric == 0
      base_end -= 1
    end
    if base_end < first_sublist_idx
      components = components[0...base_end] + components[first_sublist_idx..]
    end
  elsif first_sublist_idx.nil?
    while components.length > 0 && components.last.is_numeric && components.last.numeric == 0
      components.pop
    end
  end

  components
end

.normalize_qualifier(q, next_is_digit) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/vers/maven_version.rb', line 116

def normalize_qualifier(q, next_is_digit)
  if next_is_digit && q.length == 1
    case q
    when "a" then return "alpha"
    when "b" then return "beta"
    when "m" then return "milestone"
    end
  end

  case q
  when "cr" then "rc"
  when "ga", "final", "release" then ""
  else q
  end
end

.parse_maven_version(s) ⇒ Object



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
# File 'lib/vers/maven_version.rb', line 44

def parse_maven_version(s)
  s = s.downcase

  parts, after_dash_flags = split_with_separators(s)

  result = []
  parts.each_with_index do |part, i|
    next if part.empty?

    next_is_digit = if i + 1 < parts.length
                      parts[i + 1].match?(/\A\d+\z/)
                    else
                      false
                    end

    normalized = normalize_qualifier(part, next_is_digit)
    next if normalized.empty?

    after_dash = i < after_dash_flags.length ? after_dash_flags[i] : false

    if normalized.match?(/\A\d+\z/)
      result << MavenComponent.new(is_numeric: true, numeric: normalized.to_i, after_dash: after_dash)
    else
      result << MavenComponent.new(qualifier: normalized, after_dash: after_dash)
    end
  end

  normalize_components(result)
end

.qualifier_order(q) ⇒ Object



214
215
216
# File 'lib/vers/maven_version.rb', line 214

def qualifier_order(q)
  QUALIFIER_ORDER.fetch(q, UNKNOWN_QUALIFIER_ORDER)
end

.split_with_separators(s) ⇒ Object



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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/vers/maven_version.rb', line 74

def split_with_separators(s)
  parts = []
  after_dash = []
  current = +""
  last_was_digit = false
  first_char = true
  current_after_dash = false

  s.each_char do |c|
    if c == "." || c == "-"
      if current.length > 0
        parts << current
        after_dash << current_after_dash
        current = +""
      end
      current_after_dash = (c == "-")
      first_char = true
      next
    end

    is_digit = c >= "0" && c <= "9"

    if !first_char && is_digit != last_was_digit && current.length > 0
      parts << current
      after_dash << current_after_dash
      current = +""
      current_after_dash = true
    end

    current << c
    last_was_digit = is_digit
    first_char = false
  end

  if current.length > 0
    parts << current
    after_dash << current_after_dash
  end

  [parts, after_dash]
end