Assimilate Containers with UnpackStrategy.

This commit is contained in:
Markus Reiter 2018-07-16 20:22:37 +02:00
parent c47d51519e
commit e5865e7e7f
22 changed files with 74 additions and 86 deletions

View File

@ -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

View File

@ -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],

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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"]) })

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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"]) })

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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