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::Helpers
 | 
			
		||||
 | 
			
		||||
      include Homebrew::Search
 | 
			
		||||
 | 
			
		||||
      OPTIONS = [
 | 
			
		||||
        [:switch, "--[no-]binaries", {
 | 
			
		||||
          description: "Disable/enable linking of helper executables (default: enabled).",
 | 
			
		||||
@ -87,7 +85,7 @@ module Cask
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      def suggestion_message(cask_token)
 | 
			
		||||
        matches = search_casks(cask_token)
 | 
			
		||||
        matches = Homebrew::Search.search_casks(cask_token)
 | 
			
		||||
 | 
			
		||||
        if matches.one?
 | 
			
		||||
          "Did you mean '#{matches.first}'?"
 | 
			
		||||
 | 
			
		||||
@ -11,8 +11,6 @@ module Homebrew
 | 
			
		||||
 | 
			
		||||
  module_function
 | 
			
		||||
 | 
			
		||||
  extend Search
 | 
			
		||||
 | 
			
		||||
  sig { returns(CLI::Parser) }
 | 
			
		||||
  def desc_args
 | 
			
		||||
    Homebrew::CLI::Parser.new do
 | 
			
		||||
@ -71,8 +69,8 @@ module Homebrew
 | 
			
		||||
      Descriptions.new(desc).print
 | 
			
		||||
    else
 | 
			
		||||
      query = args.named.join(" ")
 | 
			
		||||
      string_or_regex = query_regexp(query)
 | 
			
		||||
      search_descriptions(string_or_regex, args, search_type: search_type)
 | 
			
		||||
      string_or_regex = Search.query_regexp(query)
 | 
			
		||||
      Search.search_descriptions(string_or_regex, args, search_type: search_type)
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
 | 
			
		||||
@ -8,7 +8,6 @@ require "missing_formula"
 | 
			
		||||
require "formula_installer"
 | 
			
		||||
require "development_tools"
 | 
			
		||||
require "install"
 | 
			
		||||
require "search"
 | 
			
		||||
require "cleanup"
 | 
			
		||||
require "cli/parser"
 | 
			
		||||
require "upgrade"
 | 
			
		||||
@ -16,8 +15,6 @@ require "upgrade"
 | 
			
		||||
module Homebrew
 | 
			
		||||
  extend T::Sig
 | 
			
		||||
 | 
			
		||||
  extend Search
 | 
			
		||||
 | 
			
		||||
  module_function
 | 
			
		||||
 | 
			
		||||
  sig { returns(CLI::Parser) }
 | 
			
		||||
@ -299,11 +296,12 @@ module Homebrew
 | 
			
		||||
    # so we might as well return early.
 | 
			
		||||
    return if name.include?("/")
 | 
			
		||||
 | 
			
		||||
    require "search"
 | 
			
		||||
    ohai "Searching for similarly named formulae and casks..."
 | 
			
		||||
 | 
			
		||||
    # Don't treat formula/cask name as a regex
 | 
			
		||||
    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?
 | 
			
		||||
      ohai "Formulae", Formatter.columns(all_formulae)
 | 
			
		||||
 | 
			
		||||
@ -12,8 +12,6 @@ module Homebrew
 | 
			
		||||
 | 
			
		||||
  module_function
 | 
			
		||||
 | 
			
		||||
  extend Search
 | 
			
		||||
 | 
			
		||||
  PACKAGE_MANAGERS = {
 | 
			
		||||
    repology:  ->(query) { "https://repology.org/projects/?search=#{query}" },
 | 
			
		||||
    macports:  ->(query) { "https://ports.macports.org/search/?q=#{query}" },
 | 
			
		||||
@ -76,17 +74,17 @@ module Homebrew
 | 
			
		||||
    return if search_package_manager(args)
 | 
			
		||||
 | 
			
		||||
    query = args.named.join(" ")
 | 
			
		||||
    string_or_regex = query_regexp(query)
 | 
			
		||||
    string_or_regex = Search.query_regexp(query)
 | 
			
		||||
 | 
			
		||||
    if args.desc?
 | 
			
		||||
      if !args.eval_all? && !Homebrew::EnvConfig.eval_all?
 | 
			
		||||
        odeprecated "brew search --desc", "brew search --desc --eval-all or HOMEBREW_EVAL_ALL"
 | 
			
		||||
      end
 | 
			
		||||
      search_descriptions(string_or_regex, args)
 | 
			
		||||
      Search.search_descriptions(string_or_regex, args)
 | 
			
		||||
    elsif args.pull_request?
 | 
			
		||||
      search_pull_requests(query, args)
 | 
			
		||||
    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)
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -3,15 +3,12 @@
 | 
			
		||||
 | 
			
		||||
require "formula"
 | 
			
		||||
require "formula_versions"
 | 
			
		||||
require "search"
 | 
			
		||||
require "searchable"
 | 
			
		||||
 | 
			
		||||
# Helper class for printing and searching descriptions.
 | 
			
		||||
#
 | 
			
		||||
# @api private
 | 
			
		||||
class Descriptions
 | 
			
		||||
  extend Homebrew::Search
 | 
			
		||||
 | 
			
		||||
  # 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?)
 | 
			
		||||
    cache_store.populate_if_empty!(eval_all: eval_all)
 | 
			
		||||
 | 
			
		||||
@ -9,7 +9,7 @@ module Homebrew
 | 
			
		||||
  #
 | 
			
		||||
  # @api private
 | 
			
		||||
  module Search
 | 
			
		||||
    def query_regexp(query)
 | 
			
		||||
    def self.query_regexp(query)
 | 
			
		||||
      if (m = query.match(%r{^/(.*)/$}))
 | 
			
		||||
        Regexp.new(m[1])
 | 
			
		||||
      else
 | 
			
		||||
@ -19,7 +19,7 @@ module Homebrew
 | 
			
		||||
      raise "#{query} is not a valid regex."
 | 
			
		||||
    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?
 | 
			
		||||
      eval_all = args.eval_all? || Homebrew::EnvConfig.eval_all?
 | 
			
		||||
 | 
			
		||||
@ -41,7 +41,7 @@ module Homebrew
 | 
			
		||||
      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))
 | 
			
		||||
        _, _, query = query.split("/", 3)
 | 
			
		||||
      end
 | 
			
		||||
@ -86,7 +86,7 @@ module Homebrew
 | 
			
		||||
      results
 | 
			
		||||
    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)
 | 
			
		||||
        return begin
 | 
			
		||||
          [Formulary.factory(string_or_regex).name]
 | 
			
		||||
