Class: Resource

Inherits:
Object show all
Includes:
FileUtils
Defined in:
Library/Homebrew/resource.rb

Overview

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, Patch

Defined Under Namespace

Classes: Download, Go, Partial, Patch

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FileUtils

#make, mkdir, mktemp, #rake, #ruby, #scons, #xcodebuild

Constructor Details

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



42
43
44
45
46
47
48
49
50
51
# File 'Library/Homebrew/resource.rb', line 42

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

Instance Attribute Details

#checksumObject

Returns the value of attribute checksum



14
15
16
# File 'Library/Homebrew/resource.rb', line 14

def checksum
  @checksum
end

#download_strategyObject

Returns the value of attribute download_strategy



14
15
16
# File 'Library/Homebrew/resource.rb', line 14

def download_strategy
  @download_strategy
end

#mirrorsObject (readonly)

Returns the value of attribute mirrors



12
13
14
# File 'Library/Homebrew/resource.rb', line 12

def mirrors
  @mirrors
end

#nameObject

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



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

def name
  @name
end

#ownerObject

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



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

def owner
  @owner
end

#source_modified_timeObject (readonly)

Returns the value of attribute source_modified_time



12
13
14
# File 'Library/Homebrew/resource.rb', line 12

def source_modified_time
  @source_modified_time
end

#specsObject (readonly)

Returns the value of attribute specs



12
13
14
# File 'Library/Homebrew/resource.rb', line 12

def specs
  @specs
end

#usingObject (readonly)

Returns the value of attribute using



12
13
14
# File 'Library/Homebrew/resource.rb', line 12

def using
  @using
end

#version(val = nil) ⇒ Object



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

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

Instance Method Details

#cached_downloadObject



68
69
70
# File 'Library/Homebrew/resource.rb', line 68

def cached_download
  downloader.cached_location
end

#clear_cacheObject



72
73
74
# File 'Library/Homebrew/resource.rb', line 72

def clear_cache
  downloader.clear_cache
end

#download_nameObject



64
65
66
# File 'Library/Homebrew/resource.rb', line 64

def download_name
  name.nil? ? owner.name : "#{owner.name}--#{escaped_name}"
end

#downloaderObject



53
54
55
# File 'Library/Homebrew/resource.rb', line 53

def downloader
  download_strategy.new(download_name, Download.new(self))
end

#escaped_nameObject

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



60
61
62
# File 'Library/Homebrew/resource.rb', line 60

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

#fetchObject



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

def fetch
  HOMEBREW_CACHE.mkpath

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

  cached_download
end

#files(*files) ⇒ Object



108
109
110
# File 'Library/Homebrew/resource.rb', line 108

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

#mirror(val) ⇒ Object



154
155
156
# File 'Library/Homebrew/resource.rb', line 154

def mirror(val)
  mirrors << val
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 FileUtils.mktemp so that works with all subtypes.



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

def stage(target = nil, &block)
  unless target || block
    raise ArgumentError, "target directory or block is required"
  end

  verify_download_integrity(fetch)
  unpack(target, &block)
end

#unpack(target = nil) ⇒ Object

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 ResourceStagingContext. A target or a block must be given, but not both.



93
94
95
96
97
98
99
100
101
102
103
104
# File 'Library/Homebrew/resource.rb', line 93

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

#url(val = nil, specs = {}) ⇒ Object



139
140
141
142
143
144
145
# File 'Library/Homebrew/resource.rb', line 139

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



124
125
126
127
128
129
130
131
132
133
# File 'Library/Homebrew/resource.rb', line 124

def verify_download_integrity(fn)
  if fn.file?
    ohai "Verifying #{fn.basename} checksum" if ARGV.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 SHA256 is: #{fn.sha256}"
end