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



70
71
72
73
74
75
76
77
# File 'brew/Library/Homebrew/cask/config.rb', line 70

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



68
69
70
# File 'brew/Library/Homebrew/cask/config.rb', line 68

def explicit
  @explicit
end

Class Method Details

.canonicalize(config) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'brew/Library/Homebrew/cask/config.rb', line 56

def self.canonicalize(config)
  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
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



92
93
94
# File 'brew/Library/Homebrew/cask/config.rb', line 92

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

#defaultObject



79
80
81
# File 'brew/Library/Homebrew/cask/config.rb', line 79

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

#envObject



83
84
85
86
87
88
89
90
# File 'brew/Library/Homebrew/cask/config.rb', line 83

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



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

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

#to_json(*args) ⇒ Object



110
111
112
113
114
115
116
# File 'brew/Library/Homebrew/cask/config.rb', line 110

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