Class: Suitcase::Zipper

Inherits:
Object
  • Object
show all
Defined in:
lib/suitcase/zipper.rb

Class Method Summary collapse

Class Method Details

.add(files_and_directories = [], namespace = "") ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/suitcase/zipper.rb', line 40

def self.add(files_and_directories=[], namespace="")
  files_and_directories.each do |file|
    f = ::File.expand_path(file)        
    if ::File.file? f
      items.merge!(("#{namespace}/#{::File.basename(f)}").to_s => f)
    elsif ::File.directory? f
      Dir["#{f}/*"].each do |f|
        add(f, "#{namespace.empty? ? "" : "#{namespace}/"}#{::File.basename(::File.dirname(f))}")
      end
    end
  end
end

.build_dir!(dirpath) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/suitcase/zipper.rb', line 24

def self.build_dir!(dirpath)
  ::FileUtils.mkdir_p dirpath unless ::File.directory? dirpath
  items.each do |name, path|
    end_path = "#{dirpath}/#{name}"
    unless name == ::File.basename(name)
      ::FileUtils.mkdir_p ::File.dirname(end_path) unless ::File.directory? ::File.dirname(end_path)
    end
    ::FileUtils.cp path, end_path
  end
  dirpath
end

.ensure_location_exists(loc) ⇒ Object



98
99
100
# File 'lib/suitcase/zipper.rb', line 98

def self.ensure_location_exists(loc)
  ::FileUtils.mkdir_p loc unless ::File.directory? loc
end

.flush!Object



36
37
38
# File 'lib/suitcase/zipper.rb', line 36

def self.flush!
  @items = nil
end

.gems(gem_list, gem_location) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/suitcase/zipper.rb', line 53

def self.gems(gem_list, gem_location)
  require 'rubygems/dependency_installer'
  gem_list = [gem_list] unless gem_list.is_a?(Array)
  ensure_location_exists gem_location

  cache_dir = "#{gem_location}/cache"
  ::FileUtils.mkdir_p cache_dir rescue nil unless File.exist? cache_dir

  locally_installed_gems = Gem::SourceIndex.from_installed_gems.map {|n,s| s.name }
  
  locally_installable_gems = gem_list & locally_installed_gems
  remotely_installable = gem_list - locally_installable_gems
  
  # First, add the locally installed gems
  locally_installable_gems.each do |spec|
    spec = Gem::SourceIndex.from_installed_gems.find_name(spec).last#.sort_by {|a,b| a.version <=> b.version }.last                
    f = Dir[File.join(Gem.dir, 'cache', "#{spec.full_name}.gem")].first
    add(f, "gems")
  end
  
  remotely_installable.each do |g|
    di = Gem::DependencyInstaller.new
    spec, url = di.find_spec_by_name_and_version(g).first
    f = begin
      Gem::RemoteFetcher.fetcher.download spec, "http://gems.github.com", gem_location
    rescue Exception => e
      Gem::RemoteFetcher.fetcher.download spec, url, gem_location
    end
    add(f, "gems")
  end
end

.items(default_dirs = {}) ⇒ Object



7
8
9
# File 'lib/suitcase/zipper.rb', line 7

def self.items(default_dirs={})
  @items ||= default_dirs
end

.packages(package_list, package_location = "#{Dir.pwd}/packages") ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/suitcase/zipper.rb', line 85

def self.packages(package_list, package_location="#{Dir.pwd}/packages")
  ensure_location_exists package_location
  package_list.each do |package|
    f = "#{package_location}/#{package.split('/').last}"
    unless ::File.file? f
      puts "downloading #{package} to #{f}"
      `curl -L #{package} > #{package_location}/#{package.split('/').last}`
    end
    
    add(f, "packages")
  end
end

.zip!(filepath) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/suitcase/zipper.rb', line 11

def self.zip!(filepath)
  filepath = filepath.include?(".tar.gz") ? filepath : "#{filepath}.tar.gz"
  File.open(filepath,"w") do |tarfile|
    Archive::Tar::Minitar::Writer.open(tarfile) do |tar|
      items.each do |name, path|
        data = open(path).read
        tar.add_file_simple(name, :size=>data.size, :mode=>0644) { |f| f.write(data) }
      end
    end
  end
  filepath
end