Module: Capistrano::ScmCopyCommand::Utils

Defined in:
lib/capistrano/scm_copy_command/utils.rb

Overview

Utils

Class Method Summary collapse

Class Method Details

.zip(source_directory, destination_file, prefix: nil, working_directory: Dir.getwd, exclude_patterns: [], keep_filesystem_permissions: false, file_permissions: 0664, directory_permissions: 2775) ⇒ Object

Create zip archive from source directory

Parameters:

  • source_directory (String)

    The directory

  • destination_file (String)

    Name of the zip file

  • prefix (String) (defaults to: nil)

    Prefix inside zip file

  • working_directory (String) (defaults to: Dir.getwd)

    The directory where rubyzip should change to

  • exclude_patterns (Array) (defaults to: [])

    Ignore files matching patterns

  • keep_filesystem_permissions (TrueClass, FalseClass) (defaults to: false)

    Record permissions in file system

  • file_permissions (OctalNumber) (defaults to: 0664)

    Permissions for files

  • directory_permissions (OctalNumber) (defaults to: 2775)

    Permissions for directories



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/capistrano/scm_copy_command/utils.rb', line 23

def zip(
  source_directory,
  destination_file,
  prefix: nil,
  working_directory: Dir.getwd,
  exclude_patterns: [],
  keep_filesystem_permissions: false,
  file_permissions: 0664,
  directory_permissions: 2775
)
  list = Rake::FileList.new(File.join(source_directory, '**', '*'))
  list.exclude { |f| !File.file? f }
  exclude_patterns.each { |e| list.exclude e }

  Zip::File.open(destination_file, Zip::File::CREATE) do |z|
    list.uniq.each do |filename|
      paths = []
      paths << Pathname.new(prefix) unless prefix.nil? || prefix.empty?
      paths << Pathname.new(filename).relative_path_from(Pathname.new(working_directory))

      z.add(File.join(*paths), File.expand_path(filename))

      next if keep_filesystem_permissions

      z.file.chmod(file_permissions, File.join(*paths)) if z.file.file? File.join(*paths)
      z.file.chmod(directory_permissions, File.join(*paths)) if z.file.directory? File.join(*paths)
    end
  end
end