Stop including/extending search.rb
- Instead use class methods. - This is better than use it as a mixin when only a small number of methods are used in each class or module. - It also allows us to conditionally require it in `brew install`. - Removed unused search require in descriptions.rb.
This commit is contained in:
parent
0ae416d0ee
commit
47b3f6fe6e
@ -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,8 +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)
|
||||||
search_descriptions(string_or_regex, args, search_type: search_type)
|
Search.search_descriptions(string_or_regex, args, search_type: search_type)
|
||||||
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)
|
||||||
|
|||||||
@ -9,7 +9,7 @@ module Homebrew
|
|||||||
#
|
#
|
||||||
# @api private
|
# @api private
|
||||||
module Search
|
module Search
|
||||||
def query_regexp(query)
|
def self.query_regexp(query)
|
||||||
if (m = query.match(%r{^/(.*)/$}))
|
if (m = query.match(%r{^/(.*)/$}))
|
||||||
Regexp.new(m[1])
|
Regexp.new(m[1])
|
||||||
else
|
else
|
||||||
@ -19,7 +19,7 @@ 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, search_type: :desc)
|
def self.search_descriptions(string_or_regex, args, search_type: :desc)
|
||||||
both = !args.formula? && !args.cask?
|
both = !args.formula? && !args.cask?
|
||||||
eval_all = args.eval_all? || Homebrew::EnvConfig.eval_all?
|
eval_all = args.eval_all? || Homebrew::EnvConfig.eval_all?
|
||||||
|
|
||||||
@ -41,7 +41,7 @@ module Homebrew
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def search_taps(query, silent: false)
|
def self.search_taps(query, silent: false)
|
||||||
if query.match?(Regexp.union(HOMEBREW_TAP_FORMULA_REGEX, HOMEBREW_TAP_CASK_REGEX))
|
if query.match?(Regexp.union(HOMEBREW_TAP_FORMULA_REGEX, HOMEBREW_TAP_CASK_REGEX))
|
||||||
_, _, query = query.split("/", 3)
|
_, _, query = query.split("/", 3)
|
||||||
end
|
end
|
||||||
@ -86,7 +86,7 @@ module Homebrew
|
|||||||
results
|
results
|
||||||
end
|
end
|
||||||
|
|
||||||
def search_formulae(string_or_regex)
|
def self.search_formulae(string_or_regex)
|
||||||
if string_or_regex.is_a?(String) && string_or_regex.match?(HOMEBREW_TAP_FORMULA_REGEX)
|
if string_or_regex.is_a?(String) && string_or_regex.match?(HOMEBREW_TAP_FORMULA_REGEX)
|
||||||
return begin
|
return begin
|
||||||
[Formulary.factory(string_or_regex).name]
|
[Formulary.factory(string_or_regex).name]
|
||||||
@ -122,7 +122,7 @@ module Homebrew
|
|||||||
end.compact
|
end.compact
|
||||||
end
|
end
|
||||||
|
|
||||||
def search_casks(string_or_regex)
|
def self.search_casks(string_or_regex)
|
||||||
if string_or_regex.is_a?(String) && string_or_regex.match?(HOMEBREW_TAP_CASK_REGEX)
|
if string_or_regex.is_a?(String) && string_or_regex.match?(HOMEBREW_TAP_CASK_REGEX)
|
||||||
return begin
|
return begin
|
||||||
[Cask::CaskLoader.load(string_or_regex).token]
|
[Cask::CaskLoader.load(string_or_regex).token]
|
||||||
@ -151,7 +151,7 @@ module Homebrew
|
|||||||
end.uniq
|
end.uniq
|
||||||
end
|
end
|
||||||
|
|
||||||
def search_names(query, string_or_regex, args)
|
def self.search_names(query, string_or_regex, args)
|
||||||
both = !args.formula? && !args.cask?
|
both = !args.formula? && !args.cask?
|
||||||
|
|
||||||
remote_results = search_taps(query, silent: true)
|
remote_results = search_taps(query, silent: true)
|
||||||
|
|||||||
@ -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