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

Methods included from Utils::Output::Mixin

#issue_reporting_message, #odebug, #odeprecated, #odie, #odisabled, #ofail, #oh1, #oh1_title, #ohai, #ohai_title, #onoe, #opoo, #opoo_outside_github_actions, #opoo_without_github_actions_annotation, #pretty_deprecated, #pretty_disabled, #pretty_duration, #pretty_install_status, #pretty_installed, #pretty_outdated, #pretty_uninstalled, #pretty_upgradable

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:



151
152
153
154
155
156
157
158
159
160
# File 'cask/cask_loader.rb', line 151

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

  path = Pathname(path).expand_path

  @token = T.let(path.basename(path.extname).basename(".internal").to_s, String)
  @path = T.let(path, Pathname)
  @tap = T.let(Tap.from_path(path) || Homebrew::API.tap_from_source_download(path), T.nilable(Tap))
  @from_installed_caskfile = T.let(false, T::Boolean)
end

Instance Attribute Details

#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:



148
149
150
# File 'cask/cask_loader.rb', line 148

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:



145
146
147
# File 'cask/cask_loader.rb', line 145

def token
  @token
end

Class Method Details

.invalid_path?(pathname, valid_extnames: %w[.rb .json])) ⇒ 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.

Parameters:

Returns:

  • (Boolean)


137
138
139
140
141
142
# File 'cask/cask_loader.rb', line 137

def self.invalid_path?(pathname, valid_extnames: %w[.rb .json])
  return true if valid_extnames.exclude?(pathname.extname)

  @invalid_basenames ||= T.let(%w[INSTALL_RECEIPT.json sbom.spdx.json].freeze, T.nilable(T::Array[String]))
  @invalid_basenames.include?(pathname.basename.to_s)
end

.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)


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

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

  return unless path.expand_path.exist?
  return if invalid_path?(path)
  return unless ::Utils::Path.loadable_package_path?(path, :cask)

  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:



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'cask/cask_loader.rb', line 163

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?

  Homebrew::Trust.require_trusted_cask!(token, path)

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

  if !self.class.invalid_path?(path, valid_extnames: %w[.json]) &&
     (from_json = JSON.parse(@content).presence) &&
     from_json.is_a?(Hash)
    begin
      from_internal_json = path.to_s.end_with?(".internal.json")
      return FromAPILoader.new(
        token,
        from_json:,
        path:,
        from_installed_caskfile: @from_installed_caskfile,
        from_internal_json:,
      ).load(config:)
    rescue CaskInvalidError => e
      if @from_installed_caskfile
        error = CaskUnreadableError.new(token, e.reason)
        error.set_backtrace e.backtrace
        raise error
      end
      raise
    end
  end

  begin
    ENV.clear_sensitive_environment_for_eval! do
      instance_eval(content, path.to_s).tap do |cask|
        raise CaskUnreadableError.new(token, "'#{path}' does not contain a cask.") unless cask.is_a?(Cask)
      end
    end
  rescue NameError, ArgumentError, ScriptError => e
    error = CaskUnreadableError.new(token, e.message)
    error.set_backtrace e.backtrace
    raise error
  rescue CaskInvalidError => e # e.g. NoMethodError from removed DSL methods, wrapped
    # as CaskInvalidError by Cask#refresh before reaching here.
    if @from_installed_caskfile
      error = CaskUnreadableError.new(token, e.reason)
      error.set_backtrace e.backtrace
      raise error
    end
    raise
  end
end