Add specific examples to install cmd

- provide specific install instructions
  when a cask/formula doesn't exist
  and we search for similar ones
- print and exit early if a named formula that
  was removed recently has the same name
- exit early if the tap is specified because
  we don't get good search results
This commit is contained in:
apainintheneck 2022-12-12 19:51:18 -08:00
parent a481729ade
commit daa87fa10f
3 changed files with 84 additions and 49 deletions

View File

@ -288,16 +288,47 @@ module Homebrew
end end
opoo e opoo e
reason = MissingFormula.reason(name, silent: true)
if !args.cask? && reason
$stderr.puts reason
return
end
return if name.include?("/")
ohai "Searching for similarly named formulae and casks..." ohai "Searching for similarly named formulae and casks..."
# Don't treat formula/cask name as a regex # Don't treat formula/cask name as a regex
query = string_or_regex = name query = string_or_regex = name
if search_names(query, string_or_regex, args) all_formulae, all_casks = search_names(query, string_or_regex)
puts <<~EOL
To install one of them, run: print_formulae = args.formula?
brew install 'package_name' print_casks = args.cask?
EOL print_formulae = print_casks = true if !print_formulae && !print_casks
end print_formulae &&= all_formulae.any?
print_casks &&= all_casks.any?
if print_formulae
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 print_formulae && print_casks
if print_casks
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
odie "No formulae or casks found for #{name}." if !print_formulae && !print_casks
end end
end end

View File

@ -86,7 +86,8 @@ module Homebrew
elsif args.pull_request? elsif args.pull_request?
search_pull_requests(query, args) search_pull_requests(query, args)
else else
search_names(query, string_or_regex, args) formulae, casks = search_names(query, string_or_regex)
print_results(formulae, casks, query, args)
end end
puts "Use `brew desc` to list packages with a short description." if args.verbose? puts "Use `brew desc` to list packages with a short description." if args.verbose?
@ -128,4 +129,48 @@ module Homebrew
GitHub.print_pull_requests_matching(query, only) GitHub.print_pull_requests_matching(query, only)
end end
def print_results(all_formulae, all_casks, query, args)
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?
count = 0
if all_formulae
if $stdout.tty?
ohai "Formulae", Formatter.columns(all_formulae)
else
puts all_formulae
end
count += all_formulae.count
end
puts if print_formulae && print_casks
if print_casks
if $stdout.tty?
ohai "Casks", Formatter.columns(all_casks)
else
puts all_casks
end
count += all_casks.count
end
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
def print_missing_formula_help(query, found_matches)
return unless $stdout.tty?
reason = MissingFormula.reason(query, silent: true)
return if reason.nil?
if found_matches
puts
puts "If you meant #{query.inspect} specifically:"
end
puts reason
end
end end

View File

@ -115,7 +115,7 @@ module Homebrew
[] []
end end
def search_names(query, string_or_regex, args) def search_names(query, string_or_regex)
remote_results = search_taps(query, silent: true) remote_results = search_taps(query, silent: true)
local_formulae = search_formulae(string_or_regex) local_formulae = search_formulae(string_or_regex)
@ -126,48 +126,7 @@ module Homebrew
remote_casks = remote_results[:casks] remote_casks = remote_results[:casks]
all_casks = local_casks + remote_casks all_casks = local_casks + remote_casks
print_formulae = args.formula? [all_formulae, all_casks]
print_casks = args.cask?
print_formulae = print_casks = true if !print_formulae && !print_casks
print_formulae &&= all_formulae.any?
print_casks &&= all_casks.any?
count = 0
if print_formulae
if $stdout.tty?
ohai "Formulae", Formatter.columns(all_formulae)
else
puts all_formulae
end
count += all_formulae.count
end
puts if print_formulae && print_casks
if print_casks
if $stdout.tty?
ohai "Casks", Formatter.columns(all_casks)
else
puts all_casks
end
count += all_casks.count
end
print_missing_formula_help(query, count.positive?) if local_casks.exclude?(query)
odie "No formulae or casks found for #{query.inspect}." if count.zero?
!count.zero?
end
def print_missing_formula_help(query, found_matches)
return unless $stdout.tty?
reason = MissingFormula.reason(query, silent: true)
return if reason.nil?
if found_matches
puts
puts "If you meant #{query.inspect} specifically:"
end
puts reason
end end
end end
end end