Class: Hbc::DSL

Inherits:
Object
  • Object
show all
Defined in:
Library/Homebrew/cask/lib/hbc/dsl.rb,
Library/Homebrew/cask/lib/hbc/dsl/gpg.rb,
Library/Homebrew/cask/lib/hbc/dsl/base.rb,
Library/Homebrew/cask/lib/hbc/dsl/appcast.rb,
Library/Homebrew/cask/lib/hbc/dsl/caveats.rb,
Library/Homebrew/cask/lib/hbc/dsl/version.rb,
Library/Homebrew/cask/lib/hbc/dsl/container.rb,
Library/Homebrew/cask/lib/hbc/dsl/preflight.rb,
Library/Homebrew/cask/lib/hbc/dsl/depends_on.rb,
Library/Homebrew/cask/lib/hbc/dsl/postflight.rb,
Library/Homebrew/cask/lib/hbc/dsl/stanza_proxy.rb,
Library/Homebrew/cask/lib/hbc/dsl/conflicts_with.rb,
Library/Homebrew/cask/lib/hbc/dsl/uninstall_preflight.rb,
Library/Homebrew/cask/lib/hbc/dsl/uninstall_postflight.rb

Defined Under Namespace

Classes: Appcast, Base, Caveats, ConflictsWith, Container, DependsOn, Gpg, Postflight, Preflight, StanzaProxy, UninstallPostflight, UninstallPreflight, Version

Constant Summary

ORDINARY_ARTIFACT_CLASSES =
[
  Artifact::Installer,
  Artifact::App,
  Artifact::Artifact,
  Artifact::AudioUnitPlugin,
  Artifact::Binary,
  Artifact::Colorpicker,
  Artifact::Dictionary,
  Artifact::Font,
  Artifact::InputMethod,
  Artifact::InternetPlugin,
  Artifact::Pkg,
  Artifact::Prefpane,
  Artifact::Qlplugin,
  Artifact::ScreenSaver,
  Artifact::Service,
  Artifact::StageOnly,
  Artifact::Suite,
  Artifact::VstPlugin,
  Artifact::Vst3Plugin,
  Artifact::Uninstall,
  Artifact::Zap,
].freeze
ACTIVATABLE_ARTIFACT_CLASSES =
ORDINARY_ARTIFACT_CLASSES - [Artifact::StageOnly]
ARTIFACT_BLOCK_CLASSES =
[
  Artifact::PreflightBlock,
  Artifact::PostflightBlock,
].freeze
DSL_METHODS =
Set.new [
  :accessibility_access,
  :appcast,
  :artifacts,
  :auto_updates,
  :caskroom_path,
  :caveats,
  :conflicts_with,
  :container,
  :depends_on,
  :gpg,
  :homepage,
  :language,
  :languages,
  :name,
  :sha256,
  :staged_path,
  :url,
  :version,
  :appdir,
  *ORDINARY_ARTIFACT_CLASSES.map(&:dsl_key),
  *ACTIVATABLE_ARTIFACT_CLASSES.map(&:dsl_key),
  *ARTIFACT_BLOCK_CLASSES.flat_map { |klass| [klass.dsl_key, klass.uninstall_dsl_key] },
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(cask) ⇒ DSL



80
81
82
83
# File 'Library/Homebrew/cask/lib/hbc/dsl.rb', line 80

def initialize(cask)
  @cask = cask
  @token = cask.token
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method) ⇒ Object



281
282
283
284
285
286
287
288
# File 'Library/Homebrew/cask/lib/hbc/dsl.rb', line 281

def method_missing(method, *)
  if method
    Utils.method_missing_message(method, token)
    nil
  else
    super
  end
end

Instance Attribute Details

#caskObject (readonly)

Returns the value of attribute cask



78
79
80
# File 'Library/Homebrew/cask/lib/hbc/dsl.rb', line 78

def cask
  @cask
end

#tokenObject (readonly)

Returns the value of attribute token



