brew/Library/Homebrew/linkage_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

78 lines
1.8 KiB
Ruby

# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true
require "cache_store"
#
# {LinkageCacheStore} provides methods to fetch and mutate linkage-specific data used
# by the `brew linkage` command.
#
class LinkageCacheStore < CacheStore
# @param keg_path [String]
# @param database [CacheStoreDatabase]
# @return [nil]
def initialize(keg_path, database)
@keg_path = keg_path
super(database)
end
# Returns `true` if the database has any value for the current `keg_path`.
#
# @return [Boolean]
def keg_exists?
!database.get(@keg_path).nil?
end
# Inserts dylib-related information into the cache if it does not exist or
# updates data into the linkage cache if it does exist.
#
# @param hash_values [Hash] hash containing KVPs of { :type => Hash }
# @return [nil]
def update!(hash_values)
hash_values.each_key do |type|
next if HASH_LINKAGE_TYPES.include?(type)
raise TypeError, <<~EOS
Can't update types that are not defined for the linkage store
EOS
end
database.set @keg_path, hash_values
end
# @param type [Symbol] the type to fetch from the {LinkageCacheStore}
# @raise [TypeError] error if the type is not in `HASH_LINKAGE_TYPES`
# @return [Hash]
def fetch(type)
unless HASH_LINKAGE_TYPES.include?(type)
raise TypeError, <<~EOS
Can't fetch types that are not defined for the linkage store
EOS
end
return {} unless keg_exists?
fetch_hash_values(type)
end
# Delete the keg from the {LinkageCacheStore}
#
# @return [nil]
def delete!
database.delete(@keg_path)
end
private
HASH_LINKAGE_TYPES = [:keg_files_dylibs].freeze
# @param type [Symbol]
# @return [Hash]
def fetch_hash_values(type)
keg_cache = database.get(@keg_path)
return {} unless keg_cache
keg_cache[type.to_s]
end
end