2007-11-10 02:48:56 -05:00
|
|
|
#--
|
|
|
|
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
|
|
|
|
# All rights reserved.
|
|
|
|
# See LICENSE.txt for permissions.
|
|
|
|
#++
|
|
|
|
|
2011-01-31 22:11:34 -05:00
|
|
|
require 'rubygems'
|
2010-04-22 04:24:42 -04:00
|
|
|
require 'rubygems/user_interaction'
|
|
|
|
|
2011-01-18 19:08:49 -05:00
|
|
|
Gem.load_yaml
|
|
|
|
|
|
|
|
require 'rubygems/package'
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
##
|
|
|
|
# The Builder class processes RubyGem specification files
|
|
|
|
# to produce a .gem file.
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
class Gem::Builder
|
|
|
|
|
|
|
|
include Gem::UserInteraction
|
2010-02-21 21:52:35 -05:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
##
|
2009-06-09 17:38:59 -04:00
|
|
|
# Constructs a builder instance for the provided specification
|
2007-11-10 02:48:56 -05:00
|
|
|
#
|
2009-06-09 17:38:59 -04:00
|
|
|
# spec:: [Gem::Specification] The specification instance
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def initialize(spec)
|
|
|
|
@spec = spec
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Builds the gem from the specification. Returns the name of the file
|
|
|
|
# written.
|
|
|
|
|
|
|
|
def build
|
|
|
|
@spec.mark_version
|
|
|
|
@spec.validate
|
|
|
|
@signer = sign
|
|
|
|
write_package
|
2010-02-21 21:52:35 -05:00
|
|
|
say success if Gem.configuration.verbose
|
2011-05-31 23:45:05 -04:00
|
|
|
File.basename @spec.cache_file
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
2009-03-05 22:56:38 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
def success
|
|
|
|
<<-EOM
|
2007-11-10 02:48:56 -05:00
|
|
|
Successfully built RubyGem
|
|
|
|
Name: #{@spec.name}
|
|
|
|
Version: #{@spec.version}
|
2011-05-31 23:45:05 -04:00
|
|
|
File: #{File.basename @spec.cache_file}
|
2007-11-10 02:48:56 -05:00
|
|
|
EOM
|
2009-06-09 17:38:59 -04:00
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
##
|
|
|
|
# If the signing key was specified, then load the file, and swap to the
|
|
|
|
# public key (TODO: we should probably just omit the signing key in favor of
|
|
|
|
# the signing certificate, but that's for the future, also the signature
|
|
|
|
# algorithm should be configurable)
|
|
|
|
|
|
|
|
def sign
|
|
|
|
signer = nil
|
|
|
|
|
|
|
|
if @spec.respond_to?(:signing_key) and @spec.signing_key then
|
2011-01-19 16:23:04 -05:00
|
|
|
require 'rubygems/security'
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
signer = Gem::Security::Signer.new @spec.signing_key, @spec.cert_chain
|
|
|
|
@spec.signing_key = nil
|
|
|
|
@spec.cert_chain = signer.cert_chain.map { |cert| cert.to_s }
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
signer
|
|
|
|
end
|
|
|
|
|
|
|
|
def write_package
|
2011-05-31 23:45:05 -04:00
|
|
|
file_name = File.basename @spec.cache_file
|
|
|
|
open file_name, 'wb' do |gem_io|
|
2009-06-09 17:38:59 -04:00
|
|
|
Gem::Package.open gem_io, 'w', @signer do |pkg|
|
2011-01-18 19:08:49 -05:00
|
|
|
yaml = @spec.to_yaml
|
|
|
|
pkg.metadata = yaml
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
@spec.files.each do |file|
|
2011-05-31 23:45:05 -04:00
|
|
|
next if File.directory?(file)
|
|
|
|
next if file == file_name # Don't add gem onto itself
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
stat = File.stat(file)
|
2009-06-09 17:38:59 -04:00
|
|
|
mode = stat.mode & 0777
|
|
|
|
size = stat.size
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
pkg.add_file_simple file, mode, size do |tar_io|
|
|
|
|
tar_io.write open(file, "rb") { |f| f.read }
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2010-02-21 21:52:35 -05:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|