diff --git a/Library/Homebrew/download_strategy.rb b/Library/Homebrew/download_strategy.rb index 928e1172d9..c2985f5e01 100644 --- a/Library/Homebrew/download_strategy.rb +++ b/Library/Homebrew/download_strategy.rb @@ -47,8 +47,11 @@ class AbstractDownloadStrategy # chdir into the newly-unpacked directory. # Unlike {Resource#stage}, this does not take a block. def stage - UnpackStrategy.detect(cached_location, ref_type: @ref_type, ref: @ref) + UnpackStrategy.detect(cached_location, + extension_only: true, + ref_type: @ref_type, ref: @ref) .extract_nestedly(basename: basename_without_params, + extension_only: true, verbose: ARGV.verbose? && !shutup) end diff --git a/Library/Homebrew/unpack_strategy.rb b/Library/Homebrew/unpack_strategy.rb index 3a06993f4b..d4068963f3 100644 --- a/Library/Homebrew/unpack_strategy.rb +++ b/Library/Homebrew/unpack_strategy.rb @@ -66,6 +66,7 @@ module UnpackStrategy def self.from_type(type) type = { naked: :uncompressed, + nounzip: :uncompressed, seven_zip: :p7zip, }.fetch(type, type) @@ -76,21 +77,30 @@ module UnpackStrategy end end - def self.from_path(path) - strategy = strategies - .sort_by { |s| s.extensions.map(&:length).max(0) } - .reverse - .detect { |s| s.extensions.include?(path.extname) } + def self.from_extension(extension) + strategies.sort_by { |s| s.extensions.map(&:length).max(0) } + .reverse + .detect { |s| s.extensions.any? { |ext| extension.end_with?(ext) } } + end - strategy ||= strategies.detect { |s| s.can_extract?(path) } + def self.from_magic(path) + strategies.detect { |s| s.can_extract?(path) } + end + + def self.detect(path, extension_only: false, type: nil, ref_type: nil, ref: nil) + strategy = from_type(type) if type + + if extension_only + strategy ||= from_extension(path.extname) + strategy ||= strategies.select { |s| s < Directory || s == Fossil } + .detect { |s| s.can_extract?(path) } + else + strategy ||= from_magic(path) + strategy ||= from_extension(path.extname) + end strategy ||= Uncompressed - strategy - end - - def self.detect(path, type: nil, ref_type: nil, ref: nil) - strategy = type ? from_type(type) : from_path(path) strategy.new(path, ref_type: ref_type, ref: ref) end @@ -109,7 +119,7 @@ module UnpackStrategy extract_to_dir(unpack_dir, basename: basename, verbose: verbose) end - def extract_nestedly(to: nil, basename: nil, verbose: false) + def extract_nestedly(to: nil, basename: nil, verbose: false, extension_only: false) Dir.mktmpdir do |tmp_unpack_dir| tmp_unpack_dir = Pathname(tmp_unpack_dir) @@ -118,9 +128,9 @@ module UnpackStrategy children = tmp_unpack_dir.children if children.count == 1 && !children.first.directory? - s = UnpackStrategy.detect(children.first) + s = UnpackStrategy.detect(children.first, extension_only: extension_only) - s.extract_nestedly(to: to, verbose: verbose) + s.extract_nestedly(to: to, verbose: verbose, extension_only: extension_only) next end diff --git a/Library/Homebrew/unpack_strategy/uncompressed.rb b/Library/Homebrew/unpack_strategy/uncompressed.rb index b3facb172c..fe627bf818 100644 --- a/Library/Homebrew/unpack_strategy/uncompressed.rb +++ b/Library/Homebrew/unpack_strategy/uncompressed.rb @@ -2,7 +2,9 @@ module UnpackStrategy class Uncompressed include UnpackStrategy - alias extract_nestedly extract + def extract_nestedly(extension_only: false, **options) + extract(**options) + end private