Class: Version

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
brew/Library/Homebrew/version.rb,
brew/Library/Homebrew/version/null.rb

Direct Known Subclasses

HeadVersion, OS::Mac::Version, FromURL

Defined Under Namespace

Classes: AlphaToken, BetaToken, CompositeToken, FromURL, NullToken, NumericToken, PatchToken, PreToken, RCToken, StringToken, Token

Constant Summary collapse

NULL_TOKEN =
NullToken.new.freeze
SCAN_PATTERN =
Regexp.union(
  AlphaToken::PATTERN,
  BetaToken::PATTERN,
  PreToken::PATTERN,
  RCToken::PATTERN,
  PatchToken::PATTERN,
  NumericToken::PATTERN,
  StringToken::PATTERN,
).freeze
NULL =
Class.new do
  include Comparable

  def <=>(_other)
    -1
  end

  def eql?(_other)
    # Makes sure that the same instance of Version::NULL
    # will never equal itself; normally Comparable#==
    # will return true for this regardless of the return
    # value of #<=>
    false
  end

  def detected_from_url?
    false
  end

  def head?
    false
  end

  def null?
    true
  end

  # For OS::Mac::Version compatability
  def requires_nehalem_cpu?
    false
  end
  alias_method :requires_sse4?, :requires_nehalem_cpu?
  alias_method :requires_sse41?, :requires_nehalem_cpu?
  alias_method :requires_sse42?, :requires_nehalem_cpu?
  alias_method :requires_popcnt?, :requires_nehalem_cpu?

  def to_f
    Float::NAN
  end

  def to_i
    0
  end

  def to_s
    ""
  end
  alias_method :to_str, :to_s

  def inspect
    "#<Version::NULL>"
  end
end.new.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(val) ⇒ Version

Returns a new instance of Version

Raises:

  • (TypeError)


367
368
369
370
371
# File 'brew/Library/Homebrew/version.rb', line 367

def initialize(val)
  raise TypeError, "Version value must be a string; got a #{val.class} (#{val})" unless val.respond_to?(:to_str)

  @version = val.to_str
end

Class Method Details

.create(val) ⇒ Object

Raises:

  • (TypeError)


210
211
212
213
214
215
216
217
218
# File 'brew/Library/Homebrew/version.rb', line 210

def self.create(val)
  raise TypeError, "Version value must be a string; got a #{val.class} (#{val})" unless val.respond_to?(:to_str)

  if val.to_str.start_with?("HEAD")
    HeadVersion.new(val)
  else
    Version.new(val)
  end
end

.detect(url, specs) ⇒ Object



202
203
204
205
206
207
208
# File 'brew/Library/Homebrew/version.rb', line 202

def self.detect(url, specs)
  if specs.key?(:tag)
    FromURL.new(specs[:tag][/((?:\d+\.)*\d+)/, 1])
  else
    FromURL.parse(url)
  end
end

.formula_optionally_versioned_regex(name, full: true) ⇒ Object



8
9
10
# File 'brew/Library/Homebrew/version.rb', line 8

def self.formula_optionally_versioned_regex(name, full: true)
  /#{"^" if full}#{Regexp.escape(name)}(@\d[\d.]*)?#{"$" if full}/
end

.parse(spec) ⇒ Object



220
221
222
223
# File 'brew/Library/Homebrew/version.rb', line 220

def self.parse(spec)
  version = _parse(spec)
  version.nil? ? NULL : new(version)
end

Instance Method Details

#<=>(other) ⇒ Object



385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
# File 'brew/Library/Homebrew/version.rb', line 385

def <=>(other)
  # Needed to retain API compatibility with older string comparisons
  # for compiler versions, etc.
  other = Version.new(other) if other.is_a? String
  # Used by the *_build_version comparisons, which formerly returned Fixnum
  other = Version.new(other.to_s) if other.is_a? Integer
  return 1 if other.nil?

  return 1 if other.respond_to?(:null?) && other.null?
  return unless other.is_a?(Version)
  return 0 if version == other.version
  return 1 if head? && !other.head?
  return -1 if !head? && other.head?
  return 0 if head? && other.head?

  ltokens = tokens
  rtokens = other.tokens
  max = max(ltokens.length, rtokens.length)
  l = r = 0

  while l < max
    a = ltokens[l] || NULL_TOKEN
    b = rtokens[r] || NULL_TOKEN

    if a == b
      l += 1
      r += 1
      next
    elsif a.numeric? && b.numeric?
      return a <=> b
    elsif a.numeric?
      return 1 if a > NULL_TOKEN

      l += 1
    elsif b.numeric?
      return -1 if b > NULL_TOKEN

      r += 1
    else
      return a <=> b
    end
  end

  0
end

#detected_from_url?Boolean

Returns:

  • (Boolean)


373
374
375
# File 'brew/Library/Homebrew/version.rb', line 373

def detected_from_url?
  false
end

#hashObject



432
433
434
# File 'brew/Library/Homebrew/version.rb', line 432

def hash
  version.hash
end

#head?Boolean

Returns:

  • (Boolean)


377
378
379
# File 'brew/Library/Homebrew/version.rb', line 377

def head?
  false
end

#null?Boolean

Returns:

  • (Boolean)


381
382
383
# File 'brew/Library/Homebrew/version.rb', line 381

def null?
  false
end

#to_fObject



436
437
438
# File 'brew/Library/Homebrew/version.rb', line 436

def to_f
  version.to_f
end

#to_iObject



440
441
442
# File 'brew/Library/Homebrew/version.rb', line 440

def to_i
  version.to_i
end

#to_sObject Also known as: to_str



444
445
446
# File 'brew/Library/Homebrew/version.rb', line 444

def to_s
  version.dup
end