Class: VMC::Cli::ZipUtil

Inherits:
Object show all
Defined in:
lib/cli/zip_util.rb

Constant Summary collapse

PACK_EXCLUSION_GLOBS =
['..', '.', '*~', '#*#', '*.log']

Class Method Summary collapse

Class Method Details

.entry_lines(file) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cli/zip_util.rb', line 20

def entry_lines(file)
  contents = nil
  unless VMC::Cli::Config.nozip
    contents = `unzip -l #{file} 2> #{to_dev_null}`
    contents = nil if $? != 0
  end
  # Do Ruby version if told to or native version failed
  unless contents
    entries = []
    Zip::ZipFile.foreach(file) { |zentry| entries << zentry }
    contents = entries.join("\n")
  end
  contents
end

.get_files_to_pack(dir) ⇒ Object



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

def get_files_to_pack(dir)
  Dir.glob("#{dir}/**/*", File::FNM_DOTMATCH).select do |f|
    process = true
    PACK_EXCLUSION_GLOBS.each { |e| process = false if File.fnmatch(e, File.basename(f)) }
    process && File.exists?(f)
  end
end

.pack(dir, zipfile) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/cli/zip_util.rb', line 58

def pack(dir, zipfile)
  unless VMC::Cli::Config.nozip
    excludes = PACK_EXCLUSION_GLOBS.map { |e| "\\#{e}" }
    excludes = excludes.join(' ')
    Dir.chdir(dir) do
      `zip -y -q -r #{zipfile} . -x #{excludes} 2> #{to_dev_null}`
      return unless $? != 0
    end
  end
  # Do Ruby version if told to or native version failed
  Zip::ZipFile::open(zipfile, true) do |zf|
    get_files_to_pack(dir).each do |f|
      zf.add(f.sub("#{dir}/",''), f)
    end
  end
end

.to_dev_nullObject



12
13
14
15
16
17
18
# File 'lib/cli/zip_util.rb', line 12

def to_dev_null
  if WINDOWS
    'nul'
  else
    '/dev/null'
  end
end

.unpack(file, dest) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/cli/zip_util.rb', line 35

def unpack(file, dest)
  unless VMC::Cli::Config.nozip
    FileUtils.mkdir(dest)
    `unzip -q #{file} -d #{dest} 2> #{to_dev_null}`
    return unless $? != 0
  end
  # Do Ruby version if told to or native version failed
  Zip::ZipFile.foreach(file) do |zentry|
    epath = "#{dest}/#{zentry}"
    dirname = File.dirname(epath)
    FileUtils.mkdir_p(dirname) unless File.exists?(dirname)
    zentry.extract(epath) unless File.exists?(epath)
  end
end