| 
									
										
										
										
											2020-10-10 14:16:11 +02:00
										 |  |  | # typed: true | 
					
						
							| 
									
										
										
										
											2019-04-19 15:38:03 +09:00
										 |  |  | # frozen_string_literal: true | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  | require "set" | 
					
						
							|  |  |  | require "cache_store" | 
					
						
							|  |  |  | require "searchable" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2022-03-23 00:03:11 -04:00
										 |  |  | # {DescriptionCacheStore} provides methods to fetch and mutate formula descriptions used | 
					
						
							|  |  |  | # by the `brew desc` and `brew search` commands. | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  | # | 
					
						
							|  |  |  | class DescriptionCacheStore < CacheStore | 
					
						
							|  |  |  |   include Searchable | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Inserts a formula description into the cache if it does not exist or | 
					
						
							| 
									
										
										
										
											2018-10-18 21:42:43 -04:00
										 |  |  |   # updates the formula description if it does exist. | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |   # | 
					
						
							| 
									
										
										
										
											2018-10-18 21:42:43 -04:00
										 |  |  |   # @param formula_name [String] the name of the formula to set | 
					
						
							|  |  |  |   # @param description  [String] the description from the formula to set | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |   # @return [nil] | 
					
						
							|  |  |  |   def update!(formula_name, description) | 
					
						
							|  |  |  |     database.set(formula_name, description) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |   # Delete the formula description from the {DescriptionCacheStore}. | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |   # | 
					
						
							| 
									
										
										
										
											2018-10-18 21:42:43 -04:00
										 |  |  |   # @param formula_name [String] the name of the formula to delete | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |   # @return [nil] | 
					
						
							|  |  |  |   def delete!(formula_name) | 
					
						
							|  |  |  |     database.delete(formula_name) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # If the database is empty `update!` it with all known formulae. | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |   # | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |   # @return [nil] | 
					
						
							| 
									
										
										
										
											2022-12-06 13:56:54 +00:00
										 |  |  |   def populate_if_empty!(eval_all: Homebrew::EnvConfig.eval_all?) | 
					
						
							|  |  |  |     return unless eval_all | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |     return unless database.empty? | 
					
						
							| 
									
										
										
										
											2018-10-15 22:51:43 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-01-03 14:59:10 +00:00
										 |  |  |     Formula.all.each { |f| update!(f.full_name, f.desc) } | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |   # Use an update report to update the {DescriptionCacheStore}. | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |   # | 
					
						
							| 
									
										
										
										
											2018-10-18 21:42:43 -04:00
										 |  |  |   # @param report [Report] an update report generated by cmd/update.rb | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |   # @return [nil] | 
					
						
							|  |  |  |   def update_from_report!(report) | 
					
						
							| 
									
										
										
										
											2022-09-05 13:57:22 +01:00
										 |  |  |     return unless Homebrew::EnvConfig.eval_all? | 
					
						
							| 
									
										
										
										
											2018-10-15 22:51:43 -07:00
										 |  |  |     return populate_if_empty! if database.empty? | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |     return if report.empty? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-03 19:16:15 +01:00
										 |  |  |     renamings   = report.select_formula_or_cask(:R) | 
					
						
							|  |  |  |     alterations = report.select_formula_or_cask(:A) + | 
					
						
							|  |  |  |                   report.select_formula_or_cask(:M) + | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |                   renamings.map(&:last) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     update_from_formula_names!(alterations) | 
					
						
							| 
									
										
										
										
											2022-06-03 19:16:15 +01:00
										 |  |  |     delete_from_formula_names!(report.select_formula_or_cask(:D) + | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |                                renamings.map(&:first)) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |   # Use an array of formula names to update the {DescriptionCacheStore}. | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |   # | 
					
						
							| 
									
										
										
										
											2018-10-18 21:42:43 -04:00
										 |  |  |   # @param formula_names [Array] the formulae to update | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |   # @return [nil] | 
					
						
							|  |  |  |   def update_from_formula_names!(formula_names) | 
					
						
							| 
									
										
										
										
											2022-09-05 13:57:22 +01:00
										 |  |  |     return unless Homebrew::EnvConfig.eval_all? | 
					
						
							| 
									
										
										
										
											2018-10-15 22:51:43 -07:00
										 |  |  |     return populate_if_empty! if database.empty? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |     formula_names.each do |name| | 
					
						
							| 
									
										
										
										
											2019-10-13 10:03:26 +01:00
										 |  |  |       update!(name, Formula[name].desc) | 
					
						
							|  |  |  |     rescue FormulaUnavailableError, *FormulaVersions::IGNORED_EXCEPTIONS | 
					
						
							|  |  |  |       delete!(name) | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-05 17:17:03 -05:00
										 |  |  |   # Use an array of formula names to delete them from the {DescriptionCacheStore}. | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |   # | 
					
						
							| 
									
										
										
										
											2018-10-18 21:42:43 -04:00
										 |  |  |   # @param formula_names [Array] the formulae to delete | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |   # @return [nil] | 
					
						
							|  |  |  |   def delete_from_formula_names!(formula_names) | 
					
						
							| 
									
										
										
										
											2018-10-15 22:51:43 -07:00
										 |  |  |     return if database.empty? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  |     formula_names.each(&method(:delete!)) | 
					
						
							|  |  |  |   end | 
					
						
							| 
									
										
										
										
											2022-03-23 00:03:11 -04:00
										 |  |  |   alias delete_from_cask_tokens! delete_from_formula_names! | 
					
						
							| 
									
										
										
										
											2018-10-13 08:22:51 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   private | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # `select` from the underlying database. | 
					
						
							|  |  |  |   def select(&block) | 
					
						
							|  |  |  |     database.select(&block) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end | 
					
						
							| 
									
										
										
										
											2022-03-23 00:03:11 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | # {CaskDescriptionCacheStore} provides methods to fetch and mutate cask descriptions used | 
					
						
							|  |  |  | # by the `brew desc` and `brew search` commands. | 
					
						
							|  |  |  | # | 
					
						
							|  |  |  | class CaskDescriptionCacheStore < DescriptionCacheStore | 
					
						
							|  |  |  |   # If the database is empty `update!` it with all known casks. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @return [nil] | 
					
						
							| 
									
										
										
										
											2022-12-08 09:57:12 +00:00
										 |  |  |   def populate_if_empty!(eval_all: Homebrew::EnvConfig.eval_all?) | 
					
						
							|  |  |  |     return unless eval_all | 
					
						
							| 
									
										
										
										
											2022-03-23 00:03:11 -04:00
										 |  |  |     return unless database.empty? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     Cask::Cask.all.each { |c| update!(c.full_name, [c.name.join(", "), c.desc.presence]) } | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Use an update report to update the {CaskDescriptionCacheStore}. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @param report [Report] an update report generated by cmd/update.rb | 
					
						
							|  |  |  |   # @return [nil] | 
					
						
							|  |  |  |   def update_from_report!(report) | 
					
						
							| 
									
										
										
										
											2022-09-05 13:57:22 +01:00
										 |  |  |     return unless Homebrew::EnvConfig.eval_all? | 
					
						
							| 
									
										
										
										
											2022-03-23 00:03:11 -04:00
										 |  |  |     return populate_if_empty! if database.empty? | 
					
						
							|  |  |  |     return if report.empty? | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-03 19:16:15 +01:00
										 |  |  |     alterations = report.select_formula_or_cask(:AC) + | 
					
						
							|  |  |  |                   report.select_formula_or_cask(:MC) | 
					
						
							| 
									
										
										
										
											2022-03-23 00:03:11 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  |     update_from_cask_tokens!(alterations) | 
					
						
							| 
									
										
										
										
											2022-06-03 19:16:15 +01:00
										 |  |  |     delete_from_cask_tokens!(report.select_formula_or_cask(:DC)) | 
					
						
							| 
									
										
										
										
											2022-03-23 00:03:11 -04:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   # Use an array of cask tokens to update the {CaskDescriptionCacheStore}. | 
					
						
							|  |  |  |   # | 
					
						
							|  |  |  |   # @param cask_tokens [Array] the casks to update | 
					
						
							|  |  |  |   # @return [nil] | 
					
						
							|  |  |  |   def update_from_cask_tokens!(cask_tokens) | 
					
						
							| 
									
										
										
										
											2022-09-05 13:57:22 +01:00
										 |  |  |     return unless Homebrew::EnvConfig.eval_all? | 
					
						
							| 
									
										
										
										
											2022-03-23 00:03:11 -04:00
										 |  |  |     return populate_if_empty! if database.empty? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     cask_tokens.each do |token| | 
					
						
							|  |  |  |       c = Cask::CaskLoader.load(token) | 
					
						
							|  |  |  |       update!(c.full_name, [c.name.join(", "), c.desc.presence]) | 
					
						
							|  |  |  |     rescue Cask::CaskUnavailableError, *FormulaVersions::IGNORED_EXCEPTIONS | 
					
						
							|  |  |  |       delete!(c.full_name) if c.present? | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |