-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathbuild.cake
382 lines (326 loc) · 11.8 KB
/
build.cake
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#tool nuget:?package=OpenCover&version=4.6.519
#tool nuget:?package=ReportGenerator&version=2.5.8
var target = Context.Argument("target", "Default");
var configuration =
HasArgument("Configuration") ? Argument<string>("Configuration") :
EnvironmentVariable("Configuration") != null ? EnvironmentVariable("Configuration") : "Release";
var buildSystem = Context.BuildSystem();
var isLocalBuild = buildSystem.IsLocalBuild;
var isRunningOnAppVeyor = buildSystem.AppVeyor.IsRunningOnAppVeyor;
var isRunningOnWindows = Context.IsRunningOnWindows();
var isPullRequest = buildSystem.AppVeyor.Environment.PullRequest.IsPullRequest;
var isBuildTagged = IsBuildTagged(buildSystem);
var buildNumber =
HasArgument("BuildNumber") ? Argument<int>("BuildNumber") :
isRunningOnAppVeyor ? AppVeyor.Environment.Build.Number :
EnvironmentVariable("BuildNumber") != null ? int.Parse(EnvironmentVariable("BuildNumber")) : 0;
var artifactsDir = Directory("./artifacts");
var testResultsDir = Directory("./artifacts/test-results");
var nugetDir = System.IO.Path.Combine(artifactsDir, "nuget");
//
// Tasks
//
Task("Info")
.Does(() =>
{
Information("Target: {0}", target);
Information("Configuration: {0}", configuration);
Information("Build number: {0}", buildNumber);
var projects = GetFiles("./src/**/*.csproj");
foreach (var project in projects) {
Information("{0} version: {1}", project.GetFilenameWithoutExtension(), GetVersion(project.FullPath));
}
});
Task("Clean")
.Does(() =>
{
CleanDirectory(artifactsDir);
});
Task("Restore-Packages")
.Does(() =>
{
DotNetCoreRestore();
});
Task("Build")
.IsDependentOn("Info")
.IsDependentOn("Clean")
.IsDependentOn("Restore-Packages")
.Does(() =>
{
var projects = GetFiles("./src/**/*.csproj");
projects.Add(GetFiles("./test/**/*.csproj"));
var settings = new DotNetCoreBuildSettings
{
Configuration = configuration,
ArgumentCustomization = args => args.Append("/p:DebugType=full /p:DebugSymbols=True")
};
foreach (var project in projects) {
DotNetCoreBuild(project.FullPath, settings);
}
});
Task("Sign-Assemblies")
.IsDependentOn("Build")
.Does(() =>
{
var assemblies = GetFiles("./src/*/bin/Release/*/Sharpbrake*.dll");
FilePath snPath = null;
foreach (var file in GetFiles("C:\\Program Files (x86)\\Microsoft SDKs\\Windows\\v10.0A\\bin\\**\\sn.exe")) {
snPath = file;
break;
}
Information("SN Tool " + snPath.FullPath);
var sign = FileExists("./StrongKey.snk");
foreach (var assembly in assemblies) {
if (sign)
{
Information("Trying to resign assembly " + assembly.FullPath);
StartProcess(snPath, "-R \"" + assembly.FullPath + "\" ./StrongKey.snk");
}
else
{
Information("Registering assembly for verification skipping " + assembly.FullPath);
StartProcess(snPath, "-Vr \"" + assembly.FullPath + "\"");
}
}
});
Task("Run-Unit-Tests")
.IsDependentOn("Build")
.IsDependentOn("Sign-Assemblies")
.Does(() =>
{
var testProject = MakeAbsolute(new FilePath("./test/Sharpbrake.Client.Tests/Sharpbrake.Client.Tests.csproj")).FullPath;
var workingDirectory = MakeAbsolute(new DirectoryPath("./test/Sharpbrake.Client.Tests")).FullPath;
var settings = new DotNetCoreTestSettings
{
Configuration = configuration,
WorkingDirectory = workingDirectory,
NoBuild = true
};
Action<ICakeContext> testAction = tool => {
tool.DotNetCoreTest(testProject, settings);
};
EnsureDirectoryExists(testResultsDir);
// OpenCover works only on Windows
if (isRunningOnWindows)
{
var openCoverXml = MakeAbsolute(testResultsDir.Path.CombineWithFilePath("OpenCover").AppendExtension("xml"));;
var coverageReportDir = System.IO.Path.Combine(testResultsDir, "report");
var openCoverSettings = new OpenCoverSettings
{
Register = "user",
ReturnTargetCodeOffset = 0,
WorkingDirectory = workingDirectory,
ArgumentCustomization =
args =>
args.Append(
"-skipautoprops -mergebyhash -mergeoutput -oldstyle -hideskipped:All")
}
.WithFilter("+[*]* -[xunit.*]* -[*.Tests]*")
.ExcludeByAttribute("*.ExcludeFromCodeCoverage*")
.ExcludeByFile("*/*Designer.cs;*/*.g.cs;*/*.g.i.cs");
OpenCover(testAction, openCoverXml, openCoverSettings);
// for non-local build coverage is uploaded to codecov.io so no need to generate the report
if (FileExists(openCoverXml) && isLocalBuild)
{
ReportGenerator(openCoverXml, coverageReportDir,
new ReportGeneratorSettings {
ArgumentCustomization = args => args.Append("-reporttypes:html")
}
);
}
}
else
{
testAction(Context);
}
});
Task("Publish-Coverage")
.IsDependentOn("Run-Unit-Tests")
.WithCriteria(() => !isLocalBuild && !isPullRequest)
.Does(() =>
{
var openCoverXml = MakeAbsolute(testResultsDir.Path.CombineWithFilePath("OpenCover").AppendExtension("xml"));;
if (!FileExists(openCoverXml))
throw new Exception("Missing \"" + openCoverXml + "\" file");
UploadCoverageReport(Context, openCoverXml.FullPath);
})
.OnError(exception =>
{
Information("Error: " + exception.Message);
});
Task("Create-Packages")
.IsDependentOn("Run-Unit-Tests")
.Does(() =>
{
var projects = GetFiles("./src/**/*.csproj");
var settings = new DotNetCorePackSettings()
{
Configuration = configuration,
OutputDirectory = nugetDir,
NoBuild = true,
ArgumentCustomization = args => args.Append("--include-symbols")
};
foreach (var project in projects) {
DotNetCorePack(project.GetDirectory().FullPath, settings);
}
});
Task("Publish-MyGet")
.IsDependentOn("Create-Packages")
.WithCriteria(() => !isLocalBuild && !isPullRequest && !isBuildTagged)
.Does(() =>
{
var serverUrl = EnvironmentVariable("MYGET_SERVER_URL");
if (string.IsNullOrEmpty(serverUrl))
throw new InvalidOperationException("Could not resolve MyGet server URL");
var symbolServerUrl = EnvironmentVariable("MYGET_SYMBOL_SERVER_URL");
var apiKey = EnvironmentVariable("MYGET_API_KEY");
if (string.IsNullOrEmpty(apiKey))
throw new InvalidOperationException("Could not resolve MyGet API key");
var symbolServerSettings = new NuGetPushSettings
{
Source = symbolServerUrl,
ApiKey = apiKey
};
var serverSettings = new NuGetPushSettings
{
Source = serverUrl,
ApiKey = apiKey
};
foreach (var package in GetFiles(nugetDir + "/*.nupkg"))
{
if (package.FullPath.EndsWith("symbols.nupkg", StringComparison.OrdinalIgnoreCase))
{
if (string.IsNullOrEmpty(symbolServerUrl))
continue;
NuGetPush(package.FullPath, symbolServerSettings);
}
else
{
NuGetPush(package.FullPath, serverSettings);
}
}
})
.OnError(exception =>
{
Information("Error: " + exception.Message);
});
Task("Publish-NuGet")
.IsDependentOn("Create-Packages")
.WithCriteria(() => !isLocalBuild && !isPullRequest && isBuildTagged)
.Does(() =>
{
var serverUrl = EnvironmentVariable("NUGET_SERVER_URL");
if (string.IsNullOrEmpty(serverUrl))
throw new InvalidOperationException("Could not resolve NuGet server URL");
var apiKey = EnvironmentVariable("NUGET_API_KEY");
if (string.IsNullOrEmpty(apiKey))
throw new InvalidOperationException("Could not resolve NuGet API key");
var settings = new NuGetPushSettings
{
Source = serverUrl,
ApiKey = apiKey
};
foreach (var package in GetFiles(nugetDir + "/*.nupkg"))
{
// symbols packages are pushed alongside regular ones so no need to push them explicitly
if (package.FullPath.EndsWith("symbols.nupkg", StringComparison.OrdinalIgnoreCase))
continue;
try
{
NuGetPush(package.FullPath, settings);
}
catch (Exception ex)
{
Information("Error: " + ex.Message);
}
}
})
.OnError(exception =>
{
Information("Error: " + exception.Message);
});
//
// Targets
//
Task("Default")
.IsDependentOn("Create-Packages")
.IsDependentOn("Publish-Coverage")
.IsDependentOn("Publish-MyGet")
.IsDependentOn("Publish-NuGet");
//
// Run build
//
RunTarget(target);
// **********************************************
// *** Utilities ***
// **********************************************
/// <summary>
/// Checks if build is tagged.
/// </summary>
private static bool IsBuildTagged(BuildSystem buildSystem)
{
return buildSystem.AppVeyor.Environment.Repository.Tag.IsTag
&& !string.IsNullOrWhiteSpace(buildSystem.AppVeyor.Environment.Repository.Tag.Name);
}
/// <summary>
/// Gets version from "Version" node of csproj file.
/// </summary>
private static string GetVersion(string csproj)
{
using (var reader = System.Xml.XmlReader.Create(csproj))
{
reader.MoveToContent();
while (reader.Read())
if (reader.NodeType == System.Xml.XmlNodeType.Element &&
reader.LocalName.Equals("Version", StringComparison.OrdinalIgnoreCase))
return reader.ReadElementContentAsString();
}
return null;
}
/// <summary>
/// Uploads coverage report (OpenCover.xml) to codecov.io.
/// </summary>
public static void UploadCoverageReport(ICakeContext context, string openCoverXml)
{
const string url = "https://codecov.io/upload/v2";
// query parameters: https://github.com/codecov/codecov-bash/blob/master/codecov#L1202
var queryBuilder = new System.Text.StringBuilder(url);
queryBuilder.Append("?package=bash-tbd&service=appveyor");
queryBuilder.Append("&branch=").Append(context.EnvironmentVariable("APPVEYOR_REPO_BRANCH"));
queryBuilder.Append("&commit=").Append(context.EnvironmentVariable("APPVEYOR_REPO_COMMIT"));
queryBuilder.Append("&build=").Append(context.EnvironmentVariable("APPVEYOR_JOB_ID"));
queryBuilder.Append("&pr=").Append(context.EnvironmentVariable("APPVEYOR_PULL_REQUEST_NUMBER"));
queryBuilder.Append("&job=").Append(context.EnvironmentVariable("APPVEYOR_ACCOUNT_NAME"));
queryBuilder.Append("%2F").Append(context.EnvironmentVariable("APPVEYOR_PROJECT_SLUG"));
queryBuilder.Append("%2F").Append(context.EnvironmentVariable("APPVEYOR_BUILD_VERSION"));
queryBuilder.Append("&token=").Append(context.EnvironmentVariable("CODECOV_TOKEN"));
var request = (System.Net.HttpWebRequest) System.Net.WebRequest.Create(queryBuilder.ToString());
request.Accept = "text/plain";
request.Method = "POST";
using (var requestStream = request.GetRequestStream())
using (var openCoverXmlStream = new System.IO.FileStream(openCoverXml, System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
var buffer = new byte[1024];
int readBytes;
while ((readBytes = openCoverXmlStream.Read(buffer, 0, buffer.Length)) > 0)
requestStream.Write(buffer, 0, readBytes);
}
using (var response = (System.Net.HttpWebResponse) request.GetResponse())
{
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
using (var responseStream = response.GetResponseStream())
{
if (responseStream != null)
{
using (var responseStreamReader = new System.IO.StreamReader(responseStream))
context.Information(responseStreamReader.ReadToEnd());
}
}
}
else
{
context.Information("Status code: " + response.StatusCode);
}
}
}