forked from statiqdev/Statiq.Web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.cake
464 lines (415 loc) · 16.4 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// The following environment variables need to be set for Publish target:
// NUGET_API_KEY
// WYAM_GITHUB_TOKEN
// The following environment variables need to be set for Publish-MyGet target:
// MYGET_API_KEY
// Publishing workflow:
// - Update ReleaseNotes.md and RELEASE in develop branch
// - Run a normal build with Cake to set SolutionInfo.cs in the repo and run through unit tests (`build.cmd`)
// - Push to develop and fast-forward merge to master
// - Switch to master
// - Wait for CI to complete build and publish to MyGet
// - Run a local prerelease build of Wyam.Web to verify release (`build -Script "prerelease.cake"` from Wyam.Web folder)
// - Run a Publish build with Cake (`build -target Publish`)
// - No need to add a version tag to the repo - added by GitHub on publish
// - Switch back to develop branch
// - Run a build on Wyam.Web from CI to verify final release (first make sure NuGet Gallery has updated packages by searching for "wyam")
#addin "Cake.FileHelpers"
#addin "Octokit"
#addin "Cake.Squirrel"
using Octokit;
//////////////////////////////////////////////////////////////////////
// ARGUMENTS
//////////////////////////////////////////////////////////////////////
var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
//////////////////////////////////////////////////////////////////////
// PREPARATION
//////////////////////////////////////////////////////////////////////
var isLocal = BuildSystem.IsLocalBuild;
var isRunningOnUnix = IsRunningOnUnix();
var isRunningOnWindows = IsRunningOnWindows();
var isRunningOnAppVeyor = AppVeyor.IsRunningOnAppVeyor;
var isPullRequest = AppVeyor.Environment.PullRequest.IsPullRequest;
var buildNumber = AppVeyor.Environment.Build.Number;
var releaseNotes = ParseReleaseNotes("./ReleaseNotes.md");
var version = releaseNotes.Version.ToString();
var semVersion = version + (isLocal ? string.Empty : string.Concat("-build-", buildNumber));
var buildDir = Directory("./src/clients/Wyam/bin") + Directory(configuration);
var buildResultDir = Directory("./build") + Directory(semVersion);
var nugetRoot = buildResultDir + Directory("nuget");
var binDir = buildResultDir + Directory("bin");
var windowsDir = buildResultDir + Directory("windows");
var zipFile = "Wyam-v" + semVersion + ".zip";
///////////////////////////////////////////////////////////////////////////////
// SETUP / TEARDOWN
///////////////////////////////////////////////////////////////////////////////
Setup(context =>
{
Information("Building version {0} of Wyam.", semVersion);
});
//////////////////////////////////////////////////////////////////////
// TASKS
//////////////////////////////////////////////////////////////////////
Task("Clean")
.Does(() =>
{
CleanDirectories(new DirectoryPath[] { buildDir, buildResultDir, binDir, nugetRoot, windowsDir });
});
Task("Restore-Packages")
.IsDependentOn("Clean")
.Does(() =>
{
NuGetRestore("./Wyam.sln");
if (isRunningOnWindows)
{
NuGetRestore("./Wyam.Windows.sln");
}
});
Task("Patch-Assembly-Info")
.IsDependentOn("Restore-Packages")
.Does(() =>
{
var file = "./SolutionInfo.cs";
CreateAssemblyInfo(file, new AssemblyInfoSettings {
Product = "Wyam",
Copyright = "Copyright \xa9 Wyam Contributors",
Version = version,
FileVersion = version,
InformationalVersion = semVersion
});
});
Task("Build")
.IsDependentOn("Patch-Assembly-Info")
.Does(() =>
{
MSBuild("./Wyam.sln", new MSBuildSettings()
{
ArgumentCustomization = args => args.Append("/p:WarningLevel=0")
}
.SetConfiguration(configuration)
.SetMaxCpuCount(0)
.SetVerbosity(Verbosity.Minimal)
.UseToolVersion(MSBuildToolVersion.VS2017)
);
MSBuild("./Wyam.Windows.sln", new MSBuildSettings()
{
ArgumentCustomization = args => args.Append("/p:WarningLevel=0")
}
.SetConfiguration(configuration)
.SetMaxCpuCount(0)
.SetVerbosity(Verbosity.Minimal)
);
});
Task("Run-Unit-Tests")
.IsDependentOn("Build")
.Does(() =>
{
var settings = new NUnit3Settings
{
Work = buildResultDir.Path.FullPath
};
if (isRunningOnAppVeyor)
{
settings.Where = "cat != ExcludeFromAppVeyor";
}
NUnit3("./tests/**/bin/" + configuration + "/*.Tests.dll", settings);
});
Task("Copy-Files")
.IsDependentOn("Build")
.Does(() =>
{
CopyDirectory(buildDir, binDir);
CopyFiles(new FilePath[] { "LICENSE", "README.md", "ReleaseNotes.md" }, binDir);
});
Task("Zip-Files")
.IsDependentOn("Copy-Files")
.Does(() =>
{
var zipPath = buildResultDir + File(zipFile);
var files = GetFiles(binDir.Path.FullPath + "/**/*");
Zip(binDir, zipPath, files);
});
Task("Create-Library-Packages")
.IsDependentOn("Build")
.Does(() =>
{
// Get the set of nuspecs to package
List<FilePath> nuspecs = new List<FilePath>(GetFiles("./src/**/*.nuspec"));
// The Wyam.All and Wyam.Windows are packaged specially
nuspecs.RemoveAll(x => x.GetDirectory().GetDirectoryName() == "Wyam.All");
nuspecs.RemoveAll(x => x.GetDirectory().GetDirectoryName() == "Wyam.Windows");
// Package all nuspecs
foreach (var nuspec in nuspecs)
{
NuGetPack(nuspec.ChangeExtension(".csproj"), new NuGetPackSettings
{
Version = semVersion,
BasePath = nuspec.GetDirectory(),
OutputDirectory = nugetRoot,
Symbols = false,
Files = new NuSpecContent[] {},
Properties = new Dictionary<string, string>
{
{ "Configuration", configuration }
}
});
}
});
Task("Create-Theme-Packages")
.Does(() =>
{
// All themes must be under the themes folder in a NameOfRecipe/NameOfTheme subfolder
var themeDirectories = GetDirectories("./themes/*/*");
// Package all themes
foreach (var themeDirectory in themeDirectories)
{
string[] segments = themeDirectory.Segments;
string id = "Wyam." + segments[segments.Length - 2] + "." + segments[segments.Length - 1];
NuGetPack(new NuGetPackSettings
{
Id = id,
Version = semVersion,
Title = id,
Authors = new [] { "Dave Glick" },
Owners = new [] { "Dave Glick" },
Description = "A theme for the Wyam " + segments[segments.Length - 2] + " recipe.",
ProjectUrl = new Uri("https://wyam.io"),
IconUrl = new Uri("https://wyam.io/assets/img/logo-square-64.png"),
LicenseUrl = new Uri("https://github.com/Wyamio/Wyam/blob/master/LICENSE"),
Copyright = "Copyright 2017",
Tags = new [] { "Wyam", "Theme", "Static", "StaticContent", "StaticSite" },
RequireLicenseAcceptance = false,
Symbols = false,
Files = new []
{
new NuSpecContent
{
Source = "**/*",
Target = "content"
}
},
BasePath = themeDirectory,
OutputDirectory = nugetRoot
});
}
});
Task("Create-AllModules-Package")
.IsDependentOn("Build")
.Does(() =>
{
var nuspec = GetFiles("./src/extensions/Wyam.All/*.nuspec").FirstOrDefault();
if (nuspec == null)
{
throw new InvalidOperationException("Could not find all modules nuspec.");
}
// Add dependencies for all module libraries
List<FilePath> nuspecs = new List<FilePath>(GetFiles("./src/extensions/**/*.nuspec"));
nuspecs.RemoveAll(x => x.GetDirectory().GetDirectoryName() == "Wyam.All");
List<NuSpecDependency> dependencies = new List<NuSpecDependency>(
nuspecs
.Select(x => new NuSpecDependency
{
Id = x.GetDirectory().GetDirectoryName(),
Version = semVersion
})
);
// Pack the all modules package
NuGetPack(nuspec, new NuGetPackSettings
{
Version = semVersion,
BasePath = nuspec.GetDirectory(),
OutputDirectory = nugetRoot,
Symbols = false,
Dependencies = dependencies
});
});
Task("Create-Tools-Package")
.IsDependentOn("Build")
.Does(() =>
{
var nuspec = GetFiles("./src/clients/Wyam/*.nuspec").FirstOrDefault();
if (nuspec == null)
{
throw new InvalidOperationException("Could not find tools nuspec.");
}
var pattern = string.Format("bin\\{0}\\**\\*", configuration); // This is needed to get around a Mono scripting issue (see #246, #248, #249)
NuGetPack(nuspec, new NuGetPackSettings
{
Version = semVersion,
BasePath = nuspec.GetDirectory(),
OutputDirectory = nugetRoot,
Symbols = false,
Files = new []
{
new NuSpecContent
{
Source = pattern,
Target = "tools"
}
}
});
});
// Note that we're not creating a differential release files since we're using a new releases folder per-version
// That's by design - in order to distribute diffs from GitHub and have them get picked up by Squirrel, *all* prior
// versions have to be included in *every* GitHub release. That stinks, and we're not going to do it. Since Squirrel
// won't do incremental updates if we don't upload everything, it serves no purpose to create the diffs.
Task("Create-Windows")
.IsDependentOn("Copy-Files")
.Does(() => {
if(isRunningOnWindows)
{
var nuspec = GetFiles("./src/clients/Wyam.Windows/*.nuspec").FirstOrDefault();
if (nuspec == null)
{
throw new InvalidOperationException("Could not find installer nuspec.");
}
var packageDir = nuspec.GetDirectory() + ("/bin/" + configuration);
CopyDirectory(binDir, packageDir); // Copy everything from main Wyam bin to Wyam.Windows bin prior to packaging
var pattern = string.Format("bin\\{0}\\**\\*", configuration); // This is needed to get around a Mono scripting issue (see #246, #248, #249)
NuGetPack(nuspec, new NuGetPackSettings
{
Version = semVersion,
BasePath = nuspec.GetDirectory(),
OutputDirectory = packageDir,
Symbols = false,
Files = new []
{
new NuSpecContent
{
Source = pattern,
Target = "lib/net45"
}
}
});
var package = (packageDir + "/") + File("Wyam.Windows." + semVersion + ".nupkg");
Squirrel(package, new SquirrelSettings
{
Silent = true,
NoMsi = true,
ReleaseDirectory = windowsDir,
SetupIcon = GetFiles("./src/clients/Wyam.Windows/wyam.ico").First().FullPath
});
DeleteFile(package);
}
});
Task("Publish-MyGet")
.IsDependentOn("Create-Packages")
.WithCriteria(() => !isLocal)
.WithCriteria(() => !isPullRequest)
.Does(() =>
{
// Resolve the API key.
var apiKey = EnvironmentVariable("MYGET_API_KEY");
if (string.IsNullOrEmpty(apiKey))
{
throw new InvalidOperationException("Could not resolve MyGet API key.");
}
foreach (var nupkg in GetFiles(nugetRoot.Path.FullPath + "/*.nupkg"))
{
NuGetPush(nupkg, new NuGetPushSettings
{
Source = "https://www.myget.org/F/wyam/api/v2/package",
ApiKey = apiKey
});
}
});
Task("Publish-Packages")
.IsDependentOn("Create-Packages")
.WithCriteria(() => isLocal)
// TODO: Add criteria that makes sure this is the master branch
.Does(() =>
{
var apiKey = EnvironmentVariable("NUGET_API_KEY");
if (string.IsNullOrEmpty(apiKey))
{
throw new InvalidOperationException("Could not resolve NuGet API key.");
}
foreach (var nupkg in GetFiles(nugetRoot.Path.FullPath + "/*.nupkg"))
{
NuGetPush(nupkg, new NuGetPushSettings
{
ApiKey = apiKey,
Source = "https://nuget.org/api/v2/package"
});
}
});
Task("Publish-Release")
.IsDependentOn("Zip-Files")
.IsDependentOn("Create-Windows")
.WithCriteria(() => isLocal)
// TODO: Add criteria that makes sure this is the master branch
.Does(() =>
{
var githubToken = EnvironmentVariable("WYAM_GITHUB_TOKEN");
if (string.IsNullOrEmpty(githubToken))
{
throw new InvalidOperationException("Could not resolve Wyam GitHub token.");
}
var github = new GitHubClient(new ProductHeaderValue("WyamCakeBuild"))
{
Credentials = new Credentials(githubToken)
};
var release = github.Repository.Release.Create("Wyamio", "Wyam", new NewRelease("v" + semVersion)
{
Name = semVersion,
Body = string.Join(Environment.NewLine, releaseNotes.Notes) + Environment.NewLine + Environment.NewLine
+ @"### Please see https://wyam.io/docs/usage/obtaining for important notes about downloading and installing.",
TargetCommitish = "master"
}).Result;
var zipPath = buildResultDir + File(zipFile);
using (var zipStream = System.IO.File.OpenRead(zipPath.Path.FullPath))
{
var releaseAsset = github.Repository.Release.UploadAsset(release, new ReleaseAssetUpload(zipFile, "application/zip", zipStream, null)).Result;
}
var windowsFiles = GetFiles(windowsDir.Path.FullPath + "/*");
foreach (var windowsFile in windowsFiles)
{
using (var contentStream = System.IO.File.OpenRead(windowsFile.FullPath))
{
var fileName = windowsFile.GetFilename().ToString();
var releaseAsset = github.Repository.Release.UploadAsset(release, new ReleaseAssetUpload(fileName, "application/binary", contentStream, null)).Result;
}
}
});
Task("Update-AppVeyor-Build-Number")
.WithCriteria(() => isRunningOnAppVeyor)
.Does(() =>
{
AppVeyor.UpdateBuildVersion(semVersion);
});
Task("Upload-AppVeyor-Artifacts")
.IsDependentOn("Zip-Files")
.WithCriteria(() => isRunningOnAppVeyor)
.Does(() =>
{
var artifact = buildResultDir + File(zipFile);
AppVeyor.UploadArtifact(artifact);
});
//////////////////////////////////////////////////////////////////////
// TASK TARGETS
//////////////////////////////////////////////////////////////////////
Task("Create-Packages")
.IsDependentOn("Create-Library-Packages")
.IsDependentOn("Create-Theme-Packages")
.IsDependentOn("Create-AllModules-Package")
.IsDependentOn("Create-Tools-Package");
Task("Package")
.IsDependentOn("Run-Unit-Tests")
.IsDependentOn("Zip-Files")
.IsDependentOn("Create-Windows")
.IsDependentOn("Create-Packages");
Task("Default")
.IsDependentOn("Package");
Task("Publish")
.IsDependentOn("Publish-Packages")
.IsDependentOn("Publish-Release");
Task("AppVeyor")
.IsDependentOn("Run-Unit-Tests")
.IsDependentOn("Publish-MyGet")
.IsDependentOn("Update-AppVeyor-Build-Number")
.IsDependentOn("Upload-AppVeyor-Artifacts");
//////////////////////////////////////////////////////////////////////
// EXECUTION
//////////////////////////////////////////////////////////////////////
RunTarget(target);