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

218 lines
6.8 KiB
Ruby

# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true
require "json"
require "lazy_object"
require "locale"
require "extend/hash/keys"
module Cask
# Configuration for installing casks.
#
# @api internal
class Config
DEFAULT_DIRS = {
appdir: "/Applications",
keyboard_layoutdir: "/Library/Keyboard Layouts",
colorpickerdir: "~/Library/ColorPickers",
prefpanedir: "~/Library/PreferencePanes",
qlplugindir: "~/Library/QuickLook",
mdimporterdir: "~/Library/Spotlight",
dictionarydir: "~/Library/Dictionaries",
fontdir: "~/Library/Fonts",
servicedir: "~/Library/Services",
input_methoddir: "~/Library/Input Methods",
internet_plugindir: "~/Library/Internet Plug-Ins",
audio_unit_plugindir: "~/Library/Audio/Plug-Ins/Components",
vst_plugindir: "~/Library/Audio/Plug-Ins/VST",
vst3_plugindir: "~/Library/Audio/Plug-Ins/VST3",
screen_saverdir: "~/Library/Screen Savers",
}.freeze
def self.defaults
{
languages: LazyObject.new { MacOS.languages },
}.merge(DEFAULT_DIRS).freeze
end
sig { params(args: Homebrew::CLI::Args).returns(T.attached_class) }
def self.from_args(args)
args = T.unsafe(args)
new(explicit: {
appdir: args.appdir,
keyboard_layoutdir: args.keyboard_layoutdir,
colorpickerdir: args.colorpickerdir,
prefpanedir: args.prefpanedir,
qlplugindir: args.qlplugindir,
mdimporterdir: args.mdimporterdir,
dictionarydir: args.dictionarydir,
fontdir: args.fontdir,
servicedir: args.servicedir,
input_methoddir: args.input_methoddir,
internet_plugindir: args.internet_plugindir,
audio_unit_plugindir: args.audio_unit_plugindir,
vst_plugindir: args.vst_plugindir,
vst3_plugindir: args.vst3_plugindir,
screen_saverdir: args.screen_saverdir,
languages: args.language,
}.compact)
end
sig { params(json: String, ignore_invalid_keys: T::Boolean).returns(T.attached_class) }
def self.from_json(json, ignore_invalid_keys: false)
config = JSON.parse(json)
new(
default: config.fetch("default", {}),
env: config.fetch("env", {}),
explicit: config.fetch("explicit", {}),
ignore_invalid_keys:,
)
end
sig {
params(config: T::Enumerable[[T.any(String, Symbol), T.any(String, Pathname, T::Array[String])]])
.returns(T::Hash[Symbol, T.any(String, Pathname, T::Array[String])])
}
def self.canonicalize(config)
config.to_h do |k, v|
key = k.to_sym
if DEFAULT_DIRS.key?(key)
raise TypeError, "Invalid path for default dir #{k}: #{v.inspect}" if v.is_a?(Array)
[key, Pathname(v).expand_path]
else
[key, v]
end
end
end
# Get the explicit configuration.
#
# @api internal
sig { returns(T::Hash[Symbol, T.any(String, Pathname, T::Array[String])]) }
attr_accessor :explicit
sig {
params(
default: T.nilable(T::Hash[Symbol, T.any(String, Pathname, T::Array[String])]),
env: T.nilable(T::Hash[Symbol, T.any(String, Pathname, T::Array[String])]),
explicit: T::Hash[Symbol, T.any(String, Pathname, T::Array[String])],
ignore_invalid_keys: T::Boolean,
).void
}
def initialize(default: nil, env: nil, explicit: {}, ignore_invalid_keys: false)
@default = self.class.canonicalize(self.class.defaults.merge(default)) if default
@env = self.class.canonicalize(env) if env
@explicit = self.class.canonicalize(explicit)
if ignore_invalid_keys
@env&.delete_if { |key, _| self.class.defaults.keys.exclude?(key) }
@explicit.delete_if { |key, _| self.class.defaults.keys.exclude?(key) }
return
end
@env&.assert_valid_keys(*self.class.defaults.keys)
@explicit.assert_valid_keys(*self.class.defaults.keys)
end
sig { returns(T::Hash[Symbol, T.any(String, Pathname, T::Array[String])]) }
def default
@default ||= self.class.canonicalize(self.class.defaults)
end
sig { returns(T::Hash[Symbol, T.any(String, Pathname, T::Array[String])]) }
def env
@env ||= self.class.canonicalize(
Homebrew::EnvConfig.cask_opts
.select { |arg| arg.include?("=") }
.map { |arg| T.cast(arg.split("=", 2), [String, String]) }
.map do |(flag, value)|
key = flag.sub(/^--/, "")
# converts --language flag to :languages config key
if key == "language"
key = "languages"
value = value.split(",")
end
[key, value]
end,
)
end
sig { returns(Pathname) }
def binarydir
@binarydir ||= HOMEBREW_PREFIX/"bin"
end
sig { returns(Pathname) }
def manpagedir
@manpagedir ||= HOMEBREW_PREFIX/"share/man"
end
sig { returns(T::Array[String]) }
def languages
[
*explicit.fetch(:languages, []),
*env.fetch(:languages, []),
*default.fetch(:languages, []),
].uniq.select do |lang|
# Ensure all languages are valid.
Locale.parse(lang)
true
rescue Locale::ParserError
false
end
end
def languages=(languages)
explicit[:languages] = languages
end
DEFAULT_DIRS.each_key do |dir|
define_method(dir) do
T.bind(self, Config)
explicit.fetch(dir, env.fetch(dir, default.fetch(dir)))
end
define_method(:"#{dir}=") do |path|
T.bind(self, Config)
explicit[dir] = Pathname(path).expand_path
end
end
sig { params(other: Config).returns(T.self_type) }
def merge(other)
self.class.new(explicit: other.explicit.merge(explicit))
end
# Get explicit configuration as a string.
#
# @api internal
#
# TODO: This is only used by `homebrew/bundle`, so move it there.
sig { returns(String) }
def explicit_s
explicit.map do |key, value|
# inverse of #env - converts :languages config key back to --language flag
if key == :languages
key = "language"
value = T.cast(explicit.fetch(:languages, []), T::Array[String]).join(",")
end
"#{key}: \"#{value.to_s.sub(/^#{Dir.home}/, "~")}\""
end.join(", ")
end
sig { params(options: T.untyped).returns(String) }
def to_json(*options)
{
default:,
env:,
explicit:,
}.to_json(*options)
end
end
end