brew/Library/Homebrew/description_cache_store.rb
Issy Long 45978435e7
rubocop: Use Sorbet/StrictSigil as it's better than comments
- Previously I thought that comments were fine to discourage people from
  wasting their time trying to bump things that used `undef` that Sorbet
  didn't support. But RuboCop is better at this since it'll complain if
  the comments are unnecessary.

- Suggested in https://github.com/Homebrew/brew/pull/18018#issuecomment-2283369501.

- I've gone for a mixture of `rubocop:disable` for the files that can't
  be `typed: strict` (use of undef, required before everything else, etc)
  and `rubocop:todo` for everything else that should be tried to make
  strictly typed. There's no functional difference between the two as
  `rubocop:todo` is `rubocop:disable` with a different name.

- And I entirely disabled the cop for the docs/ directory since
  `typed: strict` isn't going to gain us anything for some Markdown
  linting config files.

- This means that now it's easier to track what needs to be done rather
  than relying on checklists of files in our big Sorbet issue:

```shell
$ git grep 'typed: true # rubocop:todo Sorbet/StrictSigil' | wc -l
    268
```

- And this is confirmed working for new files:

```shell
$ git status
On branch use-rubocop-for-sorbet-strict-sigils
Untracked files:
  (use "git add <file>..." to include in what will be committed)
        Library/Homebrew/bad.rb
        Library/Homebrew/good.rb

nothing added to commit but untracked files present (use "git add" to track)

$ brew style
Offenses:

bad.rb:1:1: C: Sorbet/StrictSigil: Sorbet sigil should be at least strict got true.
^^^^^^^^^^^^^

1340 files inspected, 1 offense detected
```
2024-08-12 15:24:27 +01:00

150 lines
4.5 KiB
Ruby

# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true
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:).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)
unless Homebrew::EnvConfig.eval_all?
database.clear!
return
end
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)
unless Homebrew::EnvConfig.eval_all?
database.clear!
return
end
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(eval_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)
unless Homebrew::EnvConfig.eval_all?
database.clear!
return
end
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)
unless Homebrew::EnvConfig.eval_all?
database.clear!
return
end
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