| 
									
										
										
										
											2024-03-29 18:15:31 -07:00
										 |  |  | # typed: strict | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 18:15:31 -07:00
										 |  |  | require "abstract_command" | 
					
						
							| 
									
										
										
										
											2015-05-05 15:29:01 -07:00
										 |  |  | require "descriptions" | 
					
						
							| 
									
										
										
										
											2018-06-05 10:55:00 +02:00
										 |  |  | require "search" | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  | require "description_cache_store" | 
					
						
							| 
									
										
										
										
											2015-05-05 15:29:01 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | module Homebrew | 
					
						
							| 
									
										
										
										
											2024-03-29 18:15:31 -07:00
										 |  |  |   module Cmd | 
					
						
							|  |  |  |     class Desc < AbstractCommand | 
					
						
							|  |  |  |       cmd_args do | 
					
						
							|  |  |  |         description <<~EOS | 
					
						
							|  |  |  |           Display <formula>'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 <text>. If <text> is flanked by " \ | 
					
						
							|  |  |  |                             "slashes, it is interpreted as a regular expression." | 
					
						
							|  |  |  |         switch "-n", "--name", | 
					
						
							|  |  |  |                description: "Search just names for <text>. If <text> is flanked by slashes, it is " \ | 
					
						
							|  |  |  |                             "interpreted as a regular expression." | 
					
						
							|  |  |  |         switch "-d", "--description", | 
					
						
							|  |  |  |                description: "Search just descriptions for <text>. If <text> 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." | 
					
						
							| 
									
										
										
										
											2016-09-26 01:44:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 18:15:31 -07:00
										 |  |  |         conflicts "--search", "--name", "--description" | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 18:15:31 -07:00
										 |  |  |         named_args [:formula, :cask, :text_or_regex], min: 1
 | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2018-10-25 12:27:12 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 18:15:31 -07:00
										 |  |  |       sig { override.void } | 
					
						
							|  |  |  |       def run | 
					
						
							|  |  |  |         search_type = if args.search? | 
					
						
							|  |  |  |           :either | 
					
						
							|  |  |  |         elsif args.name? | 
					
						
							|  |  |  |           :name | 
					
						
							|  |  |  |         elsif args.description? | 
					
						
							|  |  |  |           :desc | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-17 23:42:09 +01:00
										 |  |  |         if search_type.present? | 
					
						
							| 
									
										
										
										
											2024-06-27 17:28:18 -04:00
										 |  |  |           if !args.eval_all? && !Homebrew::EnvConfig.eval_all? && Homebrew::EnvConfig.no_install_from_api? | 
					
						
							| 
									
										
										
										
											2024-04-17 23:42:09 +01:00
										 |  |  |             raise UsageError, "`brew desc --search` needs `--eval-all` passed or `HOMEBREW_EVAL_ALL` set!" | 
					
						
							| 
									
										
										
										
											2024-03-29 18:15:31 -07:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2024-04-17 23:42:09 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-29 18:15:31 -07:00
										 |  |  |           query = args.named.join(" ") | 
					
						
							|  |  |  |           string_or_regex = Search.query_regexp(query) | 
					
						
							| 
									
										
										
										
											2024-04-17 23:42:09 +01:00
										 |  |  |           return Search.search_descriptions(string_or_regex, args, search_type:) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         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 | 
					
						
							| 
									
										
										
										
											2022-03-23 00:03:11 -04:00
										 |  |  |         end | 
					
						
							| 
									
										
										
										
											2024-04-17 23:42:09 +01:00
										 |  |  |         Descriptions.new(desc).print | 
					
						
							| 
									
										
										
										
											2022-03-23 00:03:11 -04:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-05-05 15:29:01 -07:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |