Class: Origen::VersionString

Inherits:
String show all
Includes:
Utility::TimeAndDate
Defined in:
lib/origen/version_string.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Utility::TimeAndDate

#time_now

Methods inherited from String

#camel_case, #escape_underscores, #excel_col_index, #is_downcase?, #is_numeric?, #is_upcase?, #is_verilog_number?, #match_all, #pad_leading_zeros, #squeeze_lines, #symbolize, #titleize, #to_bool, #to_dec, #to_lines, #to_numeric, #to_snakecase, #to_snakecase!

Constructor Details

#initialize(version, prefix = 'v') ⇒ VersionString

returns version number string but strips out prefix



7
8
9
10
# File 'lib/origen/version_string.rb', line 7

def initialize(version, prefix = 'v')
  version.gsub!(/^#{prefix}/, '')  # remove leading prefix
  super(version)
end

Class Method Details

.development_timestampObject

Returns a new development timestamp version string



18
19
20
# File 'lib/origen/version_string.rb', line 18

def self.development_timestamp
  VersionString.new("#{User.current.initials}_#{time_now(format: :universal, underscore: true)}")
end

.maximum_version(condition) ⇒ Object

Returns the maximum Origen version that would satisfy the given condition, if the condition does not specify a maximum nil will be returned



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/origen/version_string.rb', line 352

def self.maximum_version(condition)
  if condition =~ /^==?\s*(.*)/
    version = Regexp.last_match[1]
  elsif condition =~ /^<=\s*(.*)/
    version = Regexp.last_match[1]
  elsif condition =~ /^<\s*(.*)/
    # This is tricky to support, would probably require a lookup
    # table of all versions to be maintained
    fail 'Maximum < version is currently not supported!'
  elsif condition =~ /^>=?\s*(.*)/
    nil
  else
    version = condition
  end
  VersionString.new(version).validate! if version
end

.minimum_version(condition) ⇒ Object

Returns the minimum Origen version that would satisfy the given condition, if the condition does not specify a minimum nil will be returned



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
# File 'lib/origen/version_string.rb', line 333

def self.minimum_version(condition)
  if condition =~ /^==?\s*(.*)/
    version = Regexp.last_match[1]
  elsif condition =~ /^>=\s*(.*)/
    version = Regexp.last_match[1]
  elsif condition =~ /^>\s*(.*)/
    # This is tricky to support, would probably require a lookup
    # table of all versions to be maintained
    fail 'Minimum > version is currently not supported!'
  elsif condition =~ /^<=?\s*(.*)/
    nil
  else
    version = condition
  end
  VersionString.new(version).validate! if version
end

.production_timestampObject

Returns a new production timestamp version string



13
14
15
# File 'lib/origen/version_string.rb', line 13

def self.production_timestamp
  VersionString.new("Rel#{time_now(format: :universal, include_time: false)}")
end

Instance Method Details

#bugfixObject Also known as: tiny



163
164
165
166
167
168
169
170
171
172
# File 'lib/origen/version_string.rb', line 163

def bugfix
  @bugfix ||= begin
    if semantic?
      self =~ /v?\d+.\d+.(\d+)/
      Regexp.last_match[1].to_i
    else
      fail "#{self} is not a valid semantic version number!"
    end
  end
end

#condition_met?(condition) ⇒ Boolean

Returns true if the version fulfills the supplied condition. Example conditions:

"v2.1.3"      # must equal the given version
"= v2.1.3"    # alias for the above
"> v2.1.3"    # must be greater than the given version
">= v2.1.3"   # must be greater than or equal to the given version
"< v2.1.3"    # must be less than the given version
"<= v2.1.3"   # must be less than or equal to the given version
"production"  # must be a production tag

Returns:

  • (Boolean)


208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/origen/version_string.rb', line 208

def condition_met?(condition)
  condition = condition.to_s.strip
  if condition == 'prod' || condition == 'production'
    production?

  elsif condition =~ /^>=\s*(.*)/
    tag = validate_condition!(condition, Regexp.last_match[1])
    # Force false in the case where a production and development
    # timestamp fall on the same date. Since the production tag
    # does not contain time information it is impossible to say
    # which one is greater
    if production? != tag.production? && timestamp? &&
       to_date == tag.to_date
      false
    else
      numeric >= tag.numeric && ((self.latest? || tag.latest?) || self.timestamp? == tag.timestamp?)
    end

  elsif condition =~ /^>\s*(.*)/
    tag = validate_condition!(condition, Regexp.last_match[1])
    if production? != tag.production? && timestamp? &&
       to_date == tag.to_date
      false
    else
      numeric > tag.numeric && ((self.latest? || tag.latest?) || self.timestamp? == tag.timestamp?)
    end

  elsif condition =~ /^<=\s*(.*)/
    tag = validate_condition!(condition, Regexp.last_match[1])
    numeric <= tag.numeric && ((self.latest? || tag.latest?) || self.timestamp? == tag.timestamp?)

  elsif condition =~ /^<\s*(.*)/
    tag = validate_condition!(condition, Regexp.last_match[1])
    numeric < tag.numeric && ((self.latest? || tag.latest?) || self.timestamp? == tag.timestamp?)

  elsif condition =~ /^==?\s*(.*)/
    tag = validate_condition!(condition, Regexp.last_match[1])
    self.orig_equal?(tag)

  else
    tag = validate_condition!(condition, condition)
    self.orig_equal?(tag)
  end
end

#development?Boolean

Returns true if the version is a development tag

Returns:

  • (Boolean)


28
29
30
# File 'lib/origen/version_string.rb', line 28

def development?
  !production?
end

#equal?(version) ⇒ Boolean Also known as: eq?, ==

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
# File 'lib/origen/version_string.rb', line 34

def equal?(version)
  # If not a valid version string, compare using regular string comparison
  if valid?
    condition_met?("== #{version}")
  else
    orig_equal?(version)
  end
end

#greater_than?(version) ⇒ Boolean Also known as: gt?, >

Returns:

  • (Boolean)


57
58
59
# File 'lib/origen/version_string.rb', line 57

def greater_than?(version)
  condition_met?("> #{version}")
end

#greater_than_or_equal_to?(version) ⇒ Boolean Also known as: gte?, >=

Returns:

  • (Boolean)


63
64
65
# File 'lib/origen/version_string.rb', line 63

def greater_than_or_equal_to?(version)
  condition_met?(">= #{version}")
end

#latest?Boolean

Returns:

  • (Boolean)


188
189
190
# File 'lib/origen/version_string.rb', line 188

def latest?
  downcase.orig_equal?('trunk') || downcase.orig_equal?('latest')
end

#less_than?(version) ⇒ Boolean Also known as: lt?, <

Returns:

  • (Boolean)


45
46
47
# File 'lib/origen/version_string.rb', line 45

def less_than?(version)
  condition_met?("< #{version}")
end

#less_than_or_equal_to?(version) ⇒ Boolean Also known as: lte?, <=

Returns:

  • (Boolean)


51
52
53
# File 'lib/origen/version_string.rb', line 51

def less_than_or_equal_to?(version)
  condition_met?("<= #{version}")
end

#majorObject



141
142
143
144
145
146
147
148
149
150
# File 'lib/origen/version_string.rb', line 141

def major
  @major ||= begin
    if semantic?
      self =~ /v?(\d+)/
      Regexp.last_match[1].to_i
    else
      fail "#{self} is not a valid semantic version number!"
    end
  end
end

#minorObject



152
153
154
155
156
157
158
159
160
161
# File 'lib/origen/version_string.rb', line 152

def minor
  @minor ||= begin
    if semantic?
      self =~ /v?\d+.(\d+)/
      Regexp.last_match[1].to_i
    else
      fail "#{self} is not a valid semantic version number!"
    end
  end
end

#next_dev(type = :minor) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/origen/version_string.rb', line 82

def next_dev(type = :minor)
  if semantic?
    if pre
      if self =~ /dev/
        VersionString.new("#{major}.#{minor + 1}.0.pre#{pre + 1}")
      else
        VersionString.new("#{major}.#{minor}.#{tiny}.pre#{pre + 1}")
      end
    else
      case type
      when :major
        VersionString.new("#{major + 1}.0.0.pre0")
      when :minor, :development
        VersionString.new("#{major}.#{minor + 1}.0.pre0")
      when :tiny, :bugfix
        VersionString.new("#{major}.#{minor}.#{tiny + 1}.pre0")
      else
        fail "Unknown version counter type '#{type}', must be :major, :minor or :tiny"
      end
    end
  else
    VersionString.new("#{User.current.initials}_#{time_now(format: :universal, underscore: true)}")
  end
end

#next_prod(type = :minor) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/origen/version_string.rb', line 107

def next_prod(type = :minor)
  if semantic?
    if pre
      if self =~ /dev/
        case type
        when :major
          VersionString.new("#{major + 1}.0.0")
        when :minor, :production
          VersionString.new("#{major}.#{minor + 1}.0")
        when :tiny, :bugfix
          VersionString.new("#{major}.#{minor}.#{tiny + 1}")
        else
          fail "Unknown version counter type '#{type}', must be :major, :minor or :tiny"
        end
      else
        VersionString.new("#{major}.#{minor}.#{tiny}")
      end
    else
      case type
      when :major
        VersionString.new("#{major + 1}.0.0")
      when :minor, :production
        VersionString.new("#{major}.#{minor + 1}.0")
      when :tiny, :bugfix
        VersionString.new("#{major}.#{minor}.#{tiny + 1}")
      else
        fail "Unknown version counter type '#{type}', must be :major, :minor or :tiny"
      end
    end
  else
    VersionString.new("Rel#{time_now(format: :universal, include_time: false)}")
  end
end

#numericObject

Returns a numeric representation of the version, this can be used for chronological comparison with other versions



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/origen/version_string.rb', line 255

def numeric
  if latest?
    1_000_000_000_000_000_000_000_000_000
  elsif semantic?
    # This assumes each counter will never go > 1000
    if development?
      self =~ /v?(\d+).(\d+).(\d+).(dev|pre)(\d+)/
      (Regexp.last_match[1].to_i * 1000 * 1000 * 1000) +
        (Regexp.last_match[2].to_i * 1000 * 1000) +
        (Regexp.last_match[3].to_i * 1000) +
        Regexp.last_match[5].to_i
    else
      self =~ /v?(\d+).(\d+).(\d+)/
      (Regexp.last_match[1].to_i * 1000 * 1000 * 1000) +
        (Regexp.last_match[2].to_i * 1000 * 1000) +
        (Regexp.last_match[3].to_i * 1000)
    end
  elsif timestamp?
    to_time.to_i
  else
    validate!
  end
end

#preObject Also known as: dev



175
176
177
178
179
180
181
182
183
184
185
# File 'lib/origen/version_string.rb', line 175

def pre
  @pre ||= begin
    if semantic?
      if self =~ /(dev|pre)(\d+)$/
        Regexp.last_match[2].to_i
      end
    else
      fail "#{self} is not a valid semantic version number!"
    end
  end
end

#prefixed(str = 'v') ⇒ Object

Returns the version prefixed with the given value (ā€˜vā€™ by default) if not already present



371
372
373
374
375
376
377
378
379
380
381
# File 'lib/origen/version_string.rb', line 371

def prefixed(str = 'v')
  if Origen.config.app.config.rc_tag_prepend_v
    if self =~ /^#{str}/
      to_s
    else
      "#{str}#{self}"
    end
  else
    self
  end
end

#production?Boolean

Returns true if the version is a production tag

Returns:

  • (Boolean)


23
24
25
# File 'lib/origen/version_string.rb', line 23

def production?
  !!(self =~ /^v?\d+\.\d+\.\d+$/ || self =~ /^Rel\d+$/)
end

#semantic?Boolean

Returns true if the version is a semantic format version number

Returns:

  • (Boolean)


76
77
78
79
80
# File 'lib/origen/version_string.rb', line 76

def semantic?
  !!(self =~ /^v?\d+\.\d+\.\d+$/ ||
     self =~ /^v?\d+\.\d+\.\d+\.(dev|pre)\d+$/
    )
end

#timestamp?Boolean

Returns true if the version is a timestamp format version number

Returns:

  • (Boolean)


193
194
195
196
# File 'lib/origen/version_string.rb', line 193

def timestamp?
  !!(self =~ /^Rel\d\d\d\d\d\d\d\d$/ ||
     self =~ /^\w\w\w?_\d\d\d\d_\d\d_\d\d_\d\d_\d\d$/)
end

#to_dateObject

Returns the version as a date, only applicable for timestamps, otherwise an error will be raised



299
300
301
302
303
304
305
306
307
# File 'lib/origen/version_string.rb', line 299

def to_date
  if latest?
    Date.new(10_000, 1, 1)
  elsif timestamp?
    to_time.to_date
  else
    fail "Version tag #{self} cannot be converted to a date!"
  end
end

#to_timeObject

Returns the version as a time, only applicable for timestamps, otherwise an error will be raised



281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/origen/version_string.rb', line 281

def to_time
  if latest?
    Time.new(10_000, 1, 1)
  elsif timestamp?
    if development?
      self =~ /\w+_(\d\d\d\d)_(\d\d)_(\d\d)_(\d\d)_(\d\d)$/
      Time.new(Regexp.last_match[1], Regexp.last_match[2], Regexp.last_match[3], Regexp.last_match[4], Regexp.last_match[5])
    else
      self =~ /Rel(\d\d\d\d)(\d\d)(\d\d)/
      Time.new(Regexp.last_match[1], Regexp.last_match[2], Regexp.last_match[3])
    end
  else
    fail "Version tag #{self} cannot be converted to a time!"
  end
end

#valid?(_options = {}) ⇒ Boolean

Returns true if the version is a correctly formatted semantic or timestamp version number

Returns:

  • (Boolean)


71
72
73
# File 'lib/origen/version_string.rb', line 71

def valid?(_options = {})
  latest? || semantic? || timestamp?
end

#validate!(msg = nil) ⇒ Object

Will raise an error if the version is not valid, i.e. if valid? returns false



320
321
322
323
324
325
326
327
328
329
# File 'lib/origen/version_string.rb', line 320

def validate!(msg = nil)
  unless valid?
    if msg
      fail msg
    else
      fail "The version string, #{self}, is not valid!"
    end
  end
  self
end

#validate_condition!(condition, tag) ⇒ Object

Validates the given condition and the extracted tag, returns the tag wrapped in a VersionString if valid, will raise an error if not



312
313
314
315
316
# File 'lib/origen/version_string.rb', line 312

def validate_condition!(condition, tag)
  tag = VersionString.new(tag)
  tag.validate!("The version condition, #{condition}, is not valid!")
  tag
end