Class: Pallet::Gem

Inherits:
Object
  • Object
show all
Defined in:
lib/pallet/gem.rb

Overview

Pallet::Gem

Automatic rubygem builder. Lets users automagically build a gem after specifying a few customizations.

Usage

Uses several sane defaults, so in many cases, the specification could be written simply as:

Pallet::Gem.new(pallet)

Which is identical to the following example:

Pallet::Gem.new(pallet) do |g|
  g.files       = FileList['lib/**/*', '[A-Z]*']
  g.executables = FileList['bin/*']
  g.tests       = FileList['test/**/*']
  g.readme      = 'README'
end

These can be changed, and other attributes can be set.

Pallet::Gem.new(pallet) do |g|
  g.files  = FileList['**/*']
  g.readme = 'Readme.txt'
  g.depends      << 'log4r' << 'rake' << 'pallet'
  g.requirements << 'Debian Linux' << 'a fast processor'
end

Dependencies are passed directly to the Gem::Specification backend, so they can be used as GemSpec-style arrays, if you need explicit version information.

Pallet::Gem.new(pallet) do |g|
  g.depends << ['log4r', '>= 1.4.2']
  g.depends << ['pallet', >= 1.0.0']
end

Caveats

* Due to limitations in the gem specification format, executables
  _must_ all reside in the same directory -- by default, 'bin/'.

Constant Summary collapse

DEFAULT_FILES =
FileList['lib/**/*']
DEFAULT_EXECUTABLES =
FileList['bin/*']
DEFAULT_TESTS =
FileList['test/**/*']
DEFAULT_DOCS =
%w{readme todo changelog}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pallet) {|_self| ... } ⇒ Gem

Creates a new specification for building a Gem. Yields itself to the block passed for initialization.

Yields:

  • (_self)

Yield Parameters:

  • _self (Pallet::Gem)

    the object that the method was called on



105
106
107
108
109
110
111
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
# File 'lib/pallet/gem.rb', line 105

def initialize(pallet)

  # the name of our rake task
  self.task_name = :gem
  self.dest_dir  = 'pkg'
  self.platform  = ::Gem::Platform::RUBY

  # set up defaults
  self.files         = DEFAULT_FILES
  self.executables   = DEFAULT_EXECUTABLES
  self.tests         = DEFAULT_TESTS
  self.docs          = Dir['*'].find_all {|f| DEFAULT_DOCS.include? f.downcase }
  self.depends       = []
  self.requirements  = []
  self.prerequisites = []

  yield self if block_given?

  # build the specification
  @spec = ::Gem::Specification.new do |s|

    # inherited values from the pallet
    s.name        = pallet.name
    s.version     = pallet.version
    s.author      = pallet.author
    s.email       = pallet.email
    s.summary     = pallet.summary
    s.description = pallet.description

    s.platform    = self.platform 

    # aggregate list of files to include
    s.files = files | executables | tests | docs

    # include binary files
    unless self.executables.empty?
      dirs          = self.executables.map {|f| File.dirname(f) }.uniq
      dirs.length  == 1 or raise RuntimeError, 'all binaries in a Gem must be in the same directory'
      s.bindir      = dirs.first
      s.executables = self.executables.map {|f| File.basename(f) }
    end

    # include tests
    s.test_files = self.tests

    # include additional documentation
    s.has_rdoc         = true
    s.extra_rdoc_files = self.docs
    s.rdoc_options << '-S' << '-N'
    unless self.docs.empty?
      s.rdoc_options << '--main' << self.docs.first
    end

    # add dependencies
    self.depends.each {|dependency| s.add_dependency(*dependency) }
    s.requirements = self.requirements

  end

end

Instance Attribute Details

#dependsObject

All rubygems this Gem should depend on.



86
87
88
# File 'lib/pallet/gem.rb', line 86

def depends
  @depends
end

#dest_dirObject

The directory the package will be built into



57
58
59
# File 'lib/pallet/gem.rb', line 57

def dest_dir
  @dest_dir
end

#docsObject

A list of additional documentation that should be included along with the automatically generated rdoc pages. The first one given will be the “default” rdoc page.



83
84
85
# File 'lib/pallet/gem.rb', line 83

def docs
  @docs
end

#executablesObject

A list of executables contained in the gem. All of these must be in the same directory. An exception will be thrown if they are not.



75
76
77
# File 'lib/pallet/gem.rb', line 75

def executables
  @executables
end

#filenameObject

The filename of the package, when built



60
61
62
# File 'lib/pallet/gem.rb', line 60

def filename
  @filename
end

#filesObject

A list of the files to be contained in the package. Does not have to contain files implied elsewhere (for instance, docs, executables, or tests).



70
71
72
# File 'lib/pallet/gem.rb', line 70

def files
  @files
end

#platformObject

The target platform of the Gem. Defaults to the sane default of Gem::Platform::RUBY. See the rdoc for Gem::Platform for other reasonable values.



65
66
67
# File 'lib/pallet/gem.rb', line 65

def platform
  @platform
end

#prerequisitesObject

Array of additional tasks which should be completed before the Gem is built.



94
95
96
# File 'lib/pallet/gem.rb', line 94

def prerequisites
  @prerequisites
end

#requirementsObject

Any additional requirements not satisfiable through rubygems. These are currently ignored.



90
91
92
# File 'lib/pallet/gem.rb', line 90

def requirements
  @requirements
end

#task_nameObject

The name of the rake task we generate. Defaults to ‘gem’.



54
55
56
# File 'lib/pallet/gem.rb', line 54

def task_name
  @task_name
end

#testsObject

A list of all test scripts which should be contained in the Gem.



78
79
80
# File 'lib/pallet/gem.rb', line 78

def tests
  @tests
end

Instance Method Details

#defineObject

Defines the rake task for creating a gem.



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/pallet/gem.rb', line 169

def define

  filename = @spec.full_name + '.gem'
  dest     = File.join(self.dest_dir, filename)

  # create a task for the gem 
  desc 'Build this project into a gem'
  task      self.task_name => self.prerequisites + [dest]
  directory self.dest_dir

  # task to build the package
  file dest => [self.dest_dir] + @spec.files do
    when_writing("Building Rubygem") do
      b = ::Gem::Builder.new(@spec)
      b.use_ui(::Gem::SilentUI.new) { b.build }
      mv filename, self.dest_dir
    end
  end

  # clobber task
  task 'clobber'     => 'clobber:gem'
  task 'clobber:gem' do
    rm dest rescue nil
    rmdir self.dest_dir rescue nil
  end

end