2 # Copyright (C) 2004 Mauricio Julio Fernández Pradier
3 # See LICENSE.txt for additional licensing information.
6 require 'rubygems/package'
9 # TarOutput is a wrapper to TarWriter that builds gem-format tar file.
11 # Gem-format tar files contain the following files:
12 # [data.tar.gz] A gzipped tar file containing the files that compose the gem
13 # which will be extracted into the gem/ dir on installation.
14 # [metadata.gz] A YAML format Gem::Specification.
15 # [data.tar.gz.sig] A signature for the gem's data.tar.gz.
16 # [metadata.gz.sig] A signature for the gem's metadata.gz.
18 # See TarOutput::open for usage details.
20 class Gem::Package::TarOutput
23 # Creates a new TarOutput which will yield a TarWriter object for the
24 # data.tar.gz portion of a gem-format tar file.
26 # See #initialize for details on +io+ and +signer+.
28 # See #add_gem_contents for details on adding metadata to the tar file.
30 def self.open(io, signer = nil, &block) # :yield: data_tar_writer
31 tar_outputter = new io, signer
32 tar_outputter.add_gem_contents(&block)
33 tar_outputter.add_metadata
34 tar_outputter.add_signatures
41 # Creates a new TarOutput that will write a gem-format tar file to +io+. If
42 # +signer+ is given, the data.tar.gz and metadata.gz will be signed and
43 # the signatures will be added to the tar file.
45 def initialize(io, signer)
49 @tar_writer = Gem::Package::TarWriter.new @io
58 # Yields a TarWriter for the data.tar.gz inside a gem-format tar file.
59 # The yielded TarWriter has been extended with a #metadata= method for
60 # attaching a YAML format Gem::Specification which will be written by
64 @tar_writer.add_file "data.tar.gz", 0644 do |inner|
65 sio = @signer ? StringIO.new : nil
66 Zlib::GzipWriter.wrap(sio || inner) do |os|
68 Gem::Package::TarWriter.new os do |data_tar_writer|
69 def data_tar_writer.metadata() @metadata end
70 def data_tar_writer.metadata=(metadata) @metadata = metadata end
74 @metadata = data_tar_writer.metadata
78 # if we have a signing key, then sign the data
79 # digest and return the signature
81 digest = Gem::Security::OPT[:dgst_algo].digest sio.string
82 @data_signature = @signer.sign digest
83 inner.write sio.string
91 # Adds metadata.gz to the gem-format tar file which was saved from a
92 # previous #add_gem_contents call.
95 return if @metadata.nil?
97 @tar_writer.add_file "metadata.gz", 0644 do |io|
99 sio = @signer ? StringIO.new : nil
100 gzos = Zlib::GzipWriter.new(sio || io)
106 # if we have a signing key, then sign the metadata digest and return
109 digest = Gem::Security::OPT[:dgst_algo].digest sio.string
110 @meta_signature = @signer.sign digest
118 # Adds data.tar.gz.sig and metadata.gz.sig to the gem-format tar files if
119 # a Gem::Security::Signer was sent to initialize.
122 if @data_signature then
123 @tar_writer.add_file 'data.tar.gz.sig', 0644 do |io|
124 io.write @data_signature
128 if @meta_signature then
129 @tar_writer.add_file 'metadata.gz.sig', 0644 do |io|
130 io.write @meta_signature
136 # Closes the TarOutput.