Class: Resource Private

Inherits:
Object show all
Includes:
Downloadable, FileUtils, OnSystem::MacOSAndLinux, Utils::Output::Mixin
Defined in:
resource.rb,
sorbet/rbi/dsl/resource.rbi

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.

Resource is the fundamental representation of an external resource. The primary formula download, along with other declared resources, are instances of this class.

Defined Under Namespace

Classes: BottleManifest, Formula, Local, Partial, Patch

Constant Summary collapse

Owner =

This constant is part of a private API. This constant may only be used in the Homebrew/brew repository. Third parties should avoid using this constant if possible, as it may be removed or changed without warning.

T.type_alias { T.any(Cask::Cask, ::Formula, Resource, SoftwareSpec, Homebrew::FormulaCreator) }

Instance Attribute Summary collapse

Attributes included from Downloadable

#checksum, #mirrors, #phase

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_uninstalled, #pretty_unmarked, #pretty_upgradable, #pretty_warning

Methods included from OnSystem::MacOSAndLinux

included

Methods included from Downloadable

#cached_download, #clear_cache, #download_queue_message, #download_queue_name, #downloaded!, #downloaded?, #downloaded_and_valid?, #downloader, #downloading!, #extracting!, #fetched_size, #stage_from_download_queue, #stage_from_download_queue?, #staged_path_from_download_queue, #total_size, verification_cache, #verified!, #verify_download_integrity, #verifying!

Methods included from Context

current, current=, #debug?, #deferred_environment_expansion?, #quiet?, #verbose?, #with_context

Constructor Details

#initialize(name = nil, test: false, &block) ⇒ 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:

  • name (String, nil) (defaults to: nil)
  • test (Boolean) (defaults to: false)
  • block (T.proc.bind(Resource).void, nil)


48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'resource.rb', line 48

def initialize(name = nil, test: false, &block)
  super()
  # Generally ensure this is synced with `initialize_dup` and `freeze`
  # (excluding simple objects like integers & booleans, weak refs like `owner` or permafrozen objects)
  @name = name
  @test = test
  @source_modified_time = T.let(nil, T.nilable(Time))
  @patches = T.let([], T::Array[T.any(EmbeddedPatch, ExternalPatch)])
  @owner = T.let(nil, T.nilable(Owner))
  @livecheck = T.let(Livecheck.new(self), Livecheck)
  @livecheck_defined = T.let(false, T::Boolean)
  @insecure = T.let(false, T::Boolean)
  instance_eval(&block) if block
end

Instance Attribute Details

#checksum=(value) ⇒ Checksum? (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.

Returns:



32
33
34
# File 'resource.rb', line 32

def checksum=(value)
  @checksum = value
end

#download_strategy ⇒ T::Class[AbstractDownloadStrategy]

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:



35
36
37
# File 'resource.rb', line 35

def download_strategy
  @download_strategy || super
end

#name ⇒ String?

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.

Formula name must be set after the DSL, as we have no access to the formula name before initialization of the formula.

Returns:



45
46
47
# File 'resource.rb', line 45

def name
  @name
end

#owner ⇒ Owner?

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:



29
30
31
# File 'resource.rb', line 29

def owner
  @owner
end

#patches ⇒ Array<EmbeddedPatch, ExternalPatch> (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.



26
27
28
# File 'resource.rb', line 26

def patches
  @patches
end

#source_modified_time ⇒ Time? (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:



23
24
25
# File 'resource.rb', line 23

def source_modified_time
  @source_modified_time
end

Instance Method Details

#apply_patches ⇒ 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.

This method returns an undefined value.



142
143
144
145
146
147
# File 'resource.rb', line 142

def apply_patches
  return if patches.empty?

  ohai "Patching #{name}"
  patches.each(&:apply)
end

#download_queue_type ⇒ String

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:



92
# File 'resource.rb', line 92

def download_queue_type = "Resource"

#fetch(verify_download_integrity: true, timeout: nil, quiet: false, skip_patches: false) ⇒ Pathname

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:

  • verify_download_integrity (Boolean) (defaults to: true)
  • timeout (Integer, Float, nil) (defaults to: nil)
  • quiet (Boolean) (defaults to: false)
  • skip_patches (Boolean) (defaults to: false)

Returns:



221
222
223
224
225
# File 'resource.rb', line 221

def fetch(verify_download_integrity: true, timeout: nil, quiet: false, skip_patches: false)
  fetch_patches unless skip_patches

  super(verify_download_integrity:, timeout:, quiet:)
