-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.cake
More file actions
225 lines (182 loc) · 7.97 KB
/
build.cake
File metadata and controls
225 lines (182 loc) · 7.97 KB
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
///////////////////////////////////////////////////////////////////////////////////////////////////
// Use build.ps1 to run the build script. Command line arguments are documented below.
///////////////////////////////////////////////////////////////////////////////////////////////////
const string DefaultSolutionName = "./AspNetCore.LocalhostRun.sln";
///////////////////////////////////////////////////////////////////////////////////////////////////
// COMMAND LINE ARGUMENTS:
//
// --project=<PROJECT OR SOLUTION>
// The MSBuild project or solution to build.
// Default: see DefaultSolutionName constant above.
//
// --target=<TARGET>
// Specifies the Cake target to run.
// Default: Test
// Possible Values: Clean, Restore, Build, Test, Pack
//
// --configuration=<CONFIGURATION>
// Specifies the MSBuild configuration to use.
// Default: Debug
//
// --clean
// Specifies if this is a rebuild rather than an incremental build. All artifact, bin, and test
// output folders will be cleaned prior to running the specified target.
//
// --no-tests
// Specifies that tests should be skipped.
//
// --ci
// Forces continuous integration build mode. Not required if the build is being run by a
// supported continuous integration build system.
//
// --sign-output
// Tells MSBuild that signing is required by setting the 'SignOutput' property to 'True'. The
// signing implementation must be supplied by MSBuild.
//
// --build-counter=<COUNTER>
// The build counter. This is used when generating version numbers for the build.
//
// --build-metadata=<METADATA>
// Additional build metadata that will be included in the information version number generated
// for compiled assemblies.
//
// --verbose
// Enables verbose messages.
//
// --property=<PROPERTY>
// Specifies an additional property to pass to MSBuild during Build and Pack targets. The value
// must be specified using a '<NAME>=<VALUE>' format e.g. --property="NoWarn=CS1591". This
// argument can be specified multiple times.
//
///////////////////////////////////////////////////////////////////////////////////////////////////
#addin nuget:?package=Cake.Git&version=1.0.0
#addin nuget:?package=Cake.Json&version=6.0.0
#addin nuget:?package=Newtonsoft.Json&version=12.0.3
#load "build/build-state.cake"
#load "build/build-utilities.cake"
// Get the target that was specified.
var target = Argument("target", "Test");
///////////////////////////////////////////////////////////////////////////////////////////////////
// SETUP / TEARDOWN
///////////////////////////////////////////////////////////////////////////////////////////////////
// Constructs the build state object.
Setup<BuildState>(context => {
try {
BuildUtilities.WriteTaskStartMessage(BuildSystem, "Setup");
var state = new BuildState() {
SolutionName = Argument("project", DefaultSolutionName),
Target = target,
Configuration = Argument("configuration", "Debug"),
ContinuousIntegrationBuild = HasArgument("ci") || !BuildSystem.IsLocalBuild,
Clean = HasArgument("clean"),
SkipTests = HasArgument("no-tests"),
SignOutput = HasArgument("sign-output"),
Verbose = HasArgument("verbose"),
MSBuildProperties = HasArgument("property") ? Arguments<string>("property") : new List<string>()
};
// Get raw version numbers from JSON.
var versionJson = ParseJsonFromFile("./build/version.json");
var majorVersion = versionJson.Value<int>("Major");
var minorVersion = versionJson.Value<int>("Minor");
var patchVersion = versionJson.Value<int>("Patch");
var versionSuffix = versionJson.Value<string>("PreRelease");
// Compute build number.
var buildCounter = Argument("build-counter", 0);
var branch = GitBranchCurrent(DirectoryPath.FromString(".")).FriendlyName;
state.BuildNumber = string.IsNullOrWhiteSpace(versionSuffix)
? $"{majorVersion}.{minorVersion}.{patchVersion}.{buildCounter}+{branch}"
: $"{majorVersion}.{minorVersion}.{patchVersion}-{versionSuffix}.{buildCounter}+{branch}";
if (!string.Equals(state.Target, "Clean", StringComparison.OrdinalIgnoreCase)) {
BuildUtilities.SetBuildSystemBuildNumber(BuildSystem, state);
BuildUtilities.WriteBuildStateToLog(BuildSystem, state);
}
return state;
}
finally {
BuildUtilities.WriteTaskEndMessage(BuildSystem, "Setup");
}
});
// Pre-task action.
TaskSetup(context => {
BuildUtilities.WriteTaskStartMessage(BuildSystem, context.Task.Name);
BuildUtilities.WriteLogMessage(BuildSystem, $"Running {context.Task.Name} task");
});
// Post task action.
TaskTeardown(context => {
BuildUtilities.WriteLogMessage(BuildSystem, $"Completed {context.Task.Name} task");
BuildUtilities.WriteTaskEndMessage(BuildSystem, context.Task.Name);
});
///////////////////////////////////////////////////////////////////////////////////////////////////
// TASKS
///////////////////////////////////////////////////////////////////////////////////////////////////
// Cleans up artifact and bin folders.
Task("Clean")
.WithCriteria<BuildState>((c, state) => state.RunCleanTarget)
.Does<BuildState>(state => {
foreach (var pattern in new [] { $"./src/**/bin/{state.Configuration}", "./artifacts/**", "./**/TestResults/**" }) {
BuildUtilities.WriteLogMessage(BuildSystem, $"Cleaning directories: {pattern}");
CleanDirectories(pattern);
}
});
// Restores NuGet packages.
Task("Restore")
.Does<BuildState>(state => {
DotNetCoreRestore(state.SolutionName);
});
// Builds the solution.
Task("Build")
.IsDependentOn("Clean")
.IsDependentOn("Restore")
.Does<BuildState>(state => {
var buildSettings = new DotNetCoreBuildSettings {
Configuration = state.Configuration,
NoRestore = true,
MSBuildSettings = new DotNetCoreMSBuildSettings()
};
buildSettings.MSBuildSettings.Targets.Add(state.Clean ? "Rebuild" : "Build");
BuildUtilities.ApplyMSBuildProperties(buildSettings.MSBuildSettings, state);
DotNetCoreBuild(state.SolutionName, buildSettings);
});
// Runs unit tests.
Task("Test")
.IsDependentOn("Build")
.WithCriteria<BuildState>((c, state) => !state.SkipTests)
.Does<BuildState>(state => {
var testSettings = new DotNetCoreTestSettings {
Configuration = state.Configuration,
NoBuild = true
};
var testResultsPrefix = state.ContinuousIntegrationBuild
? Guid.NewGuid().ToString()
: null;
if (testResultsPrefix != null) {
// We're using a build system; write the test results to a file so that they can be
// imported into the build system.
testSettings.Loggers = new List<string> {
$"trx;LogFilePrefix={testResultsPrefix}"
};
}
DotNetCoreTest(state.SolutionName, testSettings);
if (testResultsPrefix != null) {
foreach (var testResultsFile in GetFiles($"./**/TestResults/{testResultsPrefix}*.trx")) {
BuildUtilities.ImportTestResults(BuildSystem, "mstest", testResultsFile);
}
}
});
// Builds NuGet packages.
Task("Pack")
.IsDependentOn("Test")
.Does<BuildState>(state => {
var buildSettings = new DotNetCorePackSettings {
Configuration = state.Configuration,
NoRestore = true,
NoBuild = true,
MSBuildSettings = new DotNetCoreMSBuildSettings()
};
BuildUtilities.ApplyMSBuildProperties(buildSettings.MSBuildSettings, state);
DotNetCorePack(state.SolutionName, buildSettings);
});
///////////////////////////////////////////////////////////////////////////////////////////////////
// EXECUTION
///////////////////////////////////////////////////////////////////////////////////////////////////
RunTarget(target);