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.

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)


519
520
521
522
523
524
525
# File 'version.rb', line 519

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, tag: nil) ⇒ 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:



361
362
363
# File 'version.rb', line 361

def self.detect(url, tag: nil)
  parse(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
14
15
16
17
18
# File 'version.rb', line 11

def self.formula_optionally_versioned_regex(name, full: true)
  name = name.to_s
  suffix = "-full" if name.end_with?("-full")
  anchor_start = full ? "^" : ""
  name_pattern = Regexp.escape(name.delete_suffix("-full"))
  anchor_end = full ? "$" : ""
  /#{anchor_start}#{name_pattern}(?:@\d[\d.]*)?#{suffix}#{anchor_end}/
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:



366
367
368
369
370
371
372
373
374
375
376
377
378
# File 'version.rb', line 366

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

#blank? ⇒ 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)


569
570
571
# File 'version.rb', line 569

def blank?
  version.nil?
end

#commit ⇒ String?

Return the commit for a HEAD version.

Returns:



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

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)


574
575
576
577
578
579
580
581
582
583
584
# File 'version.rb', line 574

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)


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

def detected_from_url?
  @detected_from_url
end

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


811
812
813
814
# File 'version.rb', line 811

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

#head? ⇒ Boolean

Check if this is a HEAD version.

Returns:

  • (Boolean)


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

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

#major ⇒ Token?

The major version.

Returns:



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

def major
  return NULL_TOKEN if null?

  tokens.first
end

#major_minor ⇒ T.self_type

The major and minor version.

Returns:

  • (T.self_type)


720
721
722
723
724
725
726
727
728
729
# File 'version.rb', line 720

def major_minor
  return self if null?

  major_minor = tokens.first(2)
  if major_minor.empty?
    NULL
  else
    self.class.new(major_minor.join("."))
  end
end

#major_minor_patch ⇒ T.self_type

The major, minor and patch version.

Returns:

  • (T.self_type)


735
736
737
738
739
740
741
742
743
744
# File 'version.rb', line 735

def major_minor_patch
  return self if null?

  major_minor_patch = tokens.first(3)
  if major_minor_patch.empty?
    NULL
  else
    self.class.new(major_minor_patch.join("."))
  end
end

#minor ⇒ Token?

The minor version.

Returns:



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

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)


564
565
566
# File 'version.rb', line 564

def null?
  version.nil?
end

#patch ⇒ Token?

The patch version.

Returns:



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

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)


797
798
799
800
801
# File 'version.rb', line 797

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

  super
end

#to_f ⇒ Float

Convert the version to a floating-point number.

Returns:

  • (Float)


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

def to_f
  return Float::NAN if null?

  version.to_f
end

#to_i ⇒ Integer

Convert the version to an integer.

Returns:



771
772
773
# File 'version.rb', line 771

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:



794
# File 'version.rb', line 794

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

#to_str ⇒ String

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)


780
781
782
783
784
785
# File 'version.rb', line 780

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

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


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

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

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