require 'reap/task' begin require 'rubygems' rescue # no rubygems end ## # Package Task # # This task creates standard .zip, .tgz, or .tbz # packages, plus .gem distributions. # class Reap::Package < Reap::Task attr_accessor :dir, :name, :date, :series, :status, :author, :email, :summary, :project, :homepage, :exclude, :types, :platform, :autorequire, :dependencies, :requirements, :executables attr_reader :package_name, :include MUST_EXCLUDE = [ 'InstalledFiles', '**/CVS/**/*', '**/*~' ] def task_desc "Build distribution packages (#{(types||[]).join(', ')})." end def init @dir ||= ( File.directory?('../leaf') ? '../leaf' : 'pkg') @name ||= master['name'] @status ||= master['status'] || 'beta/stable' @date ||= master['date'] || Time.now.strftime("%Y-%m-%d") @series ||= master['series'] @version ||= master['version'] unless @series || @version puts "Series or Version is a required field." exit 0 end if @version @package_name ||= @name + '-' + @version else @package_name ||= @name + ',' + @date end @author ||= master['author'] @email ||= master['email'] @summary ||= master['summary'] @project ||= master['rubyforge']['project'] @homepage ||= master['homepage'] || master['rubyforge']['homepage'] @exclude ||= [] @exclude |= MUST_EXCLUDE # types include 'tgz', 'tbz', 'zip', 'tar.gz' 'tar.bz2' and 'gem'. @types ||= master['distribute'] || [ 'gem', 'tar.gz', 'zip' ] @types.collect! { |t| t.to_s.strip.downcase } # not setable @include ||= ['**/*'] # gem specific (at least in this context) if defined?(Gem) if @platform begin @gem_platform = ::Gem.const_get(@platform) rescue NameError @platform = nil end end @platform = ::Gem::Platform::RUBY unless @plaform #@autorequire @requirements ||= master['requirements'] || [] @dependencies ||= master['dependencies'] || [] @executables ||= master['executables'] || [] end end def run # create package image package_dir_path = "#{@dir}/#{@package_name}" if FileTest.directory?(package_dir_path) print "Package '#{package_name}' already exists. Clobber? [y/N] " until inp = $stdin.gets[0,1] ; sleep 1 ; end if (inp || 'y').downcase == 'y' puts "Removing old directory '#{File.expand_path(package_dir_path)}'..." FileUtils.rm_r(package_dir_path) #FileUtils.rm("#{package_dir_path}/*") #trash_dir = ".trash/#{File.basename(@dir)}" #FileUtils.mkdir_p(".trash") unless FileTest.directory?(".trash") #FileUtils.mv(@dir, trash_dir) else puts "Reap package task canceled." exit 0 end end package_files = FileList.new package_files.include(*@include) package_files.exclude(*@exclude) if @exclude and not @exclude.empty? FileUtils.mkdir_p @dir #rescue nil package_files.each do |f| pkgf = File.join(package_dir_path, f) fdir = File.dirname(pkgf) FileUtils.mkdir_p(fdir) if not File.exist?(fdir) if File.directory?(f) FileUtils.mkdir_p(pkgf) else FileUtils.rm_f(pkgf) FileUtils.safe_ln(f, pkgf) end end # create standard package files FileUtils.chdir(@dir) do @types.each do |t| sh_cmd = nil prefix = 'ERROR' # in case of unforseen bug case t when 'tbz', 'bz2', 'bzip2', 'tar.bz2' prefix = ( t == 'tbz' ? 'tbz' : 'tar.bz2' ) sh_cmd = 'tar --bzip2 -cvf' puts "\nReap is shelling out work to tar and bzip2..." when 'tgz', 'tar.gz' prefix = ( t == 'tgz' ? 'tgz' : 'tar.gz' ) sh_cmd = 'tar --gzip -cvf' puts "\nReap is shelling out work to tar and gzip..." when 'zip' prefix = 'zip' sh_cmd = 'zip -r' puts "\nReap is shelling out work to zip..." when 'gem' sh_cmd = nil else puts "Unknown package type '#{t}' skipped." sh_cmd = nil end sh %{#{sh_cmd} #{@package_name}.#{prefix} #{@package_name}} if sh_cmd end puts end # create gem package run_gem if defined?(Gem) and @types.include?('gem') return true end def run_gem spec = Gem::Specification.new { |s| s.name = @name if @version s.version = @version else s.version = @series + '.0' end @dependencies.each{ |d,v| if v s.add_dependency(d, v) else s.add_dependency(d) end } s.platform = @platform s.summary = @summary s.requirements = @requirements # s.files = Dir.glob("lib/**/*").delete_if {|item| item.include?("CVS")} # s.files.concat Dir.glob("bin/**/*").delete_if {|item| item.include?("CVS")} package_files = FileList.new package_files.include(*@include) package_files.exclude(*@exclude) if @exclude and not @exclude.empty? s.files = package_files.to_a s.require_path = 'lib' s.autorequire = @autorequire if @autorequire s.author = @author s.email = @email s.rubyforge_project = @project s.homepage = @homepage s.executables = @executables s.bindir = "bin" } puts "Reap is shelling out work to the Gem Package Manager..." Gem.manage_gems Gem::Builder.new(spec).build sh %{mv ./*.gem #{@dir}/} end end