Class: Gem::Micro::Unpacker

Inherits:
Object
  • Object
show all
Extended by:
Utils
Defined in:
lib/microgem/unpacker.rb

Defined Under Namespace

Classes: UnpackError

Constant Summary collapse

ZINFLATE_BIN =
File.expand_path('../../../bin/zinflate', __FILE__)

Class Method Summary collapse

Methods included from Utils

config, ensure_dir, log, replace, tmpdir

Class Method Details

.gzip(archive) ⇒ Object

Unpacks a gzip archive in place using the gunzip commandline utility.

Raises a Gem::Micro::Unpacker::UnpackError if it fails.

TODO: Make it work not in place.



27
28
29
30
31
# File 'lib/microgem/unpacker.rb', line 27

def self.gzip(archive)
  unless system("/usr/bin/gunzip --quiet --force --decompress '#{archive}' > /dev/null 2>&1")
    raise UnpackError, "Failed to unpack `#{archive}' with `gunzip'"
  end
end

.inflate(archive, out_file) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/microgem/unpacker.rb', line 33

def self.inflate(archive, out_file)
  if Config.simple_unpacker?
    inflate_with_zinflate(archive, out_file)
  else
    inflate_with_zlib(archive, out_file)
  end
end

.inflate_with_zinflate(archive, out_file) ⇒ Object



48
49
50
51
52
# File 'lib/microgem/unpacker.rb', line 48

def self.inflate_with_zinflate(archive, out_file)
  unless system("ruby '#{ZINFLATE_BIN}' '#{archive}' '#{out_file}' > /dev/null 2>&1")
    raise UnpackError, "Failed to unpack `#{archive}' with `zinflate'"
  end
end

.inflate_with_zlib(archive, out_file) ⇒ Object



41
42
43
44
45
# File 'lib/microgem/unpacker.rb', line 41

def self.inflate_with_zlib(archive, out_file)
  require "zlib"
  out = Zlib::Inflate.inflate(File.read(archive))
  File.open(out_file, 'w') { |f| f << out }
end

.tar(archive, extract_to, gzip = false) ⇒ Object

Unpacks a tar archive to extract_to using the tar commandline utility. If gzip is true the archive is expected to be a gzip archive and will be decompressed.

Raises a Gem::Micro::Unpacker::UnpackError if it fails.



13
14
15
16
17
18
19
# File 'lib/microgem/unpacker.rb', line 13

def self.tar(archive, extract_to, gzip = false)
  log(:debug, "Unpacking `#{archive}' to `#{extract_to}'")
  ensure_dir(extract_to)
  unless system("/usr/bin/tar --directory='#{extract_to}' -#{ 'z' if gzip }xf '#{archive}' > /dev/null 2>&1")
    raise UnpackError, "Failed to unpack `#{archive}' with `tar'"
  end
end