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: T.unsafe(nil)) ⇒ void

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.

Parameters:

  • token (String)
  • from_json (Hash) (defaults to: T.unsafe(nil))


284
285
286
287
288
# File 'cask/cask_loader.rb', line 284

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

Instance Attribute Details

#from_jsonHash? (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.

Returns:



262
263
264
# File 'cask/cask_loader.rb', line 262

def from_json
  @from_json
end

#pathPathname (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.

Returns:



259
260
261
# File 'cask/cask_loader.rb', line 259

def path
  @path
end

#tokenString (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.

Returns:



256
257
258
# File 'cask/cask_loader.rb', line 256

def token
  @token
end

Class Method Details

.try_new(ref, warn: false) ⇒ T.attached_class?

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.

Parameters:

Returns:

  • (T.attached_class, nil)


268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'cask/cask_loader.rb', line 268

def self.try_new(ref, warn: false)
  return if Homebrew::EnvConfig.no_install_from_api?
  return unless ref.is_a?(String)
  return unless (token = ref[HOMEBREW_DEFAULT_TAP_CASK_REGEX, :token])
  if !Homebrew::API::Cask.all_casks.key?(token) &&
     !Homebrew::API::Cask.all_renames.key?(token)
    return
  end

  ref = "#{CoreCaskTap.instance}/#{token}"

  token, tap, = CaskLoader.tap_cask_token_type(ref, warn:)
  new("#{tap}/#{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.



411
412
413
414
415
# File 'cask/cask_loader.rb', line 411

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.



423
424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'cask/cask_loader.rb', line 423

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.



417
418
419
420
421
# File 'cask/cask_loader.rb', line 417

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.



403
404
405
406
407
408
409
# File 'cask/cask_loader.rb', line 403

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.



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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'cask/cask_loader.rb', line 290

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

  cask_options = {
    loaded_from_api: true,
    source:          JSON.pretty_generate(json_cask),
    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]

    if (deprecation_date = json_cask[:deprecation_date].presence)
      reason = DeprecateDisable.to_reason_string_or_symbol json_cask[:deprecation_reason], type: :cask
      deprecate! date: deprecation_date, because: reason
    end

    if (disable_date = json_cask[:disable_date].presence)
      reason = DeprecateDisable.to_reason_string_or_symbol json_cask[:disable_reason], type: :cask
      disable! date: disable_date, because: reason
    end

    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