Skip to content

Commit

Permalink
Clean up files that use to include cachable
Browse files Browse the repository at this point in the history
These were changed to extend to make it easier to determine
where the classes come to in the extended callback but that
means that the file is somewhat inconsistent. On the one
hand we're using class methods and on the other we're extend
self. This cleans that up but now the diff is atrocious and
the blame is even worse. Oh well...
  • Loading branch information
apainintheneck committed Feb 27, 2024
1 parent 5cc1c85 commit bea2dc6
Show file tree
Hide file tree
Showing 3 changed files with 217 additions and 225 deletions.
114 changes: 56 additions & 58 deletions Library/Homebrew/api/cask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,80 +12,78 @@ module API
module Cask
extend Cachable

class << self
private :cache
private_class_method :cache

sig { params(token: String).returns(Hash) }
def fetch(token)
Homebrew::API.fetch "cask/#{token}.json"
end

sig { params(cask: ::Cask::Cask).returns(::Cask::Cask) }
def source_download(cask)
path = cask.ruby_source_path.to_s || "Casks/#{cask.token}.rb"
sha256 = cask.ruby_source_checksum[:sha256]
checksum = Checksum.new(sha256) if sha256
git_head = cask.tap_git_head || "HEAD"
tap = cask.tap&.full_name || "Homebrew/homebrew-cask"

download = Homebrew::API::Download.new(
"https://raw.githubusercontent.com/#{tap}/#{git_head}/#{path}",
checksum,
mirrors: [
"#{HOMEBREW_API_DEFAULT_DOMAIN}/cask-source/#{File.basename(path)}",
],
cache: HOMEBREW_CACHE_API_SOURCE/"#{tap}/#{git_head}/Cask",
)
download.fetch
::Cask::CaskLoader::FromPathLoader.new(download.symlink_location)
.load(config: cask.config)
end
sig { params(token: String).returns(Hash) }
def self.fetch(token)
Homebrew::API.fetch "cask/#{token}.json"
end

sig { returns(T::Boolean) }
def download_and_cache_data!
json_casks, updated = Homebrew::API.fetch_json_api_file "cask.jws.json"
sig { params(cask: ::Cask::Cask).returns(::Cask::Cask) }
def self.source_download(cask)
path = cask.ruby_source_path.to_s || "Casks/#{cask.token}.rb"
sha256 = cask.ruby_source_checksum[:sha256]

Check warning on line 25 in Library/Homebrew/api/cask.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/api/cask.rb#L24-L25

Added lines #L24 - L25 were not covered by tests
checksum = Checksum.new(sha256) if sha256
git_head = cask.tap_git_head || "HEAD"

Check warning on line 27 in Library/Homebrew/api/cask.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/api/cask.rb#L27

Added line #L27 was not covered by tests
tap = cask.tap&.full_name || "Homebrew/homebrew-cask"

download = Homebrew::API::Download.new(

Check warning on line 30 in Library/Homebrew/api/cask.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/api/cask.rb#L30

Added line #L30 was not covered by tests
"https://raw.githubusercontent.com/#{tap}/#{git_head}/#{path}",
checksum,
mirrors: [
"#{HOMEBREW_API_DEFAULT_DOMAIN}/cask-source/#{File.basename(path)}",
],
cache: HOMEBREW_CACHE_API_SOURCE/"#{tap}/#{git_head}/Cask",
)
download.fetch
::Cask::CaskLoader::FromPathLoader.new(download.symlink_location)

Check warning on line 39 in Library/Homebrew/api/cask.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/api/cask.rb#L38-L39

Added lines #L38 - L39 were not covered by tests
.load(config: cask.config)
end

cache["renames"] = {}
cache["casks"] = json_casks.to_h do |json_cask|
token = json_cask["token"]
sig { returns(T::Boolean) }
def self.download_and_cache_data!
json_casks, updated = Homebrew::API.fetch_json_api_file "cask.jws.json"

json_cask.fetch("old_tokens", []).each do |old_token|
cache["renames"][old_token] = token
end
cache["renames"] = {}
cache["casks"] = json_casks.to_h do |json_cask|
token = json_cask["token"]

