Skip to content

Commit 984dcf8

Browse files
API: Load casks/formula from JSON with missing keys
We'd like to reduce the size of the API JSON and to do that we are going to remove unused and/or blank elements from the cask/formula definition. This will reduce the amount of data that has to go over the wire and make it easier to load this data into memory.
1 parent 080e61f commit 984dcf8

File tree

4 files changed

+12
-11
lines changed

4 files changed

+12
-11
lines changed

Library/Homebrew/api.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,12 @@ def self.fetch_json_api_file(endpoint, target: HOMEBREW_CACHE_API/endpoint,
124124

125125
sig { params(json: Hash).returns(Hash) }
126126
def self.merge_variations(json)
127+
return json unless json.key?("variations")
128+
127129
bottle_tag = ::Utils::Bottles::Tag.new(system: Homebrew::SimulateSystem.current_os,
128130
arch: Homebrew::SimulateSystem.current_arch)
129131

130-
if (variations = json["variations"].presence) &&
131-
(variation = variations[bottle_tag.to_s].presence)
132+
if (variation = json.dig("variations", bottle_tag.to_s).presence)
132133
json = json.merge(variation)
133134
end
134135

Library/Homebrew/cask/cask.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ def tap_git_head
279279
def populate_from_api!(json_cask)
280280
raise ArgumentError, "Expected cask to be loaded from the API" unless loaded_from_api?
281281

282-
@languages = json_cask[:languages]
282+
@languages = json_cask.fetch(:languages, [])
283283
@tap_git_head = json_cask.fetch(:tap_git_head, "HEAD")
284284

285285
@ruby_source_path = json_cask[:ruby_source_path]

Library/Homebrew/cask/cask_loader.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def load(config:)
280280

281281
url json_cask[:url], **json_cask.fetch(:url_specs, {}) if json_cask[:url].present?
282282
appcast json_cask[:appcast] if json_cask[:appcast].present?
283-
json_cask[:name].each do |cask_name|
283+
json_cask[:name]&.each do |cask_name|
284284
name cask_name
285285
end
286286
desc json_cask[:desc]

Library/Homebrew/formulary.rb

+7-7
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,14 @@ def self.load_formula_from_api(name, flags:)
154154

155155
json_formula = Homebrew::API.merge_variations(json_formula)
156156

157-
uses_from_macos_names = json_formula["uses_from_macos"].map do |dep|
157+
uses_from_macos_names = json_formula["uses_from_macos"]&.map do |dep|
158158
next dep unless dep.is_a? Hash
159159

160160
dep.keys.first
161161
end
162162

163163
requirements = {}
164-
json_formula["requirements"].map do |req|
164+
json_formula["requirements"]&.map do |req|
165165
req_name = req["name"].to_sym
166166
next if API_SUPPORTED_REQUIREMENTS.exclude?(req_name)
167167

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

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

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

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

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

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

314-
json_formula["conflicts_with"].each_with_index do |conflict, index|
314+
json_formula["conflicts_with"]&.each_with_index do |conflict, index|
315315
conflicts_with conflict, because: json_formula.dig("conflicts_with_reasons", index)
316316
end
317317

0 commit comments

Comments
 (0)