download_strategy: don't chdir unless a block is passed

This commit is contained in:
Bo Anderson 2021-02-24 01:13:10 +00:00
parent 9b94234866
commit 03e59f4ec8
No known key found for this signature in database
GPG Key ID: 3DB94E204E137D65

View File

@ -66,9 +66,13 @@ class AbstractDownloadStrategy
Context.current.quiet? || @quiet Context.current.quiet? || @quiet
end end
# Unpack {#cached_location} into the current working directory, and possibly # Unpack {#cached_location} into the current working directory.
# chdir into the newly-unpacked directory. #
# Unlike {Resource#stage}, this does not take a block. # Additionally, if a block is given, the working directory was previously empty
# and a single directory is extracted from the archive, the block will be called
# with the working directory changed to that directory. Otherwise this method
# will return, or the block will be called, without changing the current working
# directory.
# #
# @api public # @api public
def stage(&block) def stage(&block)
@ -78,7 +82,7 @@ class AbstractDownloadStrategy
.extract_nestedly(basename: basename, .extract_nestedly(basename: basename,
prioritise_extension: true, prioritise_extension: true,
verbose: verbose? && !quiet?) verbose: verbose? && !quiet?)
chdir(&block) chdir(&block) if block
end end
def chdir(&block) def chdir(&block)
@ -86,13 +90,13 @@ class AbstractDownloadStrategy
raise "Empty archive" if entries.length.zero? raise "Empty archive" if entries.length.zero?
if entries.length != 1 if entries.length != 1
yield if block yield
return return
end end
if File.directory? entries.first if File.directory? entries.first
Dir.chdir(entries.first, &block) Dir.chdir(entries.first, &block)
elsif block else
yield yield
end end
end end