| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: false | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											2019-04-17 18:25:08 +09:00
										 |  |  | require "cli/parser" | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |   extend T::Sig | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-26 01:44:51 +02:00
										 |  |  |   module_function | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-02 20:49:14 +02:00
										 |  |  |   extend Search | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-01 14:19:00 +02:00
										 |  |  |   PACKAGE_MANAGERS = { | 
					
						
							| 
									
										
										
										
											2021-10-19 04:27:07 +08: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://apps.fedoraproject.org/packages/s/#{query}" }, | 
					
						
							|  |  |  |     archlinux: ->(query) { "https://archlinux.org/packages/?q=#{query}" }, | 
					
						
							|  |  |  |     debian:    lambda { |query| | 
					
						
							| 
									
										
										
										
											2018-09-02 16:15:09 +01:00
										 |  |  |       "https://packages.debian.org/search?keywords=#{query}&searchon=names&suite=all§ion=all" | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2021-10-19 04:27:07 +08:00
										 |  |  |     ubuntu:    lambda { |query| | 
					
						
							| 
									
										
										
										
											2018-09-02 16:15:09 +01:00
										 |  |  |       "https://packages.ubuntu.com/search?keywords=#{query}&searchon=names&suite=all§ion=all" | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2018-06-01 14:19:00 +02:00
										 |  |  |   }.freeze | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |   sig { returns(CLI::Parser) } | 
					
						
							| 
									
										
										
										
											2019-01-23 10:14:31 +05:30
										 |  |  |   def search_args | 
					
						
							|  |  |  |     Homebrew::CLI::Parser.new do | 
					
						
							| 
									
										
										
										
											2021-01-15 15:04:02 -05:00
										 |  |  |       description <<~EOS | 
					
						
							| 
									
										
										
										
											2019-03-09 13:00:15 -05:00
										 |  |  |         Perform a substring search of cask tokens and formula names for <text>. If <text> | 
					
						
							| 
									
										
										
										
											2019-08-20 00:04:14 -04:00
										 |  |  |         is flanked by slashes, it is interpreted as a regular expression. | 
					
						
							| 
									
										
										
										
											2019-03-09 13:00:15 -05:00
										 |  |  |         The search for <text> is extended online to `homebrew/core` and `homebrew/cask`. | 
					
						
							| 
									
										
										
										
											2019-01-23 10:14:31 +05:30
										 |  |  |       EOS | 
					
						
							| 
									
										
										
										
											2020-08-07 09:53:30 +01:00
										 |  |  |       switch "--formula", "--formulae", | 
					
						
							| 
									
										
										
										
											2021-04-09 09:30:36 +01:00
										 |  |  |              description: "Search online and locally for formulae." | 
					
						
							| 
									
										
										
										
											2020-08-07 09:53:30 +01:00
										 |  |  |       switch "--cask", "--casks", | 
					
						
							| 
									
										
										
										
											2021-04-09 09:30:36 +01:00
										 |  |  |              description: "Search online and locally for casks." | 
					
						
							| 
									
										
										
										
											2019-01-23 10:14:31 +05:30
										 |  |  |       switch "--desc", | 
					
						
							| 
									
										
										
										
											2022-06-28 10:09:59 +01:00
										 |  |  |              description: "Search for formulae with a description matching <text> and casks with " \ | 
					
						
							| 
									
										
										
										
											2022-03-23 00:03:11 -04:00
										 |  |  |                           "a name or description matching <text>." | 
					
						
							| 
									
										
										
										
											2020-10-14 14:34:45 -04:00
										 |  |  |       switch "--pull-request", | 
					
						
							| 
									
										
										
										
											2020-11-12 10:40:48 -05:00
										 |  |  |              description: "Search for GitHub pull requests containing <text>." | 
					
						
							| 
									
										
										
										
											2020-12-23 23:06:02 +09:00
										 |  |  |       switch "--open", | 
					
						
							|  |  |  |              depends_on:  "--pull-request", | 
					
						
							| 
									
										
										
										
											2020-11-27 11:41:08 -05:00
										 |  |  |              description: "Search for only open GitHub pull requests." | 
					
						
							| 
									
										
										
										
											2020-12-23 23:06:02 +09:00
										 |  |  |       switch "--closed", | 
					
						
							|  |  |  |              depends_on:  "--pull-request", | 
					
						
							| 
									
										
										
										
											2020-11-27 11:41:08 -05:00
										 |  |  |              description: "Search for only closed GitHub pull requests." | 
					
						
							| 
									
										
										
										
											2019-01-23 10:14:31 +05:30
										 |  |  |       package_manager_switches = PACKAGE_MANAGERS.keys.map { |name| "--#{name}" } | 
					
						
							| 
									
										
										
										
											2018-06-01 14:19:00 +02:00
										 |  |  |       package_manager_switches.each do |s| | 
					
						
							| 
									
										
										
										
											2019-01-23 10:14:31 +05:30
										 |  |  |         switch s, | 
					
						
							| 
									
										
										
										
											2021-07-20 09:32:37 +08:00
										 |  |  |                description: "Search for <text> in the given database." | 
					
						
							| 
									
										
										
										
											2018-06-01 14:19:00 +02:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-27 11:41:08 -05:00
										 |  |  |       conflicts "--desc", "--pull-request" | 
					
						
							|  |  |  |       conflicts "--open", "--closed" | 
					
						
							| 
									
										
										
										
											2018-06-01 14:19:00 +02:00
										 |  |  |       conflicts(*package_manager_switches) | 
					
						
							| 
									
										
										
										
											2021-01-10 14:26:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-17 11:34:31 +01:00
										 |  |  |       named_args :text_or_regex, min: 1
 | 
					
						
							| 
									
										
										
										
											2018-06-01 14:19:00 +02:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2019-01-23 10:14:31 +05:30
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def search | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  |     args = search_args.parse | 
					
						
							| 
									
										
										
										
											2018-06-01 14:19:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 11:22:29 -07:00
										 |  |  |     return if search_package_manager(args) | 
					
						
							| 
									
										
										
										
											2018-06-01 14:19:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-04 17:28:15 +00:00
										 |  |  |     query = args.named.join(" ") | 
					
						
							| 
									
										
										
										
											2018-06-18 16:09:13 +02:00
										 |  |  |     string_or_regex = query_regexp(query) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if args.desc? | 
					
						
							| 
									
										
										
										
											2021-07-25 20:10:35 +09:00
										 |  |  |       search_descriptions(string_or_regex, args) | 
					
						
							| 
									
										
										
										
											2020-10-14 14:34:45 -04:00
										 |  |  |     elsif args.pull_request? | 
					
						
							| 
									
										
										
										
											2021-11-05 11:22:29 -07:00
										 |  |  |       search_pull_requests(query, args) | 
					
						
							| 
									
										
										
										
											2018-06-18 16:09:13 +02:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2021-11-05 11:22:29 -07:00
										 |  |  |       search_names(query, string_or_regex, args) | 
					
						
							| 
									
										
										
										
											2010-11-14 03:52:59 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2015-10-15 17:10:25 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-09 11:59:13 +03:00
										 |  |  |     puts "Use `brew desc` to list packages with a short description." if args.verbose? | 
					
						
							| 
									
										
										
										
											2022-08-09 09:50:44 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 11:22:29 -07:00
										 |  |  |     print_regex_help(args) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def print_regex_help(args) | 
					
						
							| 
									
										
										
										
											2017-04-24 14:11:04 +01:00
										 |  |  |     return unless $stdout.tty? | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-24 14:11:04 +01:00
										 |  |  |     metacharacters = %w[\\ | ( ) [ ] { } ^ $ * + ?].freeze | 
					
						
							|  |  |  |     return unless metacharacters.any? do |char| | 
					
						
							| 
									
										
										
										
											2020-03-04 17:28:15 +00:00
										 |  |  |       args.named.any? do |arg| | 
					
						
							| 
									
										
										
										
											2017-04-24 14:11:04 +01:00
										 |  |  |         arg.include?(char) && !arg.start_with?("/") | 
					
						
							| 
									
										
										
										
											2015-04-17 12:07:45 -07:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-12 11:57:37 +00:00
										 |  |  |     opoo <<~EOS | 
					
						
							| 
									
										
										
										
											2017-04-24 14:11:04 +01:00
										 |  |  |       Did you mean to perform a regular expression search? | 
					
						
							|  |  |  |       Surround your query with /slashes/ to search locally by regex. | 
					
						
							|  |  |  |     EOS | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  |   end | 
					
						
							| 
									
										
										
										
											2021-11-05 11:22:29 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def search_package_manager(args) | 
					
						
							|  |  |  |     package_manager = PACKAGE_MANAGERS.find { |name,| args[:"#{name}?"] } | 
					
						
							|  |  |  |     return false if package_manager.nil? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     _, url = package_manager | 
					
						
							|  |  |  |     exec_browser url.call(URI.encode_www_form_component(args.named.join(" "))) | 
					
						
							|  |  |  |     true | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def search_pull_requests(query, args) | 
					
						
							|  |  |  |     only = if args.open? && !args.closed? | 
					
						
							|  |  |  |       "open" | 
					
						
							|  |  |  |     elsif args.closed? && !args.open? | 
					
						
							|  |  |  |       "closed" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     GitHub.print_pull_requests_matching(query, only) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def search_names(query, string_or_regex, args) | 
					
						
							|  |  |  |     remote_results = search_taps(query, silent: true) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     local_formulae = search_formulae(string_or_regex) | 
					
						
							|  |  |  |     remote_formulae = remote_results[:formulae] | 
					
						
							|  |  |  |     all_formulae = local_formulae + remote_formulae | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     local_casks = search_casks(string_or_regex) | 
					
						
							|  |  |  |     remote_casks = remote_results[:casks] | 
					
						
							|  |  |  |     all_casks = local_casks + remote_casks | 
					
						
							| 
									
										
										
										
											2021-11-05 11:26:16 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 11:22:29 -07:00
										 |  |  |     print_formulae = args.formula? | 
					
						
							|  |  |  |     print_casks = args.cask? | 
					
						
							|  |  |  |     print_formulae = print_casks = true if !print_formulae && !print_casks | 
					
						
							| 
									
										
										
										
											2021-11-05 11:26:16 -07:00
										 |  |  |     print_formulae &&= all_formulae.any? | 
					
						
							|  |  |  |     print_casks &&= all_casks.any? | 
					
						
							| 
									
										
										
										
											2021-11-05 11:22:29 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 11:26:16 -07:00
										 |  |  |     ohai "Formulae", Formatter.columns(all_formulae) if print_formulae | 
					
						
							|  |  |  |     puts if print_formulae && print_casks | 
					
						
							|  |  |  |     ohai "Casks", Formatter.columns(all_casks) if print_casks | 
					
						
							| 
									
										
										
										
											2021-11-05 11:22:29 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     count = all_formulae.count + all_casks.count | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-05 11:34:01 -07:00
										 |  |  |     print_missing_formula_help(query, count.positive?) if local_casks.exclude?(query) | 
					
						
							| 
									
										
										
										
											2021-11-05 11:22:29 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |     odie "No formulae or casks found for #{query.inspect}." if count.zero? | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2021-11-05 11:34:01 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   def print_missing_formula_help(query, found_matches) | 
					
						
							|  |  |  |     return unless $stdout.tty? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     reason = MissingFormula.reason(query, silent: true) | 
					
						
							|  |  |  |     return if reason.nil? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if found_matches | 
					
						
							|  |  |  |       puts | 
					
						
							|  |  |  |       puts "If you meant #{query.inspect} specifically:" | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     puts reason | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2010-09-11 20:22:54 +01:00
										 |  |  | end |