-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgeneric-tasks.cake
297 lines (233 loc) · 9.57 KB
/
generic-tasks.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
#l "generic-variables.cake"
//#addin "nuget:?package=Cake.DependencyCheck&version=1.2.0"
//#tool "nuget:?package=DependencyCheck.Runner.Tool&version=3.2.1&include=./**/dependency-check.sh&include=./**/dependency-check.bat"
//-------------------------------------------------------------
private void ValidateRequiredInput(string parameterName)
{
// TODO: Do we want to check the configuration as well?
if (!Parameters.ContainsKey(parameterName))
{
throw new Exception(string.Format("Parameter '{0}' is required but not defined", parameterName));
}
}
//-------------------------------------------------------------
private void CleanUpCode(bool failOnChanges)
{
Information("Cleaning up code using dotnet-format");
// --check: return non-0 exit code if changes are needed
// --dry-run: don't save files
// Note: disabled for now, see:
// * https://github.com/onovotny/MSBuildSdkExtras/issues/164
// * https://github.com/microsoft/msbuild/issues/4376
// var arguments = new List<string>();
// //arguments.Add("--dry-run");
// if (failOnChanges)
// {
// arguments.Add("--check");
// }
// DotNetTool(null, "format", string.Join(" ", arguments),
// new DotNetToolSettings
// {
// WorkingDirectory = "./src/"
// });
}
//-------------------------------------------------------------
private void VerifyDependencies(string pathToScan = "./src/**/*.csproj")
{
Information("Verifying dependencies for security vulnerabilities in '{0}'", pathToScan);
// Disabled for now
//DependencyCheck(new DependencyCheckSettings
//{
// Project = SolutionName,
// Scan = pathToScan,
// FailOnCVSS = "0",
// Format = "HTML",
// Data = "%temp%/dependency-check/data"
//});
}
//-------------------------------------------------------------
private void UpdateSolutionAssemblyInfo(BuildContext buildContext)
{
Information("Updating assembly info to '{0}'", buildContext.General.Version.FullSemVer);
var assemblyInfoParseResult = ParseAssemblyInfo(buildContext.General.Solution.AssemblyInfoFileName);
var assemblyInfo = new AssemblyInfoSettings
{
Company = buildContext.General.Copyright.Company,
Version = buildContext.General.Version.MajorMinorPatch,
FileVersion = buildContext.General.Version.MajorMinorPatch,
InformationalVersion = buildContext.General.Version.FullSemVer,
Copyright = string.Format("Copyright © {0} {1} - {2}",
buildContext.General.Copyright.Company, buildContext.General.Copyright.StartYear, DateTime.Now.Year)
};
CreateAssemblyInfo(buildContext.General.Solution.AssemblyInfoFileName, assemblyInfo);
}
//-------------------------------------------------------------
Task("UpdateNuGet")
.ContinueOnError()
.Does<BuildContext>(buildContext =>
{
// DISABLED UNTIL NUGET GETS FIXED: https://github.com/NuGet/Home/issues/10853
// Information("Making sure NuGet is using the latest version");
// if (buildContext.General.IsLocalBuild && buildContext.General.MaximizePerformance)
// {
// Information("Local build with maximized performance detected, skipping NuGet update check");
// return;
// }
// var nuGetExecutable = buildContext.General.NuGet.Executable;
// var exitCode = StartProcess(nuGetExecutable, new ProcessSettings
// {
// Arguments = "update -self"
// });
// var newNuGetVersionInfo = System.Diagnostics.FileVersionInfo.GetVersionInfo(nuGetExecutable);
// var newNuGetVersion = newNuGetVersionInfo.FileVersion;
// Information("Updating NuGet.exe exited with '{0}', version is '{1}'", exitCode, newNuGetVersion);
});
//-------------------------------------------------------------
Task("RestorePackages")
.IsDependentOn("Prepare")
.IsDependentOn("UpdateNuGet")
.ContinueOnError()
.Does<BuildContext>(buildContext =>
{
if (buildContext.General.IsLocalBuild && buildContext.General.MaximizePerformance)
{
Information("Local build with maximized performance detected, skipping package restore");
return;
}
//var csharpProjects = GetFiles("./**/*.csproj");
// var cProjects = GetFiles("./**/*.vcxproj");
var solutions = GetFiles("./**/*.sln");
var csharpProjects = new List<FilePath>();
foreach (var project in buildContext.AllProjects)
{
// Once a project is in AllProjects, it should always be restored
var projectFileName = GetProjectFileName(buildContext, project);
if (projectFileName.EndsWith(".csproj"))
{
Information("Adding '{0}' as C# specific project to restore", project);
csharpProjects.Add(projectFileName);
// Inject source link *before* package restore
InjectSourceLinkInProjectFile(buildContext, project, projectFileName);
}
}
var allFiles = new List<FilePath>();
//allFiles.AddRange(solutions);
allFiles.AddRange(csharpProjects);
// //allFiles.AddRange(cProjects);
Information($"Found '{allFiles.Count}' projects to restore");
foreach (var file in allFiles)
{
RestoreNuGetPackages(buildContext, file);
}
// C++ files need to be done manually
foreach (var project in buildContext.AllProjects)
{
var projectFileName = GetProjectFileName(buildContext, project);
if (IsCppProject(projectFileName))
{
buildContext.CakeContext.LogSeparator("'{0}' is a C++ project, restoring NuGet packages separately", project);
RestoreNuGetPackages(buildContext, projectFileName);
// For C++ projects, we must clean the project again after a package restore
CleanProject(buildContext, project);
}
}
});
//-------------------------------------------------------------
// Note: it might look weird that this is dependent on restore packages,
// but to clean, the msbuild projects must be able to load. However, they need
// some targets files that come in via packages
Task("Clean")
//.IsDependentOn("RestorePackages")
.IsDependentOn("Prepare")
.ContinueOnError()
.Does<BuildContext>(buildContext =>
{
if (buildContext.General.IsLocalBuild && buildContext.General.MaximizePerformance)
{
Information("Local build with maximized performance detected, skipping solution clean");
return;
}
var platforms = new Dictionary<string, PlatformTarget>();
platforms["AnyCPU"] = PlatformTarget.MSIL;
platforms["x86"] = PlatformTarget.x86;
platforms["x64"] = PlatformTarget.x64;
platforms["arm"] = PlatformTarget.ARM;
foreach (var platform in platforms)
{
try
{
Information("Cleaning output for platform '{0}'", platform.Value);
var msBuildSettings = new MSBuildSettings
{
Verbosity = Verbosity.Minimal,
ToolVersion = MSBuildToolVersion.Default,
Configuration = buildContext.General.Solution.ConfigurationName,
MSBuildPlatform = MSBuildPlatform.x86, // Always require x86, see platform for actual target platform
PlatformTarget = platform.Value
};
ConfigureMsBuild(buildContext, msBuildSettings, platform.Key, "clean");
msBuildSettings.Targets.Add("Clean");
MSBuild(buildContext.General.Solution.FileName, msBuildSettings);
}
catch (System.Exception ex)
{
Warning("Failed to clean output for platform '{0}': {1}", platform.Value, ex.Message);
}
}
// Output directory
DeleteDirectoryWithLogging(buildContext, buildContext.General.OutputRootDirectory);
// obj directories
foreach (var project in buildContext.AllProjects)
{
CleanProject(buildContext, project);
}
});
//-------------------------------------------------------------
Task("VerifyDependencies")
.IsDependentOn("Prepare")
.Does(async () =>
{
// if (DependencyCheckDisabled)
// {
// Information("Dependency analysis is disabled");
// return;
// }
// VerifyDependencies();
});
//-------------------------------------------------------------
Task("CleanupCode")
.Does<BuildContext>(buildContext =>
{
CleanUpCode(true);
});
//-------------------------------------------------------------
Task("CodeSign")
//.ContinueOnError()
.Does<BuildContext>(buildContext =>
{
if (buildContext.General.IsCiBuild)
{
Information("Skipping code signing because this is a CI build");
return;
}
if (buildContext.General.IsLocalBuild)
{
Information("Local build detected, skipping code signing");
return;
}
if (!buildContext.General.CodeSign.IsAvailable &&
!buildContext.General.AzureCodeSign.IsAvailable)
{
Information("Skipping code signing since no option is available");
return;
}
var filesToSign = new List<FilePath>();
// Note: only code-sign components & wpf apps, skip test projects & uwp apps
var projectsToCodeSign = new List<string>();
projectsToCodeSign.AddRange(buildContext.Components.Items);
projectsToCodeSign.AddRange(buildContext.Wpf.Items);
foreach (var projectToCodeSign in projectsToCodeSign)
{
SignProjectFiles(buildContext, projectToCodeSign);
}
});