| 
									
										
										
										
											2024-08-11 20:51:04 +01:00
										 |  |  | # typed: strict | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 10:12:52 -07:00
										 |  |  | require "abstract_command" | 
					
						
							| 
									
										
										
										
											2015-08-03 13:09:07 +01:00
										 |  |  | require "formula" | 
					
						
							| 
									
										
										
										
											2017-03-18 17:02:08 +02:00
										 |  |  | require "missing_formula" | 
					
						
							| 
									
										
										
										
											2015-09-09 13:00:43 +08:00
										 |  |  | require "descriptions" | 
					
						
							| 
									
										
										
										
											2018-06-02 20:49:14 +02:00
										 |  |  | require "search" | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-06-18 22:41:47 -05:00
										 |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2024-04-01 10:12:52 -07:00
										 |  |  |   module Cmd | 
					
						
							|  |  |  |     class SearchCmd < AbstractCommand | 
					
						
							| 
									
										
										
										
											2024-08-11 20:51:04 +01:00
										 |  |  |       PACKAGE_MANAGERS = T.let({ | 
					
						
							| 
									
										
										
										
											2024-04-01 10:12:52 -07:00
										 |  |  |         repology:  ->(query) { "https://repology.org/projects/?search=#{query}" }, | 
					
						
							|  |  |  |         macports:  ->(query) { "https://ports.macports.org/search/?q=#{query}" }, | 
					
						
							|  |  |  |         fink:      ->(query) { "https://pdb.finkproject.org/pdb/browse.php?summary=#{query}" }, | 
					
						
							|  |  |  |         opensuse:  ->(query) { "https://software.opensuse.org/search?q=#{query}" }, | 
					
						
							|  |  |  |         fedora:    ->(query) { "https://packages.fedoraproject.org/search?query=#{query}" }, | 
					
						
							|  |  |  |         archlinux: ->(query) { "https://archlinux.org/packages/?q=#{query}" }, | 
					
						
							|  |  |  |         debian:    lambda { |query| | 
					
						
							|  |  |  |           "https://packages.debian.org/search?keywords=#{query}&searchon=names&suite=all§ion=all" | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         ubuntu:    lambda { |query| | 
					
						
							|  |  |  |           "https://packages.ubuntu.com/search?keywords=#{query}&searchon=names&suite=all§ion=all" | 
					
						
							|  |  |  |         }, | 
					
						
							| 
									
										
										
										
											2024-08-11 20:51:04 +01:00
										 |  |  |       }.freeze, T::Hash[Symbol, T.proc.params(query: String).returns(String)]) | 
					
						
							| 
									
										
										
										
											2024-04-01 10:12:52 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |       cmd_args do | 
					
						
							|  |  |  |         description <<~EOS | 
					
						
							|  |  |  |           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. | 
					
						
							|  |  |  |         EOS | 
					
						
							|  |  |  |         switch "--formula", "--formulae", | 
					
						
							|  |  |  |                description: "Search for formulae." | 
					
						
							|  |  |  |         switch "--cask", "--casks", | 
					
						
							|  |  |  |                description: "Search for casks." | 
					
						
							|  |  |  |         switch "--desc", | 
					
						
							|  |  |  |                description: "Search for formulae with a description matching <text> and casks with " \ | 
					
						
							|  |  |  |                             "a name or description matching <text>." | 
					
						
							|  |  |  |         switch "--eval-all", | 
					
						
							|  |  |  |                depends_on:  "--desc", | 
					
						
							|  |  |  |                description: "Evaluate all available formulae and casks, whether installed or not, to search their " \ | 
					
						
							| 
									
										
										
										
											2025-01-27 14:21:27 +00:00
										 |  |  |                             "descriptions. Implied if `$HOMEBREW_EVAL_ALL` is set." | 
					
						
							| 
									
										
										
										
											2024-04-01 10:12:52 -07:00
										 |  |  |         switch "--pull-request", | 
					
						
							|  |  |  |                description: "Search for GitHub pull requests containing <text>." | 
					
						
							|  |  |  |         switch "--open", | 
					
						
							|  |  |  |                depends_on:  "--pull-request", | 
					
						
							|  |  |  |                description: "Search for only open GitHub pull requests." | 
					
						
							|  |  |  |         switch "--closed", | 
					
						
							|  |  |  |                depends_on:  "--pull-request", | 
					
						
							|  |  |  |                description: "Search for only closed GitHub pull requests." | 
					
						
							|  |  |  |         package_manager_switches = PACKAGE_MANAGERS.keys.map { |name| "--#{name}" } | 
					
						
							|  |  |  |         package_manager_switches.each do |s| | 
					
						
							|  |  |  |           switch s, | 
					
						
							|  |  |  |                  description: "Search for <text> in the given database." | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         conflicts "--desc", "--pull-request" | 
					
						
							|  |  |  |         conflicts "--open", "--closed" | 
					
						
							|  |  |  |         conflicts(*package_manager_switches) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         named_args :text_or_regex, min: 1
 | 
					
						
							| 
									
										
										
										
											2018-06-01 14:19:00 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 10:12:52 -07:00
										 |  |  |       sig { override.void } | 
					
						
							|  |  |  |       def run | 
					
						
							|  |  |  |         return if search_package_manager | 
					
						
							| 
									
										
										
										
											2021-01-10 14:26:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 10:12:52 -07:00
										 |  |  |         query = args.named.join(" ") | 
					
						
							|  |  |  |         string_or_regex = Search.query_regexp(query) | 
					
						
							| 
									
										
										
										
											2019-01-23 10:14:31 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 10:12:52 -07:00
										 |  |  |         if args.desc? | 
					
						
							| 
									
										
										
										
											2024-06-27 17:28:18 -04:00
										 |  |  |           if !args.eval_all? && !Homebrew::EnvConfig.eval_all? && Homebrew::EnvConfig.no_install_from_api? | 
					
						
							| 
									
										
										
										
											2025-01-27 14:21:27 +00:00
										 |  |  |             raise UsageError, "`brew search --desc` needs `--eval-all` passed or `$HOMEBREW_EVAL_ALL` set!" | 
					
						
							| 
									
										
										
										
											2024-04-01 10:12:52 -07:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2018-06-01 14:19:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 10:12:52 -07:00
										 |  |  |           Search.search_descriptions(string_or_regex, args) | 
					
						
							|  |  |  |         elsif args.pull_request? | 
					
						
							|  |  |  |           search_pull_requests(query) | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           formulae, casks = Search.search_names(string_or_regex, args) | 
					
						
							|  |  |  |           print_results(formulae, casks, query) | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2018-06-01 14:19:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 10:12:52 -07:00
										 |  |  |         puts "Use `brew desc` to list packages with a short description." if args.verbose? | 
					
						
							| 
									
										
										
										
											2018-06-18 16:09:13 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 10:12:52 -07:00
										 |  |  |         print_regex_help | 
					
						
							| 
									
										
										
										
											2022-09-05 13:57:22 +01:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2023-07-06 16:47:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 10:12:52 -07:00
										 |  |  |       private | 
					
						
							| 
									
										
										
										
											2022-08-09 09:50:44 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-11 20:51:04 +01:00
										 |  |  |       sig { void } | 
					
						
							| 
									
										
										
										
											2024-04-01 10:12:52 -07:00
										 |  |  |       def print_regex_help | 
					
						
							|  |  |  |         return unless $stdout.tty? | 
					
						
							| 
									
										
										
										
											2021-11-05 11:22:29 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 10:12:52 -07:00
										 |  |  |         metacharacters = %w[\\ | ( ) [ ] { } ^ $ * + ?].freeze | 
					
						
							|  |  |  |         return unless metacharacters.any? do |char| | 
					
						
							|  |  |  |           args.named.any? do |arg| | 
					
						
							|  |  |  |             arg.include?(char) && !arg.start_with?("/") | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 10:12:52 -07:00
										 |  |  |         opoo <<~EOS | 
					
						
							|  |  |  |           Did you mean to perform a regular expression search? | 
					
						
							|  |  |  |           Surround your query with /slashes/ to search locally by regex. | 
					
						
							|  |  |  |         EOS | 
					
						
							| 
									
										
										
										
											2015-04-17 12:07:45 -07:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-11 20:51:04 +01:00
										 |  |  |       sig { returns(T::Boolean) } | 
					
						
							| 
									
										
										
										
											2024-04-01 10:12:52 -07:00
										 |  |  |       def search_package_manager | 
					
						
							| 
									
										
										
										
											2024-11-30 13:52:46 -08:00
										 |  |  |         package_manager = PACKAGE_MANAGERS.find { |name,| args.public_send(:"#{name}?") } | 
					
						
							| 
									
										
										
										
											2024-04-01 10:12:52 -07:00
										 |  |  |         return false if package_manager.nil? | 
					
						
							| 
									
										
										
										
											2021-11-05 11:22:29 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 10:12:52 -07:00
										 |  |  |         _, url = package_manager | 
					
						
							|  |  |  |         exec_browser url.call(URI.encode_www_form_component(args.named.join(" "))) | 
					
						
							|  |  |  |         true | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2022-12-12 19:51:18 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-11 20:51:04 +01:00
										 |  |  |       sig { params(query: String).returns(String) } | 
					
						
							| 
									
										
										
										
											2024-04-01 10:12:52 -07:00
										 |  |  |       def search_pull_requests(query) | 
					
						
							|  |  |  |         only = if args.open? && !args.closed? | 
					
						
							|  |  |  |           "open" | 
					
						
							|  |  |  |         elsif args.closed? && !args.open? | 
					
						
							|  |  |  |           "closed" | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2022-12-12 19:51:18 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 10:12:52 -07:00
										 |  |  |         GitHub.print_pull_requests_matching(query, only) | 
					
						
							| 
									
										
										
										
											2022-12-12 19:51:18 -08:00
										 |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-11 20:51:04 +01:00
										 |  |  |       sig { params(all_formulae: T::Array[String], all_casks: T::Array[String], query: String).void } | 
					
						
							| 
									
										
										
										
											2024-04-01 10:12:52 -07:00
										 |  |  |       def print_results(all_formulae, all_casks, query) | 
					
						
							|  |  |  |         count = all_formulae.size + all_casks.size | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if all_formulae.any? | 
					
						
							|  |  |  |           if $stdout.tty? | 
					
						
							|  |  |  |             ohai "Formulae", Formatter.columns(all_formulae) | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             puts all_formulae | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         puts if all_formulae.any? && all_casks.any? | 
					
						
							|  |  |  |         if all_casks.any? | 
					
						
							|  |  |  |           if $stdout.tty? | 
					
						
							|  |  |  |             ohai "Casks", Formatter.columns(all_casks) | 
					
						
							|  |  |  |           else | 
					
						
							|  |  |  |             puts all_casks | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         print_missing_formula_help(query, count.positive?) if all_casks.exclude?(query) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         odie "No formulae or casks found for #{query.inspect}." if count.zero? | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2022-12-12 19:51:18 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-08-11 20:51:04 +01:00
										 |  |  |       sig { params(query: String, found_matches: T::Boolean).void } | 
					
						
							| 
									
										
										
										
											2024-04-01 10:12:52 -07:00
										 |  |  |       def print_missing_formula_help(query, found_matches) | 
					
						
							|  |  |  |         return unless $stdout.tty? | 
					
						
							| 
									
										
										
										
											2022-12-12 19:51:18 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 10:12:52 -07:00
										 |  |  |         reason = MissingFormula.reason(query, silent: true) | 
					
						
							|  |  |  |         return if reason.nil? | 
					
						
							| 
									
										
										
										
											2022-12-12 19:51:18 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 10:12:52 -07:00
										 |  |  |         if found_matches | 
					
						
							|  |  |  |           puts | 
					
						
							|  |  |  |           puts "If you meant #{query.inspect} specifically:" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         puts reason | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2022-12-12 19:51:18 -08:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  | end |