Class: Cask::CaskLoader::FromPathLoader Private

Inherits:
AbstractContentLoader show all
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 a path.

Instance Attribute Summary collapse

Attributes inherited from AbstractContentLoader

#content, #tap

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, token: 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:



109
110
111
112
113
114
115
116
117
# File 'cask/cask_loader.rb', line 109

def initialize(path, token: T.unsafe(nil))
  super()

  path = Pathname(path).expand_path

  @token = path.basename(path.extname).to_s
  @path = path
  @tap = Tap.from_path(path) || Homebrew::API.tap_from_source_download(path)
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.



106
107
108
# File 'cask/cask_loader.rb', line 106

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.



106
107
108
# File 'cask/cask_loader.rb', line 106

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)


90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'cask/cask_loader.rb', line 90

def self.try_new(ref, warn: false)
  path = case ref
  when String
    Pathname(ref)
  when Pathname
    ref
  else
    return
  end

  return if %w[.rb .json].exclude?(path.extname)
  return unless path.expand_path.exist?

  new(path)
end

Instance Method Details

#load(config:) ⇒ Cask

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:

Raises:



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'cask/cask_loader.rb', line 120

def load(config:)
  raise CaskUnavailableError.new(token, "'#{path}' does not exist.")  unless path.exist?
  raise CaskUnavailableError.new(token, "'#{path}' is not readable.") unless path.readable?
  raise CaskUnavailableError.new(token, "'#{path}' is not a file.")   unless path.file?

  @content = path.read(encoding: "UTF-8")
  @config = config

  return FromAPILoader.new(token, from_json: JSON.parse(@content)).load(config:) if path.extname == ".json"

  begin
    instance_eval(content, path).tap do |cask|
      raise CaskUnreadableError.new(token, "'#{path}' does not contain a cask.") unless cask.is_a?(Cask)
    end
  rescue NameError, ArgumentError, ScriptError => e
    error = CaskUnreadableError.new(token, e.message)
    error.set_backtrace e.backtrace
    raise error
  end
end