Class: Resource Private

Inherits:
Object show all
Includes:
Downloadable, FileUtils, OnSystem::MacOSAndLinux, Utils::Output::Mixin
Defined in:
resource.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.

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

Direct Known Subclasses

BottleManifest, Formula, Go, Local, Patch

Defined Under Namespace

Classes: BottleManifest, Formula, Go, 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

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

Methods included from OnSystem::MacOSAndLinux

included, #on_arch_conditional, #on_macos, #on_system_conditional

Methods included from Downloadable

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

Methods included from Context

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

Constructor Details

#initialize(name = nil, &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)
  • block (T.proc.bind(Resource).void, nil)


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

def initialize(name = nil, &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 = T.let(name, T.nilable(String))
  @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_strategyT::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

#nameString?

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

#ownerOwner?

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

#patchesArray<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_timeTime? (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_patchesvoid

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.



123
124
125
126
127
128
# File 'resource.rb', line 123

def apply_patches
  return if patches.empty?

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

#download_queue_typeString

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:



85
# File 'resource.rb', line 85

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:



174
175
176
177
178
# File 'resource.rb', line 174

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)


116
117
118
119
120
# File 'resource.rb', line 116

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:



161
162
163
# File 'resource.rb', line 161

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

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


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

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:



63
64
65
66
67
68
# File 'resource.rb', line 63

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)


195
196
197
198
199
200
# File 'resource.rb', line 195

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)


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

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:



245
246
247
# File 'resource.rb', line 245

def mirror(val)
  mirrors << val
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:



256
257
258
259
# File 'resource.rb', line 256

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

#prepare_patchesvoid

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.



111
112
113
# File 'resource.rb', line 111

def prepare_patches
  patches.grep(DATAPatch) { |p| p.path = T.cast(T.cast(T.must(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:



212
213
214
# File 'resource.rb', line 212

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

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



267
268
269
# File 'resource.rb', line 267

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

#stage(target = nil, debug_symbols: 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:

Raises:

  • (ArgumentError)


100
101
102
103
104
105
106
107
108
# File 'resource.rb', line 100

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

  prepare_patches
  fetch_patches(skip_downloaded: true)
  fetch unless downloaded?

  unpack(target, debug_symbols:, &block)
end

#unpack(target = nil, debug_symbols: 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.

Parameters:



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'resource.rb', line 141

def unpack(target = nil, debug_symbols: false, &block)
  current_working_directory = Pathname.pwd
  stage_resource(download_name, debug_symbols:) do |staging|
    downloader.stage do
      @source_modified_time = downloader.source_modified_time.freeze
      apply_patches
      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
  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:



217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'resource.rb', line 217

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

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



262
263
264
# File 'resource.rb', line 262

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:



233
234
235
236
237
238
239
240
241
242
# File 'resource.rb', line 233

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