Class: Version

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
Comparable
Defined in:
version.rb,
version/null.rb,
version/parser.rb

Direct Known Subclasses

HeadVersion, OS::Mac::Version

Defined Under Namespace

Classes: AlphaToken, BetaToken, CompositeToken, NumericToken, Parser, PatchToken, PostToken, PreToken, RCToken, RegexParser, StemParser, StringToken, Token, UrlParser

Constant Summary collapse

NULL_TOKEN =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

Represents the absence of a token.

NullToken.new.freeze
NULL =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

Represents the absence of a version.

NullVersion.instance.freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(val, detected_from_url: false) ⇒ void

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Raises:

  • (TypeError)


513
514
515
516
517
518
# File 'version.rb', line 513

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

  @version = val.to_str
  @detected_from_url = detected_from_url
end

Class Method Details

.create(val) ⇒ Version

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

Raises:

  • (TypeError)


352
353
354
355
356
357
358
359
360
# File 'version.rb', line 352

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) ⇒ Version

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:



347
348
349
# File 'version.rb', line 347

def self.detect(url, **specs)
  parse(specs.fetch(:tag, url), detected_from_url: true)
end

.formula_optionally_versioned_regex(name, full: true) ⇒ Regexp

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

Returns:

  • (Regexp)


17
18
19
# File 'version.rb', line 17

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

.parse(spec, detected_from_url: false) ⇒ Version

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • spec (String, Pathname)
  • detected_from_url (Boolean) (defaults to: false)

Returns:



363
364
365
366
# File 'version.rb', line 363

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

Instance Method Details

#<=>(other) ⇒ Integer?

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Parameters:

  • other (T.untyped)

Returns:

  • (Integer, nil)


536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'version.rb', line 536

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?

  other = Version.new(other.to_s) if other.is_a? Token
  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 1 if a > NULL_TOKEN

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

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

  0
end

#detected_from_url?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


521
522
523
# File 'version.rb', line 521

def detected_from_url?
  @detected_from_url
end

#empty?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


613
614
615
# File 'version.rb', line 613

def empty?
  version.empty?
end

#hashInteger

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Integer)


618
619
620
# File 'version.rb', line 618

def hash
  version.hash
end

#head?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


526
527
528
# File 'version.rb', line 526

def head?
  false
end

#majorToken?

Returns:



584
585
586
# File 'version.rb', line 584

def major
  tokens.first
end

#major_minorT.self_type

Returns:

  • (T.self_type)


602
603
604
# File 'version.rb', line 602

def major_minor
  self.class.new([major, minor].compact.join("."))
end

#major_minor_patchT.self_type

Returns:

  • (T.self_type)


608
609
610
# File 'version.rb', line 608

def major_minor_patch
  self.class.new([major, minor, patch].compact.join("."))
end

#minorToken?

Returns:



590
591
592
# File 'version.rb', line 590

def minor
  tokens.second
end

#null?Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


531
532
533
# File 'version.rb', line 531

def null?
  false
end

#patchToken?

Returns:



596
597
598
# File 'version.rb', line 596

def patch
  tokens.third
end

#to_fFloat

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Float)


623
624
625
# File 'version.rb', line 623

def to_f
  version.to_f
end

#to_iInteger

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Integer)


628
629
630
# File 'version.rb', line 628

def to_i
  version.to_i
end

#to_sString Also known as: to_str

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:



633
634
635
# File 'version.rb', line 633

def to_s
  version.dup
end