Class: Version

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

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)


499
500
501
502
503
504
505
# File 'version.rb', line 499

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:



349
350
351
352
# File 'version.rb', line 349

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:



344
345
346
# File 'version.rb', line 344

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)


14
15
16
# File 'version.rb', line 14

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)


355
356
357
358
359
360
361
362
363
364
365
366
# File 'version.rb', line 355

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)


558
559
560
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
# File 'version.rb', line 558

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?

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)


635
636
637
638
639
640
641
642
643
# File 'version.rb', line 635

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:



523
524
525
# File 'version.rb', line 523

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)


545
546
547
548
549
550
551
552
553
554
555
# File 'version.rb', line 545

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)


508
509
510
# File 'version.rb', line 508

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)


737
738
739
740
# File 'version.rb', line 737

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)


689
690
691
# File 'version.rb', line 689

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)


517
518
519
# File 'version.rb', line 517

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

#inspectString

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:



730
731
732
733
734
# File 'version.rb', line 730

def inspect
  return "#<Version::NULL>" if null?

  super
end

#majorToken?

Returns:



648
649
650
651
652
# File 'version.rb', line 648

def major
  return NULL_TOKEN if null?

  tokens.first
end

#major_minorT.self_type

Returns:

  • (T.self_type)


672
673
674
675
676
677
# File 'version.rb', line 672

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)


681
682
683
684
685
686
# File 'version.rb', line 681

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:



656
657
658
659
660
# File 'version.rb', line 656

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)


540
541
542
# File 'version.rb', line 540

def null?
  version.nil?
end

#patchToken?

Returns:



664
665
666
667
668
# File 'version.rb', line 664

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)


723
724
725
726
727
# File 'version.rb', line 723

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)


694
695
696
697
698
# File 'version.rb', line 694

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)


701
702
703
# File 'version.rb', line 701

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:



718
719
720
# File 'version.rb', line 718

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

#to_sString

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:



706
707
708
# File 'version.rb', line 706

def to_s
  version.to_s
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)


711
712
713
714
715
# File 'version.rb', line 711

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)


529
530
531
532
533
534
535
536
537
# File 'version.rb', line 529

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

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