forked from kayakhttp/kayak
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Rakefile.rb
308 lines (263 loc) · 7.99 KB
/
Rakefile.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
PRODUCT = "Kayak"
DESCRIPTION = "Kayak is an event-base IO libary for .NET. Kayak allows you to easily create TCP clients and servers, and contains an HTTP/1.1 server implementation."
VERSION = "0.7.2"
AUTHORS = "Benjamin van der Veen"
COPYRIGHT = "Copyright (c) 2007-2011 Benjamin van der Veen"
LICENSE_URL = "https://github.com/kayak/kayak/raw/HEAD/LICENSE"
PROJECT_URL = "https://github.com/kayak/kayak"
require 'albacore'
require 'uri'
require 'net/http'
require 'net/https'
# Monkey patch Dir.exists? for Ruby 1.8.x
if RUBY_VERSION =~ /^1\.8/
class Dir
class << self
def exists? (path)
File.directory?(path)
end
alias_method :exist?, :exists?
end
end
end
def is_nix
!RUBY_PLATFORM.match("linux|darwin").nil?
end
def invoke_runtime(cmd)
command = cmd
if is_nix()
command = "mono --runtime=v4.0 #{cmd}"
end
command
end
def transform_xml(input, output)
input_file = File.new(input)
xml = REXML::Document.new input_file
yield xml
input_file.close
output_file = File.open(output, "w")
formatter = REXML::Formatters::Default.new()
formatter.write(xml, output_file)
output_file.close
end
def ensure_submodules()
system("git submodule init")
system("git submodule update")
end
def fetch(uri, limit = 10, &block)
# We should choose a better exception.
raise ArgumentError, 'too many HTTP redirects' if limit == 0
http = Net::HTTP.new(uri.host, uri.port)
if uri.scheme == "https"
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
http.use_ssl = true
end
resp = http.request(Net::HTTP::Get.new(uri.request_uri)) { |response|
case response
when Net::HTTPRedirection then
location = response['location']
if block_given? then
fetch(URI(location), limit - 1, &block)
else
fetch(URI(location), limit - 1)
end
return
else
response.read_body do |segment|
yield segment
end
return
end
}
end
def rename_file(oldname, newname)
# Ruby 1.8.7 on Mac sometimes reports File.exist? incorrectly
# so work around this [not sure why that happens]
begin
File.delete(newname)
rescue => msg
# File probably doesn't exist, if it does File.size? will work properly
if File.size?(newname) != nil then
fail "Failed to delete old file #{newname} with: #{msg}"
raise
end
end
File.rename(oldname, newname)
end
def unpack_nuget_pkg(file, destination)
unzip = Unzip.new
unzip.destination = destination
unzip.file = file
unzip.execute
end
def ensure_nuget_package_nix(name)
# NuGet doesn't work on Mono. So we're going to download our dependencies from NuGet.org.
zip_file = PACKAGES[name][:filename]
tmp_file = "#{zip_file}.tmp"
if File.exist?(zip_file) and File.size?(zip_file) != nil then
puts "#{zip_file} already exists, skipping download"
unpack_nuget_pkg(zip_file, PACKAGES[name][:folder])
return
end
puts "fetching #{zip_file}"
File.open(tmp_file, "w") { |f|
uri = URI.parse(PACKAGES[name][:url])
fetch(uri) do |seg|
f.write(seg)
end
}
if File.size?(tmp_file) == nil then
fail "Download failed for #{zip_file}"
end
rename_file(tmp_file, zip_file)
unpack_nuget_pkg(zip_file, PACKAGES[name][:folder])
end
def all_nuget_packages_present?()
result = true
PACKAGES.values.each { |pkg|
if !Dir.exists? pkg[:folder] then
puts "Package missing: #{pkg[:name]}"
result = false
end
}
return result
end
def print_nuget_package_manifest()
puts "Building with NuGet packages:"
PACKAGES.values.each { |pkg|
puts "#{pkg[:name]} => #{pkg[:version]}"
}
end
def ensure_all_nuget_packages_nix()
PACKAGES.values.each { |pkg|
ensure_nuget_package_nix(pkg[:name])
}
end
def read_package_config(filename)
input_file = File.new(filename)
xml = REXML::Document.new input_file
xml.elements.each("packages/package") { |element|
yield element
}
input_file.close
end
def read_nuget_packages()
FileList["**/packages.config"].each { |config_file|
read_package_config(config_file) { |pkg|
name = pkg.attributes["id"]
version = pkg.attributes["version"]
# puts "Read package #{name} with version #{version}"
PACKAGES[name]={}
PACKAGES[name][:name]=name
PACKAGES[name][:version]=version
PACKAGES[name][:folder]="#{PACKAGES_DIR}/#{name}.#{version}"
PACKAGES[name][:filename]="#{PACKAGES_DIR}/#{name}.#{version}.nupkg"
PACKAGES[name][:url]="http://packages.nuget.org/api/v1/package/#{name}/#{version}"
}
}
end
def ensure_nuget_packages()
Dir.mkdir PACKAGES_DIR unless Dir.exists? PACKAGES_DIR
read_nuget_packages
if all_nuget_packages_present?() then
puts "All packages up to date"
print_nuget_package_manifest
return
end
if (is_nix()) then
puts "updating packages with internal nuget replacement"
ensure_all_nuget_packages_nix
print_nuget_package_manifest
else
puts "updating packages with nuget"
sh invoke_runtime("tools\\nuget.exe install Kayak.Tests\\packages.config -o #{PACKAGES_DIR}")
print_nuget_package_manifest
end
end
task :default => [:build, :test]
CONFIGURATION = "Release"
BUILD_DIR = File.expand_path("build")
OUTPUT_DIR = "#{BUILD_DIR}/out"
BIN_DIR = "#{BUILD_DIR}/bin"
PACKAGES_DIR = "packages"
PACKAGES = {}
assemblyinfo :assemblyinfo => :clean do |a|
a.product_name = a.title = PRODUCT
a.description = DESCRIPTION
a.version = a.file_version = VERSION
a.copyright = COPYRIGHT
a.output_file = "Kayak/Properties/AssemblyInfo.cs"
a.namespaces "System.Runtime.CompilerServices"
a.custom_attributes :InternalsVisibleTo => "Kayak.Tests"
end
msbuild :build_msbuild do |b|
b.properties :configuration => CONFIGURATION, "OutputPath" => OUTPUT_DIR
b.targets :Build
b.solution = "Kayak.sln"
end
xbuild :build_xbuild do |b|
b.properties :configuration => CONFIGURATION, "OutputPath" => OUTPUT_DIR
b.targets :Build
b.solution = "Kayak.sln"
end
task :build => :assemblyinfo do
ensure_submodules()
ensure_nuget_packages()
build_task = is_nix() ? "build_xbuild" : "build_msbuild"
Rake::Task[build_task].invoke
end
task :test => :build do
nunit = invoke_runtime("packages/NUnit.2.5.10.11092/tools/nunit-console.exe")
sh "#{nunit} -labels #{OUTPUT_DIR}/Kayak.Tests.dll"
end
task :binaries => :build do
Dir.mkdir(BIN_DIR)
binaries = FileList("#{OUTPUT_DIR}/*.dll", "#{OUTPUT_DIR}/*.pdb") do |x|
x.exclude(/nunit/)
x.exclude(/.Tests/)
x.exclude(/KayakExamples./)
end
FileUtils.cp_r binaries, BIN_DIR
end
task :dist_nuget => [:binaries, :build] do
if is_nix()
puts "Not running on Windows, skipping NuGet package creation."
else
input_nuspec = "Kayak.nuspec"
output_nuspec = "#{BUILD_DIR}/Kayak.nuspec"
transform_xml input_nuspec, output_nuspec do |x|
x.root.elements["metadata/id"].text = PRODUCT
x.root.elements["metadata/version"].text = VERSION
x.root.elements["metadata/authors"].text = AUTHORS
x.root.elements["metadata/owners"].text = AUTHORS
x.root.elements["metadata/description"].text = DESCRIPTION
x.root.elements["metadata/licenseUrl"].text = LICENSE_URL
x.root.elements["metadata/projectUrl"].text = PROJECT_URL
x.root.elements["metadata/tags"].text = "http io socket network async"
end
nuget = NuGetPack.new
nuget.command = "tools/NuGet.exe"
nuget.nuspec = output_nuspec
nuget.output = BUILD_DIR
#using base_folder throws as there are two options that begin with b in nuget 1.4
nuget.parameters = "-Symbols"
nuget.execute
end
end
zip :dist_zip => [:build, :binaries] do |z|
z.directories_to_zip BIN_DIR
z.output_file = "kayak-#{VERSION}.zip"
z.output_path = BUILD_DIR
end
task :dist => [:test, :dist_nuget, :dist_zip] do
end
task :clean do
FileUtils.rm_rf BUILD_DIR
FileUtils.rm_rf "Kayak/bin"
FileUtils.rm_rf "Kayak/obj"
FileUtils.rm_rf "Kayak.Tests/bin"
FileUtils.rm_rf "Kayak.Tests/obj"
end
task :dist_clean => [:clean] do
FileUtils.rm_rf PACKAGES_DIR
end