Class: Cask::CaskLoader::FromAPILoader Private

Inherits:
Object
  • Object
show all
Includes:
ILoader
Defined in:
cask/cask_loader.rb

Overview

This class is part of a private API. This class may only be used in the Homebrew/brew repository. Third parties should avoid using this class if possible, as it may be removed or changed without warning.

Loads a cask from the JSON API.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, from_json: nil) ⇒ FromAPILoader

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns a new instance of FromAPILoader.



246
247
248
249
250
# File 'cask/cask_loader.rb', line 246

def initialize(token, from_json: nil)
  @token = token.sub(%r{^homebrew/(?:homebrew-)?cask/}i, "")
  @path = CaskLoader.default_path(@token)
  @from_json = from_json
end

Instance Attribute Details

#pathObject (readonly)

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



235
236
237
# File 'cask/cask_loader.rb', line 235

def path
  @path
end

#tokenObject (readonly)

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



235
236
237
# File 'cask/cask_loader.rb', line 235

def token
  @token
end

Class Method Details

.can_load?(ref) ⇒ Boolean

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.

Returns:

  • (Boolean)


237
238
239
240
241
242
243
244
# File 'cask/cask_loader.rb', line 237

def self.can_load?(ref)
  return false if Homebrew::EnvConfig.no_install_from_api?
  return false unless ref.is_a?(String)
  return false unless ref.match?(HOMEBREW_MAIN_TAP_CASK_REGEX)

  token = ref.sub(%r{^homebrew/(?:homebrew-)?cask/}i, "")
  Homebrew::API::Cask.all_casks.key?(token)
end

Instance Method Details

#from_h_array_gsubs(array, appdir) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



363
364
365
366
367
# File 'cask/cask_loader.rb', line 363

def from_h_array_gsubs(array, appdir)
  array.to_a.map do |value|
    from_h_gsubs(value, appdir)
  end
end

#from_h_gsubs(value, appdir) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



375
376
377
378
379
380
381
382
383
384
385
386
387
388
# File 'cask/cask_loader.rb', line 375

def from_h_gsubs(value, appdir)
  return value if value.blank?

  case value
  when Hash
    from_h_hash_gsubs(value, appdir)
  when Array
    from_h_array_gsubs(value, appdir)
  when String
    from_h_string_gsubs(value, appdir)
  else
    value
  end
end

#from_h_hash_gsubs(hash, appdir) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



369
370
371
372
373
# File 'cask/cask_loader.rb', line 369

def from_h_hash_gsubs(hash, appdir)
  hash.to_h.transform_values do |value|
    from_h_gsubs(value, appdir)
  end
end

#from_h_string_gsubs(string, appdir) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



355
356
357
358
359
360
361
# File 'cask/cask_loader.rb', line 355

def from_h_string_gsubs(string, appdir)
  string.to_s
        .gsub(HOMEBREW_HOME_PLACEHOLDER, Dir.home)
        .gsub(HOMEBREW_PREFIX_PLACEHOLDER, HOMEBREW_PREFIX)
        .gsub(HOMEBREW_CELLAR_PLACEHOLDER, HOMEBREW_CELLAR)
        .gsub(HOMEBREW_CASK_APPDIR_PLACEHOLDER, appdir)
end

#load(config:) ⇒ Object

This method is part of a private API. This method may only be used in the Homebrew/brew repository. Third parties should avoid using this method if possible, as it may be removed or changed without warning.



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
# File 'cask/cask_loader.rb', line 252

def load(config:)
  json_cask = @from_json || Homebrew::API::Cask.all_casks[token]

  cask_options = {
    loaded_from_api: true,
    source:          JSON.pretty_generate(json_cask),
    config:          config,
    loader:          self,
  }

  json_cask = Homebrew::API.merge_variations(json_cask).deep_symbolize_keys.freeze

  cask_options[:tap] = Tap.fetch(json_cask[:tap]) if json_cask[:tap].to_s.include?("/")

  user_agent = json_cask.dig(:url_specs, :user_agent)
  json_cask[:url_specs][:user_agent] = user_agent[1..].to_sym if user_agent && user_agent[0] == ":"
  if (using = json_cask.dig(:url_specs, :using))
    json_cask[:url_specs][:using] = using.to_sym
  end

  api_cask = Cask.new(token, **cask_options) do
    version json_cask[:version]

    if json_cask[:sha256] == "no_check"
      sha256 :no_check
    else
      sha256 json_cask[:sha256]
    end

    url json_cask[:url], **json_cask.fetch(:url_specs, {}) if json_cask[:url].present?
    appcast json_cask[:appcast] if json_cask[:appcast].present?
    json_cask[:name].each do |cask_name|
      name cask_name
    end
    desc json_cask[:desc]
    homepage json_cask[:homepage]

    auto_updates json_cask[:auto_updates] unless json_cask[:auto_updates].nil?
    conflicts_with(**json_cask[:conflicts_with]) if json_cask[:conflicts_with].present?

    if json_cask[:depends_on].present?
      dep_hash = json_cask[:depends_on].to_h do |dep_key, dep_value|
        # Arch dependencies are encoded like `{ type: :intel, bits: 64 }`
        # but `depends_on arch:` only accepts `:intel` or `:arm64`
        if dep_key == :arch
          next [:arch, :intel] if dep_value.first[:type] == "intel"

          next [:arch, :arm64]
        end

        next [dep_key, dep_value] if dep_key != :macos

        dep_type = dep_value.keys.first
        if dep_type == :==
          version_symbols = dep_value[dep_type].map do |version|
            MacOSVersion::SYMBOLS.key(version) || version
          end
          next [dep_key, version_symbols]
        end

        version_symbol = dep_value[dep_type].first
        version_symbol = MacOSVersion::SYMBOLS.key(version_symbol) || version_symbol
        [dep_key, "#{dep_type} :#{version_symbol}"]
      end.compact
      depends_on(**dep_hash)
    end

    if json_cask[:container].present?
      container_hash = json_cask[:container].to_h do |container_key, container_value|
        next [container_key, container_value] if container_key != :type

        [container_key, container_value.to_sym]
      end
      container(**container_hash)
    end

    json_cask[:artifacts].each do |artifact|
      # convert generic string replacements into actual ones
      artifact = cask.loader.from_h_gsubs(artifact, appdir)
      key = artifact.keys.first
      if artifact[key].nil?
        # for artifacts with blocks that can't be loaded from the API
        send(key) {} # empty on purpose
      else
        args = artifact[key]
        kwargs = if args.last.is_a?(Hash)
          args.pop
        else
          {}
        end
        send(key, *args, **kwargs)
      end
    end

    if json_cask[:caveats].present?
      # convert generic string replacements into actual ones
      caveats cask.loader.from_h_string_gsubs(json_cask[:caveats], appdir)
    end
  end
  api_cask.populate_from_api!(json_cask)
  api_cask
end