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_once, #opoo_outside_github_actions, #opoo_without_github_actions_annotation, #pretty_cannot_install, #pretty_deprecated, #pretty_disabled, #pretty_duration, #pretty_install_status, #pretty_installed, #pretty_removed, #pretty_uninstalled, #pretty_unmarked, #pretty_upgradable, #pretty_warning

Constructor Details

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



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

def initialize(path, token: nil)
  super()

  path = Pathname(path).expand_path

  @token = T.let(CaskLoader.token_from_path(path), String)
  @path = T.let(path, Pathname)
  @tap = T.let(Tap.from_path(path), T.nilable(Tap))
  @from_installed_caskfile = T.let(false, T::Boolean)
  @api_fallback = T.let(true, T::Boolean)
end

Instance Attribute Details

#from_installed_caskfile=(value) ⇒ void (writeonly)

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.

This method returns an undefined value.



118
119
120
# File 'cask/cask_loader.rb', line 118

def from_installed_caskfile=(value)
  @from_installed_caskfile = value
end

#path ⇒ Pathname (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:



115
116
117
# File 'cask/cask_loader.rb', line 115

def path
  @path
end

#token ⇒ String (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:



112
113
114
# File 'cask/cask_loader.rb', line 112

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)


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

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:

  • _ref (String)
  • warn (Boolean) (defaults to: false)

Returns:



98
99
100
101
# File 'cask/cask_loader.rb', line 98

def self.try_new(_ref, warn: false)
  CaskLoader.odeprecated "Cask::CaskLoader::FromPathLoader.try_new", "Cask::CaskLoader.from_path"
  nil
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:



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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
# File 'cask/cask_loader.rb', line 134

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?

  source_tap = tap if path.extname == ".rb"
  Homebrew::Trust.require_trusted_cask!(token, path, tap: source_tap)

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

  if !self.class.invalid_path?(path, valid_extnames: %w[.json]) &&
     (from_json = JSON.parse(@content)) &&
     from_json.is_a?(Hash) &&
     (@from_installed_caskfile || from_json.present?)
    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:,
        api_fallback:            @api_fallback,
      ).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
      cask = instance_eval(content, path.to_s)
      raise CaskUnreadableError.new(token, "'#{path}' does not contain a cask.") unless cask.is_a?(Cask)

      cask
    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