forked from phmonte/Buildalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.cake
305 lines (266 loc) · 10.1 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
// The following environment variables need to be set for Publish target:
// NUGET_KEY
// MYGET_KEY
// GITHUB_TOKEN
// NETLIFY_TOKEN
#tool nuget:?package=Wyam&version=1.5.1
#addin nuget:?package=Cake.Wyam&version=1.5.1
#addin "Octokit"
#addin "NetlifySharp"
#tool "AzurePipelines.TestLogger&version=1.0.0"
#tool "nuget:?package=NuGet.CommandLine&version=4.9.2"
using Octokit;
using NetlifySharp;
//////////////////////////////////////////////////////////////////////
// CONST
//////////////////////////////////////////////////////////////////////
var projectName = "Buildalyzer";
var repositoryName = "Buildalyzer";
var myGetFeed = "buildalyzer";
var siteName = "buildalyzer";
//////////////////////////////////////////////////////////////////////
// ARGUMENTS
//////////////////////////////////////////////////////////////////////
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 = TFBuild.Environment.Build.Number.Replace('.', '-');
var branch = TFBuild.Environment.Repository.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("./build");
var docsDir = Directory("./docs");
///////////////////////////////////////////////////////////////////////////////
// SETUP / TEARDOWN
///////////////////////////////////////////////////////////////////////////////
Setup(context =>
{
Information($"Building version {semVersion} of {projectName}.");
});
//////////////////////////////////////////////////////////////////////
// TASKS
//////////////////////////////////////////////////////////////////////
Task("Clean")
.Description("Cleans the build directories.")
.Does(() =>
{
CleanDirectories(GetDirectories($"./src/*/bin/{ configuration }"));
CleanDirectories(GetDirectories($"./tests/*Tests/*/bin/{ configuration }"));
CleanDirectories(buildDir);
});
Task("Restore")
.Description("Restores all NuGet packages.")
.IsDependentOn("Clean")
.Does(() =>
{
DotNetCoreRestore($"./{projectName}.sln", new DotNetCoreRestoreSettings
{
MSBuildSettings = msBuildSettings
});
});
Task("Build")
.Description("Builds the solution.")
.IsDependentOn("Restore")
.Does(() =>
{
DotNetCoreBuild($"./{projectName}.sln", new DotNetCoreBuildSettings
{
Configuration = configuration,
NoRestore = true,
MSBuildSettings = msBuildSettings
});
});
Task("Test")
.Description("Runs all tests.")
.IsDependentOn("Build")
.DoesForEach(GetFiles("./tests/*Tests/*.csproj"), 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("Pack")
.Description("Packs the NuGet packages.")
.IsDependentOn("Build")
.Does(() =>
{
DotNetCorePackSettings packSettings = new DotNetCorePackSettings
{
Configuration = configuration,
OutputDirectory = buildDir,
MSBuildSettings = msBuildSettings
};
foreach (var project in GetFiles("./src/*/*.csproj"))
{
DotNetCorePack(MakeAbsolute(project).ToString(), packSettings);
}
});
Task("Zip")
.Description("Zips the build output.")
.IsDependentOn("Build")
.Does(() =>
{
foreach(var projectDir in GetDirectories("./src/*"))
{
CopyFiles(new FilePath[] { "LICENSE", "README.md", "ReleaseNotes.md" }, $"{ projectDir.FullPath }/bin/{ configuration }");
var files = GetFiles($"{ projectDir.FullPath }/bin/{ configuration }/**/*");
files.Remove(files.Where(x => x.GetExtension() == ".nupkg").ToList());
var zipFile = File($"{ projectDir.GetDirectoryName() }-v{ semVersion }.zip");
Zip(
$"{ projectDir.FullPath }/bin/{ configuration }",
$"{ buildDir }/{ zipFile }",
files);
}
});
Task("MyGet")
.Description("Pushes the packages to the MyGet feed.")
.IsDependentOn("Pack")
.WithCriteria(() => !isLocal)
.WithCriteria(() => !isPullRequest)
.WithCriteria(() => isRunningOnWindows)
.Does(() =>
{
// Resolve the API key.
var mygetKey = EnvironmentVariable("MYGET_KEY");
if (string.IsNullOrEmpty(mygetKey))
{
throw new InvalidOperationException("Could not resolve MyGet API key.");
}
foreach (var nupkg in GetFiles($"{ buildDir }/*.nupkg"))
{
NuGetPush(nupkg, new NuGetPushSettings
{
ApiKey = mygetKey,
Source = $"https://www.myget.org/F/{myGetFeed}/api/v2/package"
});
}
});
Task("NuGet")
.Description("Pushes the packages to the NuGet gallery.")
.IsDependentOn("Pack")
.WithCriteria(() => isLocal)
.Does(() =>
{
var nugetKey = EnvironmentVariable("NUGET_KEY");
if (string.IsNullOrEmpty(nugetKey))
{
throw new InvalidOperationException("Could not resolve NuGet API key.");
}
foreach (var nupkg in GetFiles($"{ buildDir }/*.nupkg"))
{
NuGetPush(nupkg, new NuGetPushSettings
{
ApiKey = nugetKey,
Source = "https://api.nuget.org/v3/index.json"
});
}
});
Task("GitHub")
.Description("Generates a release on GitHub.")
.IsDependentOn("Pack")
.IsDependentOn("Zip")
.WithCriteria(() => isLocal)
.Does(() =>
{
var githubToken = EnvironmentVariable("GITHUB_TOKEN");
if (string.IsNullOrEmpty(githubToken))
{
throw new InvalidOperationException("Could not resolve GitHub token.");
}
var github = new GitHubClient(new ProductHeaderValue("CakeBuild"))
{
Credentials = new Credentials(githubToken)
};
var release = github.Repository.Release.Create("daveaglick", repositoryName, new NewRelease("v" + semVersion)
{
Name = semVersion,
Body = string.Join(Environment.NewLine, releaseNotes.Notes),
TargetCommitish = "master"
}).Result;
foreach(var zipFile in GetFiles($"{ buildDir }/*.zip"))
{
using (var zipStream = System.IO.File.OpenRead(zipFile.FullPath))
{
var releaseAsset = github.Repository.Release.UploadAsset(
release,
new ReleaseAssetUpload(zipFile.GetFilename().FullPath, "application/zip", zipStream, null)).Result;
}
}
});
Task("Docs")
.Description("Generates and previews the docs.")
.Does(() =>
{
Wyam(new WyamSettings
{
RootPath = docsDir,
Recipe = "Docs",
Theme = "Samson",
Preview = true
});
});
Task("Netlify")
.Description("Generates and deploys the docs.")
.Does(() =>
{
var netlifyToken = EnvironmentVariable("NETLIFY_TOKEN");
if(string.IsNullOrEmpty(netlifyToken))
{
throw new Exception("Could not get Netlify token environment variable");
}
Wyam(new WyamSettings
{
RootPath = docsDir,
Recipe = "Docs",
Theme = "Samson",
UpdatePackages = true
});
Information("Deploying output to Netlify");
var client = new NetlifyClient(netlifyToken);
client.UpdateSite($"{siteName}.netlify.com", MakeAbsolute(docsDir).FullPath + "/output").SendAsync().Wait();
});
//////////////////////////////////////////////////////////////////////
// TASK TARGETS
//////////////////////////////////////////////////////////////////////
Task("Default")
.IsDependentOn("Test");
Task("Release")
.Description("Generates a GitHub release, pushes the NuGet package, and deploys the docs site.")
.IsDependentOn("GitHub")
.IsDependentOn("NuGet")
.IsDependentOn("Netlify");
Task("BuildServer")
.Description("Runs a build from the build server and updates build server data.")
.IsDependentOn("Test")
.IsDependentOn("Pack")
.IsDependentOn("Zip")
.IsDependentOn("MyGet")
.WithCriteria(() => isRunningOnBuildServer);
//////////////////////////////////////////////////////////////////////
// EXECUTION
//////////////////////////////////////////////////////////////////////
RunTarget(target);