Merge pull request #14260 from apainintheneck/cleanup-search-module
Cleanup search module
This commit is contained in:
commit
740f9ddcc6
@ -12,8 +12,6 @@ module Cask
|
|||||||
extend T::Sig
|
extend T::Sig
|
||||||
extend T::Helpers
|
extend T::Helpers
|
||||||
|
|
||||||
include Homebrew::Search
|
|
||||||
|
|
||||||
OPTIONS = [
|
OPTIONS = [
|
||||||
[:switch, "--[no-]binaries", {
|
[:switch, "--[no-]binaries", {
|
||||||
description: "Disable/enable linking of helper executables (default: enabled).",
|
description: "Disable/enable linking of helper executables (default: enabled).",
|
||||||
@ -87,7 +85,7 @@ module Cask
|
|||||||
end
|
end
|
||||||
|
|
||||||
def suggestion_message(cask_token)
|
def suggestion_message(cask_token)
|
||||||
matches = search_casks(cask_token)
|
matches = Homebrew::Search.search_casks(cask_token)
|
||||||
|
|
||||||
if matches.one?
|
if matches.one?
|
||||||
"Did you mean '#{matches.first}'?"
|
"Did you mean '#{matches.first}'?"
|
||||||
|
|||||||
@ -11,8 +11,6 @@ module Homebrew
|
|||||||
|
|
||||||
module_function
|
module_function
|
||||||
|
|
||||||
extend Search
|
|
||||||
|
|
||||||
sig { returns(CLI::Parser) }
|
sig { returns(CLI::Parser) }
|
||||||
def desc_args
|
def desc_args
|
||||||
Homebrew::CLI::Parser.new do
|
Homebrew::CLI::Parser.new do
|
||||||
@ -71,23 +69,8 @@ module Homebrew
|
|||||||
Descriptions.new(desc).print
|
Descriptions.new(desc).print
|
||||||
else
|
else
|
||||||
query = args.named.join(" ")
|
query = args.named.join(" ")
|
||||||
string_or_regex = query_regexp(query)
|
string_or_regex = Search.query_regexp(query)
|
||||||
eval_all = args.eval_all? || Homebrew::EnvConfig.eval_all?
|
Search.search_descriptions(string_or_regex, args, search_type: search_type)
|
||||||
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
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -8,7 +8,6 @@ require "missing_formula"
|
|||||||
require "formula_installer"
|
require "formula_installer"
|
||||||
require "development_tools"
|
require "development_tools"
|
||||||
require "install"
|
require "install"
|
||||||
require "search"
|
|
||||||
require "cleanup"
|
require "cleanup"
|
||||||
require "cli/parser"
|
require "cli/parser"
|
||||||
require "upgrade"
|
require "upgrade"
|
||||||
@ -16,8 +15,6 @@ require "upgrade"
|
|||||||
module Homebrew
|
module Homebrew
|
||||||
extend T::Sig
|
extend T::Sig
|
||||||
|
|
||||||
extend Search
|
|
||||||
|
|
||||||
module_function
|
module_function
|
||||||
|
|
||||||
sig { returns(CLI::Parser) }
|
sig { returns(CLI::Parser) }
|
||||||
@ -299,11 +296,12 @@ module Homebrew
|
|||||||
# so we might as well return early.
|
# so we might as well return early.
|
||||||
return if name.include?("/")
|
return if name.include?("/")
|
||||||
|
|
||||||
|
require "search"
|
||||||
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
|
||||||
all_formulae, all_casks = search_names(query, string_or_regex, args)
|
all_formulae, all_casks = Search.search_names(query, string_or_regex, args)
|
||||||
|
|
||||||
if all_formulae.any?
|
if all_formulae.any?
|
||||||
ohai "Formulae", Formatter.columns(all_formulae)
|
ohai "Formulae", Formatter.columns(all_formulae)
|
||||||
|
|||||||
@ -12,8 +12,6 @@ module Homebrew
|
|||||||
|
|
||||||
module_function
|
module_function
|
||||||
|
|
||||||
extend Search
|
|
||||||
|
|
||||||
PACKAGE_MANAGERS = {
|
PACKAGE_MANAGERS = {
|
||||||
repology: ->(query) { "https://repology.org/projects/?search=#{query}" },
|
repology: ->(query) { "https://repology.org/projects/?search=#{query}" },
|
||||||
macports: ->(query) { "https://ports.macports.org/search/?q=#{query}" },
|
macports: ->(query) { "https://ports.macports.org/search/?q=#{query}" },
|
||||||
@ -76,17 +74,17 @@ module Homebrew
|
|||||||
return if search_package_manager(args)
|
return if search_package_manager(args)
|
||||||
|
|
||||||
query = args.named.join(" ")
|
query = args.named.join(" ")
|
||||||
string_or_regex = query_regexp(query)
|
string_or_regex = Search.query_regexp(query)
|
||||||
|
|
||||||
if args.desc?
|
if args.desc?
|
||||||
if !args.eval_all? && !Homebrew::EnvConfig.eval_all?
|
if !args.eval_all? && !Homebrew::EnvConfig.eval_all?
|
||||||
odeprecated "brew search --desc", "brew search --desc --eval-all or HOMEBREW_EVAL_ALL"
|
odeprecated "brew search --desc", "brew search --desc --eval-all or HOMEBREW_EVAL_ALL"
|
||||||
end
|
end
|
||||||
search_descriptions(string_or_regex, args)
|
Search.search_descriptions(string_or_regex, args)
|
||||||
elsif args.pull_request?
|
elsif args.pull_request?
|
||||||
search_pull_requests(query, args)
|
search_pull_requests(query, args)
|
||||||
else
|
else
|
||||||
formulae, casks = search_names(query, string_or_regex, args)
|
formulae, casks = Search.search_names(query, string_or_regex, args)
|
||||||
print_results(formulae, casks, query)
|
print_results(formulae, casks, query)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -3,15 +3,12 @@
|
|||||||
|
|
||||||
require "formula"
|
require "formula"
|
||||||
require "formula_versions"
|
require "formula_versions"
|
||||||
require "search"
|
|
||||||
require "searchable"
|
require "searchable"
|
||||||
|
|
||||||
# Helper class for printing and searching descriptions.
|
# Helper class for printing and searching descriptions.
|
||||||
#
|
#
|
||||||
# @api private
|
# @api private
|
||||||
class Descriptions
|
class Descriptions
|
||||||
extend Homebrew::Search
|
|
||||||
|
|
||||||
# Given a regex, find all formulae whose specified fields contain a match.
|
# Given a regex, find all formulae whose specified fields contain a match.
|
||||||
def self.search(string_or_regex, field, cache_store, eval_all = Homebrew::EnvConfig.eval_all?)
|
def self.search(string_or_regex, field, cache_store, eval_all = Homebrew::EnvConfig.eval_all?)
|
||||||
cache_store.populate_if_empty!(eval_all: eval_all)
|
cache_store.populate_if_empty!(eval_all: eval_all)
|
||||||
|
|||||||
@ -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
|
|
||||||
@ -1,4 +0,0 @@
|
|||||||
# typed: strict
|
|
||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
require "extend/os/mac/search" if OS.mac?
|
|
||||||
@ -9,6 +9,8 @@ module Homebrew
|
|||||||
#
|
#
|
||||||
# @api private
|
# @api private
|
||||||
module Search
|
module Search
|
||||||
|
module_function
|
||||||
|
|
||||||
def query_regexp(query)
|
def query_regexp(query)
|
||||||
if (m = query.match(%r{^/(.*)/$}))
|
if (m = query.match(%r{^/(.*)/$}))
|
||||||
Regexp.new(m[1])
|
Regexp.new(m[1])
|
||||||
@ -19,14 +21,25 @@ module Homebrew
|
|||||||
raise "#{query} is not a valid regex."
|
raise "#{query} is not a valid regex."
|
||||||
end
|
end
|
||||||
|
|
||||||
def search_descriptions(string_or_regex, args)
|
def search_descriptions(string_or_regex, args, search_type: :desc)
|
||||||
return if args.cask?
|
both = !args.formula? && !args.cask?
|
||||||
|
eval_all = args.eval_all? || Homebrew::EnvConfig.eval_all?
|
||||||
|
|
||||||
|
if args.formula? || both
|
||||||
ohai "Formulae"
|
ohai "Formulae"
|
||||||
CacheStoreDatabase.use(:descriptions) do |db|
|
CacheStoreDatabase.use(:descriptions) do |db|
|
||||||
cache_store = DescriptionCacheStore.new(db)
|
cache_store = DescriptionCacheStore.new(db)
|
||||||
eval_all = args.eval_all? || Homebrew::EnvConfig.eval_all?
|
Descriptions.search(string_or_regex, search_type, cache_store, eval_all).print
|
||||||
Descriptions.search(string_or_regex, :desc, 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
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -111,9 +124,34 @@ module Homebrew
|
|||||||
end.compact
|
end.compact
|
||||||
end
|
end
|
||||||
|
|
||||||
def search_casks(_string_or_regex)
|
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
|
||||||
|
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
|
||||||
|
|
||||||
def search_names(query, string_or_regex, args)
|
def search_names(query, string_or_regex, args)
|
||||||
both = !args.formula? && !args.cask?
|
both = !args.formula? && !args.cask?
|
||||||
@ -136,5 +174,3 @@ module Homebrew
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
require "extend/os/search"
|
|
||||||
|
|||||||
@ -4,12 +4,6 @@
|
|||||||
require "search"
|
require "search"
|
||||||
|
|
||||||
describe Homebrew::Search do
|
describe Homebrew::Search do
|
||||||
subject(:mod) { Object.new }
|
|
||||||
|
|
||||||
before do
|
|
||||||
mod.extend(described_class)
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "#search_taps" do
|
describe "#search_taps" do
|
||||||
before do
|
before do
|
||||||
ENV.delete("HOMEBREW_NO_GITHUB_API")
|
ENV.delete("HOMEBREW_NO_GITHUB_API")
|
||||||
@ -17,13 +11,13 @@ describe Homebrew::Search do
|
|||||||
|
|
||||||
it "does not raise if `HOMEBREW_NO_GITHUB_API` is set" do
|
it "does not raise if `HOMEBREW_NO_GITHUB_API` is set" do
|
||||||
ENV["HOMEBREW_NO_GITHUB_API"] = "1"
|
ENV["HOMEBREW_NO_GITHUB_API"] = "1"
|
||||||
expect(mod.search_taps("some-formula")).to match(formulae: [], casks: [])
|
expect(described_class.search_taps("some-formula")).to match(formulae: [], casks: [])
|
||||||
end
|
end
|
||||||
|
|
||||||
it "does not raise if the network fails" do
|
it "does not raise if the network fails" do
|
||||||
allow(GitHub::API).to receive(:open_rest).and_raise(GitHub::API::Error)
|
allow(GitHub::API).to receive(:open_rest).and_raise(GitHub::API::Error)
|
||||||
|
|
||||||
expect(mod.search_taps("some-formula"))
|
expect(described_class.search_taps("some-formula"))
|
||||||
.to match(formulae: [], casks: [])
|
.to match(formulae: [], casks: [])
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -47,22 +41,22 @@ describe Homebrew::Search do
|
|||||||
|
|
||||||
allow(GitHub::API).to receive(:open_rest).and_yield(json_response)
|
allow(GitHub::API).to receive(:open_rest).and_yield(json_response)
|
||||||
|
|
||||||
expect(mod.search_taps("some-formula"))
|
expect(described_class.search_taps("some-formula"))
|
||||||
.to match(formulae: ["homebrew/foo/some-formula"], casks: ["homebrew/bar/some-cask"])
|
.to match(formulae: ["homebrew/foo/some-formula"], casks: ["homebrew/bar/some-cask"])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe "#query_regexp" do
|
describe "#query_regexp" do
|
||||||
it "correctly parses a regex query" do
|
it "correctly parses a regex query" do
|
||||||
expect(mod.query_regexp("/^query$/")).to eq(/^query$/)
|
expect(described_class.query_regexp("/^query$/")).to eq(/^query$/)
|
||||||
end
|
end
|
||||||
|
|
||||||
it "returns the original string if it is not a regex query" do
|
it "returns the original string if it is not a regex query" do
|
||||||
expect(mod.query_regexp("query")).to eq("query")
|
expect(described_class.query_regexp("query")).to eq("query")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "raises an error if the query is an invalid regex" do
|
it "raises an error if the query is an invalid regex" do
|
||||||
expect { mod.query_regexp("/+/") }.to raise_error(/not a valid regex/)
|
expect { described_class.query_regexp("/+/") }.to raise_error(/not a valid regex/)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user