Class: BuildpackSupport::Rake::PackageZipTask

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/buildpack_support/rake/package_zip_task.rb

Overview

A task generator for the task that creates the package zip file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&task_block) ⇒ PackageZipTask

Returns a new instance of PackageZipTask.



39
40
41
42
43
44
45
46
47
# File 'lib/buildpack_support/rake/package_zip_task.rb', line 39

def initialize(&task_block)
  @verbose = false

  task_block.call(*[self].slice(0, task_block.arity)) if task_block
  abort 'package_name must be configured' unless package_name
  abort 'staging_dir must be configured' unless staging_dir

  task(package_name) { |t| run_task t.name, staging_dir, verbose }
end

Instance Attribute Details

#package_nameString

Returns the name of the package.

Returns:

  • (String)

    the name of the package



29
30
31
# File 'lib/buildpack_support/rake/package_zip_task.rb', line 29

def package_name
  @package_name
end

#staging_dirString

Returns the directory to zip the contents of.

Returns:

  • (String)

    the directory to zip the contents of



33
34
35
# File 'lib/buildpack_support/rake/package_zip_task.rb', line 33

def staging_dir
  @staging_dir
end

#verboseBoolean

Returns the verbosity of the task. Defaults to true.

Returns:

  • (Boolean)

    the verbosity of the task. Defaults to true.



37
38
39
# File 'lib/buildpack_support/rake/package_zip_task.rb', line 37

def verbose
  @verbose
end

Instance Method Details

#run_task(name, staging_dir, verbose) ⇒ void

This method returns an undefined value.

Runs the task

Parameters:

  • name (String)

    the name of the ZIP file to create

  • staging_dir (String)

    the directory to ZIP up

  • verbose (Boolean)

    whether to print messages to the console



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/buildpack_support/rake/package_zip_task.rb', line 55

def run_task(name, staging_dir, verbose)
  RakeFileUtils.verbose(verbose) do
    mkdir_p File.dirname(name)

    Zip::File.open(name, Zip::File::CREATE) do |zipfile|
      Dir[File.join(staging_dir, '**', '**')].each do |file|
        relative_path = relative_path(file, staging_dir)
        rake_output_message "a #{relative_path}" if verbose
        zipfile.add(relative_path, file)
      end
    end
  end

  rake_output_message "Created package #{name}"
end