PageRenderTime 138ms CodeModel.GetById 122ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/Ruby/lib/ruby/site_ruby/1.8/rubygems/builder.rb

http://github.com/agross/netopenspace
Ruby | 98 lines | 55 code | 23 blank | 20 comment | 5 complexity | 99d03587ec342102f7f3657a517e7317 MD5 | raw file
 1#--
 2# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
 3# All rights reserved.
 4# See LICENSE.txt for permissions.
 5#++
 6
 7require 'rubygems'
 8require 'rubygems/user_interaction'
 9
10Gem.load_yaml
11
12require 'rubygems/package'
13
14##
15# The Builder class processes RubyGem specification files
16# to produce a .gem file.
17
18class Gem::Builder
19
20  include Gem::UserInteraction
21
22  ##
23  # Constructs a builder instance for the provided specification
24  #
25  # spec:: [Gem::Specification] The specification instance
26
27  def initialize(spec)
28    @spec = spec
29  end
30
31  ##
32  # Builds the gem from the specification.  Returns the name of the file
33  # written.
34
35  def build
36    @spec.mark_version
37    @spec.validate
38    @signer = sign
39    write_package
40    say success if Gem.configuration.verbose
41    @spec.file_name
42  end
43
44  def success
45    <<-EOM
46  Successfully built RubyGem
47  Name: #{@spec.name}
48  Version: #{@spec.version}
49  File: #{@spec.file_name}
50EOM
51  end
52
53  private
54
55  ##
56  # If the signing key was specified, then load the file, and swap to the
57  # public key (TODO: we should probably just omit the signing key in favor of
58  # the signing certificate, but that's for the future, also the signature
59  # algorithm should be configurable)
60
61  def sign
62    signer = nil
63
64    if @spec.respond_to?(:signing_key) and @spec.signing_key then
65      require 'rubygems/security'
66
67      signer = Gem::Security::Signer.new @spec.signing_key, @spec.cert_chain
68      @spec.signing_key = nil
69      @spec.cert_chain = signer.cert_chain.map { |cert| cert.to_s }
70    end
71
72    signer
73  end
74
75  def write_package
76    open @spec.file_name, 'wb' do |gem_io|
77      Gem::Package.open gem_io, 'w', @signer do |pkg|
78        yaml = @spec.to_yaml
79        pkg.metadata = yaml
80
81        @spec.files.each do |file|
82          next if File.directory? file
83          next if file == @spec.file_name # Don't add gem onto itself
84
85          stat = File.stat file
86          mode = stat.mode & 0777
87          size = stat.size
88
89          pkg.add_file_simple file, mode, size do |tar_io|
90            tar_io.write open(file, "rb") { |f| f.read }
91          end
92        end
93      end
94    end
95  end
96
97end
98