This repository has been archived by the owner on Apr 14, 2021. It is now read-only.
/
builders.rb
257 lines (213 loc) · 6.73 KB
/
builders.rb
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
module Spec
module Builders
def build_repo1
build_repo gem_repo1 do
build_gem "rake", "0.8.7" do |s|
s.executables = "rake"
end
build_gem "rack", %w(0.9.1 1.0.0) do |s|
s.executables = "rackup"
end
build_gem "rails", "2.3.2" do |s|
s.executables = "rails"
s.add_dependency "rake"
s.add_dependency "actionpack", "2.3.2"
s.add_dependency "activerecord", "2.3.2"
s.add_dependency "actionmailer", "2.3.2"
s.add_dependency "activeresource", "2.3.2"
end
build_gem "actionpack", "2.3.2" do |s|
s.add_dependency "activesupport", "2.3.2"
end
build_gem "activerecord", "2.3.2" do |s|
s.add_dependency "activesupport", "2.3.2"
end
build_gem "actionmailer", "2.3.2" do |s|
s.add_dependency "activesupport", "2.3.2"
end
build_gem "activeresource", "2.3.2" do |s|
s.add_dependency "activesupport", "2.3.2"
end
build_gem "activesupport", "2.3.2"
build_gem "missing_dep" do |s|
s.add_dependency "not_here"
end
build_gem "rspec", "1.2.7", :no_default => true do |s|
s.write "lib/spec.rb", "SPEC = '1.2.7'"
end
build_gem "rack-test", :no_default => true do |s|
s.write "lib/rack/test.rb", "RACK_TEST = '1.0'"
end
build_gem "platform_specific" do |s|
s.platform = "java"
s.write "lib/platform_specific.rb", "PLATFORM_SPECIFIC = '1.0.0 JAVA'"
end
build_gem "platform_specific" do |s|
s.platform = "ruby"
s.write "lib/platform_specific.rb", "PLATFORM_SPECIFIC = '1.0.0 RUBY'"
end
build_gem "only_java" do |s|
s.platform = "java"
end
build_gem "very-simple"
build_gem "very-simple-prerelease", "1.0.pre"
build_gem "very_simple_binary" do |s|
s.require_paths << 'ext'
s.extensions << "ext/extconf.rb"
s.write "ext/extconf.rb", <<-RUBY
require "mkmf"
exit 1 unless with_config("simple")
extension_name = "very_simple_binary_c"
dir_config extension_name
create_makefile extension_name
RUBY
s.write "ext/very_simple_binary.c", <<-C
#include "ruby.h"
void Init_very_simple_binary_c() {
rb_define_module("VerySimpleBinaryInC");
}
C
end
end
end
def build_repo2
FileUtils.rm_rf gem_repo2
FileUtils.cp_r gem_repo1, gem_repo2
end
def update_repo2
update_repo gem_repo2 do
build_gem "rack", "1.2" do |s|
s.executables = "rackup"
end
end
end
def build_repo(path, &blk)
return if File.directory?(path)
update_repo(path, &blk)
end
def update_repo(path)
@_build_path = "#{path}/gems"
yield
@_build_path = nil
Dir.chdir(path) { gem_command :generate_index }
end
def build_index(&block)
index = Gem::SourceIndex.new
IndexBuilder.run(index, &block) if block_given?
index
end
def build_spec(name, version, platform = nil, &block)
spec = Gem::Specification.new
spec.instance_variable_set(:@name, name)
spec.instance_variable_set(:@version, Gem::Version.new(version))
spec.platform = Gem::Platform.new(platform) if platform
DepBuilder.run(spec, &block) if block_given?
spec
end
def build_dep(name, requirements = Gem::Requirement.default, type = :runtime)
Bundler::Dependency.new(name, :version => requirements)
end
def build_lib(name, *args, &blk)
build_with(LibBuilder, name, args, &blk)
end
def build_gem(name, *args, &blk)
build_with(GemBuilder, name, args, &blk)
end
private
def build_with(builder, name, args, &blk)
options = args.last.is_a?(Hash) ? args.pop : {}
versions = args.last || "1.0"
options[:path] ||= @_build_path
Array(versions).each do |version|
spec = builder.new(self, name, version)
yield spec if block_given?
spec._build(options)
end
end
class IndexBuilder
include Builders
def self.run(index, &block)
new(index).run(&block)
end
def initialize(index)
@index = index
end
def run(&block)
instance_eval(&block)
end
def add_spec(*args, &block)
@index.add_spec(build_spec(*args, &block))
end
end
class DepBuilder
def self.run(spec, &block)
new(spec).run(&block)
end
def initialize(spec)
@spec = spec
end
def run(&block)
instance_eval(&block)
end
def runtime(name, requirements)
@spec.add_runtime_dependency(name, requirements)
end
end
class LibBuilder
def initialize(context, name, version)
@context = context
@name = name
@spec = Gem::Specification.new do |s|
s.name = name
s.version = version
s.summary = "This is just a fake gem for testing"
end
@files = {}
@default_files = { "lib/#{name}.rb" => "#{name.gsub('-', '').upcase} = '#{version}'" }
end
def method_missing(*args, &blk)
@spec.send(*args, &blk)
end
def write(file, source)
@files[file] = source
end
def executables=(val)
Array(val).each do |file|
write "bin/#{file}", "require '#{@name}' ; puts #{@name.upcase}"
end
@spec.executables = Array(val)
end
def _build(options)
path = options[:path] || _default_path
@files["#{name}.gemspec"] = @spec.to_ruby if options[:gemspec]
unless options[:no_default]
@files = @default_files.merge(@files)
end
@files.each do |file, source|
file = Pathname.new(path).join(file)
FileUtils.mkdir_p(file.dirname)
File.open(file, 'w') { |f| f.puts source }
end
@spec.files = @files.keys
path
end
def _default_path
@context.tmp_path('libs', @spec.full_name)
end
end
class GemBuilder < LibBuilder
def _build(opts)
lib_path = super(:path => @context.tmp_path(".tmp/#{@spec.full_name}"), :no_default => opts[:no_default])
Dir.chdir(lib_path) do
destination = opts[:path] || _default_path
FileUtils.mkdir_p(destination)
Gem::Builder.new(@spec).build
FileUtils.mv("#{@spec.full_name}.gem", opts[:path] || _default_path)
end
end
def _default_path
@context.gem_repo1('gems')
end
end
end
end