78
79
80
# File 'Library/Homebrew/cask/lib/hbc/dsl.rb', line 78

def token
  @token
end

Instance Method Details

#accessibility_access(accessibility_access = nil) ⇒ Object



249
250
251
# File 'Library/Homebrew/cask/lib/hbc/dsl.rb', line 249

def accessibility_access(accessibility_access = nil)
  set_unique_stanza(:accessibility_access, accessibility_access.nil?) { accessibility_access }
end

#appcast(*args) ⇒ Object



164
165
166
# File 'Library/Homebrew/cask/lib/hbc/dsl.rb', line 164

def appcast(*args)
  set_unique_stanza(:appcast, args.empty?) { DSL::Appcast.new(*args) }
end

#appdirObject



294
295
296
# File 'Library/Homebrew/cask/lib/hbc/dsl.rb', line 294

def appdir
  cask.config.appdir
end

#artifactsObject



221
222
223
# File 'Library/Homebrew/cask/lib/hbc/dsl.rb', line 221

def artifacts
  @artifacts ||= SortedSet.new
end

#auto_updates(auto_updates = nil) ⇒ Object



253
254
255
# File 'Library/Homebrew/cask/lib/hbc/dsl.rb', line 253

def auto_updates(auto_updates = nil)
  set_unique_stanza(:auto_updates, auto_updates.nil?) { auto_updates }
end

#caskroom_pathObject



225
226
227
# File 'Library/Homebrew/cask/lib/hbc/dsl.rb', line 225

def caskroom_path
  @caskroom_path ||= Hbc.caskroom.join(token)
end

#caveats(*strings, &block) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'Library/Homebrew/cask/lib/hbc/dsl.rb', line 235

def caveats(*strings, &block)
  @caveats ||= DSL::Caveats.new(cask)
  if block_given?
    @caveats.eval_caveats(&block)
  elsif strings.any?
    strings.each do |string|
      @caveats.eval_caveats { string }
    end
  else
    return @caveats.to_s
  end
  @caveats
end

#conflicts_with(*args) ⇒ Object



216
217
218
219
# File 'Library/Homebrew/cask/lib/hbc/dsl.rb', line 216

def conflicts_with(*args)
  # TODO: remove this constraint, and instead merge multiple conflicts_with stanzas
  set_unique_stanza(:conflicts_with, args.empty?) { DSL::ConflictsWith.new(*args) }
end

#container(*args) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'Library/Homebrew/cask/lib/hbc/dsl.rb', line 172

def container(*args)
  # TODO: remove this constraint, and instead merge multiple container stanzas
  set_unique_stanza(:container, args.empty?) do
    begin
      DSL::Container.new(*args).tap do |container|
        # TODO: remove this backward-compatibility section after removing nested_container
        if container&.nested
          artifacts.add(Artifact::NestedContainer.new(cask, container.nested))
        end
      end
    end
  end
end

#depends_on(*args) ⇒ Object

depends_on uses a load method so that multiple stanzas can be merged



205
206
207
208
209
210
211
212
213
214
# File 'Library/Homebrew/cask/lib/hbc/dsl.rb', line 205

def depends_on(*args)
  @depends_on ||= DSL::DependsOn.new
  return @depends_on if args.empty?
  begin
    @depends_on.load(*args)
  rescue RuntimeError => e
    raise CaskInvalidError.new(cask, e)
  end
  @depends_on
end

#gpg(*args) ⇒ Object



168
169
170
# File 'Library/Homebrew/cask/lib/hbc/dsl.rb', line 168

def gpg(*args)
  set_unique_stanza(:gpg, args.empty?) { DSL::Gpg.new(*args) }
end

#homepage(homepage = nil) ⇒ Object



105
106
107
# File 'Library/Homebrew/cask/lib/hbc/dsl.rb', line 105

def homepage(homepage = nil)
  set_unique_stanza(:homepage, homepage.nil?) { homepage }
end

