Refactor Search#search_descriptions

- Move cask logic back into method from extend/os and check
  whether to display it based on args since formula?
  is passed by default on Linux now
- Use #search_descriptions in `brew desc` instead of
  duplicating logic
- Remove need for extend/os/search
This commit is contained in:
apainintheneck 2022-12-17 10:03:41 -08:00
parent ba664fa1b4
commit 0ae416d0ee
4 changed files with 19 additions and 85 deletions

View File

@ -72,22 +72,7 @@ module Homebrew
else
query = args.named.join(" ")
string_or_regex = query_regexp(query)
eval_all = args.eval_all? || Homebrew::EnvConfig.eval_all?
unless args.cask?
ohai "Formulae"
CacheStoreDatabase.use(:descriptions) do |db|
cache_store = DescriptionCacheStore.new(db)
Descriptions.search(string_or_regex, search_type, cache_store, eval_all).print
end
end
unless args.formula?
puts unless args.cask?
ohai "Casks"
CacheStoreDatabase.use(:cask_descriptions) do |db|
cache_store = CaskDescriptionCacheStore.new(db)
Descriptions.search(string_or_regex, search_type, cache_store, eval_all).print
end
end
search_descriptions(string_or_regex, args, search_type: search_type)
end
end
end

View File

@ -1,56 +0,0 @@
# typed: false
# frozen_string_literal: true
require "cask/cask"
require "cask/cask_loader"
module Homebrew
module Search
module Extension
def search_descriptions(string_or_regex, args)
super
return if args.formula?
puts unless args.cask?
ohai "Casks"
CacheStoreDatabase.use(:cask_descriptions) do |db|
cache_store = CaskDescriptionCacheStore.new(db)
eval_all = args.eval_all? || Homebrew::EnvConfig.eval_all?
Descriptions.search(string_or_regex, :desc, cache_store, eval_all).print
end
end
def search_casks(string_or_regex)
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
cask_tokens = Tap.flat_map(&:cask_tokens).map do |c|
c.sub(%r{^homebrew/cask.*/}, "")
end
results = cask_tokens.extend(Searchable)
.search(string_or_regex)
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
end
end
prepend Extension
end
end

View File

@ -1,4 +0,0 @@
# typed: strict
# frozen_string_literal: true
require "extend/os/mac/search" if OS.mac?

View File

@ -19,14 +19,25 @@ module Homebrew
raise "#{query} is not a valid regex."
end
def search_descriptions(string_or_regex, args)
return if args.cask?
def search_descriptions(string_or_regex, args, search_type: :desc)
both = !args.formula? && !args.cask?
eval_all = args.eval_all? || Homebrew::EnvConfig.eval_all?
ohai "Formulae"
CacheStoreDatabase.use(:descriptions) do |db|
cache_store = DescriptionCacheStore.new(db)
eval_all = args.eval_all? || Homebrew::EnvConfig.eval_all?
Descriptions.search(string_or_regex, :desc, cache_store, eval_all).print
if args.formula? || both
ohai "Formulae"
CacheStoreDatabase.use(:descriptions) do |db|
cache_store = DescriptionCacheStore.new(db)
Descriptions.search(string_or_regex, search_type, cache_store, eval_all).print
end
end
return if !args.cask? && !both
puts if both
ohai "Casks"
CacheStoreDatabase.use(:cask_descriptions) do |db|
cache_store = CaskDescriptionCacheStore.new(db)
Descriptions.search(string_or_regex, search_type, cache_store, eval_all).print
end
end
@ -161,5 +172,3 @@ module Homebrew
end
end
end
require "extend/os/search"