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



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/suitcase/zipper.rb', line 52

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

.add_content_as(content = "", filename = "", namespace = "files") ⇒ Object



114
115
116
# File 'lib/suitcase/zipper.rb', line 114

def self.add_content_as(content="", filename="", namespace="files")
  items.merge!({"__p__string#{filename}_#{namespace}".to_sym => {:name => ::File.basename(filename), :content => content, :namespace => namespace}})
end

.build_dir!(dirpath) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/suitcase/zipper.rb', line 30

def self.build_dir!(dirpath)
  ::FileUtils.mkdir_p dirpath unless ::File.directory? dirpath
  items.each do |name, path|
    if name.to_s =~ /__p__string/
      fpath = "#{dirpath}/#{path[:namespace]}/#{path[:name]}"
      ensure_location_exists(::File.dirname(fpath))
      ::File.open(fpath, "w+") do |tf|
        tf << path[:content]
      end
    else          
      end_path = "#{dirpath}/#{name}"
      ::FileUtils.mkdir_p ::File.dirname(end_path) unless ::File.directory? ::File.dirname(end_path) unless name == ::File.basename(name)          
      ::FileUtils.cp path, end_path
    end
  end
  dirpath
end

.ensure_location_exists(loc) ⇒ Object



110
111
112
# File 'lib/suitcase/zipper.rb', line 110

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

.find_gem(named, o = {}) ⇒ Object

Find the gem named named First, search in the :search_paths passed in via the options If it’s not found there, then search in the locally installed gems and finally search online if the gem isn’t available



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/suitcase/zipper.rb', line 79

def self.find_gem(named, o={})
  require 'rubygems/dependency_installer'

  # Look for the gem in a path passed
  found_path = search_path_for_gem_in_paths(named, o[:search_paths]) if o[:search_paths]
  return found_path if found_path
  
  found_path = find_gem_in_locally_installed_gems(named)
  return found_path if found_path
  
  gem_location = o[:temp_path] || "/tmp/gems"
  ensure_location_exists(gem_location)
  
  found_path = find_gem_remotely_by_name_and_download_to(named, gem_location)
  return found_path if found_path        
  nil
end

.flush!Object



48
49
50
# File 'lib/suitcase/zipper.rb', line 48

def self.flush!
  @items = nil
end

.gems(gem_list, o = {}) ⇒ Object

TODO: MOVE



66
67
68
69
70
71
72
73
# File 'lib/suitcase/zipper.rb', line 66

def self.gems(gem_list, o={})
  require 'rubygems/dependency_installer'
  gem_list = [gem_list] unless gem_list.is_a?(Array)
  
  gem_list.each do |g|
    add find_gem(g, o), "gems"
  end
end

.items(default_dirs = {}) ⇒ Object



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

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

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



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/suitcase/zipper.rb', line 97

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

.reset!Object



118
119
120
# File 'lib/suitcase/zipper.rb', line 118

def self.reset!
  @items = nil
end

.zip!(filepath) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/suitcase/zipper.rb', line 10

def self.zip!(filepath)
  require 'archive/tar/minitar'
  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|
        if name == :string              
          ::File.open("#{dirpath}/#{path[:namespace]}/#{path[:name]}", "w+") do |tf|
            tf << path[:content]
          end
        else
          data = open(path).read
          tar.add_file_simple(name, :size=>data.size, :mode=>0644) { |f| f.write(data) }
        end
      end
    end
  end
  filepath
end