#language(*args, default: false, &block) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'Library/Homebrew/cask/lib/hbc/dsl.rb', line 109

def language(*args, default: false, &block)
  if args.empty?
    language_eval
  elsif block_given?
    @language_blocks ||= {}
    @language_blocks[args] = block

    return unless default

    unless @language_blocks.default.nil?
      raise CaskInvalidError.new(cask, "Only one default language may be defined.")
    end

    @language_blocks.default = block
  else
    raise CaskInvalidError.new(cask, "No block given to language stanza.")
  end
end

#language_evalObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'Library/Homebrew/cask/lib/hbc/dsl.rb', line 128

def language_eval
  return @language if instance_variable_defined?(:@language)

  return @language = nil if @language_blocks.nil? || @language_blocks.empty?

  if @language_blocks.default.nil?
    raise CaskInvalidError.new(cask, "No default language specified.")
  end

  MacOS.languages.map(&Locale.method(:parse)).each do |locale|
    key = @language_blocks.keys.detect do |strings|
      strings.any? { |string| locale.include?(string) }
    end

    next if key.nil?

    return @language = @language_blocks[key].call
  end

  @language = @language_blocks.default.call
end

#languagesObject



150
151
152
153
154
# File 'Library/Homebrew/cask/lib/hbc/dsl.rb', line 150

def languages
  return [] if @language_blocks.nil?

  @language_blocks.keys.flatten
end

#name(*args) ⇒ Object



85
86
87
88
89
# File 'Library/Homebrew/cask/lib/hbc/dsl.rb', line 85

def name(*args)
  @name ||= []
  return @name if args.empty?
  @name.concat(args.flatten)
end

#respond_to_missing?Boolean



290
291
292
# File 'Library/Homebrew/cask/lib/hbc/dsl.rb', line 290

def respond_to_missing?(*)
  true
end

#set_unique_stanza(stanza, should_return) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'Library/Homebrew/cask/lib/hbc/dsl.rb', line 91

def set_unique_stanza(stanza, should_return)
  return instance_variable_get("@#{stanza}") if should_return

  if instance_variable_defined?("@#{stanza}")
    raise CaskInvalidError.new(cask, "'#{stanza}' stanza may only appear once.")
  end

  instance_variable_set("@#{stanza}", yield)
rescue CaskInvalidError
  raise
rescue StandardError => e
  raise CaskInvalidError.new(cask, "'#{stanza}' stanza failed with: #{e}")
end

#sha256(arg = nil) ⇒ Object



195
196
197
198
199
200
201
202
# File 'Library/Homebrew/cask/lib/hbc/dsl.rb', line 195

def sha256(arg = nil)
  set_unique_stanza(:sha256, arg.nil?) do
    if !arg.is_a?(String) && arg != :no_check
      raise CaskInvalidError.new(cask, "invalid 'sha256' value: '#{arg.inspect}'")
    end
    arg
  end
end

#staged_pathObject



229
230
231
232
233
# File 'Library/Homebrew/cask/lib/hbc/dsl.rb', line 229

def staged_path
  return @staged_path if @staged_path
  cask_version = version || :unknown
  @staged_path = caskroom_path.join(cask_version.to_s)
end

#url(*args, &block) ⇒ Object



156
157
158
159
160
161
162
# File 'Library/Homebrew/cask/lib/hbc/dsl.rb', line 156

def url(*args, &block)
  set_unique_stanza(:url, args.empty? && !block_given?) do
    begin
      URL.from(*args, &block)
    end
  end
end

#version(arg = nil) ⇒ Object



186
187
188
189
190
191
192
193
# File 'Library/Homebrew/cask/lib/hbc/dsl.rb', line 186

def version(arg = nil)
  set_unique_stanza(:version, arg.nil?) do
    if !arg.is_a?(String) && arg != :latest
      raise CaskInvalidError.new(cask, "invalid 'version' value: '#{arg.inspect}'")
    end
    DSL::Version.new(arg)
  end
end