Class: Cask::Config

Inherits:
Object
  • Object
show all
Defined in:
brew/Library/Homebrew/cask/config.rb

Constant Summary collapse

DEFAULT_DIRS =
{
  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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default: nil, env: nil, explicit: {}) ⇒ Config

Returns a new instance of Config



62
63
64
65
66
67
68
69
# File 'brew/Library/Homebrew/cask/config.rb', line 62

def initialize(default: nil, env: nil, explicit: {})
  @default = self.class.canonicalize(default) if default
  @env = self.class.canonicalize(env) if env
  @explicit = self.class.canonicalize(explicit)

  @env&.assert_valid_keys!(*DEFAULT_DIRS.keys)
  @explicit.assert_valid_keys!(*DEFAULT_DIRS.keys)
end

Instance Attribute Details

#explicitObject

Returns the value of attribute explicit



60
61
62
# File 'brew/Library/Homebrew/cask/config.rb', line 60

def explicit
  @explicit
end

Class Method Details

.canonicalize(config) ⇒ Object



56
57
58
# File 'brew/Library/Homebrew/cask/config.rb', line 56

def self.canonicalize(config)
  config.map { |k, v| [k.to_sym, Pathname(v).expand_path] }.to_h
end

.clearObject



30
31
32
# File 'brew/Library/Homebrew/cask/config.rb', line 30

def self.clear
  @global = nil
end

.for_cask(cask) ⇒ Object



34
35
36
37
38
39
40
# File 'brew/Library/Homebrew/cask/config.rb', line 34

def self.for_cask(cask)
  if cask.config_path.exist?
    from_file(cask.config_path)
  else
    global
  end
end

.from_file(path) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'brew/Library/Homebrew/cask/config.rb', line 42

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(
    default:  config.fetch("default",  {}),
    env:      config.fetch("env",      {}),
    explicit: config.fetch("explicit", {}),
  )
end

.globalObject



26
27
28
# File 'brew/Library/Homebrew/cask/config.rb', line 26

def self.global
  @global ||= new
end

Instance Method Details

#binarydirObject



84
85
86
# File 'brew/Library/Homebrew/cask/config.rb', line 84

def binarydir
  @binarydir ||= HOMEBREW_PREFIX/"bin"
end

#defaultObject



71
72
73
# File 'brew/Library/Homebrew/cask/config.rb', line 71

def default
  @default ||= self.class.canonicalize(DEFAULT_DIRS)
end

#envObject



75
76
77
78
79
80
81
82
# File 'brew/Library/Homebrew/cask/config.rb', line 75

def env
  @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

#merge(other) ⇒ Object



98
99
100
# File 'brew/Library/Homebrew/cask/config.rb', line 98

def merge(other)
  self.class.new(explicit: other.explicit.merge(explicit))
end

#to_json(*args) ⇒ Object



102
103
104
105
106
107
108
# File 'brew/Library/Homebrew/cask/config.rb', line 102

def to_json(*args)
  {
    default:  default,
    env:      env,
    explicit: explicit,
  }.to_json(*args)
end