From 1cc983f00d552da17336405460e3adc001c95ba5 Mon Sep 17 00:00:00 2001 From: Rylan Polster Date: Fri, 12 Feb 2021 11:10:18 -0500 Subject: [PATCH] download_strategy: pass chdir block to stage --- Library/Homebrew/download_strategy.rb | 20 ++++++++++++-------- Library/Homebrew/resource.rb | 17 +++++++++-------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb index 80c31d5210..50db47d6e2 100644 --- a/Library/Homebrew/download_strategy.rb +++ b/Library/Homebrew/download_strategy.rb @@ -71,25 +71,29 @@ class AbstractDownloadStrategy # Unlike {Resource#stage}, this does not take a block. # # @api public - def stage + def stage(&block) UnpackStrategy.detect(cached_location, prioritise_extension: true, ref_type: @ref_type, ref: @ref) .extract_nestedly(basename: basename, prioritise_extension: true, verbose: verbose? && !quiet?) - chdir + chdir(&block) end - def chdir + def chdir(&block) entries = Dir["*"] raise "Empty archive" if entries.length.zero? - return if entries.length != 1 - begin - Dir.chdir entries.first - rescue - nil + if entries.length != 1 + yield if block + return + end + + if File.directory? entries.first + Dir.chdir(entries.first, &block) + elsif block + yield end end private :chdir diff --git a/Library/Homebrew/resource.rb b/Library/Homebrew/resource.rb index 47920e13c8..bcd4a97ef2 100644 --- a/Library/Homebrew/resource.rb +++ b/Library/Homebrew/resource.rb @@ -114,14 +114,15 @@ class Resource # A target or a block must be given, but not both. 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 + downloader.stage do + @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 end