123456789_123456789_123456789_123456789_123456789_

Class: Gem::PackageTask

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Rake::PackageTask
Instance Chain:
self, Rake::PackageTask
Inherits: Rake::PackageTask
  • Object
Defined in: lib/rubygems/package_task.rb

Overview

Create a package based upon a 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 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 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

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(gem_spec) {|_self| ... } ⇒ PackageTask

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.

Yields:

  • (_self)

Yield Parameters:

  • _self (PackageTask) —

    the object that the method was called on

[ GitHub ]

  
# 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

Instance Attribute Details

#content_addressable (rw)

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

[ GitHub ]

  
# File 'lib/rubygems/package_task.rb', line 70

attr_accessor :content_addressable

#gem_spec (rw)

Ruby 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.

[ GitHub ]

  
# File 'lib/rubygems/package_task.rb', line 66

attr_accessor :gem_spec

Instance Method Details

#content_addressable_ruby_abi (private)

[ GitHub ]

  
# File 'lib/rubygems/package_task.rb', line 169

def content_addressable_ruby_abi
  return unless content_addressable

  Gem::ContentAddress.ruby_abi_for(gem_spec.required_ruby_version)
end

#define

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

[ GitHub ]

  
# 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

#discard_stale_stamp_file (private)

[ GitHub ]

  
# File 'lib/rubygems/package_task.rb', line 186

def discard_stale_stamp_file
  return unless File.file?(stamp_file)

  path = recorded_gem_path
  File.delete(stamp_file) unless path && File.file?(path)
end

#init(gem)

Initialization tasks without the "yield self" or define operations.

[ GitHub ]

  
# 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

#package_name

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.

[ GitHub ]

  
# 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

#recorded_gem_path (private)

[ GitHub ]

  
# File 'lib/rubygems/package_task.rb', line 179

def recorded_gem_path
  return unless File.file?(stamp_file)

  path = File.read(stamp_file).strip
  path unless path.empty?
end

#stamp_file (private)

[ GitHub ]

  
# File 'lib/rubygems/package_task.rb', line 175

def stamp_file
  File.join package_dir, "#{package_name}.gem-built"
end