-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbuild.cake
373 lines (315 loc) · 12.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
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
// Install tools.
#tool dotnet:?package=GitVersion.Tool&version=5.12.0
///////////////////////////////////////////////////////////////////////////////
// ARGUMENTS
///////////////////////////////////////////////////////////////////////////////
var target = Argument<string>("target", "Default");
var configuration = Argument<string>("configuration", "Release");
var clearCache = Argument<bool>("clearcache", false);
///////////////////////////////////////////////////////////////////////////////
// GLOBAL VARIABLES
///////////////////////////////////////////////////////////////////////////////
var appName = "Cake.AddinDiscoverer";
var gitHubRepo = "Cake.AddinDiscoverer";
var gitHubToken = Argument<string>("GITHUB_TOKEN", EnvironmentVariable("GITHUB_TOKEN"));
var gitHubUserName = Argument<string>("GITHUB_USERNAME", EnvironmentVariable("GITHUB_USERNAME"));
var gitHubPassword = Argument<string>("GITHUB_PASSWORD", EnvironmentVariable("GITHUB_PASSWORD"));
var sourceFolder = "./Source/";
var outputDir = "./artifacts/";
var publishDir = $"{outputDir}Publish/";
var buildBranch = Context.GetBuildBranch();
var repoName = Context.GetRepoName();
var versionInfo = (GitVersion)null; // Will be calculated in SETUP
var cakeVersion = typeof(ICakeContext).Assembly.GetName().Version.ToString();
var isLocalBuild = BuildSystem.IsLocalBuild;
var isMainBranch = StringComparer.OrdinalIgnoreCase.Equals("main", buildBranch);
var isMainRepo = StringComparer.OrdinalIgnoreCase.Equals($"{gitHubUserName}/{gitHubRepo}", repoName);
var isPullRequest = BuildSystem.AppVeyor.Environment.PullRequest.IsPullRequest;
var isTagged = BuildSystem.AppVeyor.Environment.Repository.Tag.IsTag && !string.IsNullOrWhiteSpace(BuildSystem.AppVeyor.Environment.Repository.Tag.Name);
///////////////////////////////////////////////////////////////////////////////
// SETUP / TEARDOWN
///////////////////////////////////////////////////////////////////////////////
Setup(context =>
{
if (!isLocalBuild && context.Log.Verbosity != Verbosity.Diagnostic)
{
Information("Increasing verbosity to diagnostic.");
context.Log.Verbosity = Verbosity.Diagnostic;
}
Information("Calculating version info...");
versionInfo = GitVersion(new GitVersionSettings() { OutputType = GitVersionOutput.Json });
Information("Building version {0} of {1} ({2}, {3}) using version {4} of Cake",
versionInfo.LegacySemVerPadded,
appName,
configuration,
target,
cakeVersion
);
Information("Variables:\r\n\tLocalBuild: {0}\r\n\tIsMainBranch: {1}\r\n\tIsMainRepo: {2}\r\n\tIsPullRequest: {3}\r\n\tIsTagged: {4}",
isLocalBuild,
isMainBranch,
isMainRepo,
isPullRequest,
isTagged
);
if (!string.IsNullOrEmpty(gitHubToken))
{
Information("GitHub Info:\r\n\tRepo: {0}\r\n\tUserName: {1}\r\n\tToken: {2}",
gitHubRepo,
gitHubUserName,
new string('*', gitHubToken.Length)
);
}
else
{
Information("GitHub Info:\r\n\tRepo: {0}\r\n\tUserName: {1}\r\n\tPassword: {2}",
gitHubRepo,
gitHubUserName,
string.IsNullOrEmpty(gitHubPassword) ? "[NULL]" : new string('*', gitHubPassword.Length)
);
}
});
Teardown(context =>
{
// Executed AFTER the last task.
Information("Finished running tasks.");
});
///////////////////////////////////////////////////////////////////////////////
// TASK DEFINITIONS
///////////////////////////////////////////////////////////////////////////////
Task("AppVeyor-Build_Number")
.WithCriteria(() => AppVeyor.IsRunningOnAppVeyor)
.Does(() =>
{
GitVersion(new GitVersionSettings()
{
UpdateAssemblyInfo = false,
OutputType = GitVersionOutput.BuildServer
});
});
Task("Clean")
.IsDependentOn("AppVeyor-Build_Number")
.Does(() =>
{
// Clean solution directories.
Information("Cleaning {0}", sourceFolder);
CleanDirectories($"{sourceFolder}*/bin/{configuration}");
CleanDirectories($"{sourceFolder}*/obj/{configuration}");
// Clean previous artifacts
Information("Cleaning {0}", outputDir);
if (DirectoryExists(outputDir))
{
SafeCleanDirectory(outputDir, false);
}
else
{
CreateDirectory(outputDir);
}
});
Task("Restore-NuGet-Packages")
.IsDependentOn("Clean")
.Does(() =>
{
DotNetRestore(sourceFolder, new DotNetRestoreSettings
{
Runtime = "win-x64",
Sources = new [] { "https://api.nuget.org/v3/index.json", }
});
});
Task("Build")
.IsDependentOn("Restore-NuGet-Packages")
.Does(() =>
{
DotNetBuild($"{sourceFolder}{appName}.sln", new DotNetBuildSettings
{
Configuration = configuration,
NoRestore = true,
ArgumentCustomization = args => args.Append($"/p:SemVer={versionInfo.LegacySemVerPadded}")
});
});
Task("Publish")
.IsDependentOn("Build")
.Does(() =>
{
DotNetPublish($"{sourceFolder}{appName}.sln", new DotNetPublishSettings
{
Configuration = configuration,
NoBuild = true,
NoRestore = true,
PublishSingleFile = false, // It's important NOT to publish to single file otherwise some assemblies such as Cake.Core and Cake.common would not be available to the MetadataLoadContext in the Analyze step
ArgumentCustomization = args => args.Append($"/p:PublishDir={MakeAbsolute(Directory(publishDir)).FullPath}") // Avoid warning NETSDK1194: The "--output" option isn't supported when building a solution.
});
});
Task("Run")
.IsDependentOn("Publish")
.WithCriteria(() => isLocalBuild || !isPullRequest)
.Does(() =>
{
var args = new ProcessArgumentBuilder()
.AppendSwitchQuoted("-t", outputDir) // "Folder where temporary files (including reports) are saved."
.AppendSwitchQuoted("-u", gitHubUserName) // "Github username."
.AppendSwitchQuotedSecret("-p", gitHubPassword); // "Github password."
if (clearCache) args.Append("-c");
if (isMainBranch)
{
args.Append("-e"); // "Generate the Excel report and write to a file."
args.Append("-k"); // "Update addin references in CakeRecipe."
args.Append("-m"); // "Generate the Markdown report and write to a file."
args.Append("-n"); // "Synchronize the list of contributors."
args.Append("-r"); // "Commit reports and other files to cake-contrib repo."
args.Append("-s"); // "Synchronize the yaml files on Cake's web site with the packages discovered on NuGet."
}
else
{
args.Append("-e"); // "Generate the Excel report and write to a file."
args.Append("-m"); // "Generate the Markdown report and write to a file."
}
IEnumerable<string> redirectedStandardOutput = new List<string>();
IEnumerable<string> redirectedError = new List<string>();
// Execute the command
try
{
using (DiagnosticVerbosity())
{
var processResult = StartProcess(
new FilePath($"{publishDir}{appName}.exe"),
new ProcessSettings()
{
Arguments = args,
RedirectStandardOutput = true,
RedirectStandardError= true
},
out redirectedStandardOutput,
out redirectedError
);
if (processResult != 0)
{
throw new Exception($"{appName} did not complete successfully. Result code: {processResult}");
}
}
}
catch (Exception e)
{
Information("AN ERROR OCCURED: {0}", e.Message);
Information("Standard output: {0}", string.Join("\r\n", redirectedStandardOutput));
Information("Standard error: {0}", string.Join("\r\n", redirectedError));
throw;
}
});
Task("Upload-Artifacts")
.IsDependentOn("Run")
.WithCriteria(() => AppVeyor.IsRunningOnAppVeyor)
.Does(() =>
{
var artifacts = new string[]
{
"Audit.xlsx",
"Audit.md",
"Analysis_result.json"
};
foreach (var artifact in artifacts)
{
var path = $"{outputDir}{appName}/{artifact}";
if (FileExists(path))
{
AppVeyor.UploadArtifact(path);
}
}
});
///////////////////////////////////////////////////////////////////////////////
// TARGETS
///////////////////////////////////////////////////////////////////////////////
Task("AppVeyor")
.IsDependentOn("Upload-Artifacts");
Task("Default")
.IsDependentOn("Run");
///////////////////////////////////////////////////////////////////////////////
// EXECUTION
///////////////////////////////////////////////////////////////////////////////
RunTarget(target);
///////////////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
///////////////////////////////////////////////////////////////////////////////
private static string TrimStart(this string source, string value, StringComparison comparisonType)
{
if (source == null)
{
throw new ArgumentNullException(nameof(source));
}
int valueLength = value.Length;
int startIndex = 0;
while (source.IndexOf(value, startIndex, comparisonType) == startIndex)
{
startIndex += valueLength;
}
return source.Substring(startIndex);
}
private static List<string> ExecuteCommand(this ICakeContext context, FilePath exe, string args)
{
context.StartProcess(exe, new ProcessSettings { Arguments = args, RedirectStandardOutput = true }, out var redirectedOutput);
return redirectedOutput.ToList();
}
private static List<string> ExecGitCmd(this ICakeContext context, string cmd)
{
var gitExe = context.Tools.Resolve(context.IsRunningOnWindows() ? "git.exe" : "git");
return context.ExecuteCommand(gitExe, cmd);
}
private static string GetBuildBranch(this ICakeContext context)
{
var buildSystem = context.BuildSystem();
string repositoryBranch = null;
if (buildSystem.IsRunningOnAppVeyor) repositoryBranch = buildSystem.AppVeyor.Environment.Repository.Branch;
else if (buildSystem.IsRunningOnAzurePipelines) repositoryBranch = buildSystem.AzurePipelines.Environment.Repository.SourceBranchName;
else if (buildSystem.IsRunningOnBamboo) repositoryBranch = buildSystem.Bamboo.Environment.Repository.Branch;
else if (buildSystem.IsRunningOnBitbucketPipelines) repositoryBranch = buildSystem.BitbucketPipelines.Environment.Repository.Branch;
else if (buildSystem.IsRunningOnBitrise) repositoryBranch = buildSystem.Bitrise.Environment.Repository.GitBranch;
else if (buildSystem.IsRunningOnGitHubActions) repositoryBranch = buildSystem.GitHubActions.Environment.Workflow.Ref.Replace("refs/heads/", "");
else if (buildSystem.IsRunningOnGitLabCI) repositoryBranch = buildSystem.GitLabCI.Environment.Build.RefName;
else if (buildSystem.IsRunningOnTeamCity) repositoryBranch = buildSystem.TeamCity.Environment.Build.BranchName;
else if (buildSystem.IsRunningOnTravisCI) repositoryBranch = buildSystem.TravisCI.Environment.Build.Branch;
else repositoryBranch = ExecGitCmd(context, "rev-parse --abbrev-ref HEAD").Single();
return repositoryBranch;
}
public static string GetRepoName(this ICakeContext context)
{
var buildSystem = context.BuildSystem();
if (buildSystem.IsRunningOnAppVeyor) return buildSystem.AppVeyor.Environment.Repository.Name;
else if (buildSystem.IsRunningOnAzurePipelines) return buildSystem.AzurePipelines.Environment.Repository.RepoName;
else if (buildSystem.IsRunningOnTravisCI) return buildSystem.TravisCI.Environment.Repository.Slug;
else if (buildSystem.IsRunningOnGitHubActions) return buildSystem.GitHubActions.Environment.Workflow.Repository;
var originUrl = ExecGitCmd(context, "config --get remote.origin.url").Single();
var parts = originUrl.Split('/', StringSplitOptions.RemoveEmptyEntries);
return $"{parts[parts.Length - 2]}/{parts[parts.Length - 1].Replace(".git", "")}";
}
// Clean previous artifacts and make sure to preserve the content
// of folders that are used as caches (such as "packages", "analysis"
// and "archives"). Do not use Cake's "CleanDirectories" alias because
// there is no way to exclude a sub folder which prevents us from
// exluding the subfolders we want to preserve.
private void SafeCleanDirectory(string directoryPath, bool deleteFiles)
{
foreach (var directory in System.IO.Directory.EnumerateDirectories(directoryPath))
{
// Check if this folder is used for caching purposes
if (directory.EndsWith("packages") || directory.EndsWith("analysis") || directory.EndsWith("archives"))
{
Information(" -> Skipping {0}", directory);
}
else
{
Information(" -> Cleaning {0}", directory);
// Delete files in this folder
if (deleteFiles)
{
DeleteFiles($"{directory}/*.*");
}
// Clean the subfolders
SafeCleanDirectory(directory, true);
// Delete this folder if it's empty
if (!System.IO.Directory.EnumerateFileSystemEntries(directory).Any())
{
System.IO.Directory.Delete(directory, false);
}
}
}
}