Skip to content

Commit

Permalink
Merge pull request #16420 from Homebrew/allow-cask-formula-loading-fr…
Browse files Browse the repository at this point in the history
…om-api-hash-missing-more-keys

API: Load casks/formula from JSON with missing keys
  • Loading branch information
MikeMcQuaid committed Jan 2, 2024
2 parents 080e61f + 984dcf8 commit 3e027de
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
5 changes: 3 additions & 2 deletions Library/Homebrew/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,12 @@ def self.fetch_json_api_file(endpoint, target: HOMEBREW_CACHE_API/endpoint,

sig { params(json: Hash).returns(Hash) }
def self.merge_variations(json)
return json unless json.key?("variations")

bottle_tag = ::Utils::Bottles::Tag.new(system: Homebrew::SimulateSystem.current_os,
arch: Homebrew::SimulateSystem.current_arch)

if (variations = json["variations"].presence) &&
(variation = variations[bottle_tag.to_s].presence)
if (variation = json.dig("variations", bottle_tag.to_s).presence)
json = json.merge(variation)
end

Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/cask/cask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ def tap_git_head
def populate_from_api!(json_cask)
raise ArgumentError, "Expected cask to be loaded from the API" unless loaded_from_api?

@languages = json_cask[:languages]
@languages = json_cask.fetch(:languages, [])
@tap_git_head = json_cask.fetch(:tap_git_head, "HEAD")

@ruby_source_path = json_cask[:ruby_source_path]
Expand Down
2 changes: 1 addition & 1 deletion Library/Homebrew/cask/cask_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def load(config:)

url json_cask[:url], **json_cask.fetch(:url_specs, {}) if json_cask[:url].present?
appcast json_cask[:appcast] if json_cask[:appcast].present?
json_cask[:name].each do |cask_name|
json_cask[:name]&.each do |cask_name|
name cask_name
end
desc json_cask[:desc]
Expand Down
14 changes: 7 additions & 7 deletions Library/Homebrew/formulary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,14 @@ def self.load_formula_from_api(name, flags:)

json_formula = Homebrew::API.merge_variations(json_formula)

uses_from_macos_names = json_formula["uses_from_macos"].map do |dep|
uses_from_macos_names = json_formula["uses_from_macos"]&.map do |dep|
next dep unless dep.is_a? Hash

dep.keys.first
end

requirements = {}
json_formula["requirements"].map do |req|
json_formula["requirements"]&.map do |req|
req_name = req["name"].to_sym
next if API_SUPPORTED_REQUIREMENTS.exclude?(req_name)

Expand All @@ -176,7 +176,7 @@ def self.load_formula_from_api(name, flags:)

req_tags = []
req_tags << req_version if req_version.present?
req_tags += req["contexts"].map do |tag|
req_tags += req["contexts"]&.map do |tag|
case tag
when String
tag.to_sym
Expand All @@ -202,7 +202,7 @@ def self.load_formula_from_api(name, flags:)

dep_json = json_formula.fetch("#{spec}_dependencies", json_formula)

dep_json["dependencies"].each do |dep|
dep_json["dependencies"]&.each do |dep|
# Backwards compatibility check - uses_from_macos used to be a part of dependencies on Linux
next if !json_formula.key?("uses_from_macos_bounds") && uses_from_macos_names.include?(dep) &&
!Homebrew::SimulateSystem.simulating_or_running_on_macos?
Expand All @@ -211,7 +211,7 @@ def self.load_formula_from_api(name, flags:)
end

[:build, :test, :recommended, :optional].each do |type|
dep_json["#{type}_dependencies"].each do |dep|
dep_json["#{type}_dependencies"]&.each do |dep|
# Backwards compatibility check - uses_from_macos used to be a part of dependencies on Linux
next if !json_formula.key?("uses_from_macos_bounds") && uses_from_macos_names.include?(dep) &&
!Homebrew::SimulateSystem.simulating_or_running_on_macos?
Expand All @@ -220,7 +220,7 @@ def self.load_formula_from_api(name, flags:)
end
end

dep_json["uses_from_macos"].each_with_index do |dep, index|
dep_json["uses_from_macos"]&.each_with_index do |dep, index|
bounds = dep_json.fetch("uses_from_macos_bounds", [])[index] || {}
bounds.deep_transform_keys!(&:to_sym)
bounds.deep_transform_values! { |val| val.is_a?(String) ? val.to_sym : val }
Expand Down Expand Up @@ -311,7 +311,7 @@ def self.load_formula_from_api(name, flags:)
disable! date: disable_date, because: reason
end

json_formula["conflicts_with"].each_with_index do |conflict, index|
json_formula["conflicts_with"]&.each_with_index do |conflict, index|
conflicts_with conflict, because: json_formula.dig("conflicts_with_reasons", index)
end

Expand Down

0 comments on commit 3e027de

Please sign in to comment.