diff --git a/Library/Homebrew/cask/lib/hbc/cli/search.rb b/Library/Homebrew/cask/lib/hbc/cli/search.rb index addc35c98c..bda5374d53 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/search.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/search.rb @@ -36,7 +36,7 @@ module Hbc partial_matches = simplified_tokens.grep(/#{simplified_search_term}/i) { |t| all_tokens[simplified_tokens.index(t)] } end - _, remote_matches = search_taps(search_term, silent: true) + remote_matches = search_taps(search_term, silent: true)[:casks] [partial_matches, remote_matches, search_term] end diff --git a/Library/Homebrew/cmd/install.rb b/Library/Homebrew/cmd/install.rb index cd409480ac..fc1f56fc0a 100644 --- a/Library/Homebrew/cmd/install.rb +++ b/Library/Homebrew/cmd/install.rb @@ -281,7 +281,7 @@ module Homebrew # Do not search taps if the formula name is qualified return if e.name.include?("/") ohai "Searching taps..." - taps_search_results, = search_taps(e.name) + taps_search_results = search_taps(e.name)[:formulae] case taps_search_results.length when 0 ofail "No formulae found in taps." diff --git a/Library/Homebrew/cmd/search.rb b/Library/Homebrew/cmd/search.rb index 6d8ffe169c..dd45fe63ad 100644 --- a/Library/Homebrew/cmd/search.rb +++ b/Library/Homebrew/cmd/search.rb @@ -61,12 +61,12 @@ module Homebrew elsif args.remaining.first =~ HOMEBREW_TAP_FORMULA_REGEX query = args.remaining.first - begin - result = Formulary.factory(query).name - results = Array(result) + results = begin + [Formulary.factory(query).name] rescue FormulaUnavailableError _, _, name = query.split("/", 3) - results = search_taps(name).flatten.sort + remote_results = search_taps(name) + [*remote_results[:formulae], *remote_results[:casks]].sort end puts Formatter.columns(results) unless results.empty? @@ -76,7 +76,8 @@ module Homebrew local_results = search_formulae(string_or_regex) 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? if $stdout.tty? diff --git a/Library/Homebrew/search.rb b/Library/Homebrew/search.rb index 6b97e1ad87..bbabc2120d 100644 --- a/Library/Homebrew/search.rb +++ b/Library/Homebrew/search.rb @@ -13,36 +13,42 @@ module Homebrew end 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 # Use stderr to avoid breaking parsed output $stderr.puts Formatter.headline("Searching taps on GitHub...", color: :blue) end - matches = GitHub.search_code( - user: "Homebrew", - path: ["Formula", "Casks", "."], - filename: query, - extension: "rb", - ) + matches = begin + GitHub.search_code( + user: "Homebrew", + path: ["Formula", "Casks", "."], + filename: query, + 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") tap = Tap.fetch(match["repository"]["full_name"]) full_name = "#{tap.name}/#{name}" - if tap.installed? && !match["path"].start_with?("Casks/") - [formulae, casks] - elsif match["path"].start_with?("Casks/") - [formulae, [*casks, full_name].sort] + next if tap.installed? && !match["path"].start_with?("Casks/") + + if match["path"].start_with?("Casks/") + results[:casks] = [*results[:casks], full_name].sort else - [[*formulae, full_name].sort, casks] + results[:formulae] = [*results[:formulae], full_name].sort end end - rescue GitHub::Error => error - opoo "Error searching on GitHub: #{error}\n" - [[], []] + + results end def search_formulae(string_or_regex) diff --git a/Library/Homebrew/test/search_spec.rb b/Library/Homebrew/test/search_spec.rb index b64c4151b6..1afcb1314a 100644 --- a/Library/Homebrew/test/search_spec.rb +++ b/Library/Homebrew/test/search_spec.rb @@ -14,14 +14,14 @@ describe Homebrew::Search do it "does not raise if `HOMEBREW_NO_GITHUB_API` is set" do 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 it "does not raise if the network fails" do allow(GitHub).to receive(:open_api).and_raise(GitHub::Error) expect(mod.search_taps("some-formula")) - .to match([[], []]) + .to match(formulae: [], casks: []) end 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) 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