packagetask.rb 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. # Define a package task library to aid in the definition of
  2. # redistributable package files.
  3. require 'rake'
  4. require 'rake/tasklib'
  5. module Rake
  6. # Create a packaging task that will package the project into
  7. # distributable files (e.g zip archive or tar files).
  8. #
  9. # The PackageTask will create the following targets:
  10. #
  11. # [<b>:package</b>]
  12. # Create all the requested package files.
  13. #
  14. # [<b>:clobber_package</b>]
  15. # Delete all the package files. This target is automatically
  16. # added to the main clobber target.
  17. #
  18. # [<b>:repackage</b>]
  19. # Rebuild the package files from scratch, even if they are not out
  20. # of date.
  21. #
  22. # [<b>"<em>package_dir</em>/<em>name</em>-<em>version</em>.tgz"</b>]
  23. # Create a gzipped tar package (if <em>need_tar</em> is true).
  24. #
  25. # [<b>"<em>package_dir</em>/<em>name</em>-<em>version</em>.tar.gz"</b>]
  26. # Create a gzipped tar package (if <em>need_tar_gz</em> is true).
  27. #
  28. # [<b>"<em>package_dir</em>/<em>name</em>-<em>version</em>.tar.bz2"</b>]
  29. # Create a bzip2'd tar package (if <em>need_tar_bz2</em> is true).
  30. #
  31. # [<b>"<em>package_dir</em>/<em>name</em>-<em>version</em>.zip"</b>]
  32. # Create a zip package archive (if <em>need_zip</em> is true).
  33. #
  34. # Example:
  35. #
  36. # Rake::PackageTask.new("rake", "1.2.3") do |p|
  37. # p.need_tar = true
  38. # p.package_files.include("lib/**/*.rb")
  39. # end
  40. #
  41. class PackageTask < TaskLib
  42. # Name of the package (from the GEM Spec).
  43. attr_accessor :name
  44. # Version of the package (e.g. '1.3.2').
  45. attr_accessor :version
  46. # Directory used to store the package files (default is 'pkg').
  47. attr_accessor :package_dir
  48. # True if a gzipped tar file (tgz) should be produced (default is false).
  49. attr_accessor :need_tar
  50. # True if a gzipped tar file (tar.gz) should be produced (default is false).
  51. attr_accessor :need_tar_gz
  52. # True if a bzip2'd tar file (tar.bz2) should be produced (default is false).
  53. attr_accessor :need_tar_bz2
  54. # True if a zip file should be produced (default is false)
  55. attr_accessor :need_zip
  56. # List of files to be included in the package.
  57. attr_accessor :package_files
  58. # Tar command for gzipped or bzip2ed archives. The default is 'tar'.
  59. attr_accessor :tar_command
  60. # Zip command for zipped archives. The default is 'zip'.
  61. attr_accessor :zip_command
  62. # Create a Package Task with the given name and version. Use +:noversion+
  63. # as the version to build a package without a version or to provide a
  64. # fully-versioned package name.
  65. def initialize(name=nil, version=nil)
  66. init(name, version)
  67. yield self if block_given?
  68. define unless name.nil?
  69. end
  70. # Initialization that bypasses the "yield self" and "define" step.
  71. def init(name, version)
  72. @name = name
  73. @version = version
  74. @package_files = Rake::FileList.new
  75. @package_dir = 'pkg'
  76. @need_tar = false
  77. @need_tar_gz = false
  78. @need_tar_bz2 = false
  79. @need_zip = false
  80. @tar_command = 'tar'
  81. @zip_command = 'zip'
  82. end
  83. # Create the tasks defined by this task library.
  84. def define
  85. fail "Version required (or :noversion)" if @version.nil?
  86. @version = nil if :noversion == @version
  87. desc "Build all the packages"
  88. task :package
  89. desc "Force a rebuild of the package files"
  90. task :repackage => [:clobber_package, :package]
  91. desc "Remove package products"
  92. task :clobber_package do
  93. rm_r package_dir rescue nil
  94. end
  95. task :clobber => [:clobber_package]
  96. [
  97. [need_tar, tgz_file, "z"],
  98. [need_tar_gz, tar_gz_file, "z"],
  99. [need_tar_bz2, tar_bz2_file, "j"]
  100. ].each do |(need, file, flag)|
  101. if need
  102. task :package => ["#{package_dir}/#{file}"]
  103. file "#{package_dir}/#{file}" => [package_dir_path] + package_files do
  104. chdir(package_dir) do
  105. sh %{#{@tar_command} #{flag}cvf #{file} #{package_name}}
  106. end
  107. end
  108. end
  109. end
  110. if need_zip
  111. task :package => ["#{package_dir}/#{zip_file}"]
  112. file "#{package_dir}/#{zip_file}" => [package_dir_path] + package_files do
  113. chdir(package_dir) do
  114. sh %{#{@zip_command} -r #{zip_file} #{package_name}}
  115. end
  116. end
  117. end
  118. directory package_dir
  119. file package_dir_path => @package_files do
  120. mkdir_p package_dir rescue nil
  121. @package_files.each do |fn|
  122. f = File.join(package_dir_path, fn)
  123. fdir = File.dirname(f)
  124. mkdir_p(fdir) if !File.exist?(fdir)
  125. if File.directory?(fn)
  126. mkdir_p(f)
  127. else
  128. rm_f f
  129. safe_ln(fn, f)
  130. end
  131. end
  132. end
  133. self
  134. end
  135. def package_name
  136. @version ? "#{@name}-#{@version}" : @name
  137. end
  138. def package_dir_path
  139. "#{package_dir}/#{package_name}"
  140. end
  141. def tgz_file
  142. "#{package_name}.tgz"
  143. end
  144. def tar_gz_file
  145. "#{package_name}.tar.gz"
  146. end
  147. def tar_bz2_file
  148. "#{package_name}.tar.bz2"
  149. end
  150. def zip_file
  151. "#{package_name}.zip"
  152. end
  153. end
  154. end