blob: 5815dcda8767a98c79be7f0b297a7852290d1c02 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
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
##
# 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.
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
@files << @filename
File.open file_path, "wb" do |file|
@file = file
start_index
yield
end_index
end
cleanup
ensure
@file = nil
end
##
# Compress the given file.
def compress(filename, ext="rz")
data = open filename, 'rb' do |fp| fp.read end
zipped = zip data
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
|