Sha256: b6fa690749efdc3389f6f1430113c0702ac4560e59d3b41d58236ee6c3972905

Contents?: true

Size: 1.49 KB

Versions: 21

Compression:

Stored size: 1.49 KB

Contents

require 'zip'
module FaaStRuby
  class Package
    # Initialize with the directory to zip and the location of the output archive.
    def initialize(input_dir, output_file)
      @input_dir = input_dir
      @output_file = output_file
    end

    # Zip the input directory.
    def build
      entries = Dir.entries(@input_dir)
      entries.delete_if {|e| ['.', '..', '.git', @output_file.split('/').last].include?(e)}
      FileUtils.rm_f(@output_file) # Make sure file doesn't exist
      ::Zip::File.open(@output_file, ::Zip::File::CREATE) do |zipfile|
        write_entries entries, '', zipfile
      end
    end

    private

    # A helper method to make the recursion work.
    def write_entries(entries, path, zipfile)
      entries.each do |e|
        zipfile_path = path == '' ? e : File.join(path, e)
        disk_file_path = File.join(@input_dir, zipfile_path)
        if File.directory?(disk_file_path)
          recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
          next
        end
        put_into_archive(disk_file_path, zipfile, zipfile_path)
      end
    end

    def recursively_deflate_directory(disk_file_path, zipfile, zipfile_path)
      zipfile.mkdir zipfile_path
      subdir = Dir.entries(disk_file_path) - %w(. ..)
      write_entries subdir, zipfile_path, zipfile
    end

    def put_into_archive(disk_file_path, zipfile, zipfile_path)
      zipfile.get_output_stream(zipfile_path) do |f|
        f.write(File.open(disk_file_path, 'rb').read)
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
faastruby-0.5.21 lib/faastruby/cli/package.rb
faastruby-0.5.20 lib/faastruby/cli/package.rb
faastruby-0.5.19 lib/faastruby/cli/package.rb
faastruby-0.5.18 lib/faastruby/cli/package.rb
faastruby-0.5.17 lib/faastruby/cli/package.rb
faastruby-0.5.16 lib/faastruby/cli/package.rb
faastruby-0.5.15 lib/faastruby/cli/package.rb
faastruby-0.5.14 lib/faastruby/cli/package.rb
faastruby-0.5.13 lib/faastruby/cli/package.rb
faastruby-0.5.12 lib/faastruby/cli/package.rb
faastruby-0.5.11 lib/faastruby/cli/package.rb
faastruby-0.5.10 lib/faastruby/cli/package.rb
faastruby-0.5.9 lib/faastruby/cli/package.rb
faastruby-0.5.8 lib/faastruby/cli/package.rb
faastruby-0.5.7 lib/faastruby/cli/package.rb
faastruby-0.5.6 lib/faastruby/cli/package.rb
faastruby-0.5.5 lib/faastruby/cli/package.rb
faastruby-0.5.4 lib/faastruby/cli/package.rb
faastruby-0.5.3 lib/faastruby/cli/package.rb
faastruby-0.5.2 lib/faastruby/cli/package.rb