[token, json_cask.except("token")]
json_cask.fetch("old_tokens", []).each do |old_token|
cache["renames"][old_token] = token
end

updated
[token, json_cask.except("token")]
end
private :download_and_cache_data!

sig { returns(T::Hash[String, Hash]) }
def all_casks
unless cache.key?("casks")
json_updated = download_and_cache_data!
write_names(regenerate: json_updated)
end
updated
end
private_class_method :download_and_cache_data!

cache.fetch("casks")
sig { returns(T::Hash[String, Hash]) }
def self.all_casks
unless cache.key?("casks")
json_updated = download_and_cache_data!
write_names(regenerate: json_updated)
end

sig { returns(T::Hash[String, String]) }
def all_renames
unless cache.key?("renames")
json_updated = download_and_cache_data!
write_names(regenerate: json_updated)
end
cache.fetch("casks")
end

cache.fetch("renames")
sig { returns(T::Hash[String, String]) }
def self.all_renames
unless cache.key?("renames")
json_updated = download_and_cache_data!
write_names(regenerate: json_updated)

Check warning on line 76 in Library/Homebrew/api/cask.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/api/cask.rb#L76

Added line #L76 was not covered by tests
end

sig { params(regenerate: T::Boolean).void }
def write_names(regenerate: false)
download_and_cache_data! unless cache.key?("casks")
cache.fetch("renames")

Check warning on line 79 in Library/Homebrew/api/cask.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/api/cask.rb#L79

Added line #L79 was not covered by tests
end

Homebrew::API.write_names_file(all_casks.keys, "cask", regenerate: regenerate)
end
sig { params(regenerate: T::Boolean).void }
def self.write_names(regenerate: false)
download_and_cache_data! unless cache.key?("casks")

Homebrew::API.write_names_file(all_casks.keys, "cask", regenerate: regenerate)
end
end
end
Expand Down
136 changes: 67 additions & 69 deletions Library/Homebrew/api/formula.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,93 +12,91 @@ module API
module Formula
extend Cachable

class << self
private :cache
private_class_method :cache

sig { params(name: String).returns(Hash) }
def fetch(name)
Homebrew::API.fetch "formula/#{name}.json"
end
sig { params(name: String).returns(Hash) }
def self.fetch(name)
Homebrew::API.fetch "formula/#{name}.json"

Check warning on line 19 in Library/Homebrew/api/formula.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/api/formula.rb#L19

Added line #L19 was not covered by tests
end

sig { params(formula: ::Formula).returns(::Formula) }
def source_download(formula)
path = formula.ruby_source_path || "Formula/#{formula.name}.rb"
git_head = formula.tap_git_head || "HEAD"
tap = formula.tap&.full_name || "Homebrew/homebrew-core"

download = Homebrew::API::Download.new(
"https://raw.githubusercontent.com/#{tap}/#{git_head}/#{path}",
formula.ruby_source_checksum,
cache: HOMEBREW_CACHE_API_SOURCE/"#{tap}/#{git_head}/Formula",
)
download.fetch
Formulary.factory(download.symlink_location,
formula.active_spec_sym,
alias_path: formula.alias_path,
flags: formula.class.build_flags)
end
sig { params(formula: ::Formula).returns(::Formula) }
def self.source_download(formula)
path = formula.ruby_source_path || "Formula/#{formula.name}.rb"
git_head = formula.tap_git_head || "HEAD"

Check warning on line 25 in Library/Homebrew/api/formula.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/api/formula.rb#L24-L25

Added lines #L24 - L25 were not covered by tests
tap = formula.tap&.full_name || "Homebrew/homebrew-core"

download = Homebrew::API::Download.new(

Check warning on line 28 in Library/Homebrew/api/formula.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/api/formula.rb#L28

Added line #L28 was not covered by tests
"https://raw.githubusercontent.com/#{tap}/#{git_head}/#{path}",
formula.ruby_source_checksum,
cache: HOMEBREW_CACHE_API_SOURCE/"#{tap}/#{git_head}/Formula",
)
download.fetch
Formulary.factory(download.symlink_location,

Check warning on line 34 in Library/Homebrew/api/formula.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/api/formula.rb#L33-L34

Added lines #L33 - L34 were not covered by tests
formula.active_spec_sym,
alias_path: formula.alias_path,
flags: formula.class.build_flags)
end

