-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.cake
303 lines (254 loc) · 10.6 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
#tool "nuget:?package=xunit.runner.console&version=2.4.1"
#tool "nuget:?package=OpenCover&version=4.7.922"
#tool "nuget:?package=ReportGenerator&version=4.5.0"
#tool "nuget:?package=GitVersion.CommandLine&version=3.6.5"
var target = Argument("target", "Default");
var configuration = Argument("configuration", "Debug");
if ((target == "Default") && (TeamCity.IsRunningOnTeamCity)) {
target = "TeamCity";
configuration = "Release";
}
if ((target == "Default") && (AppVeyor.IsRunningOnAppVeyor)) {
target = "AppVeyor";
configuration = "Release";
}
var projectName = "HealthCheck";
var baseDirectory = MakeAbsolute(Directory("."));
var buildDirectory = baseDirectory + "\\.build";
var outputDirectory = buildDirectory + "\\output";
var packageDirectory = baseDirectory + "\\packages";
var solutionFile = String.Format("{0}\\{1}.sln", baseDirectory, projectName);
IEnumerable<string> stdout;
StartProcess ("git", new ProcessSettings {
Arguments = "describe --tags --abbrev=0",
RedirectStandardOutput = true,
}, out stdout);
List<String> result = new List<string>(stdout);
var version = String.IsNullOrEmpty(result[0]) ? "0.0.0" : result[0];
StartProcess ("git", new ProcessSettings {
Arguments = "rev-parse --short=8 HEAD",
RedirectStandardOutput = true,
}, out stdout);
result = new List<string>(stdout);
var packageId = String.IsNullOrEmpty(result[0]) ? "unknown" : result[0];
var branch = "unknown";
if (AppVeyor.IsRunningOnAppVeyor) {
branch = EnvironmentVariable("APPVEYOR_REPO_BRANCH");
if (branch != "master") {
AppVeyor.UpdateBuildVersion($"{version}-{branch}.{packageId}");
} else {
AppVeyor.UpdateBuildVersion(version);
}
} else {
StartProcess ("git", new ProcessSettings {
Arguments = "symbolic-ref --short HEAD",
RedirectStandardOutput = true,
}, out stdout);
result = new List<string>(stdout);
branch = String.IsNullOrEmpty(result[0]) ? "unknown" : result[0];
}
if (branch != "master") {
version = $"{version}-{branch}.{packageId}";
}
var msbuildSettings = new MSBuildSettings {
Configuration = configuration,
ToolVersion = MSBuildToolVersion.VS2019,
NodeReuse = false,
WarningsAsError = false
}.WithProperty("OutDir", outputDirectory);
Setup(setupContext =>
{
if (setupContext.TargetTask.Name == "Package")
{
Information("Switching to Release Configuration for packaging...");
configuration = "Release";
msbuildSettings.Configuration = "Release";
}
});
TaskSetup(setupContext =>
{
if (TeamCity.IsRunningOnTeamCity)
{
TeamCity.WriteStartBuildBlock(setupContext.Task.Description ?? setupContext.Task.Name);
TeamCity.WriteStartProgress(setupContext.Task.Description ?? setupContext.Task.Name);
}
});
TaskTeardown(teardownContext =>
{
if (TeamCity.IsRunningOnTeamCity)
{
TeamCity.WriteEndBuildBlock(teardownContext.Task.Description ?? teardownContext.Task.Name);
TeamCity.WriteEndProgress(teardownContext.Task.Description ?? teardownContext.Task.Name);
}
});
Task("Default")
.IsDependentOn("Compile");
Task("Clean")
.Does(() =>
{
CleanDirectories(buildDirectory);
MSBuild(solutionFile, msbuildSettings.WithTarget("Clean"));
});
Task("Init")
.IsDependentOn("Clean")
.Does(() =>
{
CreateDirectory(buildDirectory);
CreateDirectory(outputDirectory);
});
Task("Version")
.IsDependentOn("Init")
.Does(() =>
{
var gitversion = GitVersion(new GitVersionSettings{
UpdateAssemblyInfo = false,
OutputType = GitVersionOutput.Json
});
Information("Marking this build as version: " + version);
var assemblyVersion = "0.0.0";;
if (branch == "master") {
assemblyVersion = version;
}
CreateAssemblyInfo(buildDirectory + @"\CommonAssemblyInfo.cs", new AssemblyInfoSettings {
Version = assemblyVersion,
FileVersion = assemblyVersion,
InformationalVersion = gitversion.FullBuildMetaData,
Copyright = String.Format("(c) Julian Easterling {0}", DateTime.Now.Year),
Company = String.Empty,
Configuration = configuration
});
});
Task("PackageClean")
.Does(() =>
{
CleanDirectories(packageDirectory);
});
Task("PackageRestore")
.IsDependentOn("Init")
.Does(() =>
{
NuGetRestore(solutionFile);
// In a CI environment, there really isn't any value to the packages' PDB files and it confuses the code coverage task
var files = GetFiles(packageDirectory + "/**/*.pdb");
DeleteFiles(files);
});
Task("Compile")
.IsDependentOn("PackageRestore")
.IsDependentOn("Version")
.Does(() =>
{
MSBuild(solutionFile, msbuildSettings.WithTarget("ReBuild"));
});
Task("Test")
.IsDependentOn("UnitTest");
Task("UnitTest")
.IsDependentOn("Compile")
.Does(() =>
{
var dotNetTestSettings = new DotNetCoreTestSettings
{
Configuration = configuration,
NoBuild = true,
Logger = "console;verbosity=normal"
};
DotNetCoreTest(outputDirectory + "\\UnitTests.dll", dotNetTestSettings);
});
Task("Coverage")
.IsDependentOn("Compile")
.Does(() =>
{
CreateDirectory(buildDirectory + "\\coverage");
var dotNetTestSettings = new DotNetCoreTestSettings
{
Configuration = configuration,
NoBuild = true,
Logger = "console;verbosity=normal"
};
var openCoverSettings = new OpenCoverSettings() { Register = "user" }
.WithFilter(@"+[*]*")
.WithFilter(@"-[UnitTests]*")
.WithFilter(@"-[xunit.*]*")
.ExcludeByAttribute("System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverageAttribute")
.ExcludeByFile(@"*\\*Designer.cs;*\\*.g.cs;*.*.g.i.cs");
OpenCover(context => context.DotNetCoreTest(outputDirectory + "\\UnitTests.dll", dotNetTestSettings),
buildDirectory + "\\coverage\\coverage.xml", openCoverSettings);
ReportGenerator(buildDirectory + "\\coverage\\coverage.xml", buildDirectory + "\\coverage");
});
Task("TeamCity")
.Does(() =>
{
if (DirectoryExists(baseDirectory + "\\UnitTests")) {
RunTarget("coverage");
// Write class coverage
Information(String.Format(
"##teamcity[buildStatisticValue key='CodeCoverageAbsCCovered' value='{0}']",
XmlPeek(buildDirectory + "\\coverage\\coverage.xml", "/CoverageSession/Summary/@visitedClasses")));
Information(String.Format(
"##teamcity[buildStatisticValue key='CodeCoverageAbsCTotal' value='{0}']",
XmlPeek(buildDirectory + "\\coverage\\coverage.xml", "/CoverageSession/Summary/@numClasses")));
Information(String.Format(
"##teamcity[buildStatisticValue key='CodeCoverageC' value='{0:N2}']",
(
Convert.ToDouble(XmlPeek(buildDirectory + "\\coverage\\coverage.xml", "/CoverageSession/Summary/@visitedClasses")) /
Convert.ToDouble(XmlPeek(buildDirectory + "\\coverage\\coverage.xml", "/CoverageSession/Summary/@numClasses"))
) * 100));
// Report method coverage
Information(String.Format(
"##teamcity[buildStatisticValue key='CodeCoverageAbsMCovered' value='{0}']",
XmlPeek(buildDirectory + "\\coverage\\coverage.xml", "/CoverageSession/Summary/@visitedMethods")));
Information(String.Format(
"##teamcity[buildStatisticValue key='CodeCoverageAbsMTotal' value='{0}']",
XmlPeek(buildDirectory + "\\coverage\\coverage.xml", "/CoverageSession/Summary/@numMethods")));
Information(String.Format(
"##teamcity[buildStatisticValue key='CodeCoverageM' value='{0:N2}']",
(
Convert.ToDouble(XmlPeek(buildDirectory + "\\coverage\\coverage.xml", "/CoverageSession/Summary/@visitedMethods")) /
Convert.ToDouble(XmlPeek(buildDirectory + "\\coverage\\coverage.xml", "/CoverageSession/Summary/@numMethods"))
) * 100));
// Report branch coverage
Information(String.Format(
"##teamcity[buildStatisticValue key='CodeCoverageAbsBCovered' value='{0}']",
XmlPeek(buildDirectory + "\\coverage\\coverage.xml", "/CoverageSession/Summary/@visitedBranchPoints")));
Information(String.Format(
"##teamcity[buildStatisticValue key='CodeCoverageAbsBTotal' value='{0}']",
XmlPeek(buildDirectory + "\\coverage\\coverage.xml", "/CoverageSession/Summary/@numBranchPoints")));
Information(String.Format(
"##teamcity[buildStatisticValue key='CodeCoverageB' value='{0}']",
XmlPeek(buildDirectory + "\\coverage\\coverage.xml", "/CoverageSession/Summary/@branchCoverage")));
// Report statement coverage
Information(String.Format(
"##teamcity[buildStatisticValue key='CodeCoverageAbsSCovered' value='{0}']",
XmlPeek(buildDirectory + "\\coverage\\coverage.xml", "/CoverageSession/Summary/@visitedSequencePoints")));
Information(String.Format(
"##teamcity[buildStatisticValue key='CodeCoverageAbsSTotal' value='{0}']",
XmlPeek(buildDirectory + "\\coverage\\coverage.xml", "/CoverageSession/Summary/@numSequencePoints")));
Information(String.Format(
"##teamcity[buildStatisticValue key='CodeCoverageS' value='{0}']",
XmlPeek(buildDirectory + "\\coverage\\coverage.xml", "/CoverageSession/Summary/@sequenceCoverage")));
} else {
RunTarget("default");
}
});
Task("Package")
.IsDependentOn("Test")
.Does(() =>
{
CreateDirectory(buildDirectory + "\\packages");
var nuGetPackSettings = new NuGetPackSettings {
NoPackageAnalysis = true,
Version = version.Replace('/', '.'),
OutputDirectory = buildDirectory + "\\packages"
};
var nuspecFiles = GetFiles(baseDirectory + "\\*.nuspec");
NuGetPack(nuspecFiles, nuGetPackSettings);
});
Task("AppVeyor")
.IsDependentOn("Package")
.WithCriteria(() => AppVeyor.IsRunningOnAppVeyor)
.Does(() =>
{
GetFiles(buildDirectory + "\\packages\\*.nupkg")
.ToList()
.ForEach(f => AppVeyor.UploadArtifact(f, new AppVeyorUploadArtifactsSettings { DeploymentName = "packages" }));
});
RunTarget(target);