Class: Homebrew::BundleVersion Private

Inherits:
Object
  • Object
show all
Extended by:
SystemCommand::Mixin
Includes:
Comparable
Defined in:
bundle_version.rb

Overview

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.

Representation of a macOS bundle version, commonly found in Info.plist files.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SystemCommand::Mixin

system_command, system_command!

Constructor Details

#initialize(short_version, version) ⇒ 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)


52
53
54
55
56
57
58
59
60
61
62
# File 'bundle_version.rb', line 52

def initialize(short_version, version)
  # Remove version from short version, if present.
  short_version = short_version&.sub(/\s*\(#{Regexp.escape(version)}\)\Z/, "") if version

  @short_version = short_version.presence
  @version = version.presence

  return if @short_version || @version

  raise ArgumentError, "`short_version` and `version` cannot both be `nil` or empty"
end

Instance Attribute Details

#short_versionString? (readonly)

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:



49
50
51
# File 'bundle_version.rb', line 49

def short_version
  @short_version
end

#versionString? (readonly)

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:



49
50
51
# File 'bundle_version.rb', line 49

def version
  @version
end

Class Method Details

.from_info_plist(info_plist_path) ⇒ 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:

Returns:

  • (T.attached_class, nil)


16
17
18
19
# File 'bundle_version.rb', line 16

def self.from_info_plist(info_plist_path)
  plist = system_command!("plutil", args: ["-convert", "xml1", "-o", "-", info_plist_path]).plist
  from_info_plist_content(plist)
end

.from_info_plist_content(plist) ⇒ 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:

Returns:

  • (T.attached_class, nil)


22
23
24
25
26
27
# File 'bundle_version.rb', line 22

def self.from_info_plist_content(plist)
  short_version = plist["CFBundleShortVersionString"].presence
  version = plist["CFBundleVersion"].presence

  new(short_version, version) if short_version || version
end

.from_package_info(package_info_path) ⇒ 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:

Returns:

  • (T.attached_class, nil)


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'bundle_version.rb', line 30

def self.from_package_info(package_info_path)
  require "rexml/document"

  xml = REXML::Document.new(package_info_path.read)

  bundle_version_bundle = xml.get_elements("//pkg-info//bundle-version//bundle").first
  bundle_id = bundle_version_bundle["id"] if bundle_version_bundle
  return if bundle_id.blank?

  bundle = xml.get_elements("//pkg-info//bundle").find { |b| b["id"] == bundle_id }
  return unless bundle

  short_version = bundle["CFBundleShortVersionString"]
  version = bundle["CFBundleVersion"]

  new(short_version, version) if short_version || version
end

Instance Method Details

#<=>(other) ⇒ Object

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.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'bundle_version.rb', line 64

def <=>(other)
  return super unless instance_of?(other.class)

  make_version = ->(v) { v ? Version.new(v) : Version::NULL }

  version = self.version.then(&make_version)
  other_version = other.version.then(&make_version)

  difference = version <=> other_version

  # If `version` is equal or cannot be compared, compare `short_version` instead.
  if difference.nil? || difference.zero?
    short_version = self.short_version.then(&make_version)
    other_short_version = other.short_version.then(&make_version)

    return short_version <=> other_short_version
  end

  difference
end

#==(other) ⇒ Object 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.



85
86
87
# File 'bundle_version.rb', line 85

def ==(other)
  instance_of?(other.class) && short_version == other.short_version && version == other.version
end

#nice_versionString

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.

Create a nicely formatted version (on a best effort basis).

Returns:



92
93
94
# File 'bundle_version.rb', line 92

def nice_version
  nice_parts.join(",")
end