Port Homebrew::Cmd::Search

This commit is contained in:
Douglas Eichelberger 2024-04-01 10:12:52 -07:00
parent d875c970e9
commit 5495ff1eea
2 changed files with 138 additions and 138 deletions

View File

@ -1,15 +1,15 @@
# typed: true # typed: true
# frozen_string_literal: true # frozen_string_literal: true
require "abstract_command"
require "formula" require "formula"
require "missing_formula" require "missing_formula"
require "descriptions" require "descriptions"
require "cli/parser"
require "search" require "search"
module Homebrew module Homebrew
module_function module Cmd
class SearchCmd < AbstractCommand
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}" },
@ -25,9 +25,7 @@ module Homebrew
}, },
}.freeze }.freeze
sig { returns(CLI::Parser) } cmd_args do
def search_args
Homebrew::CLI::Parser.new do
description <<~EOS description <<~EOS
Perform a substring search of cask tokens and formula names for <text>. If <text> Perform a substring search of cask tokens and formula names for <text>. If <text>
is flanked by slashes, it is interpreted as a regular expression. is flanked by slashes, it is interpreted as a regular expression.
@ -63,12 +61,10 @@ module Homebrew
named_args :text_or_regex, min: 1 named_args :text_or_regex, min: 1
end end
end
def search sig { override.void }
args = search_args.parse def run
return if search_package_manager
return if search_package_manager(args)
query = args.named.join(" ") query = args.named.join(" ")
string_or_regex = Search.query_regexp(query) string_or_regex = Search.query_regexp(query)
@ -80,7 +76,7 @@ module Homebrew
Search.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)
else else
formulae, casks = Search.search_names(string_or_regex, args) formulae, casks = Search.search_names(string_or_regex, args)
print_results(formulae, casks, query) print_results(formulae, casks, query)
@ -88,10 +84,12 @@ module Homebrew
puts "Use `brew desc` to list packages with a short description." if args.verbose? puts "Use `brew desc` to list packages with a short description." if args.verbose?
print_regex_help(args) print_regex_help
end end
def print_regex_help(args) private
def print_regex_help
return unless $stdout.tty? return unless $stdout.tty?
metacharacters = %w[\\ | ( ) [ ] { } ^ $ * + ?].freeze metacharacters = %w[\\ | ( ) [ ] { } ^ $ * + ?].freeze
@ -107,7 +105,7 @@ module Homebrew
EOS EOS
end end
def search_package_manager(args) def search_package_manager
package_manager = PACKAGE_MANAGERS.find { |name,| args[:"#{name}?"] } package_manager = PACKAGE_MANAGERS.find { |name,| args[:"#{name}?"] }
return false if package_manager.nil? return false if package_manager.nil?
@ -116,7 +114,7 @@ module Homebrew
true true
end end
def search_pull_requests(query, args) def search_pull_requests(query)
only = if args.open? && !args.closed? only = if args.open? && !args.closed?
"open" "open"
elsif args.closed? && !args.open? elsif args.closed? && !args.open?
@ -163,3 +161,5 @@ module Homebrew
puts reason puts reason
end end
end end
end
end

View File

@ -3,7 +3,7 @@
require "cmd/search" require "cmd/search"
require "cmd/shared_examples/args_parse" require "cmd/shared_examples/args_parse"
RSpec.describe "brew search" do RSpec.describe Homebrew::Cmd::SearchCmd do
it_behaves_like "parseable arguments" it_behaves_like "parseable arguments"
it "finds formula in search", :integration_test do it "finds formula in search", :integration_test do