Unify Cask::Config data structure
This commit is contained in:
parent
08b0c2a86d
commit
f170fc7819
@ -12,7 +12,7 @@ module Cask
|
|||||||
#
|
#
|
||||||
# @api internal
|
# @api internal
|
||||||
class Config
|
class Config
|
||||||
ConfigValue = T.type_alias { T.any(LazyObject, String, Pathname, T::Array[String]) }
|
ConfigHash = T.type_alias { T::Hash[Symbol, T.any(LazyObject, String, Pathname, T::Array[String])] }
|
||||||
DEFAULT_DIRS = T.let(
|
DEFAULT_DIRS = T.let(
|
||||||
{
|
{
|
||||||
appdir: "/Applications",
|
appdir: "/Applications",
|
||||||
@ -79,24 +79,15 @@ module Cask
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
sig {
|
sig { params(config: ConfigHash).returns(ConfigHash) }
|
||||||
params(
|
|
||||||
config: T.any(
|
|
||||||
T::Hash[Symbol, T.any(LazyObject, String)],
|
|
||||||
T::Enumerable[[T.any(String, Symbol), ConfigValue]],
|
|
||||||
),
|
|
||||||
).returns(T::Hash[Symbol, ConfigValue])
|
|
||||||
}
|
|
||||||
def self.canonicalize(config)
|
def self.canonicalize(config)
|
||||||
config.to_h do |k, v|
|
config.to_h do |k, v|
|
||||||
key = k.to_sym
|
if DEFAULT_DIRS.key?(k)
|
||||||
|
|
||||||
if DEFAULT_DIRS.key?(key)
|
|
||||||
raise TypeError, "Invalid path for default dir #{k}: #{v.inspect}" if v.is_a?(Array)
|
raise TypeError, "Invalid path for default dir #{k}: #{v.inspect}" if v.is_a?(Array)
|
||||||
|
|
||||||
[key, Pathname(v.to_s).expand_path]
|
[k, Pathname(v.to_s).expand_path]
|
||||||
else
|
else
|
||||||
[key, v]
|
[k, v]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -104,14 +95,14 @@ module Cask
|
|||||||
# Get the explicit configuration.
|
# Get the explicit configuration.
|
||||||
#
|
#
|
||||||
# @api internal
|
# @api internal
|
||||||
sig { returns(T::Hash[Symbol, ConfigValue]) }
|
sig { returns(ConfigHash) }
|
||||||
attr_accessor :explicit
|
attr_accessor :explicit
|
||||||
|
|
||||||
sig {
|
sig {
|
||||||
params(
|
params(
|
||||||
default: T.nilable(T::Hash[Symbol, ConfigValue]),
|
default: T.nilable(ConfigHash),
|
||||||
env: T.nilable(T::Hash[Symbol, ConfigValue]),
|
env: T.nilable(ConfigHash),
|
||||||
explicit: T::Hash[Symbol, ConfigValue],
|
explicit: ConfigHash,
|
||||||
ignore_invalid_keys: T::Boolean,
|
ignore_invalid_keys: T::Boolean,
|
||||||
).void
|
).void
|
||||||
}
|
}
|
||||||
@ -119,18 +110,18 @@ module Cask
|
|||||||
if default
|
if default
|
||||||
@default = T.let(
|
@default = T.let(
|
||||||
self.class.canonicalize(self.class.defaults.merge(default)),
|
self.class.canonicalize(self.class.defaults.merge(default)),
|
||||||
T.nilable(T::Hash[Symbol, ConfigValue]),
|
T.nilable(ConfigHash),
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
if env
|
if env
|
||||||
@env = T.let(
|
@env = T.let(
|
||||||
self.class.canonicalize(env),
|
self.class.canonicalize(env),
|
||||||
T.nilable(T::Hash[Symbol, ConfigValue]),
|
T.nilable(ConfigHash),
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
@explicit = T.let(
|
@explicit = T.let(
|
||||||
self.class.canonicalize(explicit),
|
self.class.canonicalize(explicit),
|
||||||
T::Hash[Symbol, ConfigValue],
|
ConfigHash,
|
||||||
)
|
)
|
||||||
|
|
||||||
if ignore_invalid_keys
|
if ignore_invalid_keys
|
||||||
@ -143,18 +134,18 @@ module Cask
|
|||||||
@explicit.assert_valid_keys(*self.class.defaults.keys)
|
@explicit.assert_valid_keys(*self.class.defaults.keys)
|
||||||
end
|
end
|
||||||
|
|
||||||
sig { returns(T::Hash[Symbol, ConfigValue]) }
|
sig { returns(ConfigHash) }
|
||||||
def default
|
def default
|
||||||
@default ||= self.class.canonicalize(self.class.defaults)
|
@default ||= self.class.canonicalize(self.class.defaults)
|
||||||
end
|
end
|
||||||
|
|
||||||
sig { returns(T::Hash[Symbol, ConfigValue]) }
|
sig { returns(ConfigHash) }
|
||||||
def env
|
def env
|
||||||
@env ||= self.class.canonicalize(
|
@env ||= self.class.canonicalize(
|
||||||
Homebrew::EnvConfig.cask_opts
|
Homebrew::EnvConfig.cask_opts
|
||||||
.select { |arg| arg.include?("=") }
|
.select { |arg| arg.include?("=") }
|
||||||
.map { |arg| T.cast(arg.split("=", 2), [String, String]) }
|
.map { |arg| T.cast(arg.split("=", 2), [String, String]) }
|
||||||
.map do |(flag, value)|
|
.to_h do |(flag, value)|
|
||||||
key = flag.sub(/^--/, "")
|
key = flag.sub(/^--/, "")
|
||||||
# converts --language flag to :languages config key
|
# converts --language flag to :languages config key
|
||||||
if key == "language"
|
if key == "language"
|
||||||
@ -162,7 +153,7 @@ module Cask
|
|||||||
value = value.split(",")
|
value = value.split(",")
|
||||||
end
|
end
|
||||||
|
|
||||||
[key, value]
|
[key.to_sym, value]
|
||||||
end,
|
end,
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|||||||
@ -50,7 +50,8 @@ module Formulary
|
|||||||
end
|
end
|
||||||
private_class_method :platform_cache_tag
|
private_class_method :platform_cache_tag
|
||||||
|
|
||||||
# The untyped nested hash values are a mix of Formula instances and Formula classes.
|
# The untyped nested hash values are a mix of Formula instances and Formula classes,
|
||||||
|
# but using a union type will require a updating call sites with type guards.
|
||||||
sig { returns(T::Hash[Symbol, T::Hash[String, T.untyped]]) }
|
sig { returns(T::Hash[Symbol, T::Hash[String, T.untyped]]) }
|
||||||
def self.platform_cache
|
def self.platform_cache
|
||||||
cache[platform_cache_tag] ||= {}
|
cache[platform_cache_tag] ||= {}
|
||||||
|
|||||||
@ -235,7 +235,7 @@ class SystemCommand
|
|||||||
sig { returns(T.any(NilClass, String, Pathname)) }
|
sig { returns(T.any(NilClass, String, Pathname)) }
|
||||||
attr_reader :chdir
|
attr_reader :chdir
|
||||||
|
|
||||||
sig { returns(T::Hash[String, String]) }
|
sig { returns(T::Hash[String, T.any(NilClass, String, T::Boolean)]) }
|
||||||
attr_reader :env
|
attr_reader :env
|
||||||
|
|
||||||
sig { returns(T::Boolean) }
|
sig { returns(T::Boolean) }
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user