Add Cask::Config::canonicalize.

This commit is contained in:
Markus Reiter 2019-02-05 16:08:29 +01:00
parent defbf7d74c
commit bcdb4a3f32

View File

@ -6,19 +6,19 @@ using HashValidator
module Cask
class Config
DEFAULT_DIRS = {
appdir: Pathname("/Applications").expand_path,
prefpanedir: Pathname("~/Library/PreferencePanes").expand_path,
qlplugindir: Pathname("~/Library/QuickLook").expand_path,
dictionarydir: Pathname("~/Library/Dictionaries").expand_path,
fontdir: Pathname("~/Library/Fonts").expand_path,
colorpickerdir: Pathname("~/Library/ColorPickers").expand_path,
servicedir: Pathname("~/Library/Services").expand_path,
input_methoddir: Pathname("~/Library/Input Methods").expand_path,
internet_plugindir: Pathname("~/Library/Internet Plug-Ins").expand_path,
audio_unit_plugindir: Pathname("~/Library/Audio/Plug-Ins/Components").expand_path,
vst_plugindir: Pathname("~/Library/Audio/Plug-Ins/VST").expand_path,
vst3_plugindir: Pathname("~/Library/Audio/Plug-Ins/VST3").expand_path,
screen_saverdir: Pathname("~/Library/Screen Savers").expand_path,
appdir: "/Applications",
prefpanedir: "~/Library/PreferencePanes",
qlplugindir: "~/Library/QuickLook",
dictionarydir: "~/Library/Dictionaries",
fontdir: "~/Library/Fonts",
colorpickerdir: "~/Library/ColorPickers",
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.global
@ -45,33 +45,38 @@ module Cask
end
new(
default: config.fetch("default", {}).map { |k, v| [k.to_sym, Pathname(v).expand_path] }.to_h,
env: config.fetch("env", {}).map { |k, v| [k.to_sym, Pathname(v).expand_path] }.to_h,
explicit: config.fetch("explicit", {}).map { |k, v| [k.to_sym, Pathname(v).expand_path] }.to_h,
default: config.fetch("default", {}),
env: config.fetch("env", {}),
explicit: config.fetch("explicit", {}),
)
end
def self.canonicalize(config)
config.map { |k, v| [k.to_sym, Pathname(v).expand_path] }.to_h
end
attr_accessor :explicit
def initialize(default: nil, env: nil, explicit: {})
env&.assert_valid_keys!(*DEFAULT_DIRS.keys)
explicit.assert_valid_keys!(*DEFAULT_DIRS.keys)
@default = self.class.canonicalize(default) if default
@env = self.class.canonicalize(env) if env
@explicit = self.class.canonicalize(explicit)
@default = default
@env = env
@explicit = explicit.map { |(k, v)| [k.to_sym, Pathname(v).expand_path] }.to_h
@env&.assert_valid_keys!(*DEFAULT_DIRS.keys)
@explicit.assert_valid_keys!(*DEFAULT_DIRS.keys)
end
def default
@default ||= DEFAULT_DIRS
@default ||= self.class.canonicalize(DEFAULT_DIRS)
end
def env
@env ||= Shellwords.shellsplit(ENV.fetch("HOMEBREW_CASK_OPTS", ""))
.select { |arg| arg.include?("=") }
.map { |arg| arg.split("=", 2) }
.map { |(flag, value)| [flag.sub(/^\-\-/, "").to_sym, Pathname(value).expand_path] }
.to_h
@env ||= self.class.canonicalize(
Shellwords.shellsplit(ENV.fetch("HOMEBREW_CASK_OPTS", ""))
.select { |arg| arg.include?("=") }
.map { |arg| arg.split("=", 2) }
.map { |(flag, value)| [flag.sub(/^\-\-/, ""), value] },
)
end
def binarydir