Class: KPM::Utils

Inherits:
Object
  • Object
show all
Defined in:
lib/kpm/utils.rb

Constant Summary collapse

'././@LongLink'

Class Method Summary collapse

Class Method Details

.path_with_skipped_top_level_dir(path) ⇒ Object



37
38
39
# File 'lib/kpm/utils.rb', line 37

def path_with_skipped_top_level_dir(path)
  Pathname(path).each_filename.to_a[1..-1].join(File::SEPARATOR)
end

.unpack_tgz(tar_gz_archive, destination, skip_top_dir = false) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/kpm/utils.rb', line 10

def unpack_tgz(tar_gz_archive, destination, skip_top_dir=false)
  Gem::Package::TarReader.new(Zlib::GzipReader.open(tar_gz_archive)) do |tar|
    dest = nil
    tar.each do |entry|
      if entry.full_name == TAR_LONGLINK
        dest = File.join destination, skip_top_dir ? path_with_skipped_top_level_dir(entry.read.strip) : entry.read.strip
        next
      end
      dest ||= File.join destination, skip_top_dir ? path_with_skipped_top_level_dir(entry.full_name) : entry.full_name

      if entry.directory?
        File.delete dest if File.file? dest
        FileUtils.mkdir_p dest, :mode => entry.header.mode, :verbose => false
      elsif entry.file?
        FileUtils.rm_rf dest if File.directory? dest
        File.open dest, "wb" do |f|
          f.print entry.read
        end
        FileUtils.chmod entry.header.mode, dest, :verbose => false
      elsif entry.header.typeflag == '2' # Symlink
        File.symlink entry.header.linkname, dest
      end
      dest = nil
    end
  end
end