Class: Resource

Inherits:
Object
  • Object
show all
Includes:
Context, FileUtils
Defined in:
Library/Homebrew/resource.rb,
Library/Homebrew/extend/os/mac/resource.rb,
Library/Homebrew/extend/os/linux/resource.rb

Overview

typed: true frozen_string_literal: true

Direct Known Subclasses

Go, PatchResource

Defined Under Namespace

Classes: Go, Partial, PatchResource

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Context

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

Constructor Details

#initialize(name = nil, &block) ⇒ Resource

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Resource.


26
27
28
29
30
31
32
33
34
35
36
# File 'Library/Homebrew/resource.rb', line 26

def initialize(name = nil, &block)
  @name = name
  @url = nil
  @version = nil
  @mirrors = []
  @specs = {}
  @checksum = nil
  @using = nil
  @patches = []
  instance_eval(&block) if block_given?
end

Instance Attribute Details

#checksumObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


20
21
22
# File 'Library/Homebrew/resource.rb', line 20

def checksum
  @checksum
end

#download_strategyObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


20
21
22
# File 'Library/Homebrew/resource.rb', line 20

def download_strategy
  @download_strategy
end

#mirrorsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


18
19
20
# File 'Library/Homebrew/resource.rb', line 18

def mirrors
  @mirrors
end

#nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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


24
25
26
# File 'Library/Homebrew/resource.rb', line 24

def name
  @name
end

#ownerObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


18
19
20
# File 'Library/Homebrew/resource.rb', line 18

def owner
  @owner
end

#patchesObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


18
19
20
# File 'Library/Homebrew/resource.rb', line 18

def patches
  @patches
end

#source_modified_timeObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


18
19
20
# File 'Library/Homebrew/resource.rb', line 18

def source_modified_time
  @source_modified_time
end

#specsObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


18
19
20
# File 'Library/Homebrew/resource.rb', line 18

def specs
  @specs
end

#usingObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


18
19
20
# File 'Library/Homebrew/resource.rb', line 18

def using
  @using
end

#version(val = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


171
172
173
174
175
176
# File 'Library/Homebrew/resource.rb', line 171

def version(val = nil)
  @version ||= begin
    version = detect_version(val)
    version.null? ? nil : version
  end
end

Instance Method Details

#apply_patchesObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


100
101
102
103
104
105
# File 'Library/Homebrew/resource.rb', line 100

def apply_patches
  return if patches.empty?

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

#cached_downloadObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


66
67
68
# File 'Library/Homebrew/resource.rb', line 66

def cached_download
  downloader.cached_location
end

#clear_cacheObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


70
71
72
# File 'Library/Homebrew/resource.rb', line 70

def clear_cache
  downloader.clear_cache
end

#download_nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


55
56
57
58
59
60
# File 'Library/Homebrew/resource.rb', line 55

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

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

#downloaded?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)

62
63
64
# File 'Library/Homebrew/resource.rb', line 62

def downloaded?
  cached_download.exist?
end

#downloaderObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


43
44
45
46
# File 'Library/Homebrew/resource.rb', line 43

def downloader
  @downloader ||= download_strategy.new(url, download_name, version,
                                        mirrors: mirrors.dup, **specs)
end

#escaped_nameObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.


51
52
53
# File 'Library/Homebrew/resource.rb', line 51

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

#fetch(verify_download_integrity: true) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'Library/Homebrew/resource.rb', line 131

def fetch(verify_download_integrity: true)
  HOMEBREW_CACHE.mkpath

  fetch_patches

  begin
    downloader.fetch
  rescue ErrorDuringExecution, CurlDownloadStrategyError => e
    raise DownloadError.new(self, e)
  end

  download = cached_download
  verify_download_integrity(download) if verify_download_integrity
  download
end

#fetch_patches(skip_downloaded: false) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


94
95
96
97
98
# File 'Library/Homebrew/resource.rb', line 94

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. You should avoid using this method if possible, as it may be removed or be changed in the future.


127
128
129
# File 'Library/Homebrew/resource.rb', line 127

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

#mirror(val) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


178
179
180
# File 'Library/Homebrew/resource.rb', line 178

def mirror(val)
  mirrors << val
end

#on_linux(&_block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Block only executed on Linux. No-op on macOS.

on_linux do
  url "linux_only_url"
end

197
# File 'Library/Homebrew/resource.rb', line 197

def on_linux(&_block); end

#on_macos(&_block) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Block only executed on macOS. No-op on Linux.

on_macos do
  url "mac_only_url"
end

191
# File 'Library/Homebrew/resource.rb', line 191

def on_macos(&_block); end

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

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


182
183
184
185
# File 'Library/Homebrew/resource.rb', line 182

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. You should avoid using this method if possible, as it may be removed or be changed in the future.


90
91
92
# File 'Library/Homebrew/resource.rb', line 90

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

#stage(target = nil, &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)

80
81
82
83
84
85
86
87
88
# File 'Library/Homebrew/resource.rb', line 80

def stage(target = nil, &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, &block)
end

#unpack(target = nil) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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.


111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'Library/Homebrew/resource.rb', line 111

def unpack(target = nil)
  mktemp(download_name) do |staging|
    downloader.stage
    @source_modified_time = downloader.source_modified_time
    apply_patches
    if block_given?
      yield ResourceStageContext.new(self, staging)
    elsif target
      target = Pathname(target)
      target.install Pathname.pwd.children
    end
  end
end

#url(val = nil, **specs) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


162
163
164
165
166
167
168
169
# File 'Library/Homebrew/resource.rb', line 162

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

  @url = val
  @specs.merge!(specs)
  @using = @specs.delete(:using)
  @download_strategy = DownloadStrategyDetector.detect(url, using)
end

#verify_download_integrity(fn) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


147
148
149
150
151
152
153
154
155
156
# File 'Library/Homebrew/resource.rb', line 147

def verify_download_integrity(fn)
  if fn.file?
    ohai "Verifying #{fn.basename} checksum" if verbose?
    fn.verify_checksum(checksum)
  end
rescue ChecksumMissingError
  opoo "Cannot verify integrity of #{fn.basename}"
  puts "A checksum was not provided for this resource."
  puts "For your reference the SHA-256 is: #{fn.sha256}"
end