Refactor search
.
This commit is contained in:
parent
6a8194a60e
commit
b2c85ad945
@ -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
|
||||||
|
@ -16,31 +16,45 @@
|
|||||||
require "formula"
|
require "formula"
|
||||||
require "missing_formula"
|
require "missing_formula"
|
||||||
require "descriptions"
|
require "descriptions"
|
||||||
|
require "cli_parser"
|
||||||
|
|
||||||
module Homebrew
|
module Homebrew
|
||||||
module_function
|
module_function
|
||||||
|
|
||||||
def search
|
PACKAGE_MANAGERS = {
|
||||||
if ARGV.empty?
|
macports: ->(query) { "https://www.macports.org/ports.php?by=name&substr=#{query}" },
|
||||||
|
fink: ->(query) { "http://pdb.finkproject.org/pdb/browse.php?summary=#{query}" },
|
||||||
|
debian: ->(query) { "https://packages.debian.org/search?keywords=#{query}&searchon=names&suite=all§ion=all" },
|
||||||
|
opensuse: ->(query) { "https://software.opensuse.org/search?q=#{query}" },
|
||||||
|
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
|
||||||
|
|
||||||
|
conflicts(*package_manager_switches)
|
||||||
|
end
|
||||||
|
|
||||||
|
PACKAGE_MANAGERS.each do |name, url|
|
||||||
|
exec_browser url.call(URI.encode_www_form_component(args.remaining.join(" "))) if args[:"#{name}?"]
|
||||||
|
end
|
||||||
|
|
||||||
|
if args.remaining.empty?
|
||||||
puts Formatter.columns(Formula.full_names.sort)
|
puts Formatter.columns(Formula.full_names.sort)
|
||||||
elsif ARGV.include? "--macports"
|
elsif args.desc?
|
||||||
exec_browser "https://www.macports.org/ports.php?by=name&substr=#{ARGV.next}"
|
query = args.remaining.first
|
||||||
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)
|
regex = query_regexp(query)
|
||||||
Descriptions.search(regex, :desc).print
|
Descriptions.search(regex, :desc).print
|
||||||
elsif ARGV.first =~ HOMEBREW_TAP_FORMULA_REGEX
|
elsif args.remaining.first =~ HOMEBREW_TAP_FORMULA_REGEX
|
||||||
query = ARGV.first
|
query = args.remaining.first
|
||||||
|
|
||||||
begin
|
begin
|
||||||
result = Formulary.factory(query).name
|
result = Formulary.factory(query).name
|
||||||
@ -52,7 +66,7 @@ module Homebrew
|
|||||||
|
|
||||||
puts Formatter.columns(results.sort) unless results.empty?
|
puts Formatter.columns(results.sort) unless results.empty?
|
||||||
else
|
else
|
||||||
query = ARGV.first
|
query = args.remaining.first
|
||||||
regex = query_regexp(query)
|
regex = query_regexp(query)
|
||||||
local_results = search_formulae(regex)
|
local_results = search_formulae(regex)
|
||||||
puts Formatter.columns(local_results.sort) unless local_results.empty?
|
puts Formatter.columns(local_results.sort) unless local_results.empty?
|
||||||
@ -78,10 +92,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
|
||||||
|
@ -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