download_strategy: pass chdir block to stage

This commit is contained in:
Rylan Polster 2021-02-12 11:10:18 -05:00
parent 26722647c3
commit 1cc983f00d
No known key found for this signature in database
GPG Key ID: 46A744940CFF4D64
2 changed files with 21 additions and 16 deletions

View File

@ -71,25 +71,29 @@ class AbstractDownloadStrategy
# Unlike {Resource#stage}, this does not take a block. # Unlike {Resource#stage}, this does not take a block.
# #
# @api public # @api public
def stage def stage(&block)
UnpackStrategy.detect(cached_location, UnpackStrategy.detect(cached_location,
prioritise_extension: true, prioritise_extension: true,
ref_type: @ref_type, ref: @ref) ref_type: @ref_type, ref: @ref)
.extract_nestedly(basename: basename, .extract_nestedly(basename: basename,
prioritise_extension: true, prioritise_extension: true,
verbose: verbose? && !quiet?) verbose: verbose? && !quiet?)
chdir chdir(&block)
end end
def chdir def chdir(&block)
entries = Dir["*"] entries = Dir["*"]
raise "Empty archive" if entries.length.zero? raise "Empty archive" if entries.length.zero?
return if entries.length != 1
begin if entries.length != 1
Dir.chdir entries.first yield if block
rescue return
nil end
if File.directory? entries.first
Dir.chdir(entries.first, &block)
elsif block
yield
end end
end end
private :chdir private :chdir

View File

@ -114,7 +114,7 @@ class Resource
# 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 do
@source_modified_time = downloader.source_modified_time @source_modified_time = downloader.source_modified_time
apply_patches apply_patches
if block_given? if block_given?
@ -125,6 +125,7 @@ class Resource
end end
end end
end end
end
Partial = Struct.new(:resource, :files) Partial = Struct.new(:resource, :files)