2010-03-19 23:30:59 -04:00
|
|
|
# Define a package task library to aid in the definition of
|
2007-12-20 22:22:43 -05:00
|
|
|
# redistributable package files.
|
|
|
|
|
|
|
|
require 'rake'
|
|
|
|
require 'rake/tasklib'
|
|
|
|
|
|
|
|
module Rake
|
|
|
|
|
|
|
|
# Create a packaging task that will package the project into
|
|
|
|
# distributable files (e.g zip archive or tar files).
|
|
|
|
#
|
|
|
|
# The PackageTask will create the following targets:
|
|
|
|
#
|
2014-07-14 23:07:37 -04:00
|
|
|
# +:package+ ::
|
2007-12-20 22:22:43 -05:00
|
|
|
# Create all the requested package files.
|
|
|
|
#
|
2014-07-14 23:07:37 -04:00
|
|
|
# +:clobber_package+ ::
|
2007-12-20 22:22:43 -05:00
|
|
|
# Delete all the package files. This target is automatically
|
|
|
|
# added to the main clobber target.
|
|
|
|
#
|
2014-07-14 23:07:37 -04:00
|
|
|
# +:repackage+ ::
|
2007-12-20 22:22:43 -05:00
|
|
|
# Rebuild the package files from scratch, even if they are not out
|
|
|
|
# of date.
|
|
|
|
#
|
2014-07-14 23:07:37 -04:00
|
|
|
# <tt>"<em>package_dir</em>/<em>name</em>-<em>version</em>.tgz"</tt> ::
|
2009-03-05 22:56:38 -05:00
|
|
|
# Create a gzipped tar package (if <em>need_tar</em> is true).
|
2007-12-20 22:22:43 -05:00
|
|
|
#
|
2014-07-14 23:07:37 -04:00
|
|
|
# <tt>"<em>package_dir</em>/<em>name</em>-<em>version</em>.tar.gz"</tt> ::
|
2009-03-05 22:56:38 -05:00
|
|
|
# Create a gzipped tar package (if <em>need_tar_gz</em> is true).
|
2007-12-20 22:22:43 -05:00
|
|
|
#
|
2014-07-14 23:07:37 -04:00
|
|
|
# <tt>"<em>package_dir</em>/<em>name</em>-<em>version</em>.tar.bz2"</tt> ::
|
2009-03-05 22:56:38 -05:00
|
|
|
# Create a bzip2'd tar package (if <em>need_tar_bz2</em> is true).
|
2007-12-20 22:22:43 -05:00
|
|
|
#
|
2014-07-14 23:07:37 -04:00
|
|
|
# <tt>"<em>package_dir</em>/<em>name</em>-<em>version</em>.zip"</tt> ::
|
2007-12-20 22:22:43 -05:00
|
|
|
# Create a zip package archive (if <em>need_zip</em> is true).
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
|
|
|
# Rake::PackageTask.new("rake", "1.2.3") do |p|
|
|
|
|
# p.need_tar = true
|
|
|
|
# p.package_files.include("lib/**/*.rb")
|
|
|
|
# end
|
|
|
|
#
|
|
|
|
class PackageTask < TaskLib
|
2009-03-09 03:49:14 -04:00
|
|
|
# Name of the package (from the GEM Spec).
|
2007-12-20 22:22:43 -05:00
|
|
|
attr_accessor :name
|
|
|
|
|
|
|
|
# Version of the package (e.g. '1.3.2').
|
|
|
|
attr_accessor :version
|
|
|
|
|
|
|
|
# Directory used to store the package files (default is 'pkg').
|
|
|
|
attr_accessor :package_dir
|
|
|
|
|
2013-10-11 17:35:01 -04:00
|
|
|
# True if a gzipped tar file (tgz) should be produced (default is
|
|
|
|
# false).
|
2007-12-20 22:22:43 -05:00
|
|
|
attr_accessor :need_tar
|
|
|
|
|
2013-10-11 17:35:01 -04:00
|
|
|
# True if a gzipped tar file (tar.gz) should be produced (default
|
|
|
|
# is false).
|
2007-12-20 22:22:43 -05:00
|
|
|
attr_accessor :need_tar_gz
|
|
|
|
|
2013-10-11 17:35:01 -04:00
|
|
|
# True if a bzip2'd tar file (tar.bz2) should be produced (default
|
|
|
|
# is false).
|
2007-12-20 22:22:43 -05:00
|
|
|
attr_accessor :need_tar_bz2
|
|
|
|
|
|
|
|
# True if a zip file should be produced (default is false)
|
|
|
|
attr_accessor :need_zip
|
|
|
|
|
|
|
|
# List of files to be included in the package.
|
|
|
|
attr_accessor :package_files
|
|
|
|
|
|
|
|
# Tar command for gzipped or bzip2ed archives. The default is 'tar'.
|
|
|
|
attr_accessor :tar_command
|
|
|
|
|
|
|
|
# Zip command for zipped archives. The default is 'zip'.
|
|
|
|
attr_accessor :zip_command
|
|
|
|
|
2011-06-23 18:11:55 -04:00
|
|
|
# Create a Package Task with the given name and version. Use +:noversion+
|
|
|
|
# as the version to build a package without a version or to provide a
|
|
|
|
# fully-versioned package name.
|
|
|
|
|
2007-12-20 22:22:43 -05:00
|
|
|
def initialize(name=nil, version=nil)
|
|
|
|
init(name, version)
|
|
|
|
yield self if block_given?
|
|
|
|
define unless name.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
# Initialization that bypasses the "yield self" and "define" step.
|
|
|
|
def init(name, version)
|
|
|
|
@name = name
|
|
|
|
@version = version
|
|
|
|
@package_files = Rake::FileList.new
|
|
|
|
@package_dir = 'pkg'
|
|
|
|
@need_tar = false
|
|
|
|
@need_tar_gz = false
|
|
|
|
@need_tar_bz2 = false
|
|
|
|
@need_zip = false
|
|
|
|
@tar_command = 'tar'
|
|
|
|
@zip_command = 'zip'
|
|
|
|
end
|
|
|
|
|
|
|
|
# Create the tasks defined by this task library.
|
|
|
|
def define
|
|
|
|
fail "Version required (or :noversion)" if @version.nil?
|
|
|
|
@version = nil if :noversion == @version
|
|
|
|
|
|
|
|
desc "Build all the packages"
|
|
|
|
task :package
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-20 22:22:43 -05:00
|
|
|
desc "Force a rebuild of the package files"
|
|
|
|
task :repackage => [:clobber_package, :package]
|
2009-03-05 22:56:38 -05:00
|
|
|
|
|
|
|
desc "Remove package products"
|
2007-12-20 22:22:43 -05:00
|
|
|
task :clobber_package do
|
|
|
|
rm_r package_dir rescue nil
|
|
|
|
end
|
|
|
|
|
|
|
|
task :clobber => [:clobber_package]
|
|
|
|
|
|
|
|
[
|
|
|
|
[need_tar, tgz_file, "z"],
|
|
|
|
[need_tar_gz, tar_gz_file, "z"],
|
|
|
|
[need_tar_bz2, tar_bz2_file, "j"]
|
|
|
|
].each do |(need, file, flag)|
|
|
|
|
if need
|
|
|
|
task :package => ["#{package_dir}/#{file}"]
|
2013-10-11 17:35:01 -04:00
|
|
|
file "#{package_dir}/#{file}" =>
|
|
|
|
[package_dir_path] + package_files do
|
2007-12-20 22:22:43 -05:00
|
|
|
chdir(package_dir) do
|
2014-07-14 23:07:37 -04:00
|
|
|
sh @tar_command, "#{flag}cvf", file, package_name
|
2007-12-20 22:22:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2007-12-20 22:22:43 -05:00
|
|
|
if need_zip
|
|
|
|
task :package => ["#{package_dir}/#{zip_file}"]
|
2013-10-11 17:35:01 -04:00
|
|
|
file "#{package_dir}/#{zip_file}" =>
|
|
|
|
[package_dir_path] + package_files do
|
2007-12-20 22:22:43 -05:00
|
|
|
chdir(package_dir) do
|
2014-07-14 23:07:37 -04:00
|
|
|
sh @zip_command, "-r", zip_file, package_name
|
2007-12-20 22:22:43 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
directory package_dir
|
|
|
|
|
|
|
|
file package_dir_path => @package_files do
|
|
|
|
mkdir_p package_dir rescue nil
|
|
|
|
@package_files.each do |fn|
|
|
|
|
f = File.join(package_dir_path, fn)
|
|
|
|
fdir = File.dirname(f)
|
2013-10-11 17:35:01 -04:00
|
|
|
mkdir_p(fdir) unless File.exist?(fdir)
|
2007-12-20 22:22:43 -05:00
|
|
|
if File.directory?(fn)
|
|
|
|
mkdir_p(f)
|
|
|
|
else
|
|
|
|
rm_f f
|
|
|
|
safe_ln(fn, f)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
2014-07-14 23:07:37 -04:00
|
|
|
# The name of this package
|
|
|
|
|
2007-12-20 22:22:43 -05:00
|
|
|
def package_name
|
|
|
|
@version ? "#{@name}-#{@version}" : @name
|
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2014-07-14 23:07:37 -04:00
|
|
|
# The directory this package will be built in
|
|
|
|
|
2007-12-20 22:22:43 -05:00
|
|
|
def package_dir_path
|
|
|
|
"#{package_dir}/#{package_name}"
|
|
|
|
end
|
|
|
|
|
2014-07-14 23:07:37 -04:00
|
|
|
# The package name with .tgz added
|
|
|
|
|
2007-12-20 22:22:43 -05:00
|
|
|
def tgz_file
|
|
|
|
"#{package_name}.tgz"
|
|
|
|
end
|
|
|
|
|
2014-07-14 23:07:37 -04:00
|
|
|
# The package name with .tar.gz added
|
|
|
|
|
2007-12-20 22:22:43 -05:00
|
|
|
def tar_gz_file
|
|
|
|
"#{package_name}.tar.gz"
|
|
|
|
end
|
|
|
|
|
2014-07-14 23:07:37 -04:00
|
|
|
# The package name with .tar.bz2 added
|
|
|
|
|
2007-12-20 22:22:43 -05:00
|
|
|
def tar_bz2_file
|
|
|
|
"#{package_name}.tar.bz2"
|
|
|
|
end
|
|
|
|
|
2014-07-14 23:07:37 -04:00
|
|
|
# The package name with .zip added
|
|
|
|
|
2007-12-20 22:22:43 -05:00
|
|
|
def zip_file
|
|
|
|
"#{package_name}.zip"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|