Class: Version Private

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

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:

  • val (String, Version)
  • detected_from_url (Boolean) (defaults to: false)

Raises:

  • (ArgumentError)


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

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

  @version = T.let(version, String)
  @detected_from_url = detected_from_url
end

Class Method Details

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


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

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:



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

def self.parse(spec, detected_from_url: false)
  # This type of full-URL decoding is not technically correct but we only need a rough decode for version parsing.
  spec = URI.decode_www_form_component(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

#commitString?

Return the commit for a HEAD version.

Returns:



529
530
531
# File 'version.rb', line 529

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)


551
552
553
554
555
556
557
558
559
560
561
# File 'version.rb', line 551

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)


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

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)


762
763
764
765
# File 'version.rb', line 762

def freeze
  tokens # Determine and store tokens before freezing
  super
end

#head?Boolean

Check if this is a HEAD version.

Returns:

  • (Boolean)


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

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

#majorToken?

The major version.

Returns:



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

def major
  return NULL_TOKEN if null?

  tokens.first
end

#major_minorT.self_type

The major and minor version.

Returns:

  • (T.self_type)


686
687
688
689
690
691
# File 'version.rb', line 686

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

The major, minor and patch version.

Returns:

  • (T.self_type)


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

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?

The minor version.

Returns:



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

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)


546
547
548
# File 'version.rb', line 546

def null?
  version.nil?
end

#patchToken?

The patch version.

Returns:



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

def patch
  return NULL_TOKEN if null?

  tokens.third
end

#respond_to?(method, include_all = false) ⇒ 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: false)

Returns:

  • (Boolean)


748
749
750
751
752
# File 'version.rb', line 748

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

  super
end

#to_fFloat

Convert the version to a floating-point number.

Returns:

  • (Float)


713
714
715
716
717
# File 'version.rb', line 713

def to_f
  return Float::NAN if null?

  version.to_f
end

#to_iInteger

Convert the version to an integer.

Returns:



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

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:



745
# File 'version.rb', line 745

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

#to_strString

The implicit string conversion of this Version, for use where a String is expected. Raises NoMethodError if this is a NULL version.

Returns:

Raises:

  • (NoMethodError)


732
733
734
735
736
# File 'version.rb', line 732

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)


535
536
537
538
539
540
541
542
543
# File 'version.rb', line 535

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

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