brew/Library/Homebrew/cmd/search.rb

161 lines
5.4 KiB
Ruby
Raw Normal View History

2020-10-10 14:16:11 +02:00
# typed: false
# frozen_string_literal: true
require "formula"
require "missing_formula"
require "descriptions"
2019-04-17 18:25:08 +09:00
require "cli/parser"
require "search"
module Homebrew
2020-10-20 12:03:48 +02:00
extend T::Sig
2016-09-26 01:44:51 +02:00
module_function
extend Search
2018-06-01 14:19:00 +02:00
PACKAGE_MANAGERS = {
macports: ->(query) { "https://www.macports.org/ports.php?by=name&substr=#{query}" },
2020-09-04 00:06:43 -04:00
fink: ->(query) { "https://pdb.finkproject.org/pdb/browse.php?summary=#{query}" },
2018-06-01 14:19:00 +02:00
opensuse: ->(query) { "https://software.opensuse.org/search?q=#{query}" },
fedora: ->(query) { "https://apps.fedoraproject.org/packages/s/#{query}" },
2018-11-02 17:18:07 +00:00
debian: lambda { |query|
"https://packages.debian.org/search?keywords=#{query}&searchon=names&suite=all&section=all"
},
2018-11-02 17:18:07 +00:00
ubuntu: lambda { |query|
"https://packages.ubuntu.com/search?keywords=#{query}&searchon=names&suite=all&section=all"
},
2018-06-01 14:19:00 +02:00
}.freeze
2020-10-20 12:03:48 +02:00
sig { returns(CLI::Parser) }
def search_args
Homebrew::CLI::Parser.new do
usage_banner <<~EOS
`search` [<options>] [<text>|`/`<text>`/`]
2018-06-01 14:19:00 +02:00
Perform a substring search of cask tokens and formula names for <text>. If <text>
is flanked by slashes, it is interpreted as a regular expression.
The search for <text> is extended online to `homebrew/core` and `homebrew/cask`.
If no <text> is provided, list all locally available formulae (including tapped ones).
No online search is performed.
EOS
switch "--formula", "--formulae",
2020-03-04 13:14:39 +00:00
description: "Without <text>, list all locally available formulae (no online search is performed). " \
"With <text>, search online and locally for formulae."
switch "--cask", "--casks",
2020-03-04 13:14:39 +00:00
description: "Without <text>, list all locally available casks (including tapped ones, no online " \
"search is performed). With <text>, search online and locally for casks."
switch "--desc",
2019-08-06 14:22:24 -04:00
description: "Search for formulae with a description matching <text> and casks with "\
2019-04-30 08:44:35 +01:00
"a name matching <text>."
switch "--pull-request",
2020-11-12 10:40:48 -05:00
description: "Search for GitHub pull requests containing <text>."
2020-12-23 23:06:02 +09:00
switch "--open",
depends_on: "--pull-request",
description: "Search for only open GitHub pull requests"
switch "--closed",
depends_on: "--pull-request",
description: "Search for only closed GitHub pull requests"
conflicts "--open", "--closed"
package_manager_switches = PACKAGE_MANAGERS.keys.map { |name| "--#{name}" }
2018-06-01 14:19:00 +02:00
package_manager_switches.each do |s|
switch s,
2019-04-30 08:44:35 +01:00
description: "Search for <text> in the given package manager's list."
2018-06-01 14:19:00 +02:00
end
2020-07-30 18:40:10 +02:00
conflicts("--desc", "--pull-request")
2018-06-01 14:19:00 +02:00
conflicts(*package_manager_switches)
end
end
def search
2020-07-30 18:40:10 +02:00
args = search_args.parse
2018-06-01 14:19:00 +02:00
if package_manager = PACKAGE_MANAGERS.find { |name,| args[:"#{name}?"] }
2018-06-02 20:50:18 +02:00
_, url = package_manager
exec_browser url.call(URI.encode_www_form_component(args.named.join(" ")))
2018-06-02 20:50:18 +02:00
return
2018-06-01 14:19:00 +02:00
end
if args.no_named?
if args.cask?
raise UsageError, "specifying both --formula and --cask requires <text>" if args.formula?
2020-03-04 17:56:29 +05:30
2018-09-06 08:29:14 +02:00
puts Formatter.columns(Cask::Cask.to_a.map(&:full_name).sort)
2018-06-23 01:31:16 +02:00
else
odeprecated "'brew search' with no arguments to output formulae", "'brew formulae'"
2018-06-23 01:31:16 +02:00
puts Formatter.columns(Formula.full_names.sort)
end
2018-06-18 16:09:13 +02:00
return
end
query = args.named.join(" ")
2018-06-18 16:09:13 +02:00
string_or_regex = query_regexp(query)
if args.desc?
search_descriptions(string_or_regex)
elsif args.pull_request?
2020-12-23 23:06:02 +09:00
only = if args.open? && !args.closed?
"open"
elsif args.closed? && !args.open?
"closed"
end
GitHub.print_pull_requests_matching(query, only)
2018-06-18 16:09:13 +02:00
else
2018-06-13 07:49:01 +02:00
remote_results = search_taps(query, silent: true)
2018-06-13 07:49:01 +02:00
local_formulae = search_formulae(string_or_regex)
remote_formulae = remote_results[:formulae]
all_formulae = local_formulae + remote_formulae
2018-06-13 07:49:01 +02:00
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?
2020-03-04 17:56:29 +05:30
print_formulae = print_casks = true if !print_formulae && !print_casks
2020-03-04 17:56:29 +05:30
if print_formulae && all_formulae.any?
ohai "Formulae"
puts Formatter.columns(all_formulae)
end
2020-03-04 17:56:29 +05:30
if print_casks && all_casks.any?
puts if args.formula? && all_formulae.any?
ohai "Casks"
puts Formatter.columns(all_casks)
end
count = all_formulae.count + all_casks.count
2020-12-01 17:04:59 +00:00
if $stdout.tty? && (reason = MissingFormula.reason(query, silent: true)) && local_casks.exclude?(query)
if count.positive?
puts
puts "If you meant #{query.inspect} specifically:"
end
puts reason
end
raise "No formulae or casks found for #{query.inspect}." if count.zero?
end
return unless $stdout.tty?
return if args.no_named?
2018-09-17 02:45:00 +02:00
metacharacters = %w[\\ | ( ) [ ] { } ^ $ * + ?].freeze
return unless metacharacters.any? do |char|
args.named.any? do |arg|
arg.include?(char) && !arg.start_with?("/")
end
end
2018-09-17 02:45:00 +02:00
opoo <<~EOS
Did you mean to perform a regular expression search?
Surround your query with /slashes/ to search locally by regex.
EOS
end
end