From 1fc4a93ca1efde86739e69fa910a17a5537b5f5e Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Fri, 29 Mar 2024 18:15:31 -0700 Subject: [PATCH] Port Homebrew::Cmd::Desc --- Library/Homebrew/cmd/desc.rb | 113 +++++++++++++------------ Library/Homebrew/test/cmd/desc_spec.rb | 3 +- 2 files changed, 59 insertions(+), 57 deletions(-) diff --git a/Library/Homebrew/cmd/desc.rb b/Library/Homebrew/cmd/desc.rb index aa43dc61df..7e963e21cb 100644 --- a/Library/Homebrew/cmd/desc.rb +++ b/Library/Homebrew/cmd/desc.rb @@ -1,74 +1,75 @@ -# typed: true +# typed: strict # frozen_string_literal: true +require "abstract_command" require "descriptions" require "search" require "description_cache_store" -require "cli/parser" module Homebrew - module_function + module Cmd + class Desc < AbstractCommand + cmd_args do + description <<~EOS + Display 's name and one-line description. + The cache is created on the first search, making that search slower than subsequent ones. + EOS + switch "-s", "--search", + description: "Search both names and descriptions for . If is flanked by " \ + "slashes, it is interpreted as a regular expression." + switch "-n", "--name", + description: "Search just names for . If is flanked by slashes, it is " \ + "interpreted as a regular expression." + switch "-d", "--description", + description: "Search just descriptions for . If is flanked by slashes, " \ + "it is interpreted as a regular expression." + switch "--eval-all", + description: "Evaluate all available formulae and casks, whether installed or not, to search their " \ + "descriptions. Implied if `HOMEBREW_EVAL_ALL` is set." + switch "--formula", "--formulae", + description: "Treat all named arguments as formulae." + switch "--cask", "--casks", + description: "Treat all named arguments as casks." - sig { returns(CLI::Parser) } - def desc_args - Homebrew::CLI::Parser.new do - description <<~EOS - Display 's name and one-line description. - The cache is created on the first search, making that search slower than subsequent ones. - EOS - switch "-s", "--search", - description: "Search both names and descriptions for . If is flanked by " \ - "slashes, it is interpreted as a regular expression." - switch "-n", "--name", - description: "Search just names for . If is flanked by slashes, it is " \ - "interpreted as a regular expression." - switch "-d", "--description", - description: "Search just descriptions for . If is flanked by slashes, " \ - "it is interpreted as a regular expression." - switch "--eval-all", - description: "Evaluate all available formulae and casks, whether installed or not, to search their " \ - "descriptions. Implied if `HOMEBREW_EVAL_ALL` is set." - switch "--formula", "--formulae", - description: "Treat all named arguments as formulae." - switch "--cask", "--casks", - description: "Treat all named arguments as casks." + conflicts "--search", "--name", "--description" - conflicts "--search", "--name", "--description" + named_args [:formula, :cask, :text_or_regex], min: 1 + end - named_args [:formula, :cask, :text_or_regex], min: 1 - end - end + sig { override.void } + def run + if !args.eval_all? && !Homebrew::EnvConfig.eval_all? + raise UsageError, "`brew desc` needs `--eval-all` passed or `HOMEBREW_EVAL_ALL` set!" + end - def desc - args = desc_args.parse + search_type = if args.search? + :either + elsif args.name? + :name + elsif args.description? + :desc + end - if !args.eval_all? && !Homebrew::EnvConfig.eval_all? - raise UsageError, "`brew desc` needs `--eval-all` passed or `HOMEBREW_EVAL_ALL` set!" - end - - search_type = if args.search? - :either - elsif args.name? - :name - elsif args.description? - :desc - end - - if search_type.blank? - desc = {} - args.named.to_formulae_and_casks.each do |formula_or_cask| - if formula_or_cask.is_a? Formula - desc[formula_or_cask.full_name] = formula_or_cask.desc + if search_type.blank? + desc = {} + args.named.to_formulae_and_casks.each do |formula_or_cask| + case formula_or_cask + when Formula + desc[formula_or_cask.full_name] = formula_or_cask.desc + when Cask::Cask + description = formula_or_cask.desc.presence || Formatter.warning("[no description]") + desc[formula_or_cask.full_name] = "(#{formula_or_cask.name.join(", ")}) #{description}" + else + raise TypeError, "Unsupported formula_or_cask type: #{formula_or_cask.class}" + end + end + Descriptions.new(desc).print else - description = formula_or_cask.desc.presence || Formatter.warning("[no description]") - desc[formula_or_cask.full_name] = "(#{formula_or_cask.name.join(", ")}) #{description}" + query = args.named.join(" ") + string_or_regex = Search.query_regexp(query) + Search.search_descriptions(string_or_regex, args, search_type:) end end - Descriptions.new(desc).print - else - query = args.named.join(" ") - string_or_regex = Search.query_regexp(query) - Search.search_descriptions(string_or_regex, args, search_type:) end end end diff --git a/Library/Homebrew/test/cmd/desc_spec.rb b/Library/Homebrew/test/cmd/desc_spec.rb index 28872ed0c9..0bd9a87840 100644 --- a/Library/Homebrew/test/cmd/desc_spec.rb +++ b/Library/Homebrew/test/cmd/desc_spec.rb @@ -1,8 +1,9 @@ # frozen_string_literal: true +require "cmd/desc" require "cmd/shared_examples/args_parse" -RSpec.describe "brew desc" do +RSpec.describe Homebrew::Cmd::Desc do it_behaves_like "parseable arguments" it "shows a given Formula's description", :integration_test do