Refactor search_taps.

This commit is contained in:
Markus Reiter 2018-06-06 13:24:55 +02:00
parent 845cb99e29
commit cc93997fb7
5 changed files with 33 additions and 26 deletions

View File

@ -36,7 +36,7 @@ module Hbc
partial_matches = simplified_tokens.grep(/#{simplified_search_term}/i) { |t| all_tokens[simplified_tokens.index(t)] } partial_matches = simplified_tokens.grep(/#{simplified_search_term}/i) { |t| all_tokens[simplified_tokens.index(t)] }
end end
_, remote_matches = search_taps(search_term, silent: true) remote_matches = search_taps(search_term, silent: true)[:casks]
[partial_matches, remote_matches, search_term] [partial_matches, remote_matches, search_term]
end end

View File

@ -281,7 +281,7 @@ module Homebrew
# Do not search taps if the formula name is qualified # Do not search taps if the formula name is qualified
return if e.name.include?("/") return if e.name.include?("/")
ohai "Searching taps..." ohai "Searching taps..."
taps_search_results, = search_taps(e.name) taps_search_results = search_taps(e.name)[:formulae]
case taps_search_results.length case taps_search_results.length
when 0 when 0
ofail "No formulae found in taps." ofail "No formulae found in taps."

View File

@ -61,12 +61,12 @@ module Homebrew
elsif args.remaining.first =~ HOMEBREW_TAP_FORMULA_REGEX elsif args.remaining.first =~ HOMEBREW_TAP_FORMULA_REGEX
query = args.remaining.first query = args.remaining.first
begin results = begin
result = Formulary.factory(query).name [Formulary.factory(query).name]
results = Array(result)
rescue FormulaUnavailableError rescue FormulaUnavailableError
_, _, name = query.split("/", 3) _, _, name = query.split("/", 3)
results = search_taps(name).flatten.sort remote_results = search_taps(name)
[*remote_results[:formulae], *remote_results[:casks]].sort
end end
puts Formatter.columns(results) unless results.empty? puts Formatter.columns(results) unless results.empty?
@ -76,7 +76,8 @@ module Homebrew
local_results = search_formulae(string_or_regex) local_results = search_formulae(string_or_regex)
puts Formatter.columns(local_results.sort) unless local_results.empty? puts Formatter.columns(local_results.sort) unless local_results.empty?
tap_results = search_taps(query).flatten.sort remote_results = search_taps(query)
tap_results = [*remote_results[:formulae], *remote_results[:casks]].sort
puts Formatter.columns(tap_results) unless tap_results.empty? puts Formatter.columns(tap_results) unless tap_results.empty?
if $stdout.tty? if $stdout.tty?

View File

@ -13,36 +13,42 @@ module Homebrew
end end
def search_taps(query, silent: false) def search_taps(query, silent: false)
return [], [] if ENV["HOMEBREW_NO_GITHUB_API"] results = { formulae: [], casks: [] }
return results if ENV["HOMEBREW_NO_GITHUB_API"]
unless silent unless silent
# Use stderr to avoid breaking parsed output # Use stderr to avoid breaking parsed output
$stderr.puts Formatter.headline("Searching taps on GitHub...", color: :blue) $stderr.puts Formatter.headline("Searching taps on GitHub...", color: :blue)
end end
matches = GitHub.search_code( matches = begin
GitHub.search_code(
user: "Homebrew", user: "Homebrew",
path: ["Formula", "Casks", "."], path: ["Formula", "Casks", "."],
filename: query, filename: query,
extension: "rb", extension: "rb",
) )
rescue GitHub::Error => error
opoo "Error searching on GitHub: #{error}\n"
return results
end
matches.inject([[], []]) do |(formulae, casks), match| matches.each do |match|
name = File.basename(match["path"], ".rb") name = File.basename(match["path"], ".rb")
tap = Tap.fetch(match["repository"]["full_name"]) tap = Tap.fetch(match["repository"]["full_name"])
full_name = "#{tap.name}/#{name}" full_name = "#{tap.name}/#{name}"
if tap.installed? && !match["path"].start_with?("Casks/") next if tap.installed? && !match["path"].start_with?("Casks/")
[formulae, casks]
elsif match["path"].start_with?("Casks/") if match["path"].start_with?("Casks/")
[formulae, [*casks, full_name].sort] results[:casks] = [*results[:casks], full_name].sort
else else
[[*formulae, full_name].sort, casks] results[:formulae] = [*results[:formulae], full_name].sort
end end
end end
rescue GitHub::Error => error
opoo "Error searching on GitHub: #{error}\n" results
[[], []]
end end
def search_formulae(string_or_regex) def search_formulae(string_or_regex)

View File

@ -14,14 +14,14 @@ describe Homebrew::Search do
it "does not raise if `HOMEBREW_NO_GITHUB_API` is set" do it "does not raise if `HOMEBREW_NO_GITHUB_API` is set" do
ENV["HOMEBREW_NO_GITHUB_API"] = "1" ENV["HOMEBREW_NO_GITHUB_API"] = "1"
expect(mod.search_taps("some-formula")).to match([[], []]) expect(mod.search_taps("some-formula")).to match(formulae: [], casks: [])
end end
it "does not raise if the network fails" do it "does not raise if the network fails" do
allow(GitHub).to receive(:open_api).and_raise(GitHub::Error) allow(GitHub).to receive(:open_api).and_raise(GitHub::Error)
expect(mod.search_taps("some-formula")) expect(mod.search_taps("some-formula"))
.to match([[], []]) .to match(formulae: [], casks: [])
end end
it "returns Formulae and Casks separately" do it "returns Formulae and Casks separately" do
@ -45,7 +45,7 @@ describe Homebrew::Search do
allow(GitHub).to receive(:open_api).and_yield(json_response) allow(GitHub).to receive(:open_api).and_yield(json_response)
expect(mod.search_taps("some-formula")) expect(mod.search_taps("some-formula"))
.to match([["homebrew/foo/some-formula"], ["homebrew/bar/some-cask"]]) .to match(formulae: ["homebrew/foo/some-formula"], casks: ["homebrew/bar/some-cask"])
end end
end end