Module: BottleAPI Private

Extended by:
T::Sig
Defined in:
bottle_api.rb

Overview

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

Helper functions for using the Bottle JSON API.

Constant Summary collapse

FORMULAE_BREW_SH_BOTTLE_API_DOMAIN =

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.

if OS.mac?
  "https://formulae.brew.sh/api/bottle"
else
  "https://formulae.brew.sh/api/bottle-linux"
end.freeze
FORMULAE_BREW_SH_VERSIONS_API_URL =

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.

if OS.mac?
  "https://formulae.brew.sh/api/versions-formulae.json"
else
  "https://formulae.brew.sh/api/versions-linux.json"
end.freeze
GITHUB_PACKAGES_SHA256_REGEX =

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.

%r{#{GitHubPackages::URL_REGEX}.*/blobs/sha256:(?<sha256>\h{64})$}.freeze

Class Method Summary collapse

Class Method Details

.bottle_available?(name) ⇒ 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)


57
58
59
60
61
62
# File 'bottle_api.rb', line 57

def bottle_available?(name)
  fetch name
  true
rescue ArgumentError
  false
end

.checksum_from_url(url) ⇒ 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:

Returns:



90
91
92
93
94
95
# File 'bottle_api.rb', line 90

def checksum_from_url(url)
  match = url.match GITHUB_PACKAGES_SHA256_REGEX
  return if match.blank?

  match[:sha256]
end

.download_bottle(hash, tag) ⇒ 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.

Parameters:



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'bottle_api.rb', line 98

def download_bottle(hash, tag)
  bottle = hash["bottles"][tag]
  bottle ||= hash["bottles"]["all"]
  return if bottle.blank?

  sha256 = bottle["sha256"] || checksum_from_url(bottle["url"])
  bottle_filename = Bottle::Filename.new(hash["name"], hash["pkg_version"], tag, hash["rebuild"])

  resource = Resource.new hash["name"]
  resource.url bottle["url"]
  resource.sha256 sha256
  resource.version hash["pkg_version"]
  resource.downloader.resolved_basename = bottle_filename

  resource.fetch

  # Map the name of this formula to the local bottle path to allow the
  # formula to be loaded by passing just the name to `Formulary::factory`.
  Formulary.map_formula_name_to_local_bottle_path hash["name"], resource.downloader.cached_location
end

.fetch(name) ⇒ Hash

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:

  • (Hash)


29
30
31
32
33
34
35
36
37
38
39
40
# File 'bottle_api.rb', line 29

def fetch(name)
  return @cache[name] if @cache.present? && @cache.key?(name)

  api_url = "#{FORMULAE_BREW_SH_BOTTLE_API_DOMAIN}/#{name}.json"
  output = Utils::Curl.curl_output("--fail", api_url)
  raise ArgumentError, "No JSON file found at #{Tty.underline}#{api_url}#{Tty.reset}" unless output.success?

  @cache ||= {}
  @cache[name] = JSON.parse(output.stdout)
rescue JSON::ParserError
  raise ArgumentError, "Invalid JSON file: #{Tty.underline}#{api_url}#{Tty.reset}"
end

.fetch_bottles(name) ⇒ 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.

Parameters:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'bottle_api.rb', line 65

def fetch_bottles(name)
  hash = fetch(name)
  bottle_tag = Utils::Bottles.tag.to_s

  if !hash["bottles"].key?(bottle_tag) && !hash["bottles"].key?("all")
    odie "No bottle available for #{name} on the current OS"
  end

  download_bottle(hash, bottle_tag)

  hash["dependencies"].each do |dep_hash|
    existing_formula = begin
      Formulary.factory dep_hash["name"]
    rescue FormulaUnavailableError
      # The formula might not exist if it's not installed and homebrew/core isn't tapped
      nil
    end

    next if existing_formula.present? && existing_formula.latest_version_installed?

    download_bottle(dep_hash, bottle_tag)
  end
end

.latest_pkg_version(name) ⇒ PkgVersion?

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:



43
44
45
46
47
48
49
50
51
52
53
54
# File 'bottle_api.rb', line 43

def latest_pkg_version(name)
  @formula_versions ||= begin
    output = Utils::Curl.curl_output("--fail", FORMULAE_BREW_SH_VERSIONS_API_URL)
    JSON.parse(output.stdout)
  end

  return unless @formula_versions.key? name

  version = Version.new(@formula_versions[name]["version"])
  revision = @formula_versions[name]["revision"]
  PkgVersion.new(version, revision)
end