forked from Tenacom/Louis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.cake
354 lines (309 loc) · 14.3 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
// Copyright (C) Tenacom and contributors. Licensed under the MIT license.
// See LICENSE file in the project root for full license information.
#load "./build/BuildData.cake"
#load "./build/Changelog.cake"
#load "./build/dotnet.cake"
#load "./build/environment.cake"
#load "./build/fail.cake"
#load "./build/filesystem.cake"
#load "./build/git.cake"
#load "./build/github.cake"
#load "./build/json.cake"
#load "./build/nbgv.cake"
#load "./build/options.cake"
#load "./build/process.cake"
#load "./build/public-api.cake"
#load "./build/setup-teardown.cake"
#load "./build/utilities.cake"
#load "./build/versioning.cake"
#load "./build/workspace.cake"
#nullable enable
using System;
using System.Text;
using SysDirectory = System.IO.Directory;
using SysFile = System.IO.File;
using SysPath = System.IO.Path;
// =============================================================================================
// TASKS
// =============================================================================================
Task("Default")
.Description("Default task - Do nothing (but log build configuration data)")
.Does(context => {
context.Information("The default task does nothing. This is intentional.");
context.Information("Use `dotnet cake --description` to see the list of available tasks.");
});
Task("CleanAll")
.Description("Delete all output directories, VS data, R# caches")
.Does<BuildData>((context, data) => context.CleanAll(data));
Task("LocalCleanAll")
.Description("Like CleanAll, but only runs on a local machine")
.WithCriteria<BuildData>(data => !data.IsCI)
.Does<BuildData>((context, data) => context.CleanAll(data));
Task("Restore")
.Description("Restores dependencies")
.IsDependentOn("LocalCleanAll")
.Does<BuildData>((context, data) => context.RestoreSolution(data));
Task("Build")
.Description("Build all projects")
.IsDependentOn("Restore")
.Does<BuildData>((context, data) => context.BuildSolution(data, false));
Task("Test")
.Description("Build all projects and run tests")
.IsDependentOn("Build")
.Does<BuildData>((context, data) => context.TestSolution(data, false, false, true));
Task("Pack")
.Description("Build all projects, run tests, and prepare build artifacts")
.IsDependentOn("Test")
.Does<BuildData>((context, data) => context.PackSolution(data, false, false));
Task("Release")
.Description("Publish a new public release (CI only)")
.Does<BuildData>(async (context, data) => {
// Perform some preliminary checks
context.Ensure(data.IsCI, "The Release target cannot run on a local system.");
context.Ensure(data.IsPublicRelease, "Cannot create a release from the current branch.");
// Perform an initial versioning consistency check.
// This is a tad more relaxed than the final check, as it takes into account that we may still increment the current version
// (for example by updating the changelog).
context.CheckVersioningConsistency(
currentVersion: data.Version,
latestVersion: data.LatestVersion,
latestStableVersion: data.LatestStableVersion,
isFinalCheck: false);
// Compute the version spec change to apply, if any.
// This implies more checks and possibly throws, so do it as early as possible.
var versionSpecChange = context.ComputeVersionSpecChange(
currentVersion: data.Version,
latestVersion: data.LatestVersion,
latestStableVersion: data.LatestStableVersion,
requestedChange: context.GetOption<VersionSpecChange>("versionSpecChange", VersionSpecChange.None),
checkPublicApi: context.GetOption<bool>("checkPublicApi", true));
// Identify Git user for later possible push
context.GitSetUserIdentity("Buildvana", "[email protected]");
// Create the release as a draft first, so if the token has no permissions we can bail out early
var release = await context.CreateDraftReleaseAsync(data);
var dupeTagChecked = false;
var committed = false;
try
{
// Modify version if required.
if (versionSpecChange != VersionSpecChange.None)
{
var versionFile = VersionFile.Load(context);
if (versionFile.ApplyVersionSpecChange(context, versionSpecChange))
{
versionFile.Save();
UpdateRepo(versionFile.Path);
}
}
// Update public API files only when releasing a stable version
if (!data.IsPrerelease)
{
var modified = context.TransferAllPublicApiToShipped().ToArray();
if (modified.Length > 0)
{
context.Information($"{modified.Length} public API files were modified.");
UpdateRepo(modified);
}
else
{
context.Information("No public API files were modified.");
}
}
else
{
context.Information("Public API update skipped: not needed on prerelease.");
}
// Update changelog only on non-prerelease, unless forced
var changelog = new Changelog(context, data);
var changelogUpdated = false;
if (!changelog.Exists)
{
context.Information($"Changelog update skipped: {Changelog.FileName} not found.");
}
else if (!data.IsPrerelease || context.GetOption<bool>("forceUpdateChangelog", false))
{
if (context.GetOption<bool>("checkChangelog", true))
{
context.Ensure(
changelog.HasUnreleasedChanges(),
"Changelog check failed: the \"Unreleased changes\" section is empty or only contains sub-section headings.");
context.Information("Changelog check successful: the \"Unreleased changes\" section is not empty.");
}
else
{
context.Information("Changelog check skipped: option 'checkChangelog' is false.");
}
// Update the changelog and commit the change before building.
// This ensures that the Git height is up to date when computing a version for the build artifacts.
changelog.PrepareForRelease();
UpdateRepo(changelog.Path);
changelogUpdated = true;
}
else
{
context.Information("Changelog update skipped: not needed on prerelease.");
}
// At this point we know what the actual published version will be.
// Time for a final consistency check.
context.CheckVersioningConsistency(
currentVersion: data.Version,
latestVersion: data.LatestVersion,
latestStableVersion: data.LatestStableVersion,
isFinalCheck: true);
// Ensure that the release tag doesn't already exist.
// This assumes that full repo history has been checked out;
// however, that is already a prerequisite for using Nerdbank.GitVersioning.
context.Ensure(!context.GitTagExists(data.VersionStr), $"Tag {data.VersionStr} already exists in repository.");
dupeTagChecked = true;
context.RestoreSolution(data);
context.BuildSolution(data, false);
context.TestSolution(data, false, false, false);
context.PackSolution(data, false, false);
if (changelogUpdated)
{
// Change the new section's title in the changelog to reflect the actual version.
changelog.UpdateNewSectionTitle();
UpdateRepo(changelog.Path);
}
else
{
context.Information("Changelog section title update skipped: changelog has not been updated.");
}
if (committed)
{
context.Information($"Git pushing changes to {data.Remote}...");
_ = context.Exec("git", $"push {data.Remote} HEAD");
}
else
{
context.Information("Git push skipped: no commit to push.");
}
// Publish NuGet packages
await context.NuGetPushAllAsync(data);
// If this is not a prerelease and we are releasing from the main branch,
// dispatch a separate workflow to publish documentation.
// Unless, of course, there is no documentation to publish, or no workflow to do it.
FilePath docFxJsonPath = "docs/docfx.json";
FilePath pagesDeploymentWorkflow = ".github/workflows/deploy-pages.yml";
if (data.IsPrerelease)
{
context.Information("Documentation update skipped: not needed on prerelease.");
}
else if (data.Branch != "main")
{
context.Information($"Documentation update skipped: releasing from '{data.Branch}', not 'main'.");
}
else if (!SysFile.Exists(pagesDeploymentWorkflow.FullPath))
{
context.Information($"Documentation update skipped: {docFxJsonPath} not present.");
}
else if (!SysFile.Exists(pagesDeploymentWorkflow.FullPath))
{
context.Warning($"Documentation update skipped: there is no documentation workflow.");
}
else
{
await context.DispatchWorkflow(data, SysPath.GetFileName(pagesDeploymentWorkflow.FullPath), "main");
}
// Read release asset lists and upload assets
var assets = await GetReleaseAssetsAsync().ConfigureAwait(false);
var assetCount = assets.Count;
if (assetCount > 0)
{
var i = 0;
foreach (var asset in assets)
{
i++;
context.Information($"Uploading asset {i} of {assetCount}: {SysPath.GetFileName(asset.Path)} ({asset.Description})...");
await context.UploadReleaseAssetAsync(data, release, asset.Path, asset.MimeType, asset.Description).ConfigureAwait(false);
}
}
else
{
context.Information("Asset upload skipped: no release assets defined.");
}
// Last but not least, publish the release.
await context.PublishReleaseAsync(data, release);
// Set outputs for subsequent steps in GitHub Actions
if (data.IsGitHubAction)
{
context.SetActionsStepOutput("version", data.VersionStr);
}
}
catch (Exception e)
{
context.Error(e is CakeException ? e.Message : $"{e.GetType().Name}: {e.Message}");
await context.DeleteReleaseAsync(data, release, dupeTagChecked ? data.VersionStr : null);
throw;
}
void UpdateRepo(params FilePath[] files)
{
foreach (var path in files)
{
context.Verbose($"Git adding {path}...");
_ = context.Exec(
"git",
new ProcessArgumentBuilder()
.Append("add")
.AppendQuoted(path.FullPath));
}
context.Information(committed ? "Amending commit..." : "Committing changed files...");
var arguments = new ProcessArgumentBuilder().Append("commit");
if (committed)
{
arguments = arguments.Append("--amend");
}
arguments = arguments.Append("-m").AppendQuoted("Prepare release [skip ci]");
_ = context.Exec("git", arguments);
// The commit changed the Git height, so update build data
// and amend the commit adding the right version.
// Amending a commit does not further change the Git height.
data.Update(context);
_ = context.Exec(
"git",
new ProcessArgumentBuilder()
.Append("commit")
.Append("--amend")
.Append("-m")
.AppendQuoted($"Prepare release {data.VersionStr} [skip ci]"));
committed = true;
}
async Task<IReadOnlyList<(string Path, string MimeType, string Description)>> GetReleaseAssetsAsync()
{
const string assetListMask = "*.assets.txt";
var result = new List<(string Path, string MimeType, string Description)>();
if (!SysDirectory.EnumerateFiles(data.ArtifactsPath.FullPath, assetListMask).Any())
{
context.Information("Skipping asset upload: no release asset lists.");
return result;
}
context.Information("Reading release asset lists...");
var assetLists = SysPath.Combine(data.ArtifactsPath.FullPath, assetListMask);
foreach (var path in context.GetFiles(assetLists).Select(x => x.FullPath))
{
context.Verbose("Reading release asset list {path}...");
var i = 0;
await foreach (var line in SysFile.ReadLinesAsync(path))
{
i++;
var parts = line.Split('\t');
if (parts.Length != 3)
{
context.Warning($"Release asset list {path}, line #{i}: invalid line '{line}'");
continue;
}
if (!SysFile.Exists(parts[0]))
{
context.Warning($"Release asset list {path}, line #{i}: asset not found '{parts[0]}'");
continue;
}
result.Add((parts[0], parts[1], parts[2]));
}
}
return result;
}
});
// =============================================================================================
// EXECUTION
// =============================================================================================
RunTarget(Argument("target", "Default"));