Class: Rebi::ZipHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/rebi/zip_helper.rb

Instance Method Summary collapse

Constructor Details

#initializeZipHelper

Returns a new instance of ZipHelper.

Raises:



4
5
6
7
# File 'lib/rebi/zip_helper.rb', line 4

def initialize
  `git status`
  raise Rebi::Error::NoGit.new("Not a git repository") unless $?.success?
end

Instance Method Details

#gen(env_conf) ⇒ Object

Create zip archivement



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rebi/zip_helper.rb', line 28

def gen env_conf
  Rebi.log("Creating zip archivement", env_conf.name)
  start = Time.now
  ebextensions = env_conf.ebextensions
  files = ls_files
  tmp_file = raw_zip_archive
  tmp_folder = Dir.mktmpdir
  Zip::File.open(tmp_file) do |z|
    ebextensions.each do |ex_folder|
      Dir.glob("#{ex_folder}/*.config") do |fname|
        next unless (File.file?(fname) && files.include?(fname))
        next unless y = YAML::load(ErbHelper.new(File.read(fname), env_conf).result)
        basename = File.basename(fname)
        target = ".ebextensions/#{basename}"
        tmp_yaml = "#{tmp_folder}/#{basename}"
        File.open(tmp_yaml, 'w') do |f|
          f.write y.to_yaml
        end
        z.remove target if z.find_entry target
        z.add target, tmp_yaml
      end
    end
  end
  FileUtils.rm_rf tmp_folder
  Rebi.log("Zip was created in: #{Time.now - start}s", env_conf.name)
  return {
    label: Time.now.strftime("app_#{env_conf.name}_#{version_label}_%Y%m%d_%H%M%S"),
    file: File.open(tmp_file.path),
    message: message,
  }
end

#ls_filesObject



9
10
11
# File 'lib/rebi/zip_helper.rb', line 9

def ls_files
  `git ls-files`.split("\n")
end

#messageObject



23
24
25
# File 'lib/rebi/zip_helper.rb', line 23

def message
  `git log --oneline -1`.chomp.split(" ")[1..-1].join(" ")[0..190]
end

#raw_zip_archive(opts = {}) ⇒ Object



13
14
15
16
17
# File 'lib/rebi/zip_helper.rb', line 13

def raw_zip_archive opts={}
  tmp_file = Tempfile.new("git_archive")
  system "git archive --format=zip HEAD > #{tmp_file.path}"
  return tmp_file
end

#version_labelObject



19
20
21
# File 'lib/rebi/zip_helper.rb', line 19

def version_label
  `git describe --always --abbrev=8`.chomp
end