Permalink
Browse files

Added Tar Importer

  • Loading branch information...
1 parent 4a95d8a commit f3b8ce0bca73b89e5bf7160e5b15dc599136be23 @rogerleite committed May 6, 2010
View
@@ -18,6 +18,7 @@ def create_fake_environment
cd tmp/gems
rename 's/\.sample$//' *.sample
cd ../..
+ cp features/resources/test.tar tmp/
CMD
system "gem install tmp/gems/rake-0.8.7.gem --no-ri --no-rdoc > /dev/null"
@@ -56,3 +56,11 @@ Feature: Import gems
And I should see "Gems imported successfully."
And I run "gem list"
Then I should see "json (1.4.2)"
+
+ @wip
+ Scenario: using tar import
+ When I run "gem snapshot import tmp/test.tar"
+ Then I should see "awesome_print-0.1.4.gem"
+ And I should see "Gems imported successfully."
+ And I run "gem list"
+ Then I should see "awesome_print (0.1.4)"
View
Binary file not shown.
@@ -4,37 +4,52 @@ module GemsSnapshot
class TarImporter
- attr :errors
-
def import(filename)
- read_tar(filename)
+ files = extract_files_from_tar(filename)
+ copy_gems_to_cache_directory files
+ yml_metadata = get_metadata(files)
-# yml_exporter = GemsSnapshot::YmlExporter.new
-# yml_exporter.export("#{Dir.tmpdir}/gems.yml")
+ yml_importer = GemsSnapshot::YmlImporter.new
+ yml_importer.import(yml_metadata)
end
private
- def read_tar(filename)
+ def extract_files_from_tar(filename)
files = []
- dir = "#{Dir.tmpdir}/#{Time.now.to_i}"
- FileUtils.rm_rf dir
- FileUtils.mkdir_p dir
-
- File.open(filename, "r") do |file|
- Gem::Package::TarReader.new(file).each_entry do |entry|
+ tmp_dir = "#{Dir.tmpdir}/#{Time.now.to_i}"
+ FileUtils.rm_rf tmp_dir
+ FileUtils.mkdir_p tmp_dir
- destination_file = "#{dir}/#{entry.full_name}"
- FileUtils.mkdir_p File.dirname(destination_file)
+ begin
+ File.open(filename, "r") do |file|
+ Gem::Package::TarReader.new(file).each_entry do |entry|
+ destination_file = "#{tmp_dir}/#{entry.full_name}"
+ FileUtils.mkdir_p File.dirname(destination_file)
- File.open(destination_file, "w+") { |f| f.write entry.read }
- files << destination_file
+ File.open(destination_file, "w+") { |f| f.write entry.read }
+ files << destination_file
+ end
end
+ rescue ex
+ raise "An error occurred while extracting files. #{ex.message}"
end
files
end
+ def get_metadata(files)
+ result = files.select { |file| file =~ /gems.yml$/ }
+ raise "File gems.yml not found!" if result.nil? or result.size == 0
+ result.first
+ end
+
+ def copy_gems_to_cache_directory(files)
+ cache_directory = "#{Gem.path.first}/cache"
+ files.select { |file| file =~ /.gem$/ }.each do |file|
+ FileUtils.cp file, "#{cache_directory}/#{File.basename(file)}"
+ end
+ end
end
end

0 comments on commit f3b8ce0

Please sign in to comment.