Class: Epublishing::TgzBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/epublishing/tgz_builder.rb

Class Method Summary collapse

Class Method Details

.build(dest, srcdir, verbose = false) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/epublishing/tgz_builder.rb', line 30

def self.build(dest, srcdir, verbose=false)
  parent_idx = File.dirname(srcdir).size + 1
  tar_io = StringIO.new
  Gem::Package::TarWriter.new(tar_io) do |tar|
    Dir[File.join srcdir, '**', '**'].each do |file|
      name = file[parent_idx, file.size].sub(/^\.+\//, '')
      mode = File.stat(file).mode
      if File.directory?(file)
        tar.mkdir(name, mode)
      else
        tar.add_file(name, mode) do |out_io|
          File.open(file, 'rb') { |in_io| out_io.write in_io.read }
        end
      end
      puts "+ #{name}" if verbose
    end
  end
  Zlib::GzipWriter.open(dest) do |gz|
    gz.write tar_io.string
  end
end