@ -122,7 +122,7 @@ module Homebrew
 | 
			
		||||
      end.compact
 | 
			
		||||
    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)
 | 
			
		||||
        return begin
 | 
			
		||||
          [Cask::CaskLoader.load(string_or_regex).token]
 | 
			
		||||
@ -151,7 +151,7 @@ module Homebrew
 | 
			
		||||
      end.uniq
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
    def search_names(query, string_or_regex, args)
 | 
			
		||||
    def self.search_names(query, string_or_regex, args)
 | 
			
		||||
      both = !args.formula? && !args.cask?
 | 
			
		||||
 | 
			
		||||
      remote_results = search_taps(query, silent: true)
 | 
			
		||||
 | 
			
		||||
@ -4,12 +4,6 @@
 | 
			
		||||
require "search"
 | 
			
		||||
 | 
			
		||||
describe Homebrew::Search do
 | 
			
		||||
  subject(:mod) { Object.new }
 | 
			
		||||
 | 
			
		||||
  before do
 | 
			
		||||
    mod.extend(described_class)
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe "#search_taps" do
 | 
			
		||||
    before do
 | 
			
		||||
      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
 | 
			
		||||
      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
 | 
			
		||||
 | 
			
		||||
    it "does not raise if the network fails" do
 | 
			
		||||
      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: [])
 | 
			
		||||
    end
 | 
			
		||||
 | 
			
		||||
@ -47,22 +41,22 @@ describe Homebrew::Search do
 | 
			
		||||
 | 
			
		||||
      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"])
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe "#query_regexp" 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
 | 
			
		||||
 | 
			
		||||
    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
 | 
			
		||||
 | 
			
		||||
    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
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user