/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. require 'rubygems'
  7. require 'rubygems/user_interaction'
  8. Gem.load_yaml
  9. require 'rubygems/package'
  10. ##
  11. # The Builder class processes RubyGem specification files
  12. # to produce a .gem file.
  13. class Gem::Builder
  14. include Gem::UserInteraction
  15. ##
  16. # Constructs a builder instance for the provided specification
  17. #
  18. # spec:: [Gem::Specification] The specification instance
  19. def initialize(spec)
  20. @spec = spec
  21. end
  22. ##
  23. # Builds the gem from the specification. Returns the name of the file
  24. # written.
  25. def build
  26. @spec.mark_version
  27. @spec.validate
  28. @signer = sign
  29. write_package
  30. say success if Gem.configuration.verbose
  31. @spec.file_name
  32. end
  33. def success
  34. <<-EOM
  35. Successfully built RubyGem
  36. Name: #{@spec.name}
  37. Version: #{@spec.version}
  38. File: #{@spec.file_name}
  39. EOM
  40. end
  41. private
  42. ##
  43. # If the signing key was specified, then load the file, and swap to the
  44. # public key (TODO: we should probably just omit the signing key in favor of
  45. # the signing certificate, but that's for the future, also the signature
  46. # algorithm should be configurable)
  47. def sign
  48. signer = nil
  49. if @spec.respond_to?(:signing_key) and @spec.signing_key then
  50. require 'rubygems/security'
  51. signer = Gem::Security::Signer.new @spec.signing_key, @spec.cert_chain
  52. @spec.signing_key = nil
  53. @spec.cert_chain = signer.cert_chain.map { |cert| cert.to_s }
  54. end
  55. signer
  56. end
  57. def write_package
  58. open @spec.file_name, 'wb' do |gem_io|
  59. Gem::Package.open gem_io, 'w', @signer do |pkg|
  60. yaml = @spec.to_yaml
  61. pkg.metadata = yaml
  62. @spec.files.each do |file|
  63. next if File.directory? file
  64. next if file == @spec.file_name # Don't add gem onto itself
  65. stat = File.stat file
  66. mode = stat.mode & 0777
  67. size = stat.size
  68. pkg.add_file_simple file, mode, size do |tar_io|
  69. tar_io.write open(file, "rb") { |f| f.read }
  70. end
  71. end
  72. end
  73. end
  74. end
  75. end