stage: fix block signature back-compatibility under Ruby 1.8.7

The new stage() signature introduced by #66 breaks back-compatibility
under Ruby 1.8.7. This fixes it by switching back to a one-argument
block signature and using a new class to wrap both the Resource and
Mktemp info for the staging context, in a signature-back-compatible
way.

Addresses homebrew/homebrew-core#529.

Closes #135.

Signed-off-by: Andrew Janke <andrew@apjanke.net>
This commit is contained in:
Andrew Janke 2016-04-22 16:54:09 -04:00
parent 2921795668
commit 258a764f67
3 changed files with 30 additions and 5 deletions

View File

@ -179,7 +179,7 @@ end
unless File.respond_to?(:write) unless File.respond_to?(:write)
class File class File
def self.write(filename, contents) def self.write(filename, contents)
File.open(filename, 'w') do |file| File.open(filename, "w") do |file|
file.write contents file.write contents
end end
end end

View File

@ -1548,7 +1548,7 @@ class Formula
end end
def stage def stage
active_spec.stage do |_resource, staging| active_spec.stage do |staging|
@source_modified_time = active_spec.source_modified_time @source_modified_time = active_spec.source_modified_time
@buildpath = Pathname.pwd @buildpath = Pathname.pwd
env_home = buildpath/".brew_home" env_home = buildpath/".brew_home"

View File

@ -1,6 +1,7 @@
require "download_strategy" require "download_strategy"
require "checksum" require "checksum"
require "version" require "version"
require "forwardable"
# Resource is the fundamental representation of an external resource. The # Resource is the fundamental representation of an external resource. The
# primary formula download, along with other declared resources, are instances # primary formula download, along with other declared resources, are instances
@ -86,15 +87,15 @@ class Resource
end end
# If a target is given, unpack there; else unpack to a temp folder. # If a target is given, unpack there; else unpack to a temp folder.
# If block is given, yield to that block with |self, staging|, where staging # If block is given, yield to that block with |stage|, where stage
# is a staging context that responds to retain!(). # is a ResourceStagingContext.
# A target or a block must be given, but not both. # A target or a block must be given, but not both.
def unpack(target = nil) def unpack(target = nil)
mktemp(download_name) do |staging| mktemp(download_name) do |staging|
downloader.stage downloader.stage
@source_modified_time = downloader.source_modified_time @source_modified_time = downloader.source_modified_time
if block_given? if block_given?
yield self, staging yield ResourceStageContext.new(self, staging)
elsif target elsif target
target = Pathname.new(target) unless target.is_a? Pathname target = Pathname.new(target) unless target.is_a? Pathname
target.install Dir["*"] target.install Dir["*"]
@ -186,3 +187,27 @@ class Resource
end end
end end
end end
# The context in which a Resource.stage() occurs. Supports access to both
# the Resource and associated Mktemp in a single block argument. The interface
# is back-compatible with Resource itself as used in that context.
class ResourceStageContext
extend Forwardable
# The Resource that is being staged
attr_reader :resource
# The Mktemp in which @resource is staged
attr_reader :staging
def_delegators :@resource, :version, :url, :mirrors, :specs, :using, :source_modified_time
def_delegators :@staging, :retain!
def initialize(resource, staging)
@resource = resource
@staging = staging
end
def to_s
"<#{self.class}: resource=#{resource} staging=#{staging}>"
end
end