forked from ThatRendle/Simple.Web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rakefile.rb
354 lines (283 loc) · 11.7 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
include FileTest
# Build information
SOLUTION_NAME = "Simple.Web"
SOLUTION_DESC = "A REST-focused, object-oriented Web Framework for .NET 4."
SOLUTION_LICENSE = "http://www.opensource.org/licenses/mit-license.php"
SOLUTION_URL = "http://github.com/markrendle/Simple.Web"
SOLUTION_COMPANY = "Mark Rendle, Ian Battersby, and contributors"
SOLUTION_COPYRIGHT = "Copyright (C) Mark Rendle 2012"
# Build configuration
load "VERSION.txt"
CONFIG = ENV["config"] || "Release"
PLATFORM = ENV["platform"] || "x86"
BUILD_NUMBER = "#{BUILD_VERSION}.#{(ENV["BUILD_NUMBER"] || Time.new.strftime('5%H%M'))}"
MONO = (RUBY_PLATFORM.downcase.include?('linux') or RUBY_PLATFORM.downcase.include?('darwin'))
TEAMCITY = (!ENV["BUILD_NUMBER"].nil? or !ENV["TEAMCITY_BUILD_PROPERTIES_FILE"].nil?)
# NuGet configuration
NUGET_APIKEY_LOCAL = ENV["apikey_local"]
NUGET_APIURL_LOCAL = ENV["apiurl_local"]
NUGET_APIKEY_REMOTE = ENV["apikey_remote"]
NUGET_APIURL_REMOTE = ENV["apiurl_remote"]
ENV["EnableNuGetPackageRestore"] = "true"
# Paths
BASE_PATH = File.expand_path(File.dirname(__FILE__))
SOURCE_PATH = "#{BASE_PATH}/src"
TESTS_PATH = "#{BASE_PATH}/src"
SPECS_PATH = "#{BASE_PATH}/specs"
BUILD_PATH = "#{BASE_PATH}/build"
RESULTS_PATH = "#{BASE_PATH}/results"
ARTIFACTS_PATH = "#{BASE_PATH}/artifacts"
NUSPEC_PATH = "#{BASE_PATH}/packaging/nuspec"
NUGET_PATH = "#{ARTIFACTS_PATH}/nuget"
TOOLS_PATH = "#{BASE_PATH}/tools"
# Files
ASSEMBLY_INFO = "#{SOURCE_PATH}/CommonAssemblyInfo.cs"
SOLUTION_FILE = "#{SOURCE_PATH}/Simple.Web.sln"
VERSION_INFO = "#{BASE_PATH}/VERSION.txt"
# Matching
TEST_ASSEMBLY_PATTERN_PREFIX = ".Tests"
TEST_ASSEMBLY_PATTERN_UNIT = "#{TEST_ASSEMBLY_PATTERN_PREFIX}.Unit"
TEST_ASSEMBLY_PATTERN_INTEGRATION = "#{TEST_ASSEMBLY_PATTERN_PREFIX}.Integration"
SPEC_ASSEMBLY_PATTERN = ".Specs"
# Commands
XUNIT_COMMAND = "#{TOOLS_PATH}/xUnit/xunit.console.clr4.#{(PLATFORM.empty? or PLATFORM.eql?('x86') ? 'x86' : '')}.exe"
MSPEC_COMMAND = "#{TOOLS_PATH}/mspec/mspec.exe"
NUGET_COMMAND = "#{SOURCE_PATH}/.nuget/NuGet.exe"
# Set up our build system
require 'albacore'
require 'pathname'
require 'rake/clean'
require 'rexml/document'
# Configure albacore
Albacore.configure do |config|
config.log_level = (TEAMCITY ? :verbose : :normal)
config.msbuild.solution = SOLUTION_FILE
config.msbuild.properties = { :configuration => CONFIG }
config.msbuild.use :net4
config.msbuild.targets = [ :Clean, :Build ]
config.msbuild.verbosity = "normal"
config.xbuild.solution = SOLUTION_FILE
config.xbuild.properties = { :configuration => CONFIG, :vstoolspath => (RUBY_PLATFORM.downcase.include?('darwin') ? '/Library/Frameworks/Mono.framework/Libraries' : '/usr/lib') + '/mono/xbuild/Microsoft/VisualStudio/v9.0' }
config.xbuild.targets = [ :Build ] #:Clean upsets xbuild
config.xbuild.verbosity = "normal"
config.mspec.command = (MONO ? 'mono' : XUNIT_COMMAND)
config.mspec.assemblies = FileList.new("#{SPECS_PATH}/**/*#{SPEC_ASSEMBLY_PATTERN}.dll").exclude(/obj\//).collect! { |element| ((MONO ? "#{MSPEC_COMMAND} " : '') + element) }
CLEAN.include(FileList["#{SOURCE_PATH}/**/obj"])
CLOBBER.include(NUGET_PATH)
CLOBBER.include(FileList["#{SOURCE_PATH}/**/bin"])
CLOBBER.include(ARTIFACTS_PATH)
CLOBBER.include(BUILD_PATH)
CLOBBER.include(RESULTS_PATH)
end
# Tasks
task :default => [:test]
desc "Build"
task :build => [:init, :assemblyinfo] do
if MONO
Rake::Task[:xbuild].invoke
else
Rake::Task[:msbuild].invoke
end
end
desc "Build + Tests (default)"
task :test => [:build] do
Rake::Task[:runtests].invoke(TEST_ASSEMBLY_PATTERN_PREFIX)
end
desc "Build + Unit tests"
task :quick => [:build] do
Rake::Task[:runtests].invoke(TEST_ASSEMBLY_PATTERN_UNIT)
end
desc "Build + Tests + Specs"
task :full => [:test] #[:test, :mspec]
desc "Build + Tests + Specs + Publish (local)"
task :publocal => [:full] do
raise "Environment variable \"APIURL_LOCAL\" must be a valid nuget server url." unless !NUGET_APIURL_LOCAL.nil?
raise "Environment variable \"APIKEY_LOCAL\" must be that of your nuget api key." unless !NUGET_APIKEY_LOCAL.nil?
PublishNugets BUILD_NUMBER, NUGET_APIURL_LOCAL, NUGET_APIKEY_LOCAL
end
desc "Build + Tests + Specs + Package"
task :package => [:full] do
PackageNugets BUILD_NUMBER
end
desc "Build + Tests + Specs + Publish (remote)"
task :publish => [:full] do
raise "Environment variable \"APIURL_REMOTE\" must be a valid nuget server url." unless !NUGET_APIURL_REMOTE.nil?
raise "Environment variable \"APIKEY_REMOTE\" must be that of your nuget api key." unless !NUGET_APIKEY_REMOTE.nil?
if not TEAMCITY
puts "\n\nThis will publish your local build to the remote nuget feed. Are you sure (y/n)?"
response = $stdin.gets.chomp
raise "Publish aborted." unless response.downcase.eql?("y")
end
PublishNugets BUILD_NUMBER, NUGET_APIURL_REMOTE, NUGET_APIKEY_REMOTE
end
# Hidden tasks
task :init => [:clobber] do
if not TEAMCITY
indexupdate = `git update-index --assume-unchanged #{BASE_PATH}/src/CommonAssemblyInfo.cs`
raise "Unable to perform git index operation, cannot continue (#{indexupdate})." unless indexupdate.empty?
end
Dir.mkdir BUILD_PATH unless File.exists?(BUILD_PATH)
Dir.mkdir RESULTS_PATH unless File.exists?(RESULTS_PATH)
Dir.mkdir ARTIFACTS_PATH unless File.exists?(ARTIFACTS_PATH)
Dir.mkdir NUGET_PATH unless File.exists?(NUGET_PATH)
end
task :ci => [:package]
msbuild :msbuild
xbuild :xbuild
assemblyinfo :assemblyinfo do |asm|
asm_version = BUILD_NUMBER
begin
commit = `git log -1 --pretty=format:%H`
rescue
commit = "git unavailable"
end
testassemblies = FileList.new("#{TESTS_PATH}/*#{TEST_ASSEMBLY_PATTERN_PREFIX}/")
.pathmap("%f")
.collect! { |assemblyname|
"[assembly: InternalsVisibleTo(\"#{assemblyname}\")]"
}
asm.language = "C#"
asm.version = BUILD_NUMBER
asm.trademark = commit
asm.file_version = BUILD_NUMBER
asm.company_name = SOLUTION_COMPANY
asm.product_name = SOLUTION_NAME
asm.copyright = SOLUTION_COPYRIGHT
asm.namespaces 'System.Runtime.CompilerServices'
asm.custom_attributes :AssemblyConfiguration => CONFIG, :AssemblyInformationalVersion => asm_version
asm.custom_data testassemblies
asm.output_file = ASSEMBLY_INFO
asm.com_visible = false
end
task :runtests, [:boundary] do |t, args|
args.with_default(:boundary => "*")
runner = XUnitTestRunnerCustom.new(MONO ? 'mono' : XUNIT_COMMAND)
runner.html_output = RESULTS_PATH
assemblies = Array.new
args["boundary"].split(/,/).each do |this_boundary|
FileList.new("#{TESTS_PATH}/*#{this_boundary}")
.collect! { |element|
FileList.new("#{element}/**/*#{this_boundary}.dll")
.exclude(/obj\//)
.each do |this_file|
assemblies.push (MONO ? "#{XUNIT_COMMAND} " : '') + this_file
end
}
runner.assemblies = assemblies
runner.execute
end
end
mspec :mspec
# Helper methods
def PublishNugets(version, apiurl, apikey)
PackageNugets(version)
nupkgs = FileList["#{NUGET_PATH}/*#{$version}.nupkg"]
nupkgs.each do |nupkg|
puts "Pushing #{Pathname.new(nupkg).basename}"
nuget_push = NuGetPush.new
nuget_push.source = "\"" + apiurl + "\""
nuget_push.apikey = apikey
nuget_push.command = NUGET_COMMAND
nuget_push.package = (MONO ? nupkg : nupkg.gsub('/','\\'))
nuget_push.create_only = false
nuget_push.execute
end
end
def PackageNugets(nuspec_version)
raise "Invalid nuspec version specified." unless !nuspec_version.nil?
Dir.mkdir "#{ARTIFACTS_PATH}/nuspec" unless File.exists?("#{ARTIFACTS_PATH}/nuspec")
FileUtils.cp_r FileList["#{NUSPEC_PATH}/**/*.nuspec"], "#{ARTIFACTS_PATH}/nuspec"
nuspecs = FileList["#{ARTIFACTS_PATH}/nuspec/**/*.nuspec"]
UpdateNuSpecVersions nuspecs, nuspec_version
nuspecs.each do |nuspec|
nuget = NuGetPackCustom.new
nuget.command = NUGET_COMMAND
nuget.nuspec = nuspec
nuget.output = NUGET_PATH
nuget.base_folder = NUSPEC_PATH
nuget.execute
end
end
def UpdateNuSpecVersions(nuspecs, nuspec_version)
raise "No nuspecs to update." unless !nuspecs.nil?
raise "Invalid nuspec version specified." unless !nuspec_version.nil?
nuspecs.each do |nuspec|
puts "Updating #{Pathname.new(nuspec).basename}"
update_xml nuspec do |xml|
xml.root.elements["metadata/version"].text = nuspec_version
local_dependencies = xml.root.elements["metadata/dependencies/dependency[contains(@id,'#{SOLUTION_NAME}')]"]
local_dependencies.attributes["version"] = "#{nuspec_version}" unless local_dependencies.nil?
xml.root.elements["metadata/authors"].text = SOLUTION_COMPANY
xml.root.elements["metadata/summary"].text = SOLUTION_DESC
xml.root.elements["metadata/licenseUrl"].text = SOLUTION_LICENSE
xml.root.elements["metadata/projectUrl"].text = SOLUTION_URL
xml.root.get_elements("//file").each { |e|
e.attributes["src"] = e.attributes["src"].gsub((MONO ? '\\' : '/'), (!MONO ? '\\' : '/'))
e.attributes["target"] = e.attributes["target"].gsub((MONO ? '\\' : '/'), (!MONO ? '\\' : '/'))
}
end
end
end
def update_xml(xml_path)
xml_file = File.new(xml_path)
xml = REXML::Document.new xml_file
yield xml
xml_file.close
xml_file = File.open(xml_path, "w")
formatter = REXML::Formatters::Default.new(5)
formatter.write(xml, xml_file)
xml_file.close
end
# Albacore needs some Mono help
class XUnitTestRunnerCustom < XUnitTestRunner
def build_html_output
fail_with_message 'Directory is required for html_output' if !File.directory?(File.expand_path(@html_output))
"/nunit \"@#{File.join(File.expand_path(@html_output),"%s.html")}\""
end
end
class NuGetPackCustom < NuGetPack
def execute
fail_with_message 'nuspec must be specified.' if @nuspec.nil?
params = []
params << "pack"
params << "-Symbols" if @symbols
params << nuspec
params << "-BasePath \"#{base_folder}\"" unless @base_folder.nil?
params << "-OutputDirectory \"#{output}\"" unless @output.nil?
params << "-NoDefaultExcludes" unless !MONO
params << "-Verbosity detailed" unless !TEAMCITY
params << build_properties unless @properties.nil? || @properties.empty?
merged_params = params.join(' ')
cmd = "#{MONO ? 'mono ' : ''}#{File.expand_path(@command)} #{merged_params}"
result = false
@logger.debug "Build NuGet pack Command Line: #{cmd}"
Dir.chdir(@working_directory) do
result = system(cmd)
end
failure_message = 'NuGet Failed. See Build Log For Detail'
fail_with_message failure_message if !result
end
end
class NuGetPublishCustom < NuGetPackCustom
def execute
fail_with_message 'id must be specified.' if @id.nil?
fail_with_message 'version must be specified.' if @version.nil?
# don't validate @apikey as required, coz it might have been set in the config file using 'SetApiKey'
puts @create_only
params = []
params << "publish"
params << "#{@id}"
params << "#{@version}"
params << "#{@apikey}" if @apikey
params << "-Source #{source}" unless @source.nil?
merged_params = params.join(' ')
cmd = "#{MONO ? 'mono ' : ''}#{File.expand_path(@command)} #{merged_params}"
result = false
@logger.debug "Build NuGet publish Command Line: #{cmd}"
Dir.chdir(@working_directory) do
result = system(cmd)
end
failure_message = 'NuGet Publish Failed. See Build Log For Details'
fail_with_message failure_message if !result
end
end