1# Copyright (c) 2003, 2004 Jim Weirich, 2009 Eric Hodel
2#
3# Permission is hereby granted, free of charge, to any person obtaining
4# a copy of this software and associated documentation files (the
5# "Software"), to deal in the Software without restriction, including
6# without limitation the rights to use, copy, modify, merge, publish,
7# distribute, sublicense, and/or sell copies of the Software, and to
8# permit persons to whom the Software is furnished to do so, subject to
9# the following conditions:
10#
11# The above copyright notice and this permission notice shall be
12# included in all copies or substantial portions of the Software.
13#
14# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22require 'rubygems'
23require 'rubygems/package'
24begin
25  gem 'rake'
26rescue Gem::LoadError
27end
28
29require 'rake/packagetask'
30
31##
32# Create a package based upon a Gem::Specification.  Gem packages, as well as
33# zip files and tar/gzipped packages can be produced by this task.
34#
35# In addition to the Rake targets generated by Rake::PackageTask, a
36# Gem::PackageTask will also generate the following tasks:
37#
38# [<b>"<em>package_dir</em>/<em>name</em>-<em>version</em>.gem"</b>]
39#   Create a RubyGems package with the given name and version.
40#
41# Example using a Gem::Specification:
42#
43#   require 'rubygems'
44#   require 'rubygems/package_task'
45#
46#   spec = Gem::Specification.new do |s|
47#     s.summary = "Ruby based make-like utility."
48#     s.name = 'rake'
49#     s.version = PKG_VERSION
50#     s.requirements << 'none'
51#     s.files = PKG_FILES
52#     s.description = <<-EOF
53#   Rake is a Make-like program implemented in Ruby. Tasks
54#   and dependencies are specified in standard Ruby syntax.
55#     EOF
56#   end
57#
58#   Gem::PackageTask.new(spec) do |pkg|
59#     pkg.need_zip = true
60#     pkg.need_tar = true
61#   end
62
63class Gem::PackageTask < Rake::PackageTask
64
65  ##
66  # Ruby Gem::Specification containing the metadata for this package.  The
67  # name, version and package_files are automatically determined from the
68  # gemspec and don't need to be explicitly provided.
69
70  attr_accessor :gem_spec
71
72  ##
73  # Create a Gem Package task library.  Automatically define the gem if a
74  # block is given.  If no block is supplied, then #define needs to be called
75  # to define the task.
76
77  def initialize(gem_spec)
78    init gem_spec
79    yield self if block_given?
80    define if block_given?
81  end
82
83  ##
84  # Initialization tasks without the "yield self" or define operations.
85
86  def init(gem)
87    super gem.full_name, :noversion
88    @gem_spec = gem
89    @package_files += gem_spec.files if gem_spec.files
90  end
91
92  ##
93  # Create the Rake tasks and actions specified by this Gem::PackageTask.
94  # (+define+ is automatically called if a block is given to +new+).
95
96  def define
97    super
98
99    gem_file = File.basename gem_spec.cache_file
100    gem_path = File.join package_dir, gem_file
101    gem_dir  = File.join package_dir, gem_spec.full_name
102
103    task :package => [:gem]
104
105    directory package_dir
106    directory gem_dir
107
108    desc "Build the gem file #{gem_file}"
109    task :gem => [gem_path]
110
111    trace = Rake.application.options.trace
112    Gem.configuration.verbose = trace
113
114    file gem_path => [package_dir, gem_dir] + @gem_spec.files do
115      chdir(gem_dir) do
116        when_writing "Creating #{gem_spec.file_name}" do
117          Gem::Package.build gem_spec
118
119          verbose trace do
120            mv gem_file, '..'
121          end
122        end
123      end
124    end
125  end
126
127end
128
129