end

#fetch_patches(skip_downloaded: false) ⇒ 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.

This method returns an undefined value.

Parameters:

  • skip_downloaded (Boolean) (defaults to: false)


135
136
137
138
139
# File 'resource.rb', line 135

def fetch_patches(skip_downloaded: false)
  external_patches = patches.grep(ExternalPatch)
  external_patches.reject!(&:downloaded?) if skip_downloaded
  external_patches.each(&:fetch)
end

#files(*files) ⇒ Partial

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:

  • files (T.untyped)

Returns:



208
209
210
# File 'resource.rb', line 208

def files(*files)
  Partial.new(self, files)
end

#freeze ⇒ T.self_type

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:

  • (T.self_type)


78
79
80
81
82
83
# File 'resource.rb', line 78

def freeze
  @name.freeze
  @patches.freeze
  @livecheck.freeze
  super
end

#initialize_dup(other) ⇒ 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.

This method returns an undefined value.

Parameters:



70
71
72
73
74
75
# File 'resource.rb', line 70

def initialize_dup(other)
  super
  @name = @name.dup
  @patches = @patches.dup
  @livecheck = @livecheck.dup
end

#livecheck(&block) ⇒ T.untyped

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.

Livecheck can be used to check for newer versions of the software. This method evaluates the DSL specified in the livecheck block of the Resource (if it exists) and sets the instance variables of a Livecheck object accordingly. This is used by brew livecheck to check for newer versions of the software.

Example

livecheck do
  url "https://example.com/foo/releases"
  regex /foo-(\d+(?:\.\d+)+)\.tar/
end

Parameters:

Returns:

  • (T.untyped)


242
243
244
245
246
247
# File 'resource.rb', line 242

def livecheck(&block)
  return @livecheck unless block

  @livecheck_defined = true
  @livecheck.instance_eval(&block)
end

#livecheck_defined? ⇒ 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.

Whether a livecheck specification is defined or not.

It returns true when a livecheck block is present in the Resource and false otherwise.

Returns:

  • (Boolean)


254
255
256
# File 'resource.rb', line 254

def livecheck_defined?
  @livecheck_defined == true
end

#mirror(val) ⇒ Array<String>

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:



292
293
294
# File 'resource.rb', line 292

def mirror(val)
  mirrors << val
end

#on_arch_conditional(arm: nil, intel: nil) ⇒ T.type_parameter(:U)?

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:

  • arm (T.type_parameter(:U), nil) (defaults to: nil)
  • intel (T.type_parameter(:U), nil) (defaults to: nil)

Returns:

  • (T.type_parameter(:U), nil)


16
# File 'sorbet/rbi/dsl/resource.rbi', line 16

def on_arch_conditional(arm: nil, intel: nil); end

#on_arm(&block) ⇒ T.type_parameter(:U)?

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:

  • block (T.proc.returns(T.type_parameter(:U)))

Returns:

  • (T.type_parameter(:U), nil)


24
# File 'sorbet/rbi/dsl/resource.rbi', line 24

def on_arm(&block); end

#on_big_sur(or_condition = nil, &block) ⇒ T.type_parameter(:U)?

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:

  • or_condition (Symbol, nil) (defaults to: nil)
  • block (T.proc.returns(T.type_parameter(:U)))

Returns:

  • (T.type_parameter(:U), nil)


33
# File 'sorbet/rbi/dsl/resource.rbi', line 33

def on_big_sur(or_condition = nil, &block); end

#on_golden_gate(or_condition = nil, &block) ⇒ T.type_parameter(:U)?

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:

  • or_condition (Symbol, nil) (defaults to: nil)
  • block (T.proc.returns(T.type_parameter(:U)))

Returns:

  • (T.type_parameter(:U), nil)


42
# File 'sorbet/rbi/dsl/resource.rbi', line 42

def on_golden_gate(or_condition = nil, &block); end

#on_intel(&block) ⇒ T.type_parameter(:U)?

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:

  • block (T.proc.returns(T.type_parameter(:U)))

Returns:

  • (T.type_parameter(:U), nil)


50
# File 'sorbet/rbi/dsl/resource.rbi', line 50

def on_intel(&block); end

#on_linux(&block) ⇒ T.type_parameter(:U)?

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:

  • block (T.proc.returns(T.type_parameter(:U)))

Returns:

  • (T.type_parameter(:U), nil)


