From bcdb4a3f3273eb63076c6714500469767efdf688 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Tue, 5 Feb 2019 16:08:29 +0100 Subject: [PATCH] Add `Cask::Config::canonicalize`. --- Library/Homebrew/cask/config.rb | 59 ++++++++++++++++++--------------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/Library/Homebrew/cask/config.rb b/Library/Homebrew/cask/config.rb index a575bf27da..eba5ecfe0b 100644 --- a/Library/Homebrew/cask/config.rb +++ b/Library/Homebrew/cask/config.rb @@ -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