-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.cake
427 lines (366 loc) · 14.5 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//////////////////////////////////////////////////////////////////////
// ADDINS
//////////////////////////////////////////////////////////////////////
//#addin "nuget:?package=Cake.FileHelpers&version=1.0.4"
#addin "nuget:?package=Cake.Coveralls&version=0.4.0"
//#addin "nuget:?package=Cake.PinNuGetDependency&version=1.0.0"
//#addin "nuget:?package=Cake.Powershell&version=0.3.5"
//#addin "nuget:?package=Cake.Sonar&version=1.0.4"
//////////////////////////////////////////////////////////////////////
// TOOLS
//////////////////////////////////////////////////////////////////////
#tool "nuget:?package=GitReleaseManager&version=0.6.0"
#tool "nuget:?package=GitVersion.CommandLine&version=3.6.5"
#tool "nuget:?package=coveralls.io&version=1.3.4"
#tool "nuget:?package=OpenCover&version=4.6.519"
#tool "nuget:?package=ReportGenerator&version=2.5.11"
#tool "nuget:?package=vswhere&version=2.1.4"
#tool "nuget:?package=MSBuild.SonarQube.Runner.Tool"
//////////////////////////////////////////////////////////////////////
// ARGUMENTS
//////////////////////////////////////////////////////////////////////
var target = Argument("target", "Default");
if (string.IsNullOrWhiteSpace(target))
{
target = "Default";
}
//////////////////////////////////////////////////////////////////////
// PREPARATION
//////////////////////////////////////////////////////////////////////
// Should MSBuild treat any errors as warnings?
var treatWarningsAsErrors = false;
// Build configuration
var local = BuildSystem.IsLocalBuild;
var isPullRequest = AppVeyor.Environment.PullRequest.IsPullRequest;
var isRepository = StringComparer.OrdinalIgnoreCase.Equals("DHGMS-Solutions/cloneallrepos", AppVeyor.Environment.Repository.Name);
var isDevelopBranch = StringComparer.OrdinalIgnoreCase.Equals("develop", AppVeyor.Environment.Repository.Branch);
var isReleaseBranch = StringComparer.OrdinalIgnoreCase.Equals("master", AppVeyor.Environment.Repository.Branch);
var isTagged = AppVeyor.Environment.Repository.Tag.IsTag;
var githubOwner = "DHGMS-Solutions";
var githubRepository = "cloneallrepos";
var githubUrl = string.Format("https://github.com/{0}/{1}", githubOwner, githubRepository);
var vsPath = VSWhereLatest();
if (vsPath == null)
{
throw new Exception("Unable to find Visual Studio");
}
Information("Visual Studio Path: " + vsPath);
var msBuildPath = GetFiles(vsPath + "/**/msbuild.exe").FirstOrDefault();
if (msBuildPath == null)
{
throw new Exception("Unable to find MSBuild path");
}
Information("MSBuild Path: " + msBuildPath);
var androidHome = EnvironmentVariable("ANDROID_HOME");
// Version
var gitVersion = GitVersion();
var majorMinorPatch = gitVersion.MajorMinorPatch;
var informationalVersion = gitVersion.InformationalVersion;
var nugetVersion = gitVersion.NuGetVersion;
var buildVersion = gitVersion.FullBuildMetaData;
var assemblyVersion = gitVersion.Major + "." + gitVersion.Minor + ".0.0";
var fileVersion = majorMinorPatch;
var packageVersion = isReleaseBranch ? majorMinorPatch : informationalVersion;
Information("informationalVersion: " + informationalVersion);
Information("assemblyVersion: " + assemblyVersion);
Information("fileVersion: " + fileVersion);
// Artifacts
var artifactDirectory = "./artifacts/";
var testCoverageOutputFile = artifactDirectory + "OpenCover.xml";
var packageWhitelist = new[] { };
var runSonarQube = false;
var sonarQubePreview = false;
var sonarQubeLogin = EnvironmentVariable("sonarqubeLogin");
var sonarqubeProjectKey = "Dhgms.CloneAllRepos";
var sonarqubeOrganisationKey = "dpvreony-github";
// sonarqube
if (isRepository && !local && sonarQubeLogin != null) {
if (isPullRequest) {
sonarQubePreview = true;
runSonarQube = true;
Information("Sonar on PR " + AppVeyor.Environment.PullRequest.Number);
}
else if (isReleaseBranch) {
runSonarQube = true;
Information("Sonar on branch " + AppVeyor.Environment.Repository.Branch);
}
}
// Define global marcos.
Action Abort = () => { throw new Exception("a non-recoverable fatal error occurred."); };
///////////////////////////////////////////////////////////////////////////////
// SETUP / TEARDOWN
///////////////////////////////////////////////////////////////////////////////
Setup(context =>
{
Information("Building version {0} . (isTagged: {1})", informationalVersion, isTagged);
CreateDirectory(artifactDirectory);
});
Teardown(context =>
{
// Executed AFTER the last task.
});
Task("BuildSolution")
.IsDependentOn("SonarBegin")
.Does (() =>
{
Action<string> build = (solution) =>
{
Information("Building {0} using {1}", solution, msBuildPath);
MSBuild(solution, new MSBuildSettings() {
ToolPath = msBuildPath,
ArgumentCustomization = args => args.Append("/bl:build.binlog /m")
}
.WithTarget("build;pack")
.WithProperty("AndroidSdkDirectory", androidHome)
.WithProperty("PackageOutputPath", MakeAbsolute(Directory(artifactDirectory)).ToString().Quote())
.WithProperty("TreatWarningsAsErrors", treatWarningsAsErrors.ToString())
.SetConfiguration("Release")
.WithProperty("Version", packageVersion)
.WithProperty("AssemblyVersion", assemblyVersion)
.WithProperty("FileVersion", fileVersion)
.WithProperty("InformationalVersion", informationalVersion)
.SetVerbosity(Verbosity.Minimal)
.SetNodeReuse(false));
};
// Restore must be a separate step
MSBuild("./src/Dhgms.CloneAllRepos.sln", new MSBuildSettings() {
ToolPath = msBuildPath,
ArgumentCustomization = args => args.Append("/bl:restore.binlog /m")
}
.WithTarget("restore")
.WithProperty("AndroidSdkDirectory", androidHome)
.WithProperty("Version", nugetVersion.ToString())
.SetVerbosity(Verbosity.Minimal));
build("./src/Dhgms.CloneAllRepos.sln");
});
// https://andrewlock.net/running-tests-with-dotnet-xunit-using-cake/
// https://github.com/sawilde/opencover/wiki/Usage
// debug type and symbols are set in projects due to an issue in opencover and new pdb format
// https://github.com/OpenCover/opencover/wiki/Troubleshooting-%22Cannot-instrument-assembly%22
Task("RunUnitTests")
.IsDependentOn("BuildSolution")
.Does(() =>
{
var filePath = "./src/Dhgms.CloneAllRepos.UnitTests/Dhgms.CloneAllRepos.UnitTests.csproj";
var projectDirectory = new FilePath(filePath).GetDirectory();
var pdbDirectory = projectDirectory + "\\bin\\Debug\\netcoreapp2.0";
// workaround for https://github.com/xunit/xunit/issues/1573
// C:\Program Files\dotnet\shared\Microsoft.NETCore.App
var fxVersion = "2.0.5";
Action<ICakeContext> testAction = tool => {
//tool.DotNetCoreTest(filePath, dotNetCoreTestSettings);
tool.DotNetCoreTool(
projectPath: filePath,
command: "xunit",
arguments: "-noshadow -fxversion " + fxVersion + " -configuration Debug -diagnostics"
);
};
OpenCover(testAction,
testCoverageOutputFile,
new OpenCoverSettings {
//LogLevel = OpenCoverLogLevel.All,
MergeOutput = true,
Register = "user",
ReturnTargetCodeOffset = 0,
ArgumentCustomization = args => args.Append("-coverbytest:*.UnitTests.dll").Append("-searchdirs:" + pdbDirectory).Append("-oldstyle"),
// working dir set to allow use of dotnet-xunit
WorkingDirectory = projectDirectory
}
.WithFilter("+[Dhgms*]*")
.ExcludeByAttribute("*.ExcludeFromCodeCoverage*")
.ExcludeByFile("*/*Designer.cs")
.ExcludeByFile("*/*.g.cs")
.ExcludeByFile("*/*.g.i.cs"));
ReportGenerator(testCoverageOutputFile, artifactDirectory);
}).ReportError(exception =>
{
var apiApprovals = GetFiles("./**/ApiApprovalTests.*");
CopyFiles(apiApprovals, artifactDirectory);
});
Task("UploadTestCoverage")
.WithCriteria(() => !local)
.WithCriteria(() => isRepository)
.IsDependentOn("RunUnitTests")
.Does(() =>
{
// Resolve the API key.
var token = EnvironmentVariable("COVERALLS_TOKEN");
if (string.IsNullOrEmpty(token))
{
throw new Exception("The COVERALLS_TOKEN environment variable is not defined.");
}
CoverallsIo(testCoverageOutputFile, new CoverallsIoSettings()
{
RepoToken = token
});
});
Task("Sonar")
.IsDependentOn("SonarBegin")
.IsDependentOn("BuildSolution")
.IsDependentOn("RunUnitTests")
.IsDependentOn("UploadTestCoverage")
.IsDependentOn("SonarEnd");
Task("SonarBegin")
.WithCriteria(() => runSonarQube)
.Does(() => {
var coverageFilePath = MakeAbsolute(new FilePath(testCoverageOutputFile)).FullPath;
Information("Sonar: Test Coverage Output File: " + testCoverageOutputFile);
var arguments = "begin /k:\"" + sonarqubeProjectKey + "\" /d:\"sonar.host.url=https://sonarcloud.io\" /d:\"sonar.organization=" + sonarqubeOrganisationKey + "\" /d:\"sonar.login=" + sonarQubeLogin + "\" /d:sonar.cs.opencover.reportsPaths=\"" + coverageFilePath + "\"";
if (sonarQubePreview) {
Information("Sonar: Running Sonar on PR " + AppVeyor.Environment.PullRequest.Number);
arguments += " /d:\"sonar.projectVersion=sonar.projectVersion\" /d:\"sonar.analysis.mode=preview\"";
}
else {
Information("Sonar: Running Sonar on branch " + AppVeyor.Environment.Repository.Branch);
}
var sonarStartSettings = new ProcessSettings{ Arguments = arguments };
StartProcess("./tools/MSBuild.SonarQube.Runner.Tool/tools/MSBuild.SonarQube.Runner.exe", sonarStartSettings);
/*
SonarBegin(new SonarBeginSettings{
Url = "sonarcube.contoso.local",
Login = "admin",
Password = "admin",
Verbose = true
});
*/
});
Task("SonarEnd")
.IsDependentOn("UploadTestCoverage")
.WithCriteria(() => runSonarQube)
.Does(() => {
/*
SonarEnd(new SonarEndSettings{
Login = "admin",
Password = "admin"
});
*/
var sonarEndSettings = new ProcessSettings{ Arguments = "end /d:\"sonar.login=" + sonarQubeLogin + "\"" };
StartProcess("./tools/MSBuild.SonarQube.Runner.Tool/tools/MSBuild.SonarQube.Runner.exe", sonarEndSettings);
});
Task("Package")
.IsDependentOn("Sonar")
//.IsDependentOn("RunUnitTests")
//.IsDependentOn("PinNuGetDependencies")
.Does (() =>
{
});
/*
Task("PinNuGetDependencies")
.Does (() =>
{
// only pin whitelisted packages.
foreach(var package in packageWhitelist)
{
// only pin the package which was created during this build run.
var packagePath = artifactDirectory + File(string.Concat(package, ".", nugetVersion, ".nupkg"));
// see https://github.com/cake-contrib/Cake.PinNuGetDependency
PinNuGetDependency(packagePath, "reactiveui");
}
});
*/
Task("PublishPackages")
.IsDependentOn("RunUnitTests")
.IsDependentOn("Package")
.WithCriteria(() => !local)
.WithCriteria(() => !isPullRequest)
.WithCriteria(() => isRepository)
.WithCriteria(() => isDevelopBranch || isReleaseBranch)
.Does (() =>
{
if (isReleaseBranch && !isTagged)
{
Information("Packages will not be published as this release has not been tagged.");
return;
}
// Resolve the API key.
var apiKey = EnvironmentVariable("NUGET_APIKEY");
if (string.IsNullOrEmpty(apiKey))
{
throw new Exception("The NUGET_APIKEY environment variable is not defined.");
}
var source = EnvironmentVariable("NUGET_SOURCE");
if (string.IsNullOrEmpty(source))
{
throw new Exception("The NUGET_SOURCE environment variable is not defined.");
}
// only push whitelisted packages.
foreach(var package in packageWhitelist)
{
// only push the package which was created during this build run.
var packagePath = artifactDirectory + File(string.Concat(package, ".", nugetVersion, ".nupkg"));
// Push the package.
NuGetPush(packagePath, new NuGetPushSettings {
Source = source,
ApiKey = apiKey
});
}
});
Task("CreateRelease")
.IsDependentOn("Package")
.WithCriteria(() => !local)
.WithCriteria(() => !isPullRequest)
.WithCriteria(() => isRepository)
.WithCriteria(() => isReleaseBranch)
.WithCriteria(() => !isTagged)
.Does (() =>
{
var username = EnvironmentVariable("GITHUB_USERNAME");
if (string.IsNullOrEmpty(username))
{
throw new Exception("The GITHUB_USERNAME environment variable is not defined.");
}
var token = EnvironmentVariable("GITHUB_TOKEN");
if (string.IsNullOrEmpty(token))
{
throw new Exception("The GITHUB_TOKEN environment variable is not defined.");
}
GitReleaseManagerCreate(username, token, githubOwner, githubRepository, new GitReleaseManagerCreateSettings {
Milestone = majorMinorPatch,
Name = majorMinorPatch,
Prerelease = true,
TargetCommitish = "master"
});
});
Task("PublishRelease")
.IsDependentOn("RunUnitTests")
.IsDependentOn("Package")
.WithCriteria(() => !local)
.WithCriteria(() => !isPullRequest)
.WithCriteria(() => isRepository)
.WithCriteria(() => isReleaseBranch)
.WithCriteria(() => isTagged)
.Does (() =>
{
var username = EnvironmentVariable("GITHUB_USERNAME");
if (string.IsNullOrEmpty(username))
{
throw new Exception("The GITHUB_USERNAME environment variable is not defined.");
}
var token = EnvironmentVariable("GITHUB_TOKEN");
if (string.IsNullOrEmpty(token))
{
throw new Exception("The GITHUB_TOKEN environment variable is not defined.");
}
// only push whitelisted packages.
foreach(var package in packageWhitelist)
{
// only push the package which was created during this build run.
var packagePath = artifactDirectory + File(string.Concat(package, ".", nugetVersion, ".nupkg"));
GitReleaseManagerAddAssets(username, token, githubOwner, githubRepository, majorMinorPatch, packagePath);
}
GitReleaseManagerClose(username, token, githubOwner, githubRepository, majorMinorPatch);
});
//////////////////////////////////////////////////////////////////////
// TASK TARGETS
//////////////////////////////////////////////////////////////////////
Task("Default")
//.IsDependentOn("CreateRelease")
.IsDependentOn("PublishPackages")
//.IsDependentOn("PublishRelease")
.Does (() =>
{
});
//////////////////////////////////////////////////////////////////////
// EXECUTION
//////////////////////////////////////////////////////////////////////
RunTarget(target);