forked from tpolyak/Sharp-Architecture
-
Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathbuild.cake
332 lines (279 loc) · 11 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
// ADDINS
#addin nuget:?package=Cake.Coveralls&version=1.1.0
#addin nuget:?package=Cake.FileHelpers&version=4.0.1
#addin nuget:?package=Cake.AppVeyor&version=5.0.1
#addin nuget:?package=Cake.ReSharperReports&version=0.11.1
// TOOLS
#tool nuget:?package=GitReleaseManager&version=0.11.0
#tool nuget:?package=GitVersion.CommandLine&version=5.6.7
#tool nuget:?package=coveralls.io&version=1.4.2
#tool nuget:?package=OpenCover&version=4.7.922
#tool nuget:?package=ReportGenerator&version=4.8.7
#tool nuget:?package=JetBrains.ReSharper.CommandLineTools&version=2020.3.4
// ARGUMENTS
var target = Argument("target", "Default");
if (string.IsNullOrWhiteSpace(target))
{
target = "Default";
}
var buildConfig = Argument("buildConfig", "Release");
if (string.IsNullOrEmpty(buildConfig)) {
buildConfig = "Release";
}
// Build configuration
var repoOwner = "sharparchitecture";
var repoName = "Sharp-Architecture";
var local = BuildSystem.IsLocalBuild;
var isPullRequest = AppVeyor.Environment.PullRequest.IsPullRequest;
var isRepository = StringComparer.OrdinalIgnoreCase.Equals($"{repoOwner}/{repoName}", AppVeyor.Environment.Repository.Name);
var isDebugBuild = string.Equals(buildConfig, "Debug", StringComparison.OrdinalIgnoreCase);
var isReleaseBuild = string.Equals(buildConfig, "Release", StringComparison.OrdinalIgnoreCase);
var isDevelopBranch = StringComparer.OrdinalIgnoreCase.Equals("develop", AppVeyor.Environment.Repository.Branch);
var isReleaseBranch = AppVeyor.Environment.Repository.Branch.IndexOf("releases/", StringComparison.OrdinalIgnoreCase) >= 0
|| AppVeyor.Environment.Repository.Branch.IndexOf("hotfixes/", StringComparison.OrdinalIgnoreCase) >= 0;
var isTagged = AppVeyor.Environment.Repository.Tag.IsTag;
var appVeyorJobId = AppVeyor.Environment.JobId;
// Solution settings
// Calculate version and commit hash
GitVersion semVersion = GitVersion();
var nugetVersion = semVersion.NuGetVersion;
var buildVersion = semVersion.FullBuildMetaData;
var informationalVersion = semVersion.InformationalVersion;
var nextMajorRelease = $"{semVersion.Major+1}.0.0";
var commitHash = semVersion.Sha;
var milestone = semVersion.MajorMinorPatch;
// Artifacts
var artifactsDir = "./Drops";
var artifactsDirAbsolutePath = MakeAbsolute(Directory(artifactsDir));
var testCoverageOutputFile = new FilePath(artifactsDir + "/OpenCover.xml");
var codeCoverageReportDir = artifactsDir + "/CodeCoverageReport";
var codeInspectionsOutputFile = artifactsDir + "/Inspections/CodeInspections.xml";
var duplicateFinderOutputFile = artifactsDir + "/Inspections/CodeDuplicates.xml";
var packagesDir = artifactsDir + "/packages";
var srcDir = "./Src";
var testsRootDir = srcDir + "/tests";
var solutionFile = new FilePath(srcDir + "/SharpArch.sln");
var samplesDir = "./Samples";
var coverageFilter="+[SharpArch*]* -[SharpArch.Tests*]* -[SharpArch.Xunit*]* -[SharpArch.Infrastructure]SharpArch.Infrastructure.Logging.*";
string githubToken = null;
// SETUP / TEARDOWN
Setup((context) =>
{
Information("Building version {0} (tagged: {1}, local: {2}, release branch: {3})...", nugetVersion, isTagged, local, isReleaseBranch);
CreateDirectory(artifactsDir);
CleanDirectory(artifactsDir);
githubToken = context.EnvironmentVariable("GITHUB_TOKEN");
});
Teardown((context) =>
{
// Executed AFTER the last task.
});
Task("SetVersion")
.Does(() =>
{
CreateAssemblyInfo($"{srcDir}/Common/AssemblyVersion.cs", new AssemblyInfoSettings{
FileVersion = semVersion.MajorMinorPatch,
InformationalVersion = semVersion.InformationalVersion,
Version = semVersion.MajorMinorPatch
});
});
Task("UpdateAppVeyorBuildNumber")
.WithCriteria(() => AppVeyor.IsRunningOnAppVeyor)
.ContinueOnError()
.Does(() =>
{
AppVeyor.UpdateBuildVersion(buildVersion);
});
Task("Restore")
.DoesForEach(GetFiles(solutionFile.ToString()).Union(GetFiles($"{samplesDir}/**/*.sln")),
(sln) => {
Information("Running in {0}", sln.GetDirectory().FullPath);
DotNetCoreRestore(sln.GetDirectory().FullPath);
}
);
Task("InspectCode")
.Does(() => {
DupFinder(solutionFile, new DupFinderSettings {
CachesHome = "./tmp/DupFinderCaches",
DiscardCost = 70,
DiscardFieldsName = false,
DiscardLiterals = false,
NormalizeTypes = true,
ShowStats = true,
ShowText = true,
OutputFile = duplicateFinderOutputFile,
ExcludePattern = new string [] {
"../Docker/**/*",
"Solution Items/**/*",
"Tests/**/*",
"Samples/**/*"
}
});
ReSharperReports(
duplicateFinderOutputFile,
System.IO.Path.ChangeExtension(duplicateFinderOutputFile, "html")
);
InspectCode(solutionFile, new InspectCodeSettings() {
OutputFile = codeInspectionsOutputFile,
Profile = "SharpArch.AutoLoad.DotSettings",
CachesHome = "./tmp/ReSharperCaches",
SolutionWideAnalysis = true
});
ReSharperReports(
codeInspectionsOutputFile,
System.IO.Path.ChangeExtension(codeInspectionsOutputFile, "html")
);
});
Task("RunXunitTests")
.DoesForEach(GetFiles(solutionFile.ToString()).Union(GetFiles($"{samplesDir}/**/*.sln")),
(testProj) => {
var projectPath = testProj.GetDirectory();
var projectFilename = testProj.GetFilenameWithoutExtension();
Information("Calculating code coverage for {0} ...", projectFilename);
var openCoverSettings = new OpenCoverSettings {
OldStyle = true,
ReturnTargetCodeOffset = 0,
ArgumentCustomization = args => args.Append("-mergeoutput").Append("-hideskipped:File;Filter;Attribute"),
WorkingDirectory = projectPath,
}
.WithFilter(coverageFilter)
.ExcludeByAttribute("*.ExcludeFromCodeCoverage*")
.ExcludeByFile("*/*Designer.cs");
Func<string,ProcessArgumentBuilder> buildProcessArgs = (buildCfg) => {
var pb = new ProcessArgumentBuilder()
.AppendSwitch("--configuration", buildCfg)
.AppendSwitch("--filter", "Category!=IntegrationTests")
.AppendSwitch("--results-directory", artifactsDirAbsolutePath.FullPath)
.Append("--no-build");
if (!local) {
pb.AppendSwitch("--test-adapter-path", ".")
.AppendSwitch("--logger", $"AppVeyor");
} else {
pb.AppendSwitch("--logger", $"trx;LogFileName={projectFilename}.trx");
}
return pb;
};
// run open cover for debug build configuration
OpenCover(
tool => tool.DotNetCoreTool(projectPath.FullPath,
"test",
buildProcessArgs("Debug")
),
testCoverageOutputFile,
openCoverSettings);
// run tests again if Release mode was requested
if (isReleaseBuild) {
Information("Running Release mode tests for {0}", projectFilename.ToString());
DotNetCoreTool(testProj.FullPath,
"test",
buildProcessArgs("Release")
);
}
})
.DeferOnError();
Task("CleanPreviousTestResults")
.Does(() =>
{
if (FileExists(testCoverageOutputFile))
DeleteFile(testCoverageOutputFile);
DeleteFiles(artifactsDir + "/*.trx");
if (DirectoryExists(codeCoverageReportDir))
DeleteDirectory(codeCoverageReportDir, new DeleteDirectorySettings{
Recursive = true,
Force = true
});
});
Task("GenerateCoverageReport")
.WithCriteria(() => local)
.Does(() =>
{
ReportGenerator(testCoverageOutputFile, codeCoverageReportDir);
});
Task("RunUnitTests")
.IsDependentOn("Build")
.IsDependentOn("CleanPreviousTestResults")
.IsDependentOn("RunXunitTests")
.IsDependentOn("GenerateCoverageReport")
.Does(() =>
{
Information("Done Test");
})
.Finally(() => {
if (!local) {
CoverallsIo(testCoverageOutputFile);
}
});
Task("Build")
.IsDependentOn("SetVersion")
.IsDependentOn("UpdateAppVeyorBuildNumber")
.IsDependentOn("Restore")
.DoesForEach(GetFiles($"{srcDir}/**/*.sln").Union(GetFiles($"{samplesDir}/**/*.sln")),
(solutionFile) => {
var slnPath = solutionFile.GetDirectory().FullPath;
var sln = solutionFile.GetFilenameWithoutExtension();
if (isReleaseBuild) {
Information("Running {0} {1} build to calculate code coverage", sln, "Debug");
// need Debug mode build for code coverage calculation
DotNetCoreBuild(slnPath, new DotNetCoreBuildSettings {
NoRestore = true,
Configuration = "Debug",
});
}
Information("Running {0} {1} build in {2}", sln, buildConfig, slnPath);
DotNetCoreBuild(slnPath, new DotNetCoreBuildSettings {
NoRestore = true,
Configuration = buildConfig,
});
});
Task("CreateNugetPackages")
.Does(() => {
Action<string> buildPackage = (string projectName) => {
var projectFileName=projectName;
Information("Pack {0}", projectFileName);
DotNetCorePack(projectFileName, new DotNetCorePackSettings {
Configuration = buildConfig,
OutputDirectory = packagesDir,
NoBuild = true,
NoRestore = true,
ArgumentCustomization = args => args.Append($"-p:Version={nugetVersion}")
});
};
if (isTagged) {
var releaseNotes = $"https://github.com/{repoOwner}/{repoName}/releases/tag/{milestone}";
Information("Updating ReleaseNotes Link: {0}", releaseNotes);
XmlPoke("./Directory.Build.props",
"/Project/PropertyGroup[@Label=\"Package\"]/PackageReleaseNotes",
releaseNotes
);
}
foreach(var projectName in new[] {$"{solutionFile}"}) {
buildPackage(projectName);
};
});
Task("CreateRelease")
.WithCriteria(() => isRepository && isReleaseBranch && !isPullRequest)
.Does(() => {
GitReleaseManagerCreate(githubToken, repoOwner, repoName,
new GitReleaseManagerCreateSettings {
Milestone = milestone,
TargetCommitish = "master"
});
});
Task("CloseMilestone")
.WithCriteria(() => isRepository && isTagged && !isPullRequest)
.Does(() => {
GitReleaseManagerClose(githubToken, repoOwner, repoName, milestone);
});
Task("Default")
.IsDependentOn("UpdateAppVeyorBuildNumber")
.IsDependentOn("Build")
.IsDependentOn("RunUnitTests")
// .IsDependentOn("InspectCode")
.IsDependentOn("CreateNugetPackages")
.IsDependentOn("CreateRelease")
.IsDependentOn("CloseMilestone")
.Does(
() => {}
);
// EXECUTION
RunTarget(target);