Merge pull request #4253 from reitermarkus/refactor-search
Refactor `search`.
This commit is contained in:
commit
ce85dd051a
@ -9,7 +9,7 @@ module Hbc
|
|||||||
option "--debug", :debug, false
|
option "--debug", :debug, false
|
||||||
option "--verbose", :verbose, false
|
option "--verbose", :verbose, false
|
||||||
option "--outdated", :outdated_only, false
|
option "--outdated", :outdated_only, false
|
||||||
option "--require-sha", :require_sha, false
|
option "--require-sha", :require_sha, false
|
||||||
|
|
||||||
def self.command_name
|
def self.command_name
|
||||||
@command_name ||= name.sub(/^.*:/, "").gsub(/(.)([A-Z])/, '\1_\2').downcase
|
@command_name ||= name.sub(/^.*:/, "").gsub(/(.)([A-Z])/, '\1_\2').downcase
|
||||||
@ -49,24 +49,18 @@ module Hbc
|
|||||||
casks = args.empty? ? alternative.call : args
|
casks = args.empty? ? alternative.call : args
|
||||||
@casks = casks.map { |cask| CaskLoader.load(cask) }
|
@casks = casks.map { |cask| CaskLoader.load(cask) }
|
||||||
rescue CaskUnavailableError => e
|
rescue CaskUnavailableError => e
|
||||||
reason = [e.reason, suggestion_message(e.token)].join(" ")
|
reason = [e.reason, *suggestion_message(e.token)].join(" ")
|
||||||
raise e.class.new(e.token, reason)
|
raise e.class.new(e.token, reason)
|
||||||
end
|
end
|
||||||
|
|
||||||
def suggestion_message(cask_token)
|
def suggestion_message(cask_token)
|
||||||
exact_match, partial_matches = Search.search(cask_token)
|
matches, = Search.search(cask_token)
|
||||||
|
|
||||||
if exact_match.nil? && partial_matches.count == 1
|
if matches.one?
|
||||||
exact_match = partial_matches.first
|
"Did you mean “#{matches.first}”?"
|
||||||
end
|
elsif !matches.empty?
|
||||||
|
|
||||||
if exact_match
|
|
||||||
"Did you mean “#{exact_match}”?"
|
|
||||||
elsif !partial_matches.empty?
|
|
||||||
"Did you mean one of these?\n"
|
"Did you mean one of these?\n"
|
||||||
.concat(Formatter.columns(partial_matches.take(20)))
|
.concat(Formatter.columns(matches.take(20)))
|
||||||
else
|
|
||||||
""
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,6 +1,10 @@
|
|||||||
|
require "search"
|
||||||
|
|
||||||
module Hbc
|
module Hbc
|
||||||
class CLI
|
class CLI
|
||||||
class Search < AbstractCommand
|
class Search < AbstractCommand
|
||||||
|
extend Homebrew::Search
|
||||||
|
|
||||||
def run
|
def run
|
||||||
if args.empty?
|
if args.empty?
|
||||||
puts Formatter.columns(CLI.nice_listing(Cask.map(&:qualified_token)))
|
puts Formatter.columns(CLI.nice_listing(Cask.map(&:qualified_token)))
|
||||||
@ -18,28 +22,7 @@ module Hbc
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.search_remote(query)
|
|
||||||
matches = begin
|
|
||||||
GitHub.search_code(
|
|
||||||
user: "Homebrew",
|
|
||||||
path: "Casks",
|
|
||||||
filename: query,
|
|
||||||
extension: "rb",
|
|
||||||
)
|
|
||||||
rescue GitHub::Error => error
|
|
||||||
opoo "Error searching on GitHub: #{error}\n"
|
|
||||||
[]
|
|
||||||
end
|
|
||||||
|
|
||||||
matches.map do |match|
|
|
||||||
tap = Tap.fetch(match["repository"]["full_name"])
|
|
||||||
next if tap.installed?
|
|
||||||
"#{tap.name}/#{File.basename(match["path"], ".rb")}"
|
|
||||||
end.compact
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.search(*arguments)
|
def self.search(*arguments)
|
||||||
exact_match = nil
|
|
||||||
partial_matches = []
|
partial_matches = []
|
||||||
search_term = arguments.join(" ")
|
search_term = arguments.join(" ")
|
||||||
search_regexp = extract_regexp arguments.first
|
search_regexp = extract_regexp arguments.first
|
||||||
@ -50,36 +33,30 @@ module Hbc
|
|||||||
else
|
else
|
||||||
simplified_tokens = all_tokens.map { |t| t.sub(%r{^.*\/}, "").gsub(/[^a-z0-9]+/i, "") }
|
simplified_tokens = all_tokens.map { |t| t.sub(%r{^.*\/}, "").gsub(/[^a-z0-9]+/i, "") }
|
||||||
simplified_search_term = search_term.sub(/\.rb$/i, "").gsub(/[^a-z0-9]+/i, "")
|
simplified_search_term = search_term.sub(/\.rb$/i, "").gsub(/[^a-z0-9]+/i, "")
|
||||||
exact_match = simplified_tokens.grep(/^#{simplified_search_term}$/i) { |t| all_tokens[simplified_tokens.index(t)] }.first
|
|
||||||
partial_matches = simplified_tokens.grep(/#{simplified_search_term}/i) { |t| all_tokens[simplified_tokens.index(t)] }
|
partial_matches = simplified_tokens.grep(/#{simplified_search_term}/i) { |t| all_tokens[simplified_tokens.index(t)] }
|
||||||
partial_matches.delete(exact_match)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
remote_matches = search_remote(search_term)
|
remote_matches = search_taps(search_term, silent: true)[:casks]
|
||||||
|
|
||||||
[exact_match, partial_matches, remote_matches, search_term]
|
[partial_matches, remote_matches, search_term]
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.render_results(exact_match, partial_matches, remote_matches, search_term)
|
def self.render_results(partial_matches, remote_matches, search_term)
|
||||||
unless $stdout.tty?
|
unless $stdout.tty?
|
||||||
puts [*exact_match, *partial_matches, *remote_matches]
|
puts [*partial_matches, *remote_matches]
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
if !exact_match && partial_matches.empty? && remote_matches.empty?
|
if partial_matches.empty? && remote_matches.empty?
|
||||||
puts "No Cask found for \"#{search_term}\"."
|
puts "No Cask found for \"#{search_term}\"."
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
if exact_match
|
|
||||||
ohai "Exact Match"
|
|
||||||
puts highlight_installed exact_match
|
|
||||||
end
|
|
||||||
|
|
||||||
unless partial_matches.empty?
|
unless partial_matches.empty?
|
||||||
if extract_regexp search_term
|
if extract_regexp search_term
|
||||||
ohai "Regexp Matches"
|
ohai "Regexp Matches"
|
||||||
else
|
else
|
||||||
ohai "Partial Matches"
|
ohai "Matches"
|
||||||
end
|
end
|
||||||
puts Formatter.columns(partial_matches.map(&method(:highlight_installed)))
|
puts Formatter.columns(partial_matches.map(&method(:highlight_installed)))
|
||||||
end
|
end
|
||||||
|
|||||||
@ -5,8 +5,8 @@ require "set"
|
|||||||
module Homebrew
|
module Homebrew
|
||||||
module CLI
|
module CLI
|
||||||
class Parser
|
class Parser
|
||||||
def self.parse(&block)
|
def self.parse(args = ARGV, &block)
|
||||||
new(&block).parse
|
new(&block).parse(args)
|
||||||
end
|
end
|
||||||
|
|
||||||
def initialize(&block)
|
def initialize(&block)
|
||||||
@ -60,17 +60,28 @@ module Homebrew
|
|||||||
@conflicts << options.map { |option| option_to_name(option) }
|
@conflicts << options.map { |option| option_to_name(option) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def option_to_name(name)
|
def option_to_name(option)
|
||||||
name.sub(/\A--?/, "").tr("-", "_").delete("=")
|
option.sub(/\A--?/, "")
|
||||||
|
.tr("-", "_")
|
||||||
|
.delete("=")
|
||||||
|
end
|
||||||
|
|
||||||
|
def name_to_option(name)
|
||||||
|
if name.length == 1
|
||||||
|
"-#{name}"
|
||||||
|
else
|
||||||
|
"--#{name}"
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def option_to_description(*names)
|
def option_to_description(*names)
|
||||||
names.map { |name| name.to_s.sub(/\A--?/, "").tr("-", " ") }.max
|
names.map { |name| name.to_s.sub(/\A--?/, "").tr("-", " ") }.max
|
||||||
end
|
end
|
||||||
|
|
||||||
def parse(cmdline_args = ARGV)
|
def parse(cmdline_args)
|
||||||
@parser.parse(cmdline_args)
|
remaining_args = @parser.parse(cmdline_args)
|
||||||
check_constraint_violations
|
check_constraint_violations
|
||||||
|
Homebrew.args[:remaining] = remaining_args
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
@ -126,7 +137,9 @@ module Homebrew
|
|||||||
violations = mutually_exclusive_options_group.select do |option|
|
violations = mutually_exclusive_options_group.select do |option|
|
||||||
option_passed? option
|
option_passed? option
|
||||||
end
|
end
|
||||||
raise OptionConflictError, violations if violations.length > 1
|
|
||||||
|
next if violations.count < 2
|
||||||
|
raise OptionConflictError, violations.map(&method(:name_to_option))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -163,9 +176,10 @@ module Homebrew
|
|||||||
|
|
||||||
class OptionConflictError < RuntimeError
|
class OptionConflictError < RuntimeError
|
||||||
def initialize(args)
|
def initialize(args)
|
||||||
args_list = args.join("` and `")
|
args_list = args.map(&Formatter.public_method(:option))
|
||||||
|
.join(" and ")
|
||||||
super <<~EOS
|
super <<~EOS
|
||||||
`#{args_list}` are mutually exclusive
|
Options #{args_list} are mutually exclusive.
|
||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -9,11 +9,13 @@
|
|||||||
#: first search, making that search slower than subsequent ones.
|
#: first search, making that search slower than subsequent ones.
|
||||||
|
|
||||||
require "descriptions"
|
require "descriptions"
|
||||||
require "cmd/search"
|
require "search"
|
||||||
|
|
||||||
module Homebrew
|
module Homebrew
|
||||||
module_function
|
module_function
|
||||||
|
|
||||||
|
extend Search
|
||||||
|
|
||||||
def desc
|
def desc
|
||||||
search_type = []
|
search_type = []
|
||||||
search_type << :either if ARGV.flag? "--search"
|
search_type << :either if ARGV.flag? "--search"
|
||||||
@ -28,9 +30,10 @@ module Homebrew
|
|||||||
results.print
|
results.print
|
||||||
elsif search_type.size > 1
|
elsif search_type.size > 1
|
||||||
odie "Pick one, and only one, of -s/--search, -n/--name, or -d/--description."
|
odie "Pick one, and only one, of -s/--search, -n/--name, or -d/--description."
|
||||||
elsif arg = ARGV.named.first
|
elsif !ARGV.named.empty?
|
||||||
regex = Homebrew.query_regexp(arg)
|
arg = ARGV.named.join(" ")
|
||||||
results = Descriptions.search(regex, search_type.first)
|
string_or_regex = query_regexp(arg)
|
||||||
|
results = Descriptions.search(string_or_regex, search_type.first)
|
||||||
results.print
|
results.print
|
||||||
else
|
else
|
||||||
odie "You must provide a search term."
|
odie "You must provide a search term."
|
||||||
|
|||||||
@ -68,14 +68,16 @@
|
|||||||
#: creating patches to the software.
|
#: creating patches to the software.
|
||||||
|
|
||||||
require "missing_formula"
|
require "missing_formula"
|
||||||
require "cmd/search"
|
|
||||||
require "formula_installer"
|
require "formula_installer"
|
||||||
require "development_tools"
|
require "development_tools"
|
||||||
require "install"
|
require "install"
|
||||||
|
require "search"
|
||||||
|
|
||||||
module Homebrew
|
module Homebrew
|
||||||
module_function
|
module_function
|
||||||
|
|
||||||
|
extend Search
|
||||||
|
|
||||||
def install
|
def install
|
||||||
raise FormulaUnspecifiedError if ARGV.named.empty?
|
raise FormulaUnspecifiedError if ARGV.named.empty?
|
||||||
|
|
||||||
@ -261,10 +263,8 @@ module Homebrew
|
|||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
regex = query_regexp(e.name)
|
|
||||||
|
|
||||||
ohai "Searching for similarly named formulae..."
|
ohai "Searching for similarly named formulae..."
|
||||||
formulae_search_results = search_formulae(regex)
|
formulae_search_results = search_formulae(e.name)
|
||||||
case formulae_search_results.length
|
case formulae_search_results.length
|
||||||
when 0
|
when 0
|
||||||
ofail "No similarly named formulae found."
|
ofail "No similarly named formulae found."
|
||||||
@ -281,7 +281,7 @@ module Homebrew
|
|||||||
# Do not search taps if the formula name is qualified
|
# Do not search taps if the formula name is qualified
|
||||||
return if e.name.include?("/")
|
return if e.name.include?("/")
|
||||||
ohai "Searching taps..."
|
ohai "Searching taps..."
|
||||||
taps_search_results = search_taps(e.name)
|
taps_search_results = search_taps(e.name)[:formulae]
|
||||||
case taps_search_results.length
|
case taps_search_results.length
|
||||||
when 0
|
when 0
|
||||||
ofail "No formulae found in taps."
|
ofail "No formulae found in taps."
|
||||||
|
|||||||
@ -16,49 +16,69 @@
|
|||||||
require "formula"
|
require "formula"
|
||||||
require "missing_formula"
|
require "missing_formula"
|
||||||
require "descriptions"
|
require "descriptions"
|
||||||
|
require "cli_parser"
|
||||||
|
require "search"
|
||||||
|
|
||||||
module Homebrew
|
module Homebrew
|
||||||
module_function
|
module_function
|
||||||
|
|
||||||
def search
|
extend Search
|
||||||
if ARGV.empty?
|
|
||||||
puts Formatter.columns(Formula.full_names.sort)
|
|
||||||
elsif ARGV.include? "--macports"
|
|
||||||
exec_browser "https://www.macports.org/ports.php?by=name&substr=#{ARGV.next}"
|
|
||||||
elsif ARGV.include? "--fink"
|
|
||||||
exec_browser "http://pdb.finkproject.org/pdb/browse.php?summary=#{ARGV.next}"
|
|
||||||
elsif ARGV.include? "--debian"
|
|
||||||
exec_browser "https://packages.debian.org/search?keywords=#{ARGV.next}&searchon=names&suite=all§ion=all"
|
|
||||||
elsif ARGV.include? "--opensuse"
|
|
||||||
exec_browser "https://software.opensuse.org/search?q=#{ARGV.next}"
|
|
||||||
elsif ARGV.include? "--fedora"
|
|
||||||
exec_browser "https://apps.fedoraproject.org/packages/s/#{ARGV.next}"
|
|
||||||
elsif ARGV.include? "--ubuntu"
|
|
||||||
exec_browser "https://packages.ubuntu.com/search?keywords=#{ARGV.next}&searchon=names&suite=all§ion=all"
|
|
||||||
elsif ARGV.include? "--desc"
|
|
||||||
query = ARGV.next
|
|
||||||
regex = query_regexp(query)
|
|
||||||
Descriptions.search(regex, :desc).print
|
|
||||||
elsif ARGV.first =~ HOMEBREW_TAP_FORMULA_REGEX
|
|
||||||
query = ARGV.first
|
|
||||||
|
|
||||||
begin
|
PACKAGE_MANAGERS = {
|
||||||
result = Formulary.factory(query).name
|
macports: ->(query) { "https://www.macports.org/ports.php?by=name&substr=#{query}" },
|
||||||
results = Array(result)
|
fink: ->(query) { "http://pdb.finkproject.org/pdb/browse.php?summary=#{query}" },
|
||||||
rescue FormulaUnavailableError
|
debian: ->(query) { "https://packages.debian.org/search?keywords=#{query}&searchon=names&suite=all§ion=all" },
|
||||||
_, _, name = query.split("/", 3)
|
opensuse: ->(query) { "https://software.opensuse.org/search?q=#{query}" },
|
||||||
results = search_taps(name)
|
fedora: ->(query) { "https://apps.fedoraproject.org/packages/s/#{query}" },
|
||||||
|
ubuntu: ->(query) { "https://packages.ubuntu.com/search?keywords=#{query}&searchon=names&suite=all§ion=all" },
|
||||||
|
}.freeze
|
||||||
|
|
||||||
|
def search(argv = ARGV)
|
||||||
|
CLI::Parser.parse(argv) do
|
||||||
|
switch "--desc"
|
||||||
|
|
||||||
|
package_manager_switches = PACKAGE_MANAGERS.keys.map { |name| "--#{name}" }
|
||||||
|
|
||||||
|
package_manager_switches.each do |s|
|
||||||
|
switch s
|
||||||
end
|
end
|
||||||
|
|
||||||
puts Formatter.columns(results.sort) unless results.empty?
|
conflicts(*package_manager_switches)
|
||||||
|
end
|
||||||
|
|
||||||
|
if package_manager = PACKAGE_MANAGERS.detect { |name,| args[:"#{name}?"] }
|
||||||
|
_, url = package_manager
|
||||||
|
exec_browser url.call(URI.encode_www_form_component(args.remaining.join(" ")))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
if args.remaining.empty?
|
||||||
|
puts Formatter.columns(Formula.full_names.sort)
|
||||||
|
elsif args.desc?
|
||||||
|
query = args.remaining.join(" ")
|
||||||
|
string_or_regex = query_regexp(query)
|
||||||
|
Descriptions.search(string_or_regex, :desc).print
|
||||||
|
elsif args.remaining.first =~ HOMEBREW_TAP_FORMULA_REGEX
|
||||||
|
query = args.remaining.first
|
||||||
|
|
||||||
|
results = begin
|
||||||
|
[Formulary.factory(query).name]
|
||||||
|
rescue FormulaUnavailableError
|
||||||
|
_, _, name = query.split("/", 3)
|
||||||
|
remote_results = search_taps(name)
|
||||||
|
[*remote_results[:formulae], *remote_results[:casks]].sort
|
||||||
|
end
|
||||||
|
|
||||||
|
puts Formatter.columns(results) unless results.empty?
|
||||||
else
|
else
|
||||||
query = ARGV.first
|
query = args.remaining.join(" ")
|
||||||
regex = query_regexp(query)
|
string_or_regex = query_regexp(query)
|
||||||
local_results = search_formulae(regex)
|
local_results = search_formulae(string_or_regex)
|
||||||
puts Formatter.columns(local_results.sort) unless local_results.empty?
|
puts Formatter.columns(local_results.sort) unless local_results.empty?
|
||||||
|
|
||||||
tap_results = search_taps(query)
|
remote_results = search_taps(query)
|
||||||
puts Formatter.columns(tap_results.sort) unless tap_results.empty?
|
tap_results = [*remote_results[:formulae], *remote_results[:casks]].sort
|
||||||
|
puts Formatter.columns(tap_results) unless tap_results.empty?
|
||||||
|
|
||||||
if $stdout.tty?
|
if $stdout.tty?
|
||||||
count = local_results.length + tap_results.length
|
count = local_results.length + tap_results.length
|
||||||
@ -78,10 +98,10 @@ module Homebrew
|
|||||||
end
|
end
|
||||||
|
|
||||||
return unless $stdout.tty?
|
return unless $stdout.tty?
|
||||||
return if ARGV.empty?
|
return if args.remaining.empty?
|
||||||
metacharacters = %w[\\ | ( ) [ ] { } ^ $ * + ?].freeze
|
metacharacters = %w[\\ | ( ) [ ] { } ^ $ * + ?].freeze
|
||||||
return unless metacharacters.any? do |char|
|
return unless metacharacters.any? do |char|
|
||||||
ARGV.any? do |arg|
|
args.remaining.any? do |arg|
|
||||||
arg.include?(char) && !arg.start_with?("/")
|
arg.include?(char) && !arg.start_with?("/")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -90,67 +110,4 @@ module Homebrew
|
|||||||
Surround your query with /slashes/ to search locally by regex.
|
Surround your query with /slashes/ to search locally by regex.
|
||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
|
|
||||||
def query_regexp(query)
|
|
||||||
case query
|
|
||||||
when %r{^/(.*)/$} then Regexp.new(Regexp.last_match(1))
|
|
||||||
else /.*#{Regexp.escape(query)}.*/i
|
|
||||||
end
|
|
||||||
rescue RegexpError
|
|
||||||
odie "#{query} is not a valid regex"
|
|
||||||
end
|
|
||||||
|
|
||||||
def search_taps(query, silent: false)
|
|
||||||
return [] if ENV["HOMEBREW_NO_GITHUB_API"]
|
|
||||||
|
|
||||||
# Use stderr to avoid breaking parsed output
|
|
||||||
unless silent
|
|
||||||
$stderr.puts Formatter.headline("Searching taps on GitHub...", color: :blue)
|
|
||||||
end
|
|
||||||
|
|
||||||
matches = begin
|
|
||||||
GitHub.search_code(
|
|
||||||
user: "Homebrew",
|
|
||||||
path: ["Formula", "HomebrewFormula", "Casks", "."],
|
|
||||||
filename: query,
|
|
||||||
extension: "rb",
|
|
||||||
)
|
|
||||||
rescue GitHub::Error => error
|
|
||||||
opoo "Error searching on GitHub: #{error}\n"
|
|
||||||
[]
|
|
||||||
end
|
|
||||||
matches.map do |match|
|
|
||||||
filename = File.basename(match["path"], ".rb")
|
|
||||||
tap = Tap.fetch(match["repository"]["full_name"])
|
|
||||||
next if tap.installed? && !tap.name.start_with?("homebrew/cask")
|
|
||||||
"#{tap.name}/#{filename}"
|
|
||||||
end.compact
|
|
||||||
end
|
|
||||||
|
|
||||||
def search_formulae(regex)
|
|
||||||
# Use stderr to avoid breaking parsed output
|
|
||||||
$stderr.puts Formatter.headline("Searching local taps...", color: :blue)
|
|
||||||
|
|
||||||
aliases = Formula.alias_full_names
|
|
||||||
results = (Formula.full_names + aliases).grep(regex).sort
|
|
||||||
|
|
||||||
results.map do |name|
|
|
||||||
begin
|
|
||||||
formula = Formulary.factory(name)
|
|
||||||
canonical_name = formula.name
|
|
||||||
canonical_full_name = formula.full_name
|
|
||||||
rescue
|
|
||||||
canonical_name = canonical_full_name = name
|
|
||||||
end
|
|
||||||
|
|
||||||
# Ignore aliases from results when the full name was also found
|
|
||||||
next if aliases.include?(name) && results.include?(canonical_full_name)
|
|
||||||
|
|
||||||
if (HOMEBREW_CELLAR/canonical_name).directory?
|
|
||||||
pretty_installed(name)
|
|
||||||
else
|
|
||||||
name
|
|
||||||
end
|
|
||||||
end.compact
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,7 +1,11 @@
|
|||||||
require "formula"
|
require "formula"
|
||||||
require "formula_versions"
|
require "formula_versions"
|
||||||
|
require "search"
|
||||||
|
require "searchable"
|
||||||
|
|
||||||
class Descriptions
|
class Descriptions
|
||||||
|
extend Homebrew::Search
|
||||||
|
|
||||||
CACHE_FILE = HOMEBREW_CACHE + "desc_cache.json"
|
CACHE_FILE = HOMEBREW_CACHE + "desc_cache.json"
|
||||||
|
|
||||||
def self.cache
|
def self.cache
|
||||||
@ -94,16 +98,18 @@ class Descriptions
|
|||||||
end
|
end
|
||||||
|
|
||||||
# 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(regex, field = :either)
|
def self.search(string_or_regex, field = :either)
|
||||||
ensure_cache
|
ensure_cache
|
||||||
|
|
||||||
|
@cache.extend(Searchable)
|
||||||
|
|
||||||
results = case field
|
results = case field
|
||||||
when :name
|
when :name
|
||||||
@cache.select { |name, _| name =~ regex }
|
@cache.search(string_or_regex) { |name, _| name }
|
||||||
when :desc
|
when :desc
|
||||||
@cache.select { |_, desc| desc =~ regex }
|
@cache.search(string_or_regex) { |_, desc| desc }
|
||||||
when :either
|
when :either
|
||||||
@cache.select { |name, desc| (name =~ regex) || (desc =~ regex) }
|
@cache.search(string_or_regex)
|
||||||
end
|
end
|
||||||
|
|
||||||
new(results)
|
new(results)
|
||||||
|
|||||||
@ -39,16 +39,12 @@ module Homebrew
|
|||||||
ENV.delete("HOMEBREW_CASK_OPTS")
|
ENV.delete("HOMEBREW_CASK_OPTS")
|
||||||
ENV.delete("HOMEBREW_TEMP")
|
ENV.delete("HOMEBREW_TEMP")
|
||||||
ENV.delete("HOMEBREW_LINKAGE_CACHE")
|
ENV.delete("HOMEBREW_LINKAGE_CACHE")
|
||||||
|
ENV.delete("HOMEBREW_NO_GITHUB_API")
|
||||||
ENV["HOMEBREW_NO_ANALYTICS_THIS_RUN"] = "1"
|
ENV["HOMEBREW_NO_ANALYTICS_THIS_RUN"] = "1"
|
||||||
ENV["HOMEBREW_DEVELOPER"] = "1"
|
ENV["HOMEBREW_DEVELOPER"] = "1"
|
||||||
ENV["HOMEBREW_NO_COMPAT"] = "1" if args.no_compat?
|
ENV["HOMEBREW_NO_COMPAT"] = "1" if args.no_compat?
|
||||||
ENV["HOMEBREW_TEST_GENERIC_OS"] = "1" if args.generic?
|
ENV["HOMEBREW_TEST_GENERIC_OS"] = "1" if args.generic?
|
||||||
|
ENV["HOMEBREW_TEST_ONLINE"] = "1" if args.online?
|
||||||
if args.online?
|
|
||||||
ENV["HOMEBREW_TEST_ONLINE"] = "1"
|
|
||||||
else
|
|
||||||
ENV["HOMEBREW_NO_GITHUB_API"] = "1"
|
|
||||||
end
|
|
||||||
|
|
||||||
if args.coverage?
|
if args.coverage?
|
||||||
ENV["HOMEBREW_TESTS_COVERAGE"] = "1"
|
ENV["HOMEBREW_TESTS_COVERAGE"] = "1"
|
||||||
|
|||||||
84
Library/Homebrew/search.rb
Normal file
84
Library/Homebrew/search.rb
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
require "searchable"
|
||||||
|
|
||||||
|
module Homebrew
|
||||||
|
module Search
|
||||||
|
def query_regexp(query)
|
||||||
|
if m = query.match(%r{^/(.*)/$})
|
||||||
|
Regexp.new(m[1])
|
||||||
|
else
|
||||||
|
query
|
||||||
|
end
|
||||||
|
rescue RegexpError
|
||||||
|
raise "#{query} is not a valid regex."
|
||||||
|
end
|
||||||
|
|
||||||
|
def search_taps(query, silent: false)
|
||||||
|
results = { formulae: [], casks: [] }
|
||||||
|
|
||||||
|
return results if ENV["HOMEBREW_NO_GITHUB_API"]
|
||||||
|
|
||||||
|
unless silent
|
||||||
|
# Use stderr to avoid breaking parsed output
|
||||||
|
$stderr.puts Formatter.headline("Searching taps on GitHub...", color: :blue)
|
||||||
|
end
|
||||||
|
|
||||||
|
matches = begin
|
||||||
|
GitHub.search_code(
|
||||||
|
user: "Homebrew",
|
||||||
|
path: ["Formula", "Casks", "."],
|
||||||
|
filename: query,
|
||||||
|
extension: "rb",
|
||||||
|
)
|
||||||
|
rescue GitHub::Error => error
|
||||||
|
opoo "Error searching on GitHub: #{error}\n"
|
||||||
|
return results
|
||||||
|
end
|
||||||
|
|
||||||
|
matches.each do |match|
|
||||||
|
name = File.basename(match["path"], ".rb")
|
||||||
|
tap = Tap.fetch(match["repository"]["full_name"])
|
||||||
|
full_name = "#{tap.name}/#{name}"
|
||||||
|
|
||||||
|
next if tap.installed? && !match["path"].start_with?("Casks/")
|
||||||
|
|
||||||
|
if match["path"].start_with?("Casks/")
|
||||||
|
results[:casks] = [*results[:casks], full_name].sort
|
||||||
|
else
|
||||||
|
results[:formulae] = [*results[:formulae], full_name].sort
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
results
|
||||||
|
end
|
||||||
|
|
||||||
|
def search_formulae(string_or_regex)
|
||||||
|
# Use stderr to avoid breaking parsed output
|
||||||
|
$stderr.puts Formatter.headline("Searching local taps...", color: :blue)
|
||||||
|
|
||||||
|
aliases = Formula.alias_full_names
|
||||||
|
results = (Formula.full_names + aliases)
|
||||||
|
.extend(Searchable)
|
||||||
|
.search(string_or_regex)
|
||||||
|
.sort
|
||||||
|
|
||||||
|
results.map do |name|
|
||||||
|
begin
|
||||||
|
formula = Formulary.factory(name)
|
||||||
|
canonical_name = formula.name
|
||||||
|
canonical_full_name = formula.full_name
|
||||||
|
rescue
|
||||||
|
canonical_name = canonical_full_name = name
|
||||||
|
end
|
||||||
|
|
||||||
|
# Ignore aliases from results when the full name was also found
|
||||||
|
next if aliases.include?(name) && results.include?(canonical_full_name)
|
||||||
|
|
||||||
|
if (HOMEBREW_CELLAR/canonical_name).directory?
|
||||||
|
pretty_installed(name)
|
||||||
|
else
|
||||||
|
name
|
||||||
|
end
|
||||||
|
end.compact
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
31
Library/Homebrew/searchable.rb
Normal file
31
Library/Homebrew/searchable.rb
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
module Searchable
|
||||||
|
def search(string_or_regex, &block)
|
||||||
|
case string_or_regex
|
||||||
|
when Regexp
|
||||||
|
search_regex(string_or_regex, &block)
|
||||||
|
else
|
||||||
|
search_string(string_or_regex.to_str, &block)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def simplify_string(string)
|
||||||
|
string.downcase.gsub(/[^a-z\d]/i, "")
|
||||||
|
end
|
||||||
|
|
||||||
|
def search_regex(regex)
|
||||||
|
select do |*args|
|
||||||
|
args = yield(*args) if block_given?
|
||||||
|
[*args].any? { |arg| arg.match?(regex) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def search_string(string)
|
||||||
|
simplified_string = simplify_string(string)
|
||||||
|
select do |*args|
|
||||||
|
args = yield(*args) if block_given?
|
||||||
|
[*args].any? { |arg| simplify_string(arg).include?(simplified_string) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -13,7 +13,7 @@ describe Hbc::CLI::Search, :cask do
|
|||||||
expect {
|
expect {
|
||||||
Hbc::CLI::Search.run("local")
|
Hbc::CLI::Search.run("local")
|
||||||
}.to output(<<~EOS).to_stdout.as_tty
|
}.to output(<<~EOS).to_stdout.as_tty
|
||||||
==> Partial Matches
|
==> Matches
|
||||||
local-caffeine
|
local-caffeine
|
||||||
local-transmission
|
local-transmission
|
||||||
EOS
|
EOS
|
||||||
@ -51,6 +51,8 @@ describe Hbc::CLI::Search, :cask do
|
|||||||
end
|
end
|
||||||
|
|
||||||
it "doesn't output anything to non-TTY stdout when there are no matches" do
|
it "doesn't output anything to non-TTY stdout when there are no matches" do
|
||||||
|
allow(GitHub).to receive(:search_code).and_return([])
|
||||||
|
|
||||||
expect { Hbc::CLI::Search.run("foo-bar-baz") }
|
expect { Hbc::CLI::Search.run("foo-bar-baz") }
|
||||||
.to not_to_output.to_stdout
|
.to not_to_output.to_stdout
|
||||||
.and not_to_output.to_stderr
|
.and not_to_output.to_stderr
|
||||||
@ -94,9 +96,8 @@ describe Hbc::CLI::Search, :cask do
|
|||||||
expect {
|
expect {
|
||||||
Hbc::CLI::Search.run("test-opera")
|
Hbc::CLI::Search.run("test-opera")
|
||||||
}.to output(<<~EOS).to_stdout.as_tty
|
}.to output(<<~EOS).to_stdout.as_tty
|
||||||
==> Exact Match
|
==> Matches
|
||||||
test-opera
|
test-opera
|
||||||
==> Partial Matches
|
|
||||||
test-opera-mail
|
test-opera-mail
|
||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
|
|||||||
@ -1,24 +0,0 @@
|
|||||||
require "cmd/search"
|
|
||||||
|
|
||||||
describe Homebrew do
|
|
||||||
specify "#search_taps" do
|
|
||||||
# Otherwise the tested method returns [], regardless of our stub
|
|
||||||
ENV.delete("HOMEBREW_NO_GITHUB_API")
|
|
||||||
|
|
||||||
json_response = {
|
|
||||||
"items" => [
|
|
||||||
{
|
|
||||||
"path" => "Formula/some-formula.rb",
|
|
||||||
"repository" => {
|
|
||||||
"full_name" => "Homebrew/homebrew-foo",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
}
|
|
||||||
|
|
||||||
allow(GitHub).to receive(:open_api).and_yield(json_response)
|
|
||||||
|
|
||||||
expect(described_class.search_taps("some-formula"))
|
|
||||||
.to match(["homebrew/foo/some-formula"])
|
|
||||||
end
|
|
||||||
end
|
|
||||||
@ -1,3 +1,5 @@
|
|||||||
|
require "cmd/search"
|
||||||
|
|
||||||
describe "brew search", :integration_test do
|
describe "brew search", :integration_test do
|
||||||
before do
|
before do
|
||||||
setup_test_formula "testball"
|
setup_test_formula "testball"
|
||||||
|
|||||||
65
Library/Homebrew/test/search_spec.rb
Normal file
65
Library/Homebrew/test/search_spec.rb
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
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")
|
||||||
|
end
|
||||||
|
|
||||||
|
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: [])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "does not raise if the network fails" do
|
||||||
|
allow(GitHub).to receive(:open_api).and_raise(GitHub::Error)
|
||||||
|
|
||||||
|
expect(mod.search_taps("some-formula"))
|
||||||
|
.to match(formulae: [], casks: [])
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns Formulae and Casks separately" do
|
||||||
|
json_response = {
|
||||||
|
"items" => [
|
||||||
|
{
|
||||||
|
"path" => "Formula/some-formula.rb",
|
||||||
|
"repository" => {
|
||||||
|
"full_name" => "Homebrew/homebrew-foo",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path" => "Casks/some-cask.rb",
|
||||||
|
"repository" => {
|
||||||
|
"full_name" => "Homebrew/homebrew-bar",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
}
|
||||||
|
|
||||||
|
allow(GitHub).to receive(:open_api).and_yield(json_response)
|
||||||
|
|
||||||
|
expect(mod.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$/)
|
||||||
|
end
|
||||||
|
|
||||||
|
it "returns the original string if it is not a regex query" do
|
||||||
|
expect(mod.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/)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
30
Library/Homebrew/test/searchable_spec.rb
Normal file
30
Library/Homebrew/test/searchable_spec.rb
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
require "searchable"
|
||||||
|
|
||||||
|
describe Searchable do
|
||||||
|
subject { ary.extend(described_class) }
|
||||||
|
|
||||||
|
let(:ary) { ["with-dashes"] }
|
||||||
|
|
||||||
|
describe "#search" do
|
||||||
|
context "when given a block" do
|
||||||
|
let(:ary) { [["with-dashes", "withdashes"]] }
|
||||||
|
|
||||||
|
it "searches by the selected argument" do
|
||||||
|
expect(subject.search(/withdashes/) { |_, short_name| short_name }).not_to be_empty
|
||||||
|
expect(subject.search(/withdashes/) { |long_name, _| long_name }).to be_empty
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when given a regex" do
|
||||||
|
it "does not simplify strings" do
|
||||||
|
expect(subject.search(/with\-dashes/)).to eq ["with-dashes"]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when given a string" do
|
||||||
|
it "simplifies both the query and searched strings" do
|
||||||
|
expect(subject.search("with dashes")).to eq ["with-dashes"]
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -15,6 +15,10 @@ module Formatter
|
|||||||
"#{Tty.green}#{string}#{Tty.default}"
|
"#{Tty.green}#{string}#{Tty.default}"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def option(string)
|
||||||
|
"#{Tty.bold}#{string}#{Tty.reset}"
|
||||||
|
end
|
||||||
|
|
||||||
def success(string, label: nil)
|
def success(string, label: nil)
|
||||||
label(label, string, :green)
|
label(label, string, :green)
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user