class Nucleon::Util::Package
Attributes
data[R]
Public Class Methods
new(options = {})
click to toggle source
# File lib/core/util/package.rb 9 def initialize(options = {}) 10 if options.is_a?(String) 11 @data = Config.new 12 decode(options) 13 else 14 @data = Config.ensure(options) 15 end 16 end
Public Instance Methods
add_file(file, target_path = nil, perm = 0600)
click to toggle source
# File lib/core/util/package.rb 36 def add_file(file, target_path = nil, perm = 0600) 37 target_path = file if target_path.nil? 38 file = File.expand_path(file) 39 40 if File.exists?(file) 41 content = Disk.read(file) 42 data.set([ :files, target_path ], { :perm => perm, :content => content }) if content 43 end 44 self 45 end
add_files(base_path, file_glob, target_path = nil, perm = 0600)
click to toggle source
# File lib/core/util/package.rb 49 def add_files(base_path, file_glob, target_path = nil, perm = 0600) 50 target_path = base_path if target_path.nil? 51 curr_dir = Dir.pwd 52 53 Dir.chdir(File.expand_path(base_path)) 54 Dir.glob(file_glob.gsub(/^[\/\\]+/, '')) do |file| 55 content = Disk.read(file) 56 57 if content 58 data.set([ :dir, target_path, file ], { :perm => perm, :content => content }) 59 end 60 end 61 Dir.chdir(curr_dir) 62 self 63 end
decode(encoded_string)
click to toggle source
# File lib/core/util/package.rb 30 def decode(encoded_string) 31 data.import(Data.symbol_map(Data.parse_json(Base64.decode64(encoded_string)))) 32 end
encode()
click to toggle source
# File lib/core/util/package.rb 26 def encode 27 Base64.encode64(Data.to_json(data.export, false)) 28 end
extract(base_path)
click to toggle source
# File lib/core/util/package.rb 67 def extract(base_path) 68 success = true 69 70 data.get_hash(:files).each do |target_path, info| 71 file = File.join(base_path.to_s, target_path.to_s) 72 perm = info[:perm] 73 content = info[:content] 74 75 FileUtils.mkdir_p(File.dirname(file)) 76 success = false unless Disk.write(file, content) && File.chmod(perm, file) 77 end 78 79 data.get_hash(:dir).each do |target_path, files| 80 files.each do |file, info| 81 file = File.join(base_path.to_s, target_path.to_s, file.to_s) 82 perm = info[:perm] 83 content = info[:content] 84 85 FileUtils.mkdir_p(File.dirname(file)) 86 success = false unless Disk.write(file, content) && File.chmod(perm, file) 87 end 88 end 89 success 90 end