2007-11-10 02:48:56 -05:00
|
|
|
require 'zlib'
|
|
|
|
|
|
|
|
require 'rubygems/indexer'
|
|
|
|
|
|
|
|
# Abstract base class for building gem indicies. Uses the template pattern
|
|
|
|
# with subclass specialization in the +begin_index+, +end_index+ and +cleanup+
|
|
|
|
# methods.
|
|
|
|
class Gem::Indexer::AbstractIndexBuilder
|
|
|
|
|
|
|
|
# Directory to put index files in
|
|
|
|
attr_reader :directory
|
|
|
|
|
|
|
|
# File name of the generated index
|
|
|
|
attr_reader :filename
|
|
|
|
|
|
|
|
# List of written files/directories to move into production
|
|
|
|
attr_reader :files
|
|
|
|
|
|
|
|
def initialize(filename, directory)
|
|
|
|
@filename = filename
|
|
|
|
@directory = directory
|
|
|
|
@files = []
|
|
|
|
end
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
##
|
2007-11-10 02:48:56 -05:00
|
|
|
# Build a Gem index. Yields to block to handle the details of the
|
|
|
|
# actual building. Calls +begin_index+, +end_index+ and +cleanup+ at
|
|
|
|
# appropriate times to customize basic operations.
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def build
|
|
|
|
FileUtils.mkdir_p @directory unless File.exist? @directory
|
|
|
|
raise "not a directory: #{@directory}" unless File.directory? @directory
|
|
|
|
|
|
|
|
file_path = File.join @directory, @filename
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
@files << @filename
|
2007-11-10 02:48:56 -05:00
|
|
|
|
|
|
|
File.open file_path, "wb" do |file|
|
|
|
|
@file = file
|
|
|
|
start_index
|
|
|
|
yield
|
|
|
|
end_index
|
|
|
|
end
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
cleanup
|
|
|
|
ensure
|
|
|
|
@file = nil
|
|
|
|
end
|
|
|
|
|
2008-03-31 18:40:06 -04:00
|
|
|
##
|
2007-11-10 02:48:56 -05:00
|
|
|
# Compress the given file.
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def compress(filename, ext="rz")
|
2008-03-31 18:40:06 -04:00
|
|
|
data = open filename, 'rb' do |fp| fp.read end
|
|
|
|
|
|
|
|
zipped = zip data
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
File.open "#{filename}.#{ext}", "wb" do |file|
|
|
|
|
file.write zipped
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Called immediately before the yield in build. The index file is open and
|
|
|
|
# available as @file.
|
|
|
|
def start_index
|
|
|
|
end
|
|
|
|
|
|
|
|
# Called immediately after the yield in build. The index file is still open
|
|
|
|
# and available as @file.
|
|
|
|
def end_index
|
|
|
|
end
|
|
|
|
|
|
|
|
# Called from within builder after the index file has been closed.
|
|
|
|
def cleanup
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return an uncompressed version of a compressed string.
|
|
|
|
def unzip(string)
|
|
|
|
Zlib::Inflate.inflate(string)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Return a compressed version of the given string.
|
|
|
|
def zip(string)
|
|
|
|
Zlib::Deflate.deflate(string)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|