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), path: 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{String => T.untyped}, nil) (defaults to: T.unsafe(nil))
  • path (Pathname, nil) (defaults to: nil)


327
328
329
330
331
332
# File 'cask/cask_loader.rb', line 327

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

Instance Attribute Details

#from_jsonHash{String => T.untyped}? (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:



299
300
301
# File 'cask/cask_loader.rb', line 299

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:



296
297
298
# File 'cask/cask_loader.rb', line 296

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:



293
294
295
# File 'cask/cask_loader.rb', line 293

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)


305
306
307
308
309
310
311
312
313
314
315
316
317
318
# File 'cask/cask_loader.rb', line 305

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.



455
456
457
458
459
# File 'cask/cask_loader.rb', line 455

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.



467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'cask/cask_loader.rb', line 467

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.



461
462
463
464
465
# File 'cask/cask_loader.rb', line 461

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.



447
448
449
450
451
452
453
# File 'cask/cask_loader.rb', line 447

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.



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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'cask/cask_loader.rb', line 334

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

  cask_options = {
    loaded_from_api: true,
    sourcefile_path: @sourcefile_path,
    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?
    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