Class: Rake::PackageTask

Inherits:
TaskLib show all
Defined in:
lib/rake/packagetask.rb

Overview

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:

:package

Create all the requested package files.

:clobber_package

Delete all the package files. This target is automatically added to the main clobber target.

:repackage

Rebuild the package files from scratch, even if they are not out of date.

"package_dir/name-version.tgz"

Create a gzipped tar package (if need_tar is true).

"package_dir/name-version.tar.gz"

Create a gzipped tar package (if need_tar_gz is true).

"package_dir/name-version.tar.bz2"

Create a bzip2’d tar package (if need_tar_bz2 is true).

"package_dir/name-version.zip"

Create a zip package archive (if need_zip is true).

Example:

Rake::PackageTask.new("rake", "1.2.3") do |p|
  p.need_tar = true
  p.package_files.include("lib/**/*.rb")
end

Constant Summary

Constants included from FileUtilsExt

FileUtilsExt::DEFAULT

Constants included from FileUtils

FileUtils::LN_SUPPORTED, FileUtils::RUBY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from FileUtilsExt

#nowrite, #rake_check_options, #rake_output_message, #verbose, #when_writing

Methods included from FileUtils

#ruby, #safe_ln, #sh, #split_all

Methods included from Cloneable

#initialize_copy

Constructor Details

#initialize(name = nil, version = nil) {|_self| ... } ⇒ PackageTask

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.

Yields:

  • (_self)

Yield Parameters:



89
90
91
92
93
# File 'lib/rake/packagetask.rb', line 89

def initialize(name=nil, version=nil)
  init(name, version)
  yield self if block_given?
  define unless name.nil?
end

Instance Attribute Details

#nameObject

Name of the package (from the GEM Spec).



47
48
49
# File 'lib/rake/packagetask.rb', line 47

def name
  @name
end

#need_tarObject

True if a gzipped tar file (tgz) should be produced (default is false).



57
58
59
# File 'lib/rake/packagetask.rb', line 57

def need_tar
  @need_tar
end

#need_tar_bz2Object

True if a bzip2’d tar file (tar.bz2) should be produced (default is false).



65
66
67
# File 'lib/rake/packagetask.rb', line 65

def need_tar_bz2
  @need_tar_bz2
end

#need_tar_gzObject

True if a gzipped tar file (tar.gz) should be produced (default is false).



61
62
63
# File 'lib/rake/packagetask.rb', line 61

def need_tar_gz
  @need_tar_gz
end

#need_tar_xzObject

True if a xz’d tar file (tar.xz) should be produced (default is false)



68
69
70
# File 'lib/rake/packagetask.rb', line 68

def need_tar_xz
  @need_tar_xz
end

#need_zipObject

True if a zip file should be produced (default is false)



71
72
73
# File 'lib/rake/packagetask.rb', line 71

def need_zip
  @need_zip
end

#package_dirObject

Directory used to store the package files (default is ‘pkg’).



53
54
55
# File 'lib/rake/packagetask.rb', line 53

def package_dir
  @package_dir
end

#package_filesObject

List of files to be included in the package.



74
75
76
# File 'lib/rake/packagetask.rb', line 74

def package_files
  @package_files
end

#tar_commandObject

Tar command for gzipped or bzip2ed archives. The default is ‘tar’.



77
78
79
# File 'lib/rake/packagetask.rb', line 77

def tar_command
  @tar_command
end

#versionObject

Version of the package (e.g. ‘1.3.2’).



50
51
52
# File 'lib/rake/packagetask.rb', line 50

def version
  @version
end

#without_parent_dirObject

True if parent directory should be omitted (default is false)



83
84
85
# File 'lib/rake/packagetask.rb', line 83

def without_parent_dir
  @without_parent_dir
end

#zip_commandObject

Zip command for zipped archives. The default is ‘zip’.



80
81
82
# File 'lib/rake/packagetask.rb', line 80

def zip_command
  @zip_command
end

Instance Method Details

#defineObject

Create the tasks defined by this task library.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/rake/packagetask.rb', line 112

def define
  fail "Version required (or :noversion)" if @version.nil?
  @version = nil if :noversion == @version

  desc "Build all the packages"
  task :package

  desc "Force a rebuild of the package files"
  task repackage: [:clobber_package, :package]

  desc "Remove package products"
  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"],
    [need_tar_xz, tar_xz_file, "J"]
  ].each do |need, file, flag|
    if need
      task package: ["#{package_dir}/#{file}"]
      file "#{package_dir}/#{file}" =>
        [package_dir_path] + package_files do
        chdir(working_dir) { sh @tar_command, "#{flag}cvf", file, target_dir }
        mv "#{package_dir_path}/#{target_dir}", package_dir if without_parent_dir
      end
    end
  end

  if need_zip
    task package: ["#{package_dir}/#{zip_file}"]
    file "#{package_dir}/#{zip_file}" =>
      [package_dir_path] + package_files do
      chdir(working_dir) { sh @zip_command, "-r", zip_file, target_dir }
      mv "#{package_dir_path}/#{zip_file}", package_dir if without_parent_dir
    end
  end

  directory package_dir_path => @package_files do
    @package_files.each do |fn|
      f = File.join(package_dir_path, fn)
      fdir = File.dirname(f)
      mkdir_p(fdir) unless File.exist?(fdir)
      if File.directory?(fn)
        mkdir_p(f)
      else
        rm_f f
        safe_ln(fn, f)
      end
    end
  end
  self
end

#init(name, version) ⇒ Object

Initialization that bypasses the “yield self” and “define” step.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rake/packagetask.rb', line 96

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_tar_xz = false
  @need_zip = false
  @tar_command = "tar"
  @zip_command = "zip"
  @without_parent_dir = false
end

#package_dir_pathObject

The directory this package will be built in



178
179
180
# File 'lib/rake/packagetask.rb', line 178

def package_dir_path
  "#{package_dir}/#{package_name}"
end

#package_nameObject

The name of this package



172
173
174
# File 'lib/rake/packagetask.rb', line 172

def package_name
  @version ? "#{@name}-#{@version}" : @name
end

#tar_bz2_fileObject

The package name with .tar.bz2 added



196
197
198
# File 'lib/rake/packagetask.rb', line 196

def tar_bz2_file
  "#{package_name}.tar.bz2"
end

#tar_gz_fileObject

The package name with .tar.gz added



190
191
192
# File 'lib/rake/packagetask.rb', line 190

def tar_gz_file
  "#{package_name}.tar.gz"
end

#tar_xz_fileObject

The package name with .tar.xz added



202
203
204
# File 'lib/rake/packagetask.rb', line 202

def tar_xz_file
  "#{package_name}.tar.xz"
end

#target_dirObject

target directory relative to working_dir



217
218
219
# File 'lib/rake/packagetask.rb', line 217

def target_dir
  without_parent_dir ? "." : package_name
end

#tgz_fileObject

The package name with .tgz added



184
185
186
# File 'lib/rake/packagetask.rb', line 184

def tgz_file
  "#{package_name}.tgz"
end

#working_dirObject



212
213
214
# File 'lib/rake/packagetask.rb', line 212

def working_dir
  without_parent_dir ? package_dir_path : package_dir
end

#zip_fileObject

The package name with .zip added



208
209
210
# File 'lib/rake/packagetask.rb', line 208

def zip_file
  "#{package_name}.zip"
end