Class: Resource Private

Inherits:
Downloadable show all
Includes:
FileUtils, OnSystem::MacOSAndLinux
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

Go, PatchResource

Defined Under Namespace

Classes: Go, Partial, PatchResource

Instance Attribute Summary collapse

Attributes inherited from Downloadable

#checksum, #mirrors

Instance Method Summary collapse

Methods included from OnSystem::MacOSAndLinux

included

Methods inherited from Downloadable

#cached_download, #clear_cache, #downloaded?, #downloader, #verify_download_integrity

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)


25
26
27
28
29
30
31
32
33
34
# File 'resource.rb', line 25

def initialize(name = nil, &block)
  super()
  # Ensure this is synced with `initialize_dup` and `freeze` (excluding simple objects like integers and booleans)
  @name = name
  @patches = []
  @livecheck = Livecheck.new(self)
  @livecheckable = false
  @insecure = false
  instance_eval(&block) if block
end

Instance Attribute Details

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



17
18
19
# File 'resource.rb', line 17

def checksum=(value)
  @checksum = value
end

#download_strategyObject

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.



18
19
20
# File 'resource.rb', line 18

def download_strategy
  @download_strategy
end

#livecheck(&block) ⇒ Object

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.

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


160
161
162
163
164
165
# File 'resource.rb', line 160

def livecheck(&block)
  return @livecheck unless block

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

#nameObject

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.



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

def name
  @name
end

#ownerObject

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.



16
17
18
# File 'resource.rb', line 16

def owner
  @owner
end

#patchesObject (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.



16
17
18
# File 'resource.rb', line 16

def patches
  @patches
end

#source_modified_timeObject (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.



16
17
18
# File 'resource.rb', line 16

def source_modified_time
  @source_modified_time
end

Instance Method Details

#apply_patchesObject

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.



109
110
111
112
113
114
# File 'resource.rb', line 109

def apply_patches
  return if patches.empty?

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

#download_nameObject

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.



76
77
78
79
80
81
# File 'resource.rb', line 76

def download_name
  return owner.name if name.nil?
  return escaped_name if owner.nil?

  "#{owner.name}--#{escaped_name}"
end

#escaped_nameObject

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.

Removes /s from resource names; this allows Go package names to be used as resource names without confusing software that interacts with #download_name, e.g. github.com/foo/bar.



72
73
74
# File 'resource.rb', line 72

def escaped_name
  name.tr("/", "-")
end

#fetch(verify_download_integrity: true) ⇒ Object

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.



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

def fetch(verify_download_integrity: true)
  fetch_patches

  super(verify_download_integrity:)
end

#fetch_patches(skip_downloaded: false) ⇒ Object

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.



103
104
105
106
107
# File 'resource.rb', line 103

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

#files(*files) ⇒ Object

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.



139
140
141
# File 'resource.rb', line 139

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

#freezeObject

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.



43
44
45
46
47
48
# File 'resource.rb', line 43

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

#initialize_dup(other) ⇒ Object

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.



36
37
38
39
40
41
# File 'resource.rb', line 36

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

#livecheckable?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, and is used by livecheck.

Returns:

  • (Boolean)


170
171
172
# File 'resource.rb', line 170

def livecheckable?
  @livecheckable == true
end

#mirror(val) ⇒ Object

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.



204
205
206
# File 'resource.rb', line 204

def mirror(val)
  mirrors << val
end

#patch(strip = :p1, src = nil, &block) ⇒ Object

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.



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

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

#prepare_patchesObject

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.



99
100
101
# File 'resource.rb', line 99

def prepare_patches
  patches.grep(DATAPatch) { |p| p.path = owner.owner.path }
end

#sha256(val) ⇒ Object

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.



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

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

#specsObject

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.



217
218
219
# File 'resource.rb', line 217

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

#stage(target = nil, debug_symbols: false, &block) ⇒ Object

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.

Raises:

  • (ArgumentError)


89
90
91
92
93
94
95
96
97
# File 'resource.rb', line 89

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

  prepare_patches
  fetch_patches(skip_downloaded: true)
  fetch unless downloaded?

  unpack(target, debug_symbols:, &block)
end

#unpack(target = nil, debug_symbols: false) ⇒ Object

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.

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.



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'resource.rb', line 120

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

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.



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'resource.rb', line 178

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
end

#usingObject

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.



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

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:



193
194
195
196
197
198
199
200
201
202
# File 'resource.rb', line 193

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