class Gem::PackageTask

Create a package based upon a Gem::Specification. Gem packages, as well as zip files and tar/gzipped packages can be produced by this task.

In addition to the Rake targets generated by Rake::PackageTask, a Gem::PackageTask will also generate the following tasks:

“package_dir/name-version.gem”

Create a RubyGems package with the given name and version.

Example using a Gem::Specification:

require 'rubygems'
require 'rubygems/package_task'

spec = Gem::Specification.new do |s|
  s.summary = "Ruby based make-like utility."
  s.name = 'rake'
  s.version = PKG_VERSION
  s.requirements << 'none'
  s.files = PKG_FILES
  s.description = <<-EOF
Rake is a Make-like program implemented in Ruby. Tasks
and dependencies are specified in standard Ruby syntax.
  EOF
end

Gem::PackageTask.new(spec) do |pkg|
  pkg.need_zip = true
  pkg.need_tar = true
end

Attributes

Indicates whether this package should be built as a content-addressable gem.

Ruby Gem::Specification containing the metadata for this package. The name, version and package_files are automatically determined from the gemspec and don’t need to be explicitly provided.

Public Class Methods

Create a Gem Package task library. Automatically define the gem if a block is given. If no block is supplied, then define needs to be called to define the task.

# File lib/rubygems/package_task.rb, line 77
def initialize(gem_spec)
  init gem_spec
  yield self if block_given?
  define if block_given?
end

Public Instance Methods

Create the Rake tasks and actions specified by this Gem::PackageTask. (define is automatically called if a block is given to new).

Calls superclass method
# File lib/rubygems/package_task.rb, line 98
def define
  Gem::ContentAddress.eligible!(gem_spec) if content_addressable

  super

  gem_file = File.basename gem_spec.cache_file
  gem_path = File.join package_dir, gem_file
  gem_dir = package_dir_path

  if content_addressable
    build_target = stamp_file
    gem_description = "content-addressable gem #{gem_spec.name}-#{gem_spec.version} " \
      "(Platform: #{gem_spec.platform}, Ruby ABI: #{content_addressable_ruby_abi})"

    discard_stale_stamp_file
  else
    build_target = gem_path
    gem_description = "gem file #{gem_file}"
  end

  task package: [:gem]

  directory package_dir
  directory gem_dir

  desc "Build the #{gem_description}"
  task gem: [build_target]

  trace = Rake.application.options.trace
  Gem.configuration.verbose = trace

  file build_target => [package_dir, gem_dir] + @gem_spec.files do
    previous_gem = recorded_gem_path if content_addressable
    built_gem_path = nil

    chdir(gem_dir) do
      when_writing "Creating #{gem_description}" do
        built_gem_file = Gem::Package.build gem_spec, content_addressable: content_addressable

        verbose trace do
          mv built_gem_file, ".."
        end

        if content_addressable
          built_gem_path = File.join(package_dir, built_gem_file)
          File.write File.join("..", File.basename(stamp_file)), built_gem_path
        end
      end
    end

    if previous_gem && built_gem_path && previous_gem != built_gem_path
      verbose trace do
        rm_f previous_gem
      end
    end
  end
end

Initialization tasks without the “yield self” or define operations.

Calls superclass method
# File lib/rubygems/package_task.rb, line 86
def init(gem)
  super gem.full_name, :noversion
  @gem_spec = gem
  @content_addressable = false
  @package_files += gem_spec.files if gem_spec.files
  @fileutils_output = $stdout
end

The name of this package. Content-addressable builds append the Ruby ABI so per-ABI builds of the same full_name do not share a staging directory.

Calls superclass method
# File lib/rubygems/package_task.rb, line 161
def package_name
  name = super
  ruby_abi = content_addressable_ruby_abi
  ruby_abi ? "#{name}-#{ruby_abi}" : name
end