201 lines
6.2 KiB
Ruby
Raw Normal View History

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 10:30:59 +01:00
# typed: true # rubocop:todo Sorbet/StrictSigil
# frozen_string_literal: true
2019-02-02 17:11:37 +01:00
require "json"
2017-12-03 09:06:23 +01:00
require "lazy_object"
require "locale"
2024-01-12 09:38:49 -08:00
require "extend/hash/keys"
2019-02-02 17:11:37 +01:00
module Cask
2020-08-24 22:51:23 +02:00
# Configuration for installing casks.
#
2024-04-22 21:05:48 +02:00
# @api internal
2019-02-03 02:40:27 +01:00
class Config
2019-02-02 17:11:37 +01:00
DEFAULT_DIRS = {
2019-02-05 16:08:29 +01:00
appdir: "/Applications",
2023-03-26 08:10:40 +02:00
keyboard_layoutdir: "/Library/Keyboard Layouts",
2020-08-01 02:30:46 +02:00
colorpickerdir: "~/Library/ColorPickers",
2019-02-05 16:08:29 +01:00
prefpanedir: "~/Library/PreferencePanes",
qlplugindir: "~/Library/QuickLook",
2020-04-05 15:30:37 +02:00
mdimporterdir: "~/Library/Spotlight",
2019-02-05 16:08:29 +01:00
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",
2019-02-02 17:11:37 +01:00
}.freeze
2017-12-03 09:06:23 +01:00
def self.defaults
{
languages: LazyObject.new { MacOS.languages },
}.merge(DEFAULT_DIRS).freeze
end
sig { params(args: Homebrew::CLI::Args).returns(T.attached_class) }
2020-09-29 23:46:30 +02:00
def self.from_args(args)
args = T.unsafe(args)
2020-09-29 23:46:30 +02:00
new(explicit: {
appdir: args.appdir,
2023-03-26 08:10:40 +02:00
keyboard_layoutdir: args.keyboard_layoutdir,
2020-09-29 23:46:30 +02:00
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)
2017-12-03 09:06:23 +01:00
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)
2019-02-02 17:11:37 +01:00
2019-02-03 02:40:27 +01:00
new(
default: config.fetch("default", {}),
env: config.fetch("env", {}),
explicit: config.fetch("explicit", {}),
2024-03-07 16:20:20 +00:00
ignore_invalid_keys:,
2019-02-03 02:40:27 +01:00
)
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])])
}
2019-02-05 16:08:29 +01:00
def self.canonicalize(config)
2021-12-23 14:49:05 -05:00
config.to_h do |k, v|
key = k.to_sym
if DEFAULT_DIRS.key?(key)
2023-11-05 08:55:58 -08:00
raise TypeError, "Invalid path for default dir #{k}: #{v.inspect}" if v.is_a?(Array)
[key, Pathname(v).expand_path]
else
[key, v]
end
2021-12-23 14:49:05 -05:00
end
2019-02-05 16:08:29 +01:00
end
2024-04-22 21:05:48 +02:00
# Get the explicit configuration.
#
# @api internal
sig { returns(T::Hash[Symbol, T.any(String, Pathname, T::Array[String])]) }
2019-02-03 02:40:27 +01:00
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
2019-02-05 16:08:29 +01:00
@env = self.class.canonicalize(env) if env
@explicit = self.class.canonicalize(explicit)
2019-02-03 02:40:27 +01:00
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)
2019-02-02 17:11:37 +01:00
end
sig { returns(T::Hash[Symbol, T.any(String, Pathname, T::Array[String])]) }
2019-02-03 02:40:27 +01:00
def default
@default ||= self.class.canonicalize(self.class.defaults)
2019-02-03 02:40:27 +01:00
end
sig { returns(T::Hash[Symbol, T.any(String, Pathname, T::Array[String])]) }
2019-02-03 02:40:27 +01:00
def env
2019-02-05 16:08:29 +01:00
@env ||= self.class.canonicalize(
2020-09-29 23:46:30 +02:00
Homebrew::EnvConfig.cask_opts
.select { |arg| arg.include?("=") }
.map { |arg| T.cast(arg.split("=", 2), [String, String]) }
2020-09-29 23:46:30 +02:00
.map do |(flag, value)|
key = flag.sub(/^--/, "")
# converts --language flag to :languages config key
2020-09-29 23:46:30 +02:00
if key == "language"
key = "languages"
value = value.split(",")
end
[key, value]
end,
2019-02-05 16:08:29 +01:00
)
2019-02-02 17:11:37 +01:00
end
sig { returns(Pathname) }
2019-02-02 17:11:37 +01:00
def binarydir
@binarydir ||= HOMEBREW_PREFIX/"bin"
end
sig { returns(Pathname) }
2019-10-22 15:19:40 +03:00
def manpagedir
@manpagedir ||= HOMEBREW_PREFIX/"share/man"
end
sig { returns(T::Array[String]) }
def languages
[
2024-05-31 15:49:12 -07:00
*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
2020-02-19 11:18:40 +00:00
DEFAULT_DIRS.each_key do |dir|
2019-02-02 17:11:37 +01:00
define_method(dir) do
T.bind(self, Config)
2019-02-03 02:40:27 +01:00
explicit.fetch(dir, env.fetch(dir, default.fetch(dir)))
2019-02-02 17:11:37 +01:00
end
2017-12-03 09:06:23 +01:00
define_method(:"#{dir}=") do |path|
T.bind(self, Config)
2019-02-03 02:40:27 +01:00
explicit[dir] = Pathname(path).expand_path
2017-12-03 09:06:23 +01:00
end
end
2019-02-02 17:11:37 +01:00
sig { params(other: Config).returns(T.self_type) }
2019-02-03 02:40:27 +01:00
def merge(other)
self.class.new(explicit: other.explicit.merge(explicit))
2019-02-03 02:40:27 +01:00
end
sig { params(options: T.untyped).returns(String) }
def to_json(*options)
2019-02-03 02:40:27 +01:00
{
2024-03-07 16:20:20 +00:00
default:,
env:,
explicit:,
}.to_json(*options)
2019-02-03 02:40:27 +01:00
end
2017-12-03 09:06:23 +01:00
end
end