Class: Version Private

Inherits:
Object show all
Includes:
Comparable
Defined in:
version.rb,
version/parser.rb,
version.rbi

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

Direct Known Subclasses

MacOSVersion

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

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:

  • (ArgumentError)


502
503
504
505
506
507
508
# File 'version.rb', line 502

def initialize(val, detected_from_url: false)
  version = val.to_str
  raise ArgumentError, "Version must not be empty" if version.blank?

  @version = version
  @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:



352
353
354
355
# File 'version.rb', line 352

def self.create(val)
  odisabled "Version.create", "Version.new"
  new(val)
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)


12
13
14
# File 'version.rb', line 12

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) ⇒ T.attached_class

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:

  • (T.attached_class)


358
359
360
361
362
363
364
365
366
367
368
369
# File 'version.rb', line 358

def self.parse(spec, detected_from_url: false)
  spec = CGI.unescape(spec.to_s) if detected_from_url

  spec = Pathname(spec)

  VERSION_PARSERS.each do |parser|
    version = parser.parse(spec)
    return new(version, detected_from_url:) if version.present?
  end

  NULL
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)


561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
# File 'version.rb', line 561

def <=>(other)
  other = case other
  when String
    if other.blank?
      # Cannot compare `NULL` to empty string.
      return if null?

      return 1
    end

    # Needed to retain API compatibility with older string comparisons for compiler versions, etc.
    Version.new(other)
  when Integer
    # Used by the `*_build_version` comparisons, which formerly returned an integer.
    Version.new(other.to_s)
  when Token
    if other.null?
      # Cannot compare `NULL` to `NULL`.
      return if null?

      return 1
    end

    Version.new(other.to_s)
  when Version
    if other.null?
      # Cannot compare `NULL` to `NULL`.
      return if null?

      return 1
    end

    other
  when nil
    return 1
  else
    return
  end

  # All `other.null?` cases are handled at this point.
  return -1 if null?

  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

#==(other) ⇒ Boolean Also known as: eql?

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.

For alias eql? == See discussions: - https://github.com/sorbet/sorbet/pull/1443 - https://github.com/sorbet/sorbet/issues/2378

Parameters:

  • other (T.untyped)

Returns:

  • (Boolean)


638
639
640
641
642
643
644
645
646
# File 'version.rb', line 638

def ==(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 #<=>
  return false if null?

  super
end

#commitString?

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.

Return the commit for a HEAD version.

Returns:



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

def commit
  version&.match(HEAD_VERSION_REGEX)&.[](:commit)
end

#compare(comparator, other) ⇒ 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.

Parameters:

Returns:

  • (Boolean)


548
549
550
551
552
553
554
555
556
557
558
# File 'version.rb', line 548

def compare(comparator, other)
  case comparator
  when ">=" then self >= other
  when ">" then self > other
  when "<" then self < other
  when "<=" then self <= other
  when "==" then self == other
  when "!=" then self != other
  else raise ArgumentError, "Unknown comparator: #{comparator}"
  end
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)


511
512
513
# File 'version.rb', line 511

def detected_from_url?
  @detected_from_url
end

#freezeT.self_type

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:

  • (T.self_type)


740
741
742
743
# File 'version.rb', line 740

def freeze
  tokens # Determine and store tokens before freezing
  super
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)


692
693
694
# File 'version.rb', line 692

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.

Check if this is a HEAD version.

Returns:

  • (Boolean)


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

def head?
  version&.match?(HEAD_VERSION_REGEX) || false
end

#majorToken?

Returns:



651
652
653
654
655
# File 'version.rb', line 651

def major
  return NULL_TOKEN if null?

  tokens.first
end

#major_minorT.self_type

Returns:

  • (T.self_type)


675
676
677
678
679
680
# File 'version.rb', line 675

def major_minor
  return self if null?

  major_minor = T.must(tokens[0..1])
  major_minor.empty? ? NULL : self.class.new(major_minor.join("."))
end

#major_minor_patchT.self_type

Returns:

  • (T.self_type)


684
685
686
687
688
689
# File 'version.rb', line 684

def major_minor_patch
  return self if null?

  major_minor_patch = T.must(tokens[0..2])
  major_minor_patch.empty? ? NULL : self.class.new(major_minor_patch.join("."))
end

#minorToken?

Returns:



659
660
661
662
663
# File 'version.rb', line 659

def minor
  return NULL_TOKEN if null?

  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)


543
544
545
# File 'version.rb', line 543

def null?
  version.nil?
end

#patchToken?

Returns:



667
668
669
670
671
# File 'version.rb', line 667

def patch
  return NULL_TOKEN if null?

  tokens.third
end

#respond_to?(method, include_all = T.unsafe(nil)) ⇒ 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.

Parameters:

  • method (Symbol, String)
  • include_all (Boolean) (defaults to: T.unsafe(nil))

Returns:

  • (Boolean)


725
726
727
728
729
# File 'version.rb', line 725

def respond_to?(method, include_all = T.unsafe(nil))
  return !null? if ["to_str", :to_str].include?(method)

  super
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)


697
698
699
700
701
# File 'version.rb', line 697

def to_f
  return Float::NAN if null?

  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)


704
705
706
# File 'version.rb', line 704

def to_i
  version.to_i
end

#to_json(*options) ⇒ String

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:

  • options (T.untyped)

Returns:



720
721
722
# File 'version.rb', line 720

def to_json(*options)
  version.to_json(*options)
end

#to_strString

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:

Raises:

  • (NoMethodError)


709
710
711
712
713
# File 'version.rb', line 709

def to_str
  raise NoMethodError, "undefined method `to_str' for #{self.class}:NULL" if null?

  T.must(version).to_str
end

#update_commit(commit) ⇒ 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.

This method returns an undefined value.

Update the commit for a HEAD version.

Parameters:

Raises:

  • (ArgumentError)


532
533
534
535
536
537
538
539
540
# File 'version.rb', line 532

def update_commit(commit)
  raise ArgumentError, "Cannot update commit for non-HEAD version." unless head?

  @version = if commit
    "HEAD-#{commit}"
  else
    "HEAD"
  end
end