2023-03-06 22:37:09 -08:00
|
|
|
# typed: true
|
2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-10-13 08:22:51 -07:00
|
|
|
require "description_cache_store"
|
2018-06-05 15:39:09 +02:00
|
|
|
|
2018-06-02 20:49:14 +02:00
|
|
|
module Homebrew
|
2020-08-19 07:02:45 +02:00
|
|
|
# Helper module for searching formulae or casks.
|
2018-06-02 20:49:14 +02:00
|
|
|
module Search
|
2023-04-17 10:37:59 -07:00
|
|
|
def self.query_regexp(query)
|
2021-02-12 18:33:37 +05:30
|
|
|
if (m = query.match(%r{^/(.*)/$}))
|
2018-06-02 20:49:14 +02:00
|
|
|
Regexp.new(m[1])
|
|
|
|
else
|
2018-06-05 15:39:09 +02:00
|
|
|
query
|
2018-06-02 20:49:14 +02:00
|
|
|
end
|
|
|
|
rescue RegexpError
|
|
|
|
raise "#{query} is not a valid regex."
|
|
|
|
end
|
|
|
|
|
2023-04-17 10:37:59 -07:00
|
|
|
def self.search_descriptions(string_or_regex, args, search_type: :desc)
|
2022-12-17 10:03:41 -08:00
|
|
|
both = !args.formula? && !args.cask?
|
|
|
|
eval_all = args.eval_all? || Homebrew::EnvConfig.eval_all?
|
|
|
|
|
|
|
|
if args.formula? || both
|
|
|
|
ohai "Formulae"
|
2024-06-27 17:28:18 -04:00
|
|
|
if eval_all
|
|
|
|
CacheStoreDatabase.use(:descriptions) do |db|
|
|
|
|
cache_store = DescriptionCacheStore.new(db)
|
|
|
|
Descriptions.search(string_or_regex, search_type, cache_store, eval_all).print
|
|
|
|
end
|
|
|
|
else
|
|
|
|
unofficial = Tap.all.sum { |tap| tap.official? ? 0 : tap.formula_files.size }
|
|
|
|
if unofficial.positive?
|
|
|
|
opoo "Use `--eval-all` to search #{unofficial} additional " \
|
|
|
|
"#{Utils.pluralize("formula", unofficial, plural: "e")} in third party taps."
|
|
|
|
end
|
|
|
|
descriptions = Homebrew::API::Formula.all_formulae.transform_values { |data| data["desc"] }
|
|
|
|
Descriptions.search(string_or_regex, search_type, descriptions, eval_all, cache_store_hash: true).print
|
2022-12-17 10:03:41 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return if !args.cask? && !both
|
|
|
|
|
|
|
|
puts if both
|
|
|
|
|
|
|
|
ohai "Casks"
|
2024-06-27 17:28:18 -04:00
|
|
|
if eval_all
|
|
|
|
CacheStoreDatabase.use(:cask_descriptions) do |db|
|
|
|
|
cache_store = CaskDescriptionCacheStore.new(db)
|
|
|
|
Descriptions.search(string_or_regex, search_type, cache_store, eval_all).print
|
|
|
|
end
|
|
|
|
else
|
|
|
|
unofficial = Tap.all.sum { |tap| tap.official? ? 0 : tap.cask_files.size }
|
|
|
|
if unofficial.positive?
|
|
|
|
opoo "Use `--eval-all` to search #{unofficial} additional " \
|
|
|
|
"#{Utils.pluralize("cask", unofficial)} in third party taps."
|
|
|
|
end
|
|
|
|
descriptions = Homebrew::API::Cask.all_casks.transform_values { |c| [c["name"].join(", "), c["desc"]] }
|
|
|
|
Descriptions.search(string_or_regex, search_type, descriptions, eval_all, cache_store_hash: true).print
|
2018-10-13 08:22:51 -07:00
|
|
|
end
|
2018-06-18 16:09:13 +02:00
|
|
|
end
|
|
|
|
|
2023-04-17 10:37:59 -07:00
|
|
|
def self.search_formulae(string_or_regex)
|
2018-06-13 07:49:01 +02:00
|
|
|
if string_or_regex.is_a?(String) && string_or_regex.match?(HOMEBREW_TAP_FORMULA_REGEX)
|
|
|
|
return begin
|
|
|
|
[Formulary.factory(string_or_regex).name]
|
|
|
|
rescue FormulaUnavailableError
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-02 20:49:14 +02:00
|
|
|
aliases = Formula.alias_full_names
|
2023-03-06 22:37:09 -08:00
|
|
|
results = search(Formula.full_names + aliases, string_or_regex).sort
|
2022-03-23 00:03:11 -04:00
|
|
|
results |= Formula.fuzzy_search(string_or_regex).map { |n| Formulary.factory(n).full_name }
|
2021-06-19 00:19:24 +01:00
|
|
|
|
2024-02-22 23:29:55 +00:00
|
|
|
results.filter_map do |name|
|
2018-06-07 17:57:26 +02:00
|
|
|
formula, canonical_full_name = begin
|
|
|
|
f = Formulary.factory(name)
|
|
|
|
[f, f.full_name]
|
2018-06-02 20:49:14 +02:00
|
|
|
rescue
|
2018-06-07 17:57:26 +02:00
|
|
|
[nil, name]
|
2018-06-02 20:49:14 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
# Ignore aliases from results when the full name was also found
|
|
|
|
next if aliases.include?(name) && results.include?(canonical_full_name)
|
|
|
|
|
2018-06-07 17:57:26 +02:00
|
|
|
if formula&.any_version_installed?
|
2018-06-02 20:49:14 +02:00
|
|
|
pretty_installed(name)
|
2023-03-27 19:35:28 -07:00
|
|
|
elsif formula.nil? || formula.valid_platform?
|
2018-06-02 20:49:14 +02:00
|
|
|
name
|
|
|
|
end
|
2024-02-22 23:29:55 +00:00
|
|
|
end
|
2018-06-02 20:49:14 +02:00
|
|
|
end
|
2018-06-07 14:42:58 +02:00
|
|
|
|
2023-04-17 10:37:59 -07:00
|
|
|
def self.search_casks(string_or_regex)
|
2022-12-17 09:27:33 -08:00
|
|
|
if string_or_regex.is_a?(String) && string_or_regex.match?(HOMEBREW_TAP_CASK_REGEX)
|
|
|
|
return begin
|
|
|
|
[Cask::CaskLoader.load(string_or_regex).token]
|
|
|
|
rescue Cask::CaskUnavailableError
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-03-09 19:38:43 -08:00
|
|
|
cask_tokens = Tap.each_with_object([]) do |tap, array|
|
|
|
|
# We can exclude the core cask tap because `CoreCaskTap#cask_tokens` returns short names by default.
|
|
|
|
if tap.official? && !tap.core_cask_tap?
|
|
|
|
tap.cask_tokens.each { |token| array << token.sub(%r{^homebrew/cask.*/}, "") }
|
|
|
|
else
|
|
|
|
tap.cask_tokens.each { |token| array << token }
|
|
|
|
end
|
|
|
|
end.uniq
|
2023-01-23 13:32:54 -05:00
|
|
|
|
2023-03-06 22:37:09 -08:00
|
|
|
results = search(cask_tokens, string_or_regex)
|
2022-12-17 09:27:33 -08:00
|
|
|
results += DidYouMean::SpellChecker.new(dictionary: cask_tokens)
|
|
|
|
.correct(string_or_regex)
|
|
|
|
|
|
|
|
results.sort.map do |name|
|
|
|
|
cask = Cask::CaskLoader.load(name)
|
|
|
|
if cask.installed?
|
|
|
|
pretty_installed(cask.full_name)
|
|
|
|
else
|
|
|
|
cask.full_name
|
|
|
|
end
|
|
|
|
end.uniq
|
2018-06-07 14:42:58 +02:00
|
|
|
end
|
2022-12-10 12:59:06 -08:00
|
|
|
|
2023-04-17 10:37:59 -07:00
|
|
|
def self.search_names(string_or_regex, args)
|
2022-12-13 20:38:00 -08:00
|
|
|
both = !args.formula? && !args.cask?
|
|
|
|
|
|
|
|
all_formulae = if args.formula? || both
|
2023-04-12 00:00:48 +01:00
|
|
|
search_formulae(string_or_regex)
|
2022-12-13 20:38:00 -08:00
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2022-12-10 12:59:06 -08:00
|
|
|
|
2022-12-13 20:38:00 -08:00
|
|
|
all_casks = if args.cask? || both
|
2023-04-12 00:00:48 +01:00
|
|
|
search_casks(string_or_regex)
|
2022-12-13 20:38:00 -08:00
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
2022-12-10 12:59:06 -08:00
|
|
|
|
2022-12-12 19:51:18 -08:00
|
|
|
[all_formulae, all_casks]
|
2022-12-10 12:59:06 -08:00
|
|
|
end
|
2023-03-06 22:37:09 -08:00
|
|
|
|
2023-04-17 10:37:59 -07:00
|
|
|
def self.search(selectable, string_or_regex, &block)
|
2023-03-06 22:37:09 -08:00
|
|
|
case string_or_regex
|
|
|
|
when Regexp
|
2023-03-09 18:33:11 -08:00
|
|
|
search_regex(selectable, string_or_regex, &block)
|
2023-03-06 22:37:09 -08:00
|
|
|
else
|
2023-03-09 18:33:11 -08:00
|
|
|
search_string(selectable, string_or_regex.to_str, &block)
|
2023-03-06 22:37:09 -08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-17 10:37:59 -07:00
|
|
|
def self.simplify_string(string)
|
2023-03-06 22:37:09 -08:00
|
|
|
string.downcase.gsub(/[^a-z\d]/i, "")
|
|
|
|
end
|
|
|
|
|
2023-04-17 10:37:59 -07:00
|
|
|
def self.search_regex(selectable, regex)
|
2023-03-09 18:33:11 -08:00
|
|
|
selectable.select do |*args|
|
2023-03-06 22:37:09 -08:00
|
|
|
args = yield(*args) if block_given?
|
|
|
|
args = Array(args).flatten.compact
|
|
|
|
args.any? { |arg| arg.match?(regex) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-17 10:37:59 -07:00
|
|
|
def self.search_string(selectable, string)
|
2023-03-06 22:37:09 -08:00
|
|
|
simplified_string = simplify_string(string)
|
2023-03-09 18:33:11 -08:00
|
|
|
selectable.select do |*args|
|
2023-03-06 22:37:09 -08:00
|
|
|
args = yield(*args) if block_given?
|
|
|
|
args = Array(args).flatten.compact
|
|
|
|
args.any? { |arg| simplify_string(arg).include?(simplified_string) }
|
|
|
|
end
|
|
|
|
end
|
2018-06-02 20:49:14 +02:00
|
|
|
end
|
|
|
|
end
|