58
# File 'sorbet/rbi/dsl/resource.rbi', line 58

def on_linux(&block); end

#on_macos(&block) ⇒ T.type_parameter(:U)?

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:

  • block (T.proc.returns(T.type_parameter(:U)))

Returns:

  • (T.type_parameter(:U), nil)


66
# File 'sorbet/rbi/dsl/resource.rbi', line 66

def on_macos(&block); end

#on_monterey(or_condition = nil, &block) ⇒ T.type_parameter(:U)?

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:

  • or_condition (Symbol, nil) (defaults to: nil)
  • block (T.proc.returns(T.type_parameter(:U)))

Returns:

  • (T.type_parameter(:U), nil)


75
# File 'sorbet/rbi/dsl/resource.rbi', line 75

def on_monterey(or_condition = nil, &block); end

#on_sequoia(or_condition = nil, &block) ⇒ T.type_parameter(:U)?

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:

  • or_condition (Symbol, nil) (defaults to: nil)
  • block (T.proc.returns(T.type_parameter(:U)))

Returns:

  • (T.type_parameter(:U), nil)


84
# File 'sorbet/rbi/dsl/resource.rbi', line 84

def on_sequoia(or_condition = nil, &block); end

#on_sonoma(or_condition = nil, &block) ⇒ T.type_parameter(:U)?

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:

  • or_condition (Symbol, nil) (defaults to: nil)
  • block (T.proc.returns(T.type_parameter(:U)))

Returns:

  • (T.type_parameter(:U), nil)


93
# File 'sorbet/rbi/dsl/resource.rbi', line 93

def on_sonoma(or_condition = nil, &block); end

#on_system(linux, macos:, &block) ⇒ T.type_parameter(:U)?

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:

  • linux (Symbol)
  • macos (Symbol)
  • block (T.proc.returns(T.type_parameter(:U)))

Returns:

  • (T.type_parameter(:U), nil)


103
# File 'sorbet/rbi/dsl/resource.rbi', line 103

def on_system(linux, macos:, &block); end

#on_system_conditional(macos: nil, linux: nil) ⇒ T.type_parameter(:U)?

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:

  • macos (T.type_parameter(:U), nil) (defaults to: nil)
  • linux (T.type_parameter(:U), nil) (defaults to: nil)

Returns:

  • (T.type_parameter(:U), nil)


112
# File 'sorbet/rbi/dsl/resource.rbi', line 112

def on_system_conditional(macos: nil, linux: nil); end

#on_tahoe(or_condition = nil, &block) ⇒ T.type_parameter(:U)?

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:

  • or_condition (Symbol, nil) (defaults to: nil)
  • block (T.proc.returns(T.type_parameter(:U)))

Returns:

  • (T.type_parameter(:U), nil)


121
# File 'sorbet/rbi/dsl/resource.rbi', line 121

def on_tahoe(or_condition = nil, &block); end

#on_ventura(or_condition = nil, &block) ⇒ T.type_parameter(:U)?

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:

  • or_condition (Symbol, nil) (defaults to: nil)
  • block (T.proc.returns(T.type_parameter(:U)))

Returns:

  • (T.type_parameter(:U), nil)


130
# File 'sorbet/rbi/dsl/resource.rbi', line 130

def on_ventura(or_condition = nil, &block); end

#patch(strip = :p1, src = nil, &block) ⇒ Array<EmbeddedPatch, ExternalPatch>

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:



303
304
305
306
# File 'resource.rb', line 303

def patch(strip = :p1, src = nil, &block)
  p = ::Patch.create(strip, src, &block)
  patches << p
end

#prepare_patches ⇒ 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.

This method returns an undefined value.



130
131
132
# File 'resource.rb', line 130

def prepare_patches
  patches.grep(DATAPatch) { |p| p.path = T.cast(T.cast(owner, SoftwareSpec).owner, ::Formula).path }
end

#sha256(val) ⇒ Checksum

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:



259
260
261
# File 'resource.rb', line 259

def sha256(val)
  @checksum = Checksum.new(val)
end

#specs ⇒ Hash{Symbol => T.untyped}

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:



314
315
316
# File 'resource.rb', line 314

def specs
  @url&.specs || {}.freeze
end

#stage(target = nil, debug_symbols: false, staging_path: nil, staged: false, &block) ⇒ void

This method returns an undefined value.

