diff --git a/Library/Homebrew/cask/lib/hbc/artifact/nested_container.rb b/Library/Homebrew/cask/lib/hbc/artifact/nested_container.rb index 801fdfdf74..ca669267f5 100644 --- a/Library/Homebrew/cask/lib/hbc/artifact/nested_container.rb +++ b/Library/Homebrew/cask/lib/hbc/artifact/nested_container.rb @@ -28,7 +28,7 @@ module Hbc end ohai "Extracting nested container #{path.relative_path_from(cask.staged_path)}" - container.new(cask, path, command, verbose: verbose).extract + container.new(cask, path, command, verbose: verbose).extract(to: cask.staged_path) FileUtils.remove_entry_secure(path) end end diff --git a/Library/Homebrew/cask/lib/hbc/container/air.rb b/Library/Homebrew/cask/lib/hbc/container/air.rb index d4bafc1b80..6b3ac97923 100644 --- a/Library/Homebrew/cask/lib/hbc/container/air.rb +++ b/Library/Homebrew/cask/lib/hbc/container/air.rb @@ -7,9 +7,7 @@ module Hbc path.extname == ".air" end - def extract - unpack_dir = @cask.staged_path - + def extract_to_dir(unpack_dir, basename:) @command.run!( "/Applications/Utilities/Adobe AIR Application Installer.app/Contents/MacOS/Adobe AIR Application Installer", args: ["-silent", "-location", unpack_dir, path], diff --git a/Library/Homebrew/cask/lib/hbc/container/base.rb b/Library/Homebrew/cask/lib/hbc/container/base.rb index ab81173bfa..9e9d595a97 100644 --- a/Library/Homebrew/cask/lib/hbc/container/base.rb +++ b/Library/Homebrew/cask/lib/hbc/container/base.rb @@ -1,21 +1,27 @@ module Hbc class Container class Base + extend Predicable + attr_reader :path + attr_predicate :verbose? + def initialize(cask, path, command, nested: false, verbose: false) @cask = cask @path = path @command = command - @nested = nested @verbose = verbose end - def verbose? - @verbose + def extract(to: nil, basename: nil) + basename ||= path.basename + unpack_dir = Pathname(to || Dir.pwd).expand_path + unpack_dir.mkpath + extract_to_dir(unpack_dir, basename: basename) end - def extract_nested_inside(dir) + def extract_nested_inside(dir, to:) children = Pathname.new(dir).children nested_container = children[0] @@ -23,7 +29,7 @@ module Hbc unless children.count == 1 && !nested_container.directory? && @cask.artifacts.none? { |a| a.is_a?(Artifact::NestedContainer) } && - extract_nested_container(nested_container) + extract_nested_container(nested_container, to: to) children.each do |src| dest = @cask.staged_path.join(src.basename) @@ -33,13 +39,13 @@ module Hbc end end - def extract_nested_container(source) + def extract_nested_container(source, to:) container = Container.for_path(source) return false unless container ohai "Extracting nested container #{source.basename}" - container.new(@cask, source, @command, nested: true, verbose: verbose?).extract + container.new(@cask, source, @command, verbose: verbose?).extract(to: to) true end diff --git a/Library/Homebrew/cask/lib/hbc/container/bzip2.rb b/Library/Homebrew/cask/lib/hbc/container/bzip2.rb index e9746d6772..78c4f096fb 100644 --- a/Library/Homebrew/cask/lib/hbc/container/bzip2.rb +++ b/Library/Homebrew/cask/lib/hbc/container/bzip2.rb @@ -7,12 +7,14 @@ module Hbc magic_number.match?(/\ABZh/n) end - def extract - Dir.mktmpdir do |unpack_dir| - @command.run!("/usr/bin/ditto", args: ["--", @path, unpack_dir]) - @command.run!("bunzip2", args: ["--quiet", "--", Pathname.new(unpack_dir).join(@path.basename)]) + def extract_to_dir(unpack_dir, basename:) + Dir.mktmpdir do |tmp_unpack_dir| + tmp_unpack_dir = Pathname(tmp_unpack_dir) - extract_nested_inside(unpack_dir) + FileUtils.cp path, tmp_unpack_dir/basename, preserve: true + @command.run!("bunzip2", args: ["--quiet", "--", tmp_unpack_dir/basename]) + + extract_nested_inside(tmp_unpack_dir, to: unpack_dir) end end end diff --git a/Library/Homebrew/cask/lib/hbc/container/cab.rb b/Library/Homebrew/cask/lib/hbc/container/cab.rb index 2615e8e680..28c4a0c665 100644 --- a/Library/Homebrew/cask/lib/hbc/container/cab.rb +++ b/Library/Homebrew/cask/lib/hbc/container/cab.rb @@ -7,13 +7,10 @@ module Hbc magic_number.match?(/\A(MSCF|MZ)/n) end - def extract - Dir.mktmpdir do |unpack_dir| - @command.run!("cabextract", - args: ["-d", unpack_dir, "--", @path], - env: { "PATH" => PATH.new(Formula["cabextract"].opt_bin, ENV["PATH"]) }) - @command.run!("/usr/bin/ditto", args: ["--", unpack_dir, @cask.staged_path]) - end + def extract_to_dir(unpack_dir, basename:) + @command.run!("cabextract", + args: ["-d", unpack_dir, "--", path], + env: { "PATH" => PATH.new(Formula["cabextract"].opt_bin, ENV["PATH"]) }) end def dependencies diff --git a/Library/Homebrew/cask/lib/hbc/container/dmg.rb b/Library/Homebrew/cask/lib/hbc/container/dmg.rb index bd3e9103a8..a3ca9754e3 100644 --- a/Library/Homebrew/cask/lib/hbc/container/dmg.rb +++ b/Library/Homebrew/cask/lib/hbc/container/dmg.rb @@ -14,11 +14,13 @@ module Hbc !imageinfo.empty? end - def extract + def extract_to_dir(unpack_dir, basename:) mount do |mounts| begin raise CaskError, "No mounts found in '#{@path}'; perhaps it is a bad disk image?" if mounts.empty? - mounts.each(&method(:extract_mount)) + mounts.each do |mount| + extract_mount(mount, to: unpack_dir) + end ensure mounts.each(&method(:eject)) end @@ -84,7 +86,7 @@ module Hbc private - def extract_mount(mount) + def extract_mount(mount, to:) Tempfile.open(["", ".bom"]) do |bomfile| bomfile.close @@ -93,7 +95,7 @@ module Hbc filelist.close @command.run!("/usr/bin/mkbom", args: ["-s", "-i", filelist.path, "--", bomfile.path]) - @command.run!("/usr/bin/ditto", args: ["--bom", bomfile.path, "--", mount, @cask.staged_path]) + @command.run!("/usr/bin/ditto", args: ["--bom", bomfile.path, "--", mount, to]) end end end diff --git a/Library/Homebrew/cask/lib/hbc/container/generic_unar.rb b/Library/Homebrew/cask/lib/hbc/container/generic_unar.rb index bfd1c53729..0b3f2f8ea5 100644 --- a/Library/Homebrew/cask/lib/hbc/container/generic_unar.rb +++ b/Library/Homebrew/cask/lib/hbc/container/generic_unar.rb @@ -7,9 +7,7 @@ module Hbc false end - def extract - unpack_dir = @cask.staged_path - + def extract_to_dir(unpack_dir, basename:) @command.run!("unar", args: ["-force-overwrite", "-quiet", "-no-directory", "-output-directory", unpack_dir, "--", path], env: { "PATH" => PATH.new(Formula["unar"].opt_bin, ENV["PATH"]) }) diff --git a/Library/Homebrew/cask/lib/hbc/container/gpg.rb b/Library/Homebrew/cask/lib/hbc/container/gpg.rb index fc39488ea8..10ca691338 100644 --- a/Library/Homebrew/cask/lib/hbc/container/gpg.rb +++ b/Library/Homebrew/cask/lib/hbc/container/gpg.rb @@ -23,15 +23,15 @@ module Hbc env: { "PATH" => PATH.new(Formula["gnupg"].opt_bin, ENV["PATH"]) }) end - def extract + def extract_to_dir(unpack_dir, basename:) import_key - Dir.mktmpdir do |unpack_dir| + Dir.mktmpdir do |tmp_unpack_dir| @command.run!("gpg", - args: ["--batch", "--yes", "--output", Pathname(unpack_dir).join(@path.basename(".gpg")), "--decrypt", @path], + args: ["--batch", "--yes", "--output", Pathname(tmp_unpack_dir).join(basename.basename(".gpg")), "--decrypt", path], env: { "PATH" => PATH.new(Formula["gnupg"].opt_bin, ENV["PATH"]) }) - extract_nested_inside(unpack_dir) + extract_nested_inside(tmp_unpack_dir, to: unpack_dir) end end diff --git a/Library/Homebrew/cask/lib/hbc/container/gzip.rb b/Library/Homebrew/cask/lib/hbc/container/gzip.rb index 7263f91f8a..bc67c64bb5 100644 --- a/Library/Homebrew/cask/lib/hbc/container/gzip.rb +++ b/Library/Homebrew/cask/lib/hbc/container/gzip.rb @@ -7,12 +7,14 @@ module Hbc magic_number.match?(/\A\037\213/n) end - def extract - Dir.mktmpdir do |unpack_dir| - @command.run!("/usr/bin/ditto", args: ["--", @path, unpack_dir]) - @command.run!("gunzip", args: ["--quiet", "--name", "--", Pathname.new(unpack_dir).join(@path.basename)]) + def extract_to_dir(unpack_dir, basename:) + Dir.mktmpdir do |tmp_unpack_dir| + tmp_unpack_dir = Pathname(tmp_unpack_dir) - extract_nested_inside(unpack_dir) + FileUtils.cp path, tmp_unpack_dir/basename, preserve: true + @command.run!("gunzip", args: ["--quiet", "--name", "--", tmp_unpack_dir/basename]) + + extract_nested_inside(tmp_unpack_dir, to: unpack_dir) end end end diff --git a/Library/Homebrew/cask/lib/hbc/container/lzma.rb b/Library/Homebrew/cask/lib/hbc/container/lzma.rb index a1980e903a..79be731bf4 100644 --- a/Library/Homebrew/cask/lib/hbc/container/lzma.rb +++ b/Library/Homebrew/cask/lib/hbc/container/lzma.rb @@ -7,14 +7,11 @@ module Hbc magic_number.match?(/\A\]\000\000\200\000/n) end - def extract - Dir.mktmpdir do |unpack_dir| - @command.run!("/usr/bin/ditto", args: ["--", @path, unpack_dir]) - @command.run!("unlzma", - args: ["-q", "--", Pathname(unpack_dir).join(@path.basename)], - env: { "PATH" => PATH.new(Formula["unlzma"].opt_bin, ENV["PATH"]) }) - @command.run!("/usr/bin/ditto", args: ["--", unpack_dir, @cask.staged_path]) - end + def extract_to_dir(unpack_dir, basename:) + @command.run!("/usr/bin/ditto", args: ["--", path, unpack_dir]) + @command.run!("unlzma", + args: ["-q", "--", Pathname(unpack_dir).join(basename)], + env: { "PATH" => PATH.new(Formula["unlzma"].opt_bin, ENV["PATH"]) }) end def dependencies diff --git a/Library/Homebrew/cask/lib/hbc/container/naked.rb b/Library/Homebrew/cask/lib/hbc/container/naked.rb index 7ba94f8040..4b32a222b5 100644 --- a/Library/Homebrew/cask/lib/hbc/container/naked.rb +++ b/Library/Homebrew/cask/lib/hbc/container/naked.rb @@ -7,13 +7,8 @@ module Hbc false end - def extract - @command.run!("/usr/bin/ditto", args: ["--", @path, @cask.staged_path.join(target_file)]) - end - - def target_file - return @path.basename if @nested - CGI.unescape(File.basename(@cask.url.path)) + def extract_to_dir(unpack_dir, basename:) + @command.run!("/usr/bin/ditto", args: ["--", path, unpack_dir/basename]) end end end diff --git a/Library/Homebrew/cask/lib/hbc/container/rar.rb b/Library/Homebrew/cask/lib/hbc/container/rar.rb index 1ad334202a..b060fc515c 100644 --- a/Library/Homebrew/cask/lib/hbc/container/rar.rb +++ b/Library/Homebrew/cask/lib/hbc/container/rar.rb @@ -7,10 +7,10 @@ module Hbc magic_number.match?(/\ARar!/n) end - def extract - path = @path - unpack_dir = @cask.staged_path - @command.run!(Formula["unrar"].opt_bin/"unrar", args: ["x", "-inul", path, unpack_dir]) + def extract_to_dir(unpack_dir, basename:) + @command.run!("unrar", + args: ["x", "-inul", path, unpack_dir], + env: { "PATH" => PATH.new(Formula["unrar"].opt_bin, ENV["PATH"]) }) end def dependencies diff --git a/Library/Homebrew/cask/lib/hbc/container/seven_zip.rb b/Library/Homebrew/cask/lib/hbc/container/seven_zip.rb index 33612fb9c0..76ab0590ee 100644 --- a/Library/Homebrew/cask/lib/hbc/container/seven_zip.rb +++ b/Library/Homebrew/cask/lib/hbc/container/seven_zip.rb @@ -7,9 +7,7 @@ module Hbc magic_number.match?(/\A7z\xBC\xAF\x27\x1C/n) end - def extract - unpack_dir = @cask.staged_path - + def extract_to_dir(unpack_dir, basename:) @command.run!("7zr", args: ["x", "-y", "-bd", "-bso0", path, "-o#{unpack_dir}"], env: { "PATH" => PATH.new(Formula["p7zip"].opt_bin, ENV["PATH"]) }) diff --git a/Library/Homebrew/cask/lib/hbc/container/svn_repository.rb b/Library/Homebrew/cask/lib/hbc/container/svn_repository.rb index fd49857fd8..eaa5a8f213 100644 --- a/Library/Homebrew/cask/lib/hbc/container/svn_repository.rb +++ b/Library/Homebrew/cask/lib/hbc/container/svn_repository.rb @@ -5,10 +5,7 @@ module Hbc (path/".svn").directory? end - def extract - path = @path - unpack_dir = @cask.staged_path - + def extract_to_dir(unpack_dir, basename:) @command.run!("svn", args: ["export", "--force", path, unpack_dir]) end end diff --git a/Library/Homebrew/cask/lib/hbc/container/tar.rb b/Library/Homebrew/cask/lib/hbc/container/tar.rb index 3cdbcb226d..5f7f401458 100644 --- a/Library/Homebrew/cask/lib/hbc/container/tar.rb +++ b/Library/Homebrew/cask/lib/hbc/container/tar.rb @@ -12,9 +12,7 @@ module Hbc end end - def extract - unpack_dir = @cask.staged_path - + def extract_to_dir(unpack_dir, basename:) @command.run!("tar", args: ["xf", path, "-C", unpack_dir]) end end diff --git a/Library/Homebrew/cask/lib/hbc/container/xar.rb b/Library/Homebrew/cask/lib/hbc/container/xar.rb index 0e665b64a0..7b653a787c 100644 --- a/Library/Homebrew/cask/lib/hbc/container/xar.rb +++ b/Library/Homebrew/cask/lib/hbc/container/xar.rb @@ -7,9 +7,7 @@ module Hbc magic_number.match?(/\Axar!/n) end - def extract - unpack_dir = @cask.staged_path - + def extract_to_dir(unpack_dir, basename:) @command.run!("xar", args: ["-x", "-f", @path, "-C", unpack_dir]) end end diff --git a/Library/Homebrew/cask/lib/hbc/container/xz.rb b/Library/Homebrew/cask/lib/hbc/container/xz.rb index 5980e84d58..043bf50152 100644 --- a/Library/Homebrew/cask/lib/hbc/container/xz.rb +++ b/Library/Homebrew/cask/lib/hbc/container/xz.rb @@ -7,12 +7,9 @@ module Hbc magic_number.match?(/\A\xFD7zXZ\x00/n) end - def extract - unpack_dir = @cask.staged_path - basename = path.basename - + def extract_to_dir(unpack_dir, basename:) @command.run!("/usr/bin/ditto", args: ["--", path, unpack_dir]) - @command.run!("xz", + @command.run!("unxz", args: ["-q", "--", unpack_dir/basename], env: { "PATH" => PATH.new(Formula["xz"].opt_bin, ENV["PATH"]) }) end diff --git a/Library/Homebrew/cask/lib/hbc/container/zip.rb b/Library/Homebrew/cask/lib/hbc/container/zip.rb index aee5485ffe..390ee48421 100644 --- a/Library/Homebrew/cask/lib/hbc/container/zip.rb +++ b/Library/Homebrew/cask/lib/hbc/container/zip.rb @@ -7,11 +7,11 @@ module Hbc magic_number.match?(/\APK(\003\004|\005\006)/n) end - def extract - Dir.mktmpdir do |unpack_dir| - @command.run!("/usr/bin/ditto", args: ["-x", "-k", "--", @path, unpack_dir]) + def extract_to_dir(unpack_dir, basename:) + Dir.mktmpdir do |tmp_unpack_dir| + @command.run!("/usr/bin/ditto", args: ["-x", "-k", "--", path, tmp_unpack_dir]) - extract_nested_inside(unpack_dir) + extract_nested_inside(tmp_unpack_dir, to: unpack_dir) end end end diff --git a/Library/Homebrew/cask/lib/hbc/installer.rb b/Library/Homebrew/cask/lib/hbc/installer.rb index 47bb770261..64d2d01274 100644 --- a/Library/Homebrew/cask/lib/hbc/installer.rb +++ b/Library/Homebrew/cask/lib/hbc/installer.rb @@ -6,6 +6,8 @@ require "hbc/cask_dependencies" require "hbc/staged" require "hbc/verify" +require "cgi" + module Hbc class Installer extend Predicable @@ -164,8 +166,9 @@ module Hbc end odebug "Using container class #{primary_container.class} for #{@downloaded_path}" - FileUtils.mkdir_p @cask.staged_path - primary_container.extract + + basename = CGI.unescape(File.basename(@cask.url.path)) + primary_container.extract(to: @cask.staged_path, basename: basename) end def install_artifacts diff --git a/Library/Homebrew/test/cask/container/naked_spec.rb b/Library/Homebrew/test/cask/container/naked_spec.rb index 1167a9cc10..f147b06b9d 100644 --- a/Library/Homebrew/test/cask/container/naked_spec.rb +++ b/Library/Homebrew/test/cask/container/naked_spec.rb @@ -5,7 +5,7 @@ describe Hbc::Container::Naked, :cask do version "1.2" end - path = "/tmp/downloads/kevin-spacey-1.2.pkg" + path = Pathname("/tmp/downloads/kevin-spacey.pkg") expected_destination = cask.staged_path.join("kevin spacey.pkg") container = Hbc::Container::Naked.new(cask, path, Hbc::FakeSystemCommand) @@ -15,7 +15,7 @@ describe Hbc::Container::Naked, :cask do ) expect { - container.extract + container.extract(to: cask.staged_path, basename: "kevin spacey.pkg") }.not_to raise_error end end diff --git a/Library/Homebrew/test/cask/installer_spec.rb b/Library/Homebrew/test/cask/installer_spec.rb index f966176546..ab25dde2a3 100644 --- a/Library/Homebrew/test/cask/installer_spec.rb +++ b/Library/Homebrew/test/cask/installer_spec.rb @@ -46,7 +46,7 @@ describe Hbc::Installer, :cask do Hbc::Installer.new(asset).install expect(Hbc::Caskroom.path.join("container-bzip2", asset.version)).to be_a_directory - expect(Hbc::Config.global.appdir.join("container-bzip2--#{asset.version}")).to be_a_file + expect(Hbc::Config.global.appdir.join("container")).to be_a_file end it "works with pure gzip-based Casks" do diff --git a/Library/Homebrew/test/support/fixtures/cask/Casks/container-bzip2.rb b/Library/Homebrew/test/support/fixtures/cask/Casks/container-bzip2.rb index 60f834b7f3..8d0d76e1b7 100644 --- a/Library/Homebrew/test/support/fixtures/cask/Casks/container-bzip2.rb +++ b/Library/Homebrew/test/support/fixtures/cask/Casks/container-bzip2.rb @@ -5,5 +5,5 @@ cask 'container-bzip2' do url "file://#{TEST_FIXTURE_DIR}/cask/container.bz2" homepage 'https://example.com/container-bzip2' - app 'container-bzip2--1.2.3' + app 'container' end