search: cache remote tap formulae

Closes Homebrew/homebrew#40775.

Signed-off-by: Xu Cheng <xucheng@me.com>
This commit is contained in:
Xu Cheng 2015-06-16 17:55:19 +08:00
parent 7a59a3ee37
commit 6af47815b0

View File

@ -108,36 +108,36 @@ module Homebrew
return [] return []
end end
results = [] @@remote_tap_formulae ||= Hash.new do |cache, key|
tree = {} user, repo = key.split("/", 2)
tree = {}
GitHub.open "https://api.github.com/repos/#{user}/homebrew-#{repo}/git/trees/HEAD?recursive=1" do |json| GitHub.open "https://api.github.com/repos/#{user}/homebrew-#{repo}/git/trees/HEAD?recursive=1" do |json|
user = user.downcase if user == "Homebrew" # special handling for the Homebrew organization json["tree"].each do |object|
json["tree"].each do |object| next unless object["type"] == "blob"
next unless object["type"] == "blob"
subtree, file = File.split(object["path"]) subtree, file = File.split(object["path"])
if File.extname(file) == ".rb" if File.extname(file) == ".rb"
tree[subtree] ||= [] tree[subtree] ||= []
tree[subtree] << file tree[subtree] << file
end
end end
end end
paths = tree["Formula"] || tree["HomebrewFormula"] || tree["Casks"] || tree["."] || []
cache[key] = paths.map { |path| File.basename(path, ".rb") }
end end
paths = tree["Formula"] || tree["HomebrewFormula"] || tree["Casks"] || tree["."] || [] names = @@remote_tap_formulae["#{user}/#{repo}"]
paths.each do |path| user = user.downcase if user == "Homebrew" # special handling for the Homebrew organization
name = File.basename(path, ".rb") names.select { |name| rx === name }.map { |name| "#{user}/#{repo}/#{name}" }
results << "#{user}/#{repo}/#{name}" if rx === name
end
rescue GitHub::HTTPNotFoundError => e rescue GitHub::HTTPNotFoundError => e
opoo "Failed to search tap: #{user}/#{repo}. Please run `brew update`" opoo "Failed to search tap: #{user}/#{repo}. Please run `brew update`"
[] []
rescue GitHub::Error => e rescue GitHub::Error => e
SEARCH_ERROR_QUEUE << e SEARCH_ERROR_QUEUE << e
[] []
else
results
end end
def search_formulae rx def search_formulae rx