-
Notifications
You must be signed in to change notification settings - Fork 1
/
build.cake
290 lines (262 loc) · 10.9 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
#tool nuget:?package=xunit.runner.console&version=2.3.1
#tool nuget:?package=xunit.runner.visualstudio&version=2.3.1
#tool nuget:?package=OpenCover&version=4.6.519
#tool nuget:?package=ReportGenerator&version=3.1.2
#tool nuget:?package=GitVersion.CommandLine&version=3.6.5
// Load other scripts.
#load "./build/parameters.cake"
///////////////////////////////////////////////////////////////////////////////
// ARGUMENTS
///////////////////////////////////////////////////////////////////////////////
// Target - The task you want to start. Runs the Default task if not specified.
var target = Argument<string>("Target", "Default");
var configuration =
HasArgument("Configuration") ? Argument<string>("Configuration") :
EnvironmentVariable("Configuration") != null ? EnvironmentVariable("Configuration") : "Release";
// The build number to use in the version number of the built NuGet packages.
// There are multiple ways this value can be passed, this is a common pattern.
// 1. If command line parameter parameter passed, use that.
// 2. Otherwise if running on AppVeyor, get it's build number.
// 3. Otherwise if running on Travis CI, get it's build number.
// 4. Otherwise if an Environment variable exists, use that.
// 5. Otherwise default the build number to 0.
var preReleaseSuffix =
HasArgument("PreReleaseSuffix") ? Argument<string>("PreReleaseSuffix") :
(AppVeyor.IsRunningOnAppVeyor && AppVeyor.Environment.Repository.Tag.IsTag) ? null :
EnvironmentVariable("PreReleaseSuffix") != null ? EnvironmentVariable("PreReleaseSuffix") :
"beta";
var buildNumber = HasArgument("BuildNumber") ?
Argument<int>("BuildNumber") :
AppVeyor.IsRunningOnAppVeyor ? AppVeyor.Environment.Build.Number :
EnvironmentVariable("BuildNumber") != null ? int.Parse(EnvironmentVariable("BuildNumber")) :
0;
static DirectoryPath GetOutputArtifactFromProjectFile(DirectoryPath artifactsBinFolder,FilePath csprojFile)
{
var fileWithoutExtension=System.IO.Path.GetFileNameWithoutExtension(csprojFile.FullPath);
return artifactsBinFolder.Combine(fileWithoutExtension);
}
//////////////////////////////////////////////////////////////////////
// PARAMETERS
//////////////////////////////////////////////////////////////////////
BuildParameters parameters = BuildParameters.GetParameters(Context,target,configuration,preReleaseSuffix,buildNumber);
BuildPaths paths = parameters.Paths;
BuildVersion buildVersion = parameters.Version;
DotNetCoreMSBuildSettings msBuildSettings = new DotNetCoreMSBuildSettings()
.WithProperty("Version", buildVersion.Version)
.WithProperty("AssemblyVersion",buildVersion.VersionPrefix)
.WithProperty("FileVersion", buildVersion.VersionPrefix)
;
DotNetCoreBuildSettings dotNetCoreBuildSettings = new DotNetCoreBuildSettings()
{
Configuration = configuration,
NoRestore = true,
VersionSuffix = buildVersion.VersionSuffix,
ArgumentCustomization = args => args
.Append("--no-restore")
.AppendSwitch("/p:DebugType","=","Full")
,
MSBuildSettings=msBuildSettings
};
///////////////////////////////////////////////////////////////////////////////
// SETUP / TEARDOWN
///////////////////////////////////////////////////////////////////////////////
Setup(context =>
{
Information($"Running tasks...{parameters}");
Information($"FinalVersion {buildVersion.Version}");
Information($"VersionPrefix {buildVersion.VersionPrefix}");
Information($"VersionSuffix {buildVersion.VersionSuffix}");
EnsureDirectoryExists(paths.Directories.Artifacts);
EnsureDirectoryExists(paths.Directories.ArtifactsTestResults);
EnsureDirectoryExists(paths.Directories.ArtifactCodeCoverageReportDirectory);
EnsureDirectoryExists(paths.Directories.ArtifactNugetsDirectory);
EnsureDirectoryExists(paths.Directories.ArtifactsBinDir);
EnsureDirectoryExists(paths.Directories.ArtifactsBinNetStandard20);
EnsureDirectoryExists(paths.Directories.ArtifactsBinNetCoreapp20);
EnsureDirectoryExists(paths.Directories.ArtifactsBinNetCoreapp21);
});
Teardown(ctx =>Information("Finished running tasks."));
///////////////////////////////////////////////////////////////////////////////
// TASKS
///////////////////////////////////////////////////////////////////////////////
Task("Clean")
.Does(() =>
{
CleanDirectory(paths.Directories.ArtifactCodeCoverageReportDirectory);
CleanDirectory(paths.Directories.ArtifactNugetsDirectory);
CleanDirectory(paths.Directories.ArtifactsTestResults);
CleanDirectory(paths.Directories.ArtifactsBinNetStandard20);
CleanDirectory(paths.Directories.ArtifactsBinNetCoreapp20);
CleanDirectory(paths.Directories.ArtifactsBinNetCoreapp21);
CleanDirectory(paths.Directories.ArtifactsBinDir);
CleanDirectories(GetDirectories("**/bin"));
CleanDirectories(GetDirectories("**/obj"));
CleanDirectories(paths.Directories.ToClean);
});
// NuGet restore packages for .NET Framework projects (and .NET Core projects)
Task("NuGet-Restore")
.IsDependentOn("Clean")
.Does(() =>NuGetRestore(paths.Files.Solution.ToString()));
// NuGet restore packages for .NET Core projects only
Task("DotNet-Core-Package-Restore")
.IsDependentOn("NuGet-Restore")
.Does(() =>
{
DotNetCoreRestore(paths.Files.Solution.ToString(), new DotNetCoreRestoreSettings
{
Verbosity = DotNetCoreVerbosity.Minimal,
});
});
Task("Build")
.Does(() =>DotNetCoreBuild(paths.Files.Solution.ToString(), dotNetCoreBuildSettings));
// Look under a 'Tests' folder and run dotnet test against all of those projects.
// Then drop the XML test results file in the Artifacts folder at the root.
Task("Run-Unit-tests")
.Does(() =>
{
foreach(var project in paths.Files.TestProjects)
{
Information("Testing project " + project);
DotNetCoreTest(project.ToString(),new DotNetCoreTestSettings()
{
Configuration = configuration,
NoBuild = true,
NoRestore = true
});
}
});
Task("Test-OpenCover")
.IsDependentOn("Build")
//.IsDependentOn("Run-Unit-tests")
.WithCriteria(() => BuildSystem.IsLocalBuild || BuildSystem.IsRunningOnAppVeyor)
.Does(() =>
{
var success = true;
var openCoverSettings = new OpenCoverSettings
{
OldStyle = true,
MergeOutput = true,
Register = "user",
SkipAutoProps = true
}
.ExcludeByAttribute("*.ExcludeFromCodeCoverage*")
.WithFilter("+[*]* -[*.UnitsTests]*")
.WithFilter("-[xunit.*]*")
.WithFilter("-[*.*Tests]*")
;
foreach(var project in paths.Files.TestProjects)
{
try
{
Information("Testing project " + MakeAbsolute(project).ToString());
var projectFile = MakeAbsolute(project).ToString();
var dotNetTestSettings = new DotNetCoreTestSettings
{
Configuration = configuration,
NoBuild = true,
NoRestore = true,
ArgumentCustomization = args => args
.Append("--no-restore")
.AppendSwitch("/p:DebugType","=","Full")
};
OpenCover(context => context.DotNetCoreTest(projectFile, dotNetTestSettings),
paths.Directories.ArtifactCodeCoverageResultFile,
openCoverSettings
);
}
catch(Exception ex)
{
success = false;
Error("There was an error while running Test-OpenCover", ex);
throw;
}
}
try
{
ReportGenerator(paths.Directories.ArtifactCodeCoverageResultFile, paths.Directories.ArtifactsTestResults);
}
catch(Exception ex)
{
success = false;
Error("There was an error while running the Test-OpenCover", ex);
throw;
}
if(success == false)
{
throw new CakeException("There was an error while running the Test-OpenCover");
}
});
Task("Report-Coverage")
.IsDependentOn("Test-OpenCover").WithCriteria(() => BuildSystem.IsLocalBuild).Does(() =>{});
Task("Test-VSTest")
.IsDependentOn("Build")
.WithCriteria(() => BuildSystem.IsRunningOnVSTS)
.Does(() =>
{
VSTest(
GetFiles($"**/bin/{configuration}/*Tests.dll"),
new VSTestSettings
{
Logger = "trx",
EnableCodeCoverage = true,
InIsolation = true,
TestAdapterPath = $"tools/xunit.runner.visualstudio/build/_common",
ToolPath = $"{VS2017InstallDirectory(Context)}/Common7/IDE/CommonExtensions/Microsoft/TestWindow/vstest.console.exe"
});
});
Task("Test")
.IsDependentOn("Run-Unit-tests")
.IsDependentOn("Test-OpenCover")
.IsDependentOn("Test-VSTest");
// A meta-task that runs all the steps to Build and Test the app
Task("BuildAndTest")
.IsDependentOn("Clean")
.IsDependentOn("DotNet-Core-Package-Restore")
.IsDependentOn("Build")
.IsDependentOn("Test")
.Does(() =>
{
Information($"Builded and tested successfully");
})
.OnError(ex => {
Error("Build Failed, throwing exception...");
throw ex;
});
//////////////////////////////////////////////////////////////////////
// Versionning + Packaging
//////////////////////////////////////////////////////////////////////
Task("Version").Does(() =>{});
Task("Remove-Packages").Does(() =>CleanDirectory(paths.Directories.ArtifactNugetsDirectory));
Task("Package-NuGet")
.IsDependentOn("BuildAndTest")
.IsDependentOn("Remove-Packages")
.IsDependentOn("Version")
.DoesForEach(paths.Files.AllNuspecsProjects,projectNuSpecToPack =>
{
// .NET Core
var nuspecFile=projectNuSpecToPack.FullPath;
var csprojFile = projectNuSpecToPack.ChangeExtension(".csproj");
var fullBasePath = GetOutputArtifactFromProjectFile(paths.Directories.ArtifactsBinNetCoreapp21,nuspecFile);
DotNetCorePack(csprojFile.FullPath,new DotNetCorePackSettings()
{
Configuration = configuration,
OutputDirectory = paths.Directories.ArtifactNugetsDirectory,
VersionSuffix = buildVersion.VersionSuffix,
NoRestore=true,
NoBuild=true,
ArgumentCustomization = args => args
.Append("--no-restore")
.Append("--no-build")
.AppendSwitch("/p:PackageVersion","=",buildVersion.Version)
});
});
//////////////////////////////////////////////////////////////////////
// TASK TARGETS
//////////////////////////////////////////////////////////////////////
Task("Default")
.IsDependentOn("Package-NuGet")
;
//////////////////////////////////////////////////////////////////////
// EXECUTION
//////////////////////////////////////////////////////////////////////
RunTarget(target);