66 lines
1.7 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-02 17:11:37 +01:00
module Cask
class Config < DelegateClass(Hash)
DEFAULT_DIRS = {
2017-12-03 09:06:23 +01:00
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",
2019-02-02 17:11:37 +01:00
screen_saverdir: "~/Library/Screen Savers",
}.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-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
new(Hash[config.map { |k, v| [k.to_sym, v] }])
end
def initialize(**dirs)
super(Hash[DEFAULT_DIRS.map { |(k, v)| [k, Pathname(dirs.fetch(k, v)).expand_path] }])
end
def binarydir
@binarydir ||= HOMEBREW_PREFIX/"bin"
end
DEFAULT_DIRS.keys.each do |dir|
define_method(dir) do
self[dir]
end
2017-12-03 09:06:23 +01:00
define_method(:"#{dir}=") do |path|
2019-02-02 17:11:37 +01:00
self[dir] = Pathname(path).expand_path
2017-12-03 09:06:23 +01:00
end
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