-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.cake
312 lines (267 loc) · 10.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
// adapted from https://github.com/Wyamio/Wyam/blob/develop/build.cake
#addin "Cake.FileHelpers"
#addin "Octokit"
#tool "nuget:?package=NUnit.ConsoleRunner&version=3.7.0"
#tool "nuget:?package=NuGet.CommandLine&version=4.9.2"
#tool "AzurePipelines.TestLogger&version=1.0.2"
using Octokit;
//////////////////////////////////////////////////////////////////////
// SCRIPT CONFIGURATION
//////////////////////////////////////////////////////////////////////
var applicationName = "shapeflow";
var solutionRoot = "./src/engine";
var runnableRoot = $"{solutionRoot}/{applicationName}";
var runnableProject = $"{runnableRoot}/{applicationName}.csproj";
var assemblyInfoFile = $"{solutionRoot}/SolutionInfo.cs";
var packagingGlob = $"{solutionRoot}/**/*.csproj";
var solutionFile = $"./{applicationName}.sln";
var unitTestsGlob = $"./tests/**/*.csproj";
var mygeturi = $"https://www.myget.org/F/{applicationName}/api/v2/package";
var target = Argument("target", "Default");
var configuration = Argument("configuration", "Release");
//////////////////////////////////////////////////////////////////////
// PREPARATION
//////////////////////////////////////////////////////////////////////
var isLocal = BuildSystem.IsLocalBuild;
var isRunningOnUnix = IsRunningOnUnix();
var isRunningOnWindows = IsRunningOnWindows();
var isRunningOnBuildServer = !string.IsNullOrEmpty(EnvironmentVariable("AGENT_NAME")); // See https://github.com/cake-build/cake/issues/1684#issuecomment-397682686
var isPullRequest = !string.IsNullOrWhiteSpace(EnvironmentVariable("SYSTEM_PULLREQUEST_PULLREQUESTID")); // See https://github.com/cake-build/cake/issues/2149
var buildNumber = (EnvironmentVariable("APPVEYOR_BUILD_NUMBER") ?? string.Empty).Replace('.', '-');
var branch = EnvironmentVariable("APPVEYOR_REPO_BRANCH");
var releaseNotes = ParseReleaseNotes("./ReleaseNotes.md");
var version = releaseNotes.Version.ToString();
var semVersion = version + (isLocal ? string.Empty : string.Concat("-build-", buildNumber));
var msBuildSettings = new DotNetCoreMSBuildSettings()
.WithProperty("Version", semVersion)
.WithProperty("AssemblyVersion", version)
.WithProperty("FileVersion", version);
var buildDir = Directory(runnableRoot) + Directory("bin") + Directory(configuration);
var buildResultDir = Directory("./build");
var nugetRoot = buildResultDir + Directory("nuget");
var chocoRoot = buildResultDir + Directory("choco");
var binDir = buildResultDir + Directory("bin");
var zipFile = $"{applicationName}-v" + semVersion + ".zip";
///////////////////////////////////////////////////////////////////////////////
// SETUP / TEARDOWN
///////////////////////////////////////////////////////////////////////////////
Setup(context =>
{
Information($"Building version { semVersion } of { applicationName }.");
});
//////////////////////////////////////////////////////////////////////
// TASKS
//////////////////////////////////////////////////////////////////////
Task("Clean")
.Does(() =>
{
CleanDirectories(new DirectoryPath[] { buildDir, buildResultDir, binDir, nugetRoot, chocoRoot });
});
Task("Patch-Assembly-Info")
.IsDependentOn("Clean")
.Does(() =>
{
var file = assemblyInfoFile;
CreateAssemblyInfo(file, new AssemblyInfoSettings {
Product = applicationName,
Copyright = $"Copyright \xa9 { applicationName } Contributors",
Version = version,
FileVersion = version,
InformationalVersion = semVersion
});
});
Task("Restore-Packages")
.IsDependentOn("Patch-Assembly-Info")
.Does(() =>
{
DotNetCoreRestore(solutionFile, new DotNetCoreRestoreSettings
{
MSBuildSettings = msBuildSettings
});
});
Task("Build")
.IsDependentOn("Restore-Packages")
.Does(() =>
{
DotNetCoreBuild(solutionFile, new DotNetCoreBuildSettings
{
Configuration = configuration,
NoRestore = true,
MSBuildSettings = msBuildSettings
});
});
Task("Publish-Client")
.IsDependentOn("Build")
.Does(() =>
{
DotNetCorePublish(runnableProject, new DotNetCorePublishSettings
{
Configuration = configuration,
NoBuild = true,
NoRestore = true,
});
});
Task("Run-Unit-Tests")
.IsDependentOn("Build")
.DoesForEach(GetFiles(unitTestsGlob), project =>
{
DotNetCoreTestSettings testSettings = new DotNetCoreTestSettings()
{
NoBuild = true,
NoRestore = true,
Configuration = configuration
};
if (isRunningOnBuildServer)
{
testSettings.Filter = "TestCategory!=ExcludeFromBuildServer";
testSettings.Logger = "AzurePipelines";
testSettings.TestAdapterPath = GetDirectories($"./tools/AzurePipelines.TestLogger.*/contentFiles/any/any").First();
}
Information($"Running tests in {project}");
DotNetCoreTest(MakeAbsolute(project).ToString(), testSettings);
})
.DeferOnError();
Task("Copy-Files")
.IsDependentOn("Publish-Client")
.Does(() =>
{
CopyDirectory(buildDir.Path.FullPath + "/netcoreapp2.1/publish", binDir);
CopyFiles(new FilePath[] { "LICENSE.txt", "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")
.IsDependentOn("Publish-Client")
.Does(() =>
{
// Get the set of projects to package
List<FilePath> projects = new List<FilePath>(GetFiles(packagingGlob));
// Package all nuspecs
foreach (var project in projects)
{
DotNetCorePack(
MakeAbsolute(project).ToString(),
new DotNetCorePackSettings
{
Configuration = configuration,
NoBuild = true,
NoRestore = true,
OutputDirectory = nugetRoot,
MSBuildSettings = msBuildSettings
});
}
});
Task("Create-Tools-Package")
.IsDependentOn("Publish-Client")
.WithCriteria(() => isRunningOnWindows)
.Does(() =>
{
DotNetCorePack(runnableProject, new DotNetCorePackSettings {
Configuration = configuration,
OutputDirectory = nugetRoot,
MSBuildSettings = msBuildSettings
});
});
Task("Publish-MyGet")
.IsDependentOn("Create-Packages")
.WithCriteria(() => !isLocal)
.WithCriteria(() => !isPullRequest)
.WithCriteria(() => isRunningOnWindows)
.WithCriteria(() => branch == "development")
.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 = mygeturi,
ApiKey = apiKey,
Timeout = TimeSpan.FromSeconds(600)
});
}
});
Task("Publish-Packages")
.IsDependentOn("Create-Packages")
.WithCriteria(() => isLocal)
.WithCriteria(() => isRunningOnWindows)
// TODO: Add criteria that makes sure this is the master branch
.Does(() =>
{
var apiKey = EnvironmentVariable($"{ applicationName.ToUpper() }_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://api.nuget.org/v3/index.json"
});
}
});
Task("Publish-Release")
.IsDependentOn("Zip-Files")
.WithCriteria(() => isLocal)
.WithCriteria(() => isRunningOnWindows)
// TODO: Add criteria that makes sure this is the master branch
.Does(() =>
{
var githubToken = EnvironmentVariable($"{ applicationName.ToUpper() }_GITHUB_TOKEN");
if (string.IsNullOrEmpty(githubToken))
{
throw new InvalidOperationException("Could not resolve SHAPEFLOW GitHub token.");
}
var github = new GitHubClient(new ProductHeaderValue($"{ applicationName.ToUpper() }CakeBuild"))
{
Credentials = new Credentials(githubToken)
};
var release = github.Repository.Release.Create($"{ applicationName.ToUpper() }io", $"{ applicationName.ToUpper() }", new NewRelease("v" + semVersion)
{
Name = semVersion,
Body = string.Join(Environment.NewLine, releaseNotes.Notes) + Environment.NewLine + Environment.NewLine
+ $@"### Please see https://{applicationName}.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;
}
});
//////////////////////////////////////////////////////////////////////
// TASK TARGETS
//////////////////////////////////////////////////////////////////////
Task("Create-Packages")
.IsDependentOn("Create-Library-Packages")
.IsDependentOn("Create-Tools-Package");
Task("Package")
.IsDependentOn("Run-Unit-Tests")
.IsDependentOn("Zip-Files")
.IsDependentOn("Create-Packages");
Task("Default")
.IsDependentOn("Package");
Task("Publish")
.IsDependentOn("Publish-Packages")
.IsDependentOn("Publish-Release");
Task("BuildServer")
.IsDependentOn("Run-Unit-Tests")
.IsDependentOn("Publish-MyGet");
//////////////////////////////////////////////////////////////////////
// EXECUTION
//////////////////////////////////////////////////////////////////////
RunTarget(target);