2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-02-02 17:11:37 +01:00
|
|
|
require "json"
|
2017-12-03 09:06:23 +01:00
|
|
|
|
2020-07-21 21:28:58 +02:00
|
|
|
require "lazy_object"
|
|
|
|
require "locale"
|
|
|
|
|
2019-02-03 02:40:27 +01:00
|
|
|
require "extend/hash_validator"
|
|
|
|
using HashValidator
|
|
|
|
|
2019-02-02 17:11:37 +01:00
|
|
|
module Cask
|
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",
|
|
|
|
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",
|
|
|
|
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",
|
2019-02-02 17:11:37 +01:00
|
|
|
}.freeze
|
2017-12-03 09:06:23 +01:00
|
|
|
|
2020-07-21 21:28:58 +02:00
|
|
|
def self.defaults
|
|
|
|
{
|
|
|
|
languages: LazyObject.new { MacOS.languages },
|
|
|
|
}.merge(DEFAULT_DIRS).freeze
|
|
|
|
end
|
|
|
|
|
2019-02-02 17:11:37 +01:00
|
|
|
def self.global
|
|
|
|
@global ||= new
|
|
|
|
end
|
2017-12-03 09:06:23 +01:00
|
|
|
|
2019-02-03 02:40:27 +01:00
|
|
|
def self.clear
|
|
|
|
@global = nil
|
|
|
|
end
|
|
|
|
|
2019-02-02 17:11:37 +01:00
|
|
|
def self.for_cask(cask)
|
|
|
|
if cask.config_path.exist?
|
2020-04-27 14:54:56 +02:00
|
|
|
from_json(File.read(cask.config_path))
|
2019-02-02 17:11:37 +01:00
|
|
|
else
|
|
|
|
global
|
|
|
|
end
|
2017-12-03 09:06:23 +01:00
|
|
|
end
|
|
|
|
|
2020-04-27 14:54:56 +02:00
|
|
|
def self.from_json(json)
|
2019-02-02 17:11:37 +01:00
|
|
|
config = begin
|
2020-04-27 14:54:56 +02:00
|
|
|
JSON.parse(json)
|
2019-02-02 17:11:37 +01:00
|
|
|
rescue JSON::ParserError => e
|
|
|
|
raise e, "Cannot parse #{path}: #{e}", e.backtrace
|
|
|
|
end
|
|
|
|
|
2019-02-03 02:40:27 +01:00
|
|
|
new(
|
2019-02-05 16:08:29 +01:00
|
|
|
default: config.fetch("default", {}),
|
|
|
|
env: config.fetch("env", {}),
|
|
|
|
explicit: config.fetch("explicit", {}),
|
2019-02-03 02:40:27 +01:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-02-05 16:08:29 +01:00
|
|
|
def self.canonicalize(config)
|
2019-07-01 13:34:41 +02:00
|
|
|
config.map do |k, v|
|
|
|
|
key = k.to_sym
|
|
|
|
|
|
|
|
if DEFAULT_DIRS.key?(key)
|
|
|
|
[key, Pathname(v).expand_path]
|
|
|
|
else
|
|
|
|
[key, v]
|
|
|
|
end
|
|
|
|
end.to_h
|
2019-02-05 16:08:29 +01:00
|
|
|
end
|
|
|
|
|
2019-02-03 02:40:27 +01:00
|
|
|
attr_accessor :explicit
|
|
|
|
|
|
|
|
def initialize(default: nil, env: nil, explicit: {})
|
2020-07-21 21:28:58 +02:00
|
|
|
@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
|
|
|
|
2020-07-21 21:28:58 +02:00
|
|
|
@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
|
|
|
|
|
2019-02-03 02:40:27 +01:00
|
|
|
def default
|
2020-07-21 21:28:58 +02:00
|
|
|
@default ||= self.class.canonicalize(self.class.defaults)
|
2019-02-03 02:40:27 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def env
|
2019-02-05 16:08:29 +01:00
|
|
|
@env ||= self.class.canonicalize(
|
|
|
|
Shellwords.shellsplit(ENV.fetch("HOMEBREW_CASK_OPTS", ""))
|
|
|
|
.select { |arg| arg.include?("=") }
|
|
|
|
.map { |arg| arg.split("=", 2) }
|
2020-07-21 21:28:58 +02:00
|
|
|
.map do |(flag, value)|
|
|
|
|
key = flag.sub(/^--/, "")
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def binarydir
|
|
|
|
@binarydir ||= HOMEBREW_PREFIX/"bin"
|
|
|
|
end
|
|
|
|
|
2019-10-22 15:19:40 +03:00
|
|
|
def manpagedir
|
|
|
|
@manpagedir ||= HOMEBREW_PREFIX/"share/man"
|
|
|
|
end
|
|
|
|
|
2020-07-21 21:28:58 +02:00
|
|
|
def languages
|
|
|
|
[
|
|
|
|
*explicit[:languages],
|
|
|
|
*env[:languages],
|
|
|
|
*default[: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
|
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|
|
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
|
|
|
|
2019-02-03 02:40:27 +01:00
|
|
|
def merge(other)
|
2019-02-03 13:03:16 +01:00
|
|
|
self.class.new(explicit: other.explicit.merge(explicit))
|
2019-02-03 02:40:27 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_json(*args)
|
|
|
|
{
|
|
|
|
default: default,
|
|
|
|
env: env,
|
|
|
|
explicit: explicit,
|
|
|
|
}.to_json(*args)
|
|
|
|
end
|
2017-12-03 09:06:23 +01:00
|
|
|
end
|
|
|
|
end
|