Class: GemsSnapshot::TarExporter

Inherits:
Object
  • Object
show all
Includes:
ExporterHelper
Defined in:
lib/gems_snapshot/exporter/tar_exporter.rb

Instance Method Summary collapse

Methods included from ExporterHelper

#installed_gems

Instance Method Details

#create_tar_file(filename, files) ⇒ Object

Create a tar file with Gem::Package::TarWriter from Rubygems. filename file destination. files Array of Hash. Each hash have to be :name and :path keys.



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

def create_tar_file(filename, files)
  File.open(filename, "w+") do |file|
    Gem::Package::TarWriter.new(file) do |tar_file|

      files.each do |hash|
        next unless File.exists?(hash[:path])

        filepath = hash[:path]
        filename = hash[:name]

        stat = File.stat(filepath)
        tar_file.add_file_simple(filename, stat.mode, stat.size) do |tar_io|
          File.open(filepath, "rb") do |file_io|
            tar_io.write(file_io.read(4096)) until file_io.eof?
          end
        end
      end

    end
  end
end

#export(filename) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/gems_snapshot/exporter/tar_exporter.rb', line 10

def export(filename)
  filename = "snapshot.gems" if filename.nil?

  files = []
  installed_gems.each do |gem|
    files << {:name => "gems/#{gem.full_name}.gem", :path => "#{gem.installation_path}/cache/#{gem.full_name}.gem"}
  end
  files << {:name => "gems.yml", :path => export_to_yml("#{Dir.tmpdir}/gems.yml")}

  create_tar_file(filename, files)
  filename
end

#export_to_yml(filename) ⇒ Object



23
24
25
26
# File 'lib/gems_snapshot/exporter/tar_exporter.rb', line 23

def export_to_yml(filename)
  yml_exporter = GemsSnapshot::YmlExporter.new
  yml_exporter.export("#{Dir.tmpdir}/gems.yml")
end