Class: Capsium::Packager

Inherits:
Object
  • Object
show all
Defined in:
lib/capsium/packager.rb

Defined Under Namespace

Classes: FileAlreadyExistsError

Instance Method Summary collapse

Instance Method Details

#compress_package(package, cap_file) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/capsium/packager.rb', line 50

def compress_package(package, cap_file)
  Zip::File.open(cap_file, Zip::File::CREATE) do |zipfile|
    Dir[File.join(package.path, "**", "**")].each do |file|
      zipfile.add(file.sub("#{package.path}/", ""), file)
    end
  end
end

#pack(package, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/capsium/packager.rb', line 13

def pack(package, options = {})
  directory = package.path
  output_file_name = "#{package..name}-#{package..version}.cap"
  output_directory = File.dirname(directory)
  cap_file_path = File.join(output_directory, output_file_name)

  if File.exist?(cap_file_path) && !options[:force]
    raise FileAlreadyExistsError,
          "Package target already exists, aborting: `#{relative_path_current(cap_file_path)}`"
  elsif File.exist?(cap_file_path)
    puts "Package target already exists, overwriting: `#{relative_path_current(cap_file_path)}`"
    FileUtils.rm_f(cap_file_path)
  end

  Dir.mktmpdir do |dir|
    FileUtils.cp_r("#{directory}/.", dir)
    new_package = Package.new(dir)
    new_package.solidify
    new_cap_file_path = File.join(dir, output_file_name)
    compress_package(new_package, new_cap_file_path)
    puts "Package built at: #{new_cap_file_path}"
    FileUtils.mv(new_cap_file_path, cap_file_path)
    puts "Package created: #{relative_path_current(cap_file_path)}"
    return cap_file_path
  end
end

#relative_path_current(absolute_path) ⇒ Object



58
59
60
# File 'lib/capsium/packager.rb', line 58

def relative_path_current(absolute_path)
  Pathname.new(absolute_path).relative_path_from(Dir.pwd).to_s
end

#unpack(cap_file_path, destination) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/capsium/packager.rb', line 40

def unpack(cap_file_path, destination)
  Zip::File.open(cap_file_path) do |zip_file|
    zip_file.each do |entry|
      entry_path = File.join(destination, entry.name)
      FileUtils.mkdir_p(File.dirname(entry_path))
      entry.extract(entry_path)
    end
  end
end