108 lines
3.2 KiB
Ruby
Raw Normal View History

2019-02-02 17:11:37 +01:00
require "json"
2017-12-03 09:06:23 +01:00
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-03 02:40:27 +01:00
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,
2019-02-02 17:11:37 +01:00
}.freeze
2017-12-03 09:06:23 +01:00
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?
from_file(cask.config_path)
else
global
end
2017-12-03 09:06:23 +01:00
end
2019-02-02 17:11:37 +01:00
def self.from_file(path)
config = begin
JSON.parse(File.read(path))
rescue JSON::ParserError => e
raise e, "Cannot parse #{path}: #{e}", e.backtrace
end
2019-02-03 02:40:27 +01:00
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,
)
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 = default
@env = env
@explicit = explicit.map { |(k, v)| [k.to_sym, Pathname(v).expand_path] }.to_h
2019-02-02 17:11:37 +01:00
end
2019-02-03 02:40:27 +01:00
def default
@default ||= DEFAULT_DIRS
end
def env
@env ||= Shellwords.shellsplit(ENV.fetch("HOMEBREW_CASK_OPTS", ""))
.select { |arg| arg.include?("=") }
2019-02-03 02:40:27 +01:00
.map { |arg| arg.split("=", 2) }
.map { |(flag, value)| [flag.sub(/^\-\-/, "").to_sym, Pathname(value).expand_path] }
.to_h
2019-02-02 17:11:37 +01:00
end
def binarydir
@binarydir ||= HOMEBREW_PREFIX/"bin"
end
DEFAULT_DIRS.keys.each do |dir|
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)
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
2019-02-02 17:11:37 +01:00
def write(path)
path.atomic_write(to_json)
end
2017-12-03 09:06:23 +01:00
end
end