Merge pull request #14240 from apainintheneck/add-casks-to-install-search

Show casks in install not found output
This commit is contained in:
Mike McQuaid 2022-12-15 09:07:04 +00:00 committed by GitHub
commit 82d89bbbcb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 66 additions and 56 deletions

View File

@ -278,48 +278,54 @@ module Homebrew
# formula was found, but there's a problem with its implementation).
$stderr.puts e.backtrace if Homebrew::EnvConfig.developer?
ofail e.message
rescue FormulaOrCaskUnavailableError => e
if e.name == "updog"
rescue FormulaOrCaskUnavailableError, Cask::CaskUnavailableError => e
# formula name or cask token
name = e.try(:name) || e.token
if name == "updog"
ofail "What's updog?"
return
end
opoo e
ohai "Searching for similarly named formulae..."
formulae_search_results = search_formulae(e.name)
case formulae_search_results.length
when 0
ofail "No similarly named formulae found."
when 1
puts "This similarly named formula was found:"
puts formulae_search_results
puts "To install it, run:\n brew install #{formulae_search_results.first}"
else
puts "These similarly named formulae were found:"
puts Formatter.columns(formulae_search_results)
puts "To install one of them, run (for example):\n brew install #{formulae_search_results.first}"
end
if (reason = MissingFormula.reason(e.name))
reason = MissingFormula.reason(name, silent: true)
if !args.cask? && reason
$stderr.puts reason
return
end
# Do not search taps if the formula name is qualified
return if e.name.include?("/")
# We don't seem to get good search results when the tap is specified
# so we might as well return early.
return if name.include?("/")
taps_search_results = search_taps(e.name)[:formulae]
case taps_search_results.length
when 0
ofail "No formulae found in taps."
when 1
puts "This formula was found in a tap:"
puts taps_search_results
puts "To install it, run:\n brew install #{taps_search_results.first}"
else
puts "These formulae were found in taps:"
puts Formatter.columns(taps_search_results)
puts "To install one of them, run (for example):\n brew install #{taps_search_results.first}"
ohai "Searching for similarly named formulae and casks..."
# Don't treat formula/cask name as a regex
query = string_or_regex = name
all_formulae, all_casks = search_names(query, string_or_regex, args)
if all_formulae.any?
ohai "Formulae", Formatter.columns(all_formulae)
first_formula = all_formulae.first.to_s
puts <<~EOS
To install #{first_formula}, run:
brew install #{first_formula}
EOS
end
puts if all_formulae.any? && all_casks.any?
if all_casks.any?
ohai "Casks", Formatter.columns(all_casks)
first_cask = all_casks.first.to_s
puts <<~EOS
To install #{first_cask}, run:
brew install --cask #{first_cask}
EOS
end
return if all_formulae.any? || all_casks.any?
odie "No formulae or casks found for #{name}."
end
end

View File

@ -86,7 +86,8 @@ module Homebrew
elsif args.pull_request?
search_pull_requests(query, args)
else
search_names(query, string_or_regex, args)
formulae, casks = search_names(query, string_or_regex, args)
print_results(formulae, casks, query)
end
puts "Use `brew desc` to list packages with a short description." if args.verbose?
@ -129,32 +130,18 @@ module Homebrew
GitHub.print_pull_requests_matching(query, only)
end
def search_names(query, string_or_regex, args)
remote_results = search_taps(query, silent: true)
def print_results(all_formulae, all_casks, query)
count = all_formulae.size + all_casks.size
local_formulae = search_formulae(string_or_regex)
remote_formulae = remote_results[:formulae]
all_formulae = local_formulae + remote_formulae
local_casks = search_casks(string_or_regex)
remote_casks = remote_results[:casks]
all_casks = local_casks + remote_casks
print_formulae = args.formula?
print_casks = args.cask?
print_formulae = print_casks = true if !print_formulae && !print_casks
print_formulae &&= all_formulae.any?
print_casks &&= all_casks.any?
if print_formulae
if all_formulae.any?
if $stdout.tty?
ohai "Formulae", Formatter.columns(all_formulae)
else
puts all_formulae
end
end
puts if print_formulae && print_casks
if print_casks
puts if all_formulae.any? && all_casks.any?
if all_casks.any?
if $stdout.tty?
ohai "Casks", Formatter.columns(all_casks)
else
@ -162,9 +149,7 @@ module Homebrew
end
end
count = all_formulae.count + all_casks.count
print_missing_formula_help(query, count.positive?) if local_casks.exclude?(query)
print_missing_formula_help(query, count.positive?) if all_casks.exclude?(query)
odie "No formulae or casks found for #{query.inspect}." if count.zero?
end

View File

@ -37,8 +37,7 @@ module Homebrew
results = cask_tokens.extend(Searchable)
.search(string_or_regex)
cask_names = Cask::Cask.all.map(&:full_name)
results += DidYouMean::SpellChecker.new(dictionary: cask_names)
results += DidYouMean::SpellChecker.new(dictionary: cask_tokens)
.correct(string_or_regex)
results.sort.map do |name|

View File

@ -114,6 +114,26 @@ module Homebrew
def search_casks(_string_or_regex)
[]
end
def search_names(query, string_or_regex, args)
both = !args.formula? && !args.cask?
remote_results = search_taps(query, silent: true)
all_formulae = if args.formula? || both
search_formulae(string_or_regex) + remote_results[:formulae]
else
[]
end
all_casks = if args.cask? || both
search_casks(string_or_regex) + remote_results[:casks]
else
[]
end
[all_formulae, all_casks]
end
end
end