sig { returns(T::Boolean) }
def download_and_cache_data!
json_formulae, updated = Homebrew::API.fetch_json_api_file "formula.jws.json"

cache["aliases"] = {}
cache["renames"] = {}
cache["formulae"] = json_formulae.to_h do |json_formula|
json_formula["aliases"].each do |alias_name|
cache["aliases"][alias_name] = json_formula["name"]
end
(json_formula["oldnames"] || [json_formula["oldname"]].compact).each do |oldname|
cache["renames"][oldname] = json_formula["name"]
end

[json_formula["name"], json_formula.except("name")]
sig { returns(T::Boolean) }
def self.download_and_cache_data!
json_formulae, updated = Homebrew::API.fetch_json_api_file "formula.jws.json"

cache["aliases"] = {}
cache["renames"] = {}
cache["formulae"] = json_formulae.to_h do |json_formula|
json_formula["aliases"].each do |alias_name|
cache["aliases"][alias_name] = json_formula["name"]
end
(json_formula["oldnames"] || [json_formula["oldname"]].compact).each do |oldname|
cache["renames"][oldname] = json_formula["name"]
end

updated
[json_formula["name"], json_formula.except("name")]
end
private :download_and_cache_data!

sig { returns(T::Hash[String, Hash]) }
def all_formulae
unless cache.key?("formulae")
json_updated = download_and_cache_data!
write_names_and_aliases(regenerate: json_updated)
end
updated
end
private_class_method :download_and_cache_data!

cache["formulae"]
sig { returns(T::Hash[String, Hash]) }
def self.all_formulae
unless cache.key?("formulae")
json_updated = download_and_cache_data!
write_names_and_aliases(regenerate: json_updated)
end

sig { returns(T::Hash[String, String]) }
def all_aliases
unless cache.key?("aliases")
json_updated = download_and_cache_data!
write_names_and_aliases(regenerate: json_updated)
end
cache["formulae"]
end

cache["aliases"]
sig { returns(T::Hash[String, String]) }
def self.all_aliases
unless cache.key?("aliases")
json_updated = download_and_cache_data!
write_names_and_aliases(regenerate: json_updated)
end

sig { returns(T::Hash[String, String]) }
def all_renames
unless cache.key?("renames")
json_updated = download_and_cache_data!
write_names_and_aliases(regenerate: json_updated)
end
cache["aliases"]
end

cache["renames"]
sig { returns(T::Hash[String, String]) }
def self.all_renames
unless cache.key?("renames")
json_updated = download_and_cache_data!
write_names_and_aliases(regenerate: json_updated)

Check warning on line 85 in Library/Homebrew/api/formula.rb

View check run for this annotation

Codecov / codecov/patch

Library/Homebrew/api/formula.rb#L85

Added line #L85 was not covered by tests
end

sig { params(regenerate: T::Boolean).void }
def write_names_and_aliases(regenerate: false)
download_and_cache_data! unless cache.key?("formulae")
cache["renames"]
end

sig { params(regenerate: T::Boolean).void }
def self.write_names_and_aliases(regenerate: false)
download_and_cache_data! unless cache.key?("formulae")

return unless Homebrew::API.write_names_file(all_formulae.keys, "formula", regenerate: regenerate)
return unless Homebrew::API.write_names_file(all_formulae.keys, "formula", regenerate: regenerate)

(HOMEBREW_CACHE_API/"formula_aliases.txt").open("w") do |file|
all_aliases.each do |alias_name, real_name|
file.puts "#{alias_name}|#{real_name}"
end
(HOMEBREW_CACHE_API/"formula_aliases.txt").open("w") do |file|
all_aliases.each do |alias_name, real_name|
file.puts "#{alias_name}|#{real_name}"
end
end
end
Expand Down
Loading

0 comments on commit bea2dc6

Please sign in to comment.