| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: false | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											2019-04-17 18:25:08 +09:00
										 |  |  | require "cli/parser" | 
					
						
							| 
									
										
										
										
											2015-05-05 15:29:01 -07: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-05 10:55:00 +02:00
										 |  |  |   extend Search | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-20 12:03:48 +02:00
										 |  |  |   sig { returns(CLI::Parser) } | 
					
						
							| 
									
										
										
										
											2018-10-25 12:27:12 +05:30
										 |  |  |   def desc_args | 
					
						
							|  |  |  |     Homebrew::CLI::Parser.new do | 
					
						
							| 
									
										
										
										
											2021-01-15 15:04:02 -05:00
										 |  |  |       description <<~EOS | 
					
						
							| 
									
										
										
										
											2018-10-25 12:27:12 +05:30
										 |  |  |         Display <formula>'s name and one-line description. | 
					
						
							|  |  |  |         Formula descriptions are cached; the cache is created on the | 
					
						
							|  |  |  |         first search, making that search slower than subsequent ones. | 
					
						
							|  |  |  |       EOS | 
					
						
							| 
									
										
										
										
											2020-03-10 23:18:40 -04:00
										 |  |  |       switch "-s", "--search", | 
					
						
							| 
									
										
										
										
											2019-08-20 00:04:14 -04:00
										 |  |  |              description: "Search both names and descriptions for <text>. If <text> is flanked by "\ | 
					
						
							| 
									
										
										
										
											2019-08-06 13:23:19 -04:00
										 |  |  |                           "slashes, it is interpreted as a regular expression." | 
					
						
							| 
									
										
										
										
											2020-03-10 23:18:40 -04:00
										 |  |  |       switch "-n", "--name", | 
					
						
							| 
									
										
										
										
											2019-08-20 00:04:14 -04:00
										 |  |  |              description: "Search just names for <text>. If <text> is flanked by slashes, it is "\ | 
					
						
							| 
									
										
										
										
											2019-08-06 13:23:19 -04:00
										 |  |  |                           "interpreted as a regular expression." | 
					
						
							| 
									
										
										
										
											2020-03-10 23:18:40 -04:00
										 |  |  |       switch "-d", "--description", | 
					
						
							| 
									
										
										
										
											2019-08-20 00:04:14 -04:00
										 |  |  |              description: "Search just descriptions for <text>. If <text> is flanked by slashes, "\ | 
					
						
							| 
									
										
										
										
											2019-08-06 13:23:19 -04:00
										 |  |  |                           "it is interpreted as a regular expression." | 
					
						
							| 
									
										
										
										
											2022-03-23 00:03:11 -04:00
										 |  |  |       switch "--formula", "--formulae", | 
					
						
							|  |  |  |              description: "Treat all named arguments as formulae." | 
					
						
							|  |  |  |       switch "--cask", "--casks", | 
					
						
							|  |  |  |              description: "Treat all named arguments as casks." | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-10 23:18:40 -04:00
										 |  |  |       conflicts "--search", "--name", "--description" | 
					
						
							| 
									
										
										
										
											2021-01-10 14:26:40 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-23 00:03:11 -04:00
										 |  |  |       named_args [:formula, :cask, :text_or_regex], min: 1
 | 
					
						
							| 
									
										
										
										
											2018-10-25 12:27:12 +05:30
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-05-05 15:29:01 -07:00
										 |  |  |   def desc | 
					
						
							| 
									
										
										
										
											2020-07-30 18:40:10 +02:00
										 |  |  |     args = desc_args.parse | 
					
						
							| 
									
										
										
										
											2018-10-25 12:27:12 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-10 23:18:40 -04:00
										 |  |  |     search_type = if args.search? | 
					
						
							|  |  |  |       :either | 
					
						
							|  |  |  |     elsif args.name? | 
					
						
							|  |  |  |       :name | 
					
						
							|  |  |  |     elsif args.description? | 
					
						
							|  |  |  |       :desc | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2018-09-17 02:45:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-23 00:03:11 -04:00
										 |  |  |     if search_type.blank? | 
					
						
							| 
									
										
										
										
											2015-09-09 15:21:18 +08:00
										 |  |  |       desc = {} | 
					
						
							| 
									
										
										
										
											2022-03-23 00:03:11 -04:00
										 |  |  |       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 | 
					
						
							|  |  |  |         else | 
					
						
							|  |  |  |           description = formula_or_cask.desc.presence || Formatter.warning("[no description]") | 
					
						
							|  |  |  |           desc[formula_or_cask.full_name] = "(#{formula_or_cask.name.join(", ")}) #{description}" | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       Descriptions.new(desc).print | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2020-03-10 23:18:40 -04:00
										 |  |  |       query = args.named.join(" ") | 
					
						
							|  |  |  |       string_or_regex = query_regexp(query) | 
					
						
							| 
									
										
										
										
											2022-03-23 00:03:11 -04:00
										 |  |  |       unless args.cask? | 
					
						
							|  |  |  |         ohai "Formulae" | 
					
						
							|  |  |  |         CacheStoreDatabase.use(:descriptions) do |db| | 
					
						
							|  |  |  |           cache_store = DescriptionCacheStore.new(db) | 
					
						
							|  |  |  |           Descriptions.search(string_or_regex, search_type, cache_store).print | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       unless args.formula? | 
					
						
							|  |  |  |         puts unless args.cask? | 
					
						
							|  |  |  |         ohai "Casks" | 
					
						
							|  |  |  |         CacheStoreDatabase.use(:cask_descriptions) do |db| | 
					
						
							|  |  |  |           cache_store = CaskDescriptionCacheStore.new(db) | 
					
						
							|  |  |  |           Descriptions.search(string_or_regex, search_type, cache_store).print | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2015-05-05 15:29:01 -07:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |