class Origen::VersionString

Public Class Methods

development_timestamp() click to toggle source

Returns a new development timestamp version string

# 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) click to toggle source

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

# File lib/origen/version_string.rb, line 344
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) click to toggle source

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

# File lib/origen/version_string.rb, line 325
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
new(version, prefix = 'v') click to toggle source

returns version number string but strips out prefix

Calls superclass method
# File lib/origen/version_string.rb, line 7
def initialize(version, prefix = 'v')
  version.gsub!(/^#{prefix}/, '') # remove leading prefix
  super(version)
end
production_timestamp() click to toggle source

Returns a new production timestamp version string

# File lib/origen/version_string.rb, line 13
def self.production_timestamp
  VersionString.new("Rel#{time_now(format: :universal, include_time: false)}")
end

Public Instance Methods

<(version)
Alias for: less_than?
<=(version)
==(version)
Also aliased as: orig_equal?
Alias for: equal?
>(version)
Alias for: greater_than?
>=(version)
bugfix() click to toggle source
# File lib/origen/version_string.rb, line 159
def bugfix
  @bugfix ||= 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
Also aliased as: tiny
condition_met?(condition) click to toggle source

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
# File lib/origen/version_string.rb, line 200
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 && ((latest? || tag.latest?) || 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 && ((latest? || tag.latest?) || timestamp? == tag.timestamp?)
    end

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

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

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

  else
    tag = validate_condition!(condition, condition)
    orig_equal?(tag)
  end
end
dev()
Alias for: pre
development?() click to toggle source

Returns true if the version is a development tag

# File lib/origen/version_string.rb, line 28
def development?
  !production?
end
eq?(version)
Alias for: equal?
equal?(version) click to toggle source
# 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
Also aliased as: eq?, ==
greater_than?(version) click to toggle source
# File lib/origen/version_string.rb, line 57
def greater_than?(version)
  condition_met?("> #{version}")
end
Also aliased as: gt?, >
greater_than_or_equal_to?(version) click to toggle source
# File lib/origen/version_string.rb, line 63
def greater_than_or_equal_to?(version)
  condition_met?(">= #{version}")
end
Also aliased as: gte?, >=
gt?(version)
Alias for: greater_than?
gte?(version)
latest?() click to toggle source
# File lib/origen/version_string.rb, line 180
def latest?
  downcase.to_s.eql?('trunk') || downcase.to_s.eql?('latest')
end
less_than?(version) click to toggle source
# File lib/origen/version_string.rb, line 45
def less_than?(version)
  condition_met?("< #{version}")
end
Also aliased as: lt?, <
less_than_or_equal_to?(version) click to toggle source
# File lib/origen/version_string.rb, line 51
def less_than_or_equal_to?(version)
  condition_met?("<= #{version}")
end
Also aliased as: lte?, <=
lt?(version)
Alias for: less_than?
lte?(version)
major() click to toggle source
# File lib/origen/version_string.rb, line 141
def major
  @major ||= if semantic?
               self =~ /v?(\d+)/
               Regexp.last_match[1].to_i
             else
               fail "#{self} is not a valid semantic version number!"
             end
end
minor() click to toggle source
# File lib/origen/version_string.rb, line 150
def minor
  @minor ||= if semantic?
               self =~ /v?\d+.(\d+)/
               Regexp.last_match[1].to_i
             else
               fail "#{self} is not a valid semantic version number!"
             end
end
next_dev(type = :minor) click to toggle source
# 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) click to toggle source
# 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
numeric() click to toggle source

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

# File lib/origen/version_string.rb, line 247
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
orig_equal?(version)
Alias for: ==
pre() click to toggle source
# File lib/origen/version_string.rb, line 169
def pre
  @pre ||= 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
Also aliased as: dev
prefixed(str = 'v') click to toggle source

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

# File lib/origen/version_string.rb, line 363
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?() click to toggle source

Returns true if the version is a production tag

# File lib/origen/version_string.rb, line 23
def production?
  !!(self =~ /^v?\d+\.\d+\.\d+$/ || self =~ /^Rel\d+$/)
end
semantic?() click to toggle source

Returns true if the version is a semantic format version number

# 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?() click to toggle source

Returns true if the version is a timestamp format version number

# File lib/origen/version_string.rb, line 185
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
tiny()
Alias for: bugfix
to_date() click to toggle source

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

# File lib/origen/version_string.rb, line 291
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_time() click to toggle source

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

# File lib/origen/version_string.rb, line 273
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 = {}) click to toggle source

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

# File lib/origen/version_string.rb, line 71
def valid?(_options = {})
  latest? || semantic? || timestamp?
end
validate!(msg = nil) click to toggle source

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

# File lib/origen/version_string.rb, line 312
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) click to toggle source

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

# File lib/origen/version_string.rb, line 304
def validate_condition!(condition, tag)
  tag = VersionString.new(tag)
  tag.validate!("The version condition, #{condition}, is not valid!")
  tag
end