Verifies download and unpacks it. The block may call |resource, staging| staging.retain! to retain the staging directory. Subclasses that override stage should implement the tmp dir using Mktemp so that works with all subtypes.

Parameters:

  • target (String, Pathname, nil) (defaults to: nil)
  • debug_symbols (Boolean) (defaults to: false)
  • staging_path (Pathname, nil) (defaults to: nil)
  • staged (Boolean) (defaults to: false)
  • block (T.proc.params(arg0: ResourceStageContext).void, nil)

Raises:

  • (ArgumentError)


109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'resource.rb', line 109

def stage(target = nil, debug_symbols: false, staging_path: nil, staged: false, &block)
  raise ArgumentError, "Target directory or block is required" if !target && !block_given?

  prepare_patches
  fetch_patches(skip_downloaded: true)
  begin
    if !downloaded?
      fetch
    elsif !staged && checksum.present?
      verify_download_integrity(cached_download)
    end
  rescue ChecksumMismatchError
    # Remove the known-bad download so the next attempt fetches it again.
    clear_cache
    raise
  end

  unpack(target, debug_symbols:, staging_path:, staged:, &block)
end

#test? ⇒ Boolean

Whether this resource is only needed for formula tests.

Returns:

  • (Boolean)


67
# File 'resource.rb', line 67

def test? = @test

#unpack(target = nil, debug_symbols: false, staging_path: nil, staged: false, &block) ⇒ 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.

This method returns an undefined value.

If a target is given, unpack there; else unpack to a temp folder. If block is given, yield to that block with |stage|, where stage is a ResourceStageContext. A target or a block must be given, but not both. With staging_path, unpack into that directory rather than a fresh temporary one and, with staged too, reuse its already unpacked and patched contents.

Parameters:

  • target (String, Pathname, nil) (defaults to: nil)
  • debug_symbols (Boolean) (defaults to: false)
  • staging_path (Pathname, nil) (defaults to: nil)
  • staged (Boolean) (defaults to: false)
  • block (T.proc.params(arg0: ResourceStageContext).void, nil)


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
# File 'resource.rb', line 165

def unpack(target = nil, debug_symbols: false, staging_path: nil, staged: false, &block)
  current_working_directory = Pathname.pwd
  stage_resource(download_name, debug_symbols:, staging_path:) do |staging|
    # A formula's `fetch` adds files to the shared staging directory, so
    # record the unpacked source's modification time for the build phase.
    source_modified_time_path = (staging_path/".source_modified_time" if staging_path)
    stage_unpacked = proc do
      source_modified_time = if staged && source_modified_time_path&.exist?
        Time.at(source_modified_time_path.read.to_i)
      else
        downloader.source_modified_time
      end
      if source_modified_time_path && !staged
        source_modified_time_path.write(source_modified_time.to_i.to_s)
        # Don't dirty the git tree for git clones, which some builds embed in their version.
        git_dir = staging_path/".git"
        if git_dir.directory? && !git_dir.symlink?
          (git_dir/"info").mkpath
          (git_dir/"info/exclude").open("a") { |file| file.puts source_modified_time_path.basename }
        end
      end
      @source_modified_time = source_modified_time.freeze
      apply_patches unless staged
      if block
        yield(ResourceStageContext.new(self, staging))
      elsif target
        target = Pathname(target)
        target = current_working_directory/target if target.relative?
        target.install Pathname.pwd.children
      end
    end

    if staged
      downloader.chdir(&stage_unpacked)
    else
      downloader.stage(&stage_unpacked)
    end
  end
end

#url(val = nil, **specs) ⇒ String?

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:

  • val (String, nil) (defaults to: nil)
  • specs (T.anything)

Returns:



264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'resource.rb', line 264

def url(val = nil, **specs)
  return @url&.to_s if val.nil?

  specs = specs.dup
  # Don't allow this to be set.
  specs.delete(:insecure)

  specs[:insecure] = true if @insecure

  @url = URL.new(val, specs)
  @downloader = nil
  @download_strategy = @url.download_strategy
  @url.to_s
end

#using ⇒ T::Class[AbstractDownloadStrategy], ...

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:



309
310
311
# File 'resource.rb', line 309

def using
  @url&.using
end

#version(val = nil) ⇒ Version?

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:



280
281
282
283
284
285
286
287
288
289
# File 'resource.rb', line 280

def version(val = nil)
  return super() if val.nil?

  @version = case val
  when String
    val.blank? ? Version::NULL : Version.new(val)
  when Version
    val
  end
end