/
description_cache_store.rb
137 lines (117 loc) 路 4.31 KB
/
description_cache_store.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# typed: true
# frozen_string_literal: true
require "set"
require "cache_store"
#
# {DescriptionCacheStore} provides methods to fetch and mutate formula descriptions used
# by the `brew desc` and `brew search` commands.
#
class DescriptionCacheStore < CacheStore
# Inserts a formula description into the cache if it does not exist or
# updates the formula description if it does exist.
#
# @param formula_name [String] the name of the formula to set
# @param description [String] the description from the formula to set
# @return [nil]
def update!(formula_name, description)
database.set(formula_name, description)
end
# Delete the formula description from the {DescriptionCacheStore}.
#
# @param formula_name [String] the name of the formula to delete
# @return [nil]
def delete!(formula_name)
database.delete(formula_name)
end
# If the database is empty `update!` it with all known formulae.
#
# @return [nil]
def populate_if_empty!(eval_all: Homebrew::EnvConfig.eval_all?)
return unless eval_all
return unless database.empty?
Formula.all(eval_all: eval_all).each { |f| update!(f.full_name, f.desc) }
end
# Use an update report to update the {DescriptionCacheStore}.
#
# @param report [Report] an update report generated by cmd/update.rb
# @return [nil]
def update_from_report!(report)
return unless Homebrew::EnvConfig.eval_all?
return populate_if_empty! if database.empty?
return if report.empty?
renamings = report.select_formula_or_cask(:R)
alterations = report.select_formula_or_cask(:A) +
report.select_formula_or_cask(:M) +
renamings.map(&:last)
update_from_formula_names!(alterations)
delete_from_formula_names!(report.select_formula_or_cask(:D) +
renamings.map(&:first))
end
# Use an array of formula names to update the {DescriptionCacheStore}.
#
# @param formula_names [Array] the formulae to update
# @return [nil]
def update_from_formula_names!(formula_names)
return unless Homebrew::EnvConfig.eval_all?
return populate_if_empty! if database.empty?
formula_names.each do |name|
update!(name, Formula[name].desc)
rescue FormulaUnavailableError, *FormulaVersions::IGNORED_EXCEPTIONS
delete!(name)
end
end
# Use an array of formula names to delete them from the {DescriptionCacheStore}.
#
# @param formula_names [Array] the formulae to delete
# @return [nil]
def delete_from_formula_names!(formula_names)
return if database.empty?
formula_names.each(&method(:delete!))
end
alias delete_from_cask_tokens! delete_from_formula_names!
# `select` from the underlying database.
def select(&block)
database.select(&block)
end
end
#
# {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]
def populate_if_empty!(eval_all: Homebrew::EnvConfig.eval_all?)
return unless eval_all
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)
return unless Homebrew::EnvConfig.eval_all?
return populate_if_empty! if database.empty?
return if report.empty?
alterations = report.select_formula_or_cask(:AC) +
report.select_formula_or_cask(:MC)
update_from_cask_tokens!(alterations)
delete_from_cask_tokens!(report.select_formula_or_cask(:DC))
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)
return unless Homebrew::EnvConfig.eval_all?
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