Repository navigation
Expand file tree
/
Copy pathBuildContext.cs
More file actions
519 lines (425 loc) · 19.6 KB
/
Copy pathBuildContext.cs
File metadata and controls
519 lines (425 loc) · 19.6 KB
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
// Copyright (c) 2023 MTS spol. s r.o, and Contributors. All Rights Reserved.
// Contributors: https://github.com/inxton/AXOpen/graphs/contributors
// See the LICENSE file in the repository root for more information.
// https://github.com/inxton/AXOpen/blob/master/LICENSE
// Third party licenses: https://github.com/inxton/AXOpen/blob/master/notices.md
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using Cake.Common.Build;
using Cake.Common;
using Cake.Common.Tools.DotNet;
using Cake.Common.Tools.DotNet.Build;
using Cake.Common.Tools.DotNet.MSBuild;
using Cake.Common.Tools.DotNet.Run;
using Cake.Common.Tools.DotNet.Test;
using Cake.Core;
using Cake.Frosting;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Octokit;
using Polly;
using static NuGet.Packaging.PackagingConstants;
using Path = System.IO.Path;
using Cake.Core.IO;
using System;
using YamlDotNet.RepresentationModel;
using Cake.Core.Diagnostics;
public partial class BuildContext : FrostingContext
{
public bool IsGitHubActions { get; set; }
public string ApaxRegistry => "inxton";
public void UpdateApaxVersion(string file, string version)
{
var sb = new StringBuilder();
foreach (var line in System.IO.File.ReadLines(file))
{
var newLine = line;
if (line.Trim().StartsWith("name"))
{
// Do not change the version of the catalog in the declaration field
if (line.Contains(".catalog"))
{
return;
}
}
if (line.Trim().StartsWith("version"))
{
var semicPosition = line.IndexOf(":");
var lenght = line.Length - semicPosition;
newLine = $"{line.Substring(0, semicPosition)} : '{version}'";
}
sb.AppendLine(newLine);
}
System.IO.File.WriteAllText(file, sb.ToString());
}
public void UpdateApaxDependencies(string file, string version)
{
var sb = new StringBuilder();
foreach (var line in System.IO.File.ReadLines(file))
{
var newLine = line;
// Do not change the version of the catalog when used
if (line.Trim().StartsWith($"\"@{ApaxRegistry}/") && line.Contains(":") && !line.Contains(".catalog"))
{
var semicPosition = line.IndexOf(":");
var lenght = line.Length - semicPosition;
newLine = $"{line.Substring(0, semicPosition)} : '{version}'";
}
sb.AppendLine(newLine);
}
System.IO.File.WriteAllText(file, sb.ToString());
}
public string Artifacts => Path.Combine(Environment.WorkingDirectory.FullPath, "..//artifacts//");
public string BuildsOutput => Path.Combine(RootDir, ".builds");
public string ArtifactsApax => EnsureFolder(Path.Combine(Artifacts, "apax"));
public string ArtifactsNugets => EnsureFolder(Path.Combine(Artifacts, "nugets"));
public string PackableNugetsSlnf => Path.Combine(RootDir, "AXOpen-packable-only.proj");
public string WorkDirName => Environment.WorkingDirectory.GetDirectoryName();
public string ApiDocumentationDir => Path.GetFullPath(Path.Combine(Environment.WorkingDirectory.FullPath, "..//docs//api//"));
public string RootDir => Path.GetFullPath(Path.Combine(Environment.WorkingDirectory.FullPath, "..//src//"));
public Cake.Common.Tools.DotNet.Build.DotNetBuildSettings DotNetBuildSettings { get; }
public Cake.Common.Tools.DotNet.Test.DotNetTestSettings DotNetTestSettings { get; }
public DotNetRunSettings DotNetRunSettings { get; }
public BuildParameters BuildParameters { get; }
public IEnumerable<string> TargetFrameworks { get; } = new List<string>() { "net9.0" };
public string TestResults => Path.Combine(Environment.WorkingDirectory.FullPath, "..//TestResults//");
public string TestResultsCtrl => Path.Combine(Environment.WorkingDirectory.FullPath, "..//TestResultsCtrl//");
public string AppTestResultsDir => Path.GetFullPath(Path.Combine(Environment.WorkingDirectory.FullPath, "..//..//..//..//..//AppRunTest//app_test_results//"));
public string SourceDirPlcSim => Path.GetFullPath(Path.Combine(Environment.WorkingDirectory.FullPath, "..//..//..//..//..//AppRunTest//source//plcsim//"));
public string PlcSimVirtualMemoryCardLocation => Path.GetFullPath(Path.Combine(Environment.WorkingDirectory.FullPath, "..//..//..//..//..//AppRunTest//plcsim//"));
public string PlcName => "plc_line";
public string PlcIpAddress => "10.10.10.120";
public string SourceDirSecurityFiles => Path.GetFullPath(Path.Combine(Environment.WorkingDirectory.FullPath, "..//..//..//..//..//AppRunTest//source//"));
public BuildContext(ICakeContext context, BuildParameters buildParameters)
: base(context)
{
BuildParameters = buildParameters;
DotNetBuildSettings = new DotNetBuildSettings()
{
Verbosity = buildParameters.Verbosity,
Configuration = buildParameters.Configuration,
NoRestore = false,
MSBuildSettings = new DotNetMSBuildSettings()
{
Verbosity = buildParameters.Verbosity
}
};
DotNetTestSettings = new DotNetTestSettings()
{
ResultsDirectory = Path.Combine(TestResults),
Verbosity = buildParameters.Verbosity,
Configuration = buildParameters.Configuration,
NoRestore = true,
NoBuild = true
};
DotNetRunSettings = new DotNetRunSettings()
{
Verbosity = buildParameters.Verbosity,
Framework = "net9.0",
Configuration = buildParameters.Configuration,
NoBuild = true,
NoRestore = true,
};
IsGitHubActions = context.EnvironmentVariable("GITHUB_ACTIONS") == "true";
}
#region Libraries
// ─────────────────────────────────────────────────────────────────────────────
// Library discovery
//
// The libraries the per-library build tasks iterate (clean, catalog-install,
// provision, apax build/test, apax pack) are discovered from the file system so a
// newly scaffolded library under 'src/<folder>' (see
// scripts/create_library_from_template.ps1) is picked up automatically - WITHOUT
// editing this file.
//
// The effective set is: CuratedBaseLibraries ∪ auto-discovered component libraries,
// de-duplicated by folder with the curated entry winning.
// ─────────────────────────────────────────────────────────────────────────────
// Non-standard entries that must NOT be auto-inferred: they either do not follow the
// 'src/<folder>/ctrl/apax.yml' component pattern, have a folder name that differs from
// their apax name (sdk-ax -> ax-sdk), or carry deliberately non-default pack/test flags
// (template.axolibrary is never packed or tested). These are always excluded from
// discovery and emitted verbatim.
private static readonly (string folder, string name, bool pack, bool app_run, bool test)[] CuratedBaseLibraries =
{
("ax.axopen.min", "ax.axopen.min", true, false, false),
("ax.axopen.hwlibrary", "ax.axopen.hwlibrary", true, false, false),
("ax.axopen.app", "ax.axopen.app", true, false, false),
("sdk-ax", "ax-sdk", true, false, false),
("template.axolibrary", "template.axolibrary", false, false, false),
};
// Folders under 'src' that must never be treated as buildable libraries.
// ► Add a folder name here to exclude it from the whole per-library build pipeline. ◄
// (The CuratedBaseLibraries folders above are excluded from discovery automatically.)
private static readonly HashSet<string> DiscoveryExcludes = new(StringComparer.OrdinalIgnoreCase)
{
"components.citemplate", // throwaway library scaffolded + deleted by TemplateTestTask
};
// Per-folder flag overrides for discovered libraries. Discovery defaults every library
// to pack=true, test=true; list a folder here only to deviate from that default
// (null = keep the default). 'io' and 'probers' build & pack but are intentionally
// excluded from the apax test run.
private static readonly Dictionary<string, (bool? pack, bool? test)> DiscoveryOverrides =
new(StringComparer.OrdinalIgnoreCase)
{
{ "io", (pack: null, test: false) },
{ "probers", (pack: null, test: false) },
};
private IReadOnlyList<(string folder, string name, bool pack, bool app_run, bool test)> _libraries;
public IEnumerable<(string folder, string name, bool pack, bool app_run, bool test)> Libraries =>
_libraries ??= BuildLibrarySet();
private IReadOnlyList<(string folder, string name, bool pack, bool app_run, bool test)> BuildLibrarySet()
{
// Curated entries are concatenated first so they win on any folder collision.
return CuratedBaseLibraries
.Concat(DiscoverComponentLibraries())
.GroupBy(l => l.folder, StringComparer.OrdinalIgnoreCase)
.Select(g => g.First())
.ToList();
}
private IEnumerable<(string folder, string name, bool pack, bool app_run, bool test)> DiscoverComponentLibraries()
{
if (!Directory.Exists(RootDir))
{
yield break;
}
// Curated-base folders are always excluded from discovery (added verbatim instead).
var excluded = new HashSet<string>(DiscoveryExcludes, StringComparer.OrdinalIgnoreCase);
foreach (var curated in CuratedBaseLibraries)
{
excluded.Add(curated.folder);
}
// Ordinal sort -> deterministic, machine-independent ordering of discovered libraries.
foreach (var dir in Directory.GetDirectories(RootDir).OrderBy(Path.GetFileName, StringComparer.Ordinal))
{
var folder = Path.GetFileName(dir);
if (excluded.Contains(folder))
{
continue;
}
// A controller library is identified by 'ctrl/apax.yml'. This naturally skips
// non-library folders such as 'traversals', 'styling', 'showcase', 'docs'.
var ctrlApax = Path.Combine(dir, "ctrl", "apax.yml");
if (!File.Exists(ctrlApax))
{
continue;
}
string name;
try
{
name = ApaxFile.CreateApaxDto(ctrlApax).Name;
}
catch (Exception ex)
{
Log.Warning($"Skipping '{folder}': could not read apax name from '{ctrlApax}': {ex.Message}");
continue;
}
if (string.IsNullOrWhiteSpace(name))
{
Log.Warning($"Skipping '{folder}': empty apax name in '{ctrlApax}'.");
continue;
}
// Strip the registry prefix: '@inxton/axopen.components.drives' -> 'axopen.components.drives'.
if (name.StartsWith("@"))
{
var slash = name.IndexOf('/');
if (slash >= 0 && slash < name.Length - 1)
{
name = name.Substring(slash + 1);
}
}
var ov = DiscoveryOverrides.TryGetValue(folder, out var o) ? o : (pack: (bool?)null, test: (bool?)null);
var pack = ov.pack ?? true;
var test = ov.test ?? true;
yield return (folder, name, pack, false, test);
}
}
#endregion
public string GitHubUser { get; } = System.Environment.GetEnvironmentVariable("GH_USER");
public string GitHubToken { get; } = System.Environment.GetEnvironmentVariable("GH_TOKEN");
public string ApaxSignKey { get; } = System.Environment.GetEnvironmentVariable("APAX_KEY");
public IEnumerable<string> GetAxFolders((string folder, string name, bool pack, bool app_run, bool test) library)
{
var paths = new string[]
{
Path.Combine(Path.Combine(RootDir, library.folder), "ctrl"),
Path.Combine(Path.Combine(RootDir, library.folder), "ax")
};
return paths.Where(p => File.Exists(Path.Combine(p, "apax.yml")));
}
public IEnumerable<string> GetApplicationAxFolders((string folder, string name, bool pack) library)
{
var paths = new string[]
{
Path.Combine(Path.Combine(RootDir, library.folder), "ax")
};
return paths.Where(p => File.Exists(Path.Combine(p, "apax.yml")));
}
public IEnumerable<string> GetLibraryAxFolders((string folder, string name, bool pack, bool app_run, bool test) library)
{
var paths = new string[]
{
Path.Combine(Path.Combine(RootDir, library.folder), "ctrl")
};
return paths.Where(p => File.Exists(Path.Combine(p, "apax.yml")));
}
public IEnumerable<string> GetLibraryWithTestAxFolders((string folder, string name, bool pack, bool app_run) library)
{
var paths = new string[]
{
Path.Combine(Path.Combine(RootDir, library.folder), "ctrl")
};
return paths.Where(p => File.Exists(Path.Combine(p, "apax.yml")) && Directory.Exists(Path.Combine(p, "tests"))).ToList();
}
public string GetLibFolder((string folder, string name, bool pack, bool app_run, bool test) library)
{
return Path.Combine(Path.Combine(RootDir, library.folder), "ctrl");
}
public string GetAppFolder((string folder, string name) library)
{
return Path.Combine(Path.Combine(RootDir, library.folder), "app");
}
public string GetAppFolder((string folder, string name, bool pack, bool app_run, bool test) library)
{
return Path.Combine(Path.Combine(RootDir, library.folder), "app");
}
public string GetAxTestResultsFolder(string axFolder)
{
return Path.Combine(axFolder, "testresult");
}
public string GetAppFolder((string folder, string name, string targetIp, string targetPlatform) app)
{
return GetAppFolder((app.folder, app.name));
}
public IEnumerable<string> GetApaxFiles((string folder, string name, bool pack, bool app_run, bool test) library)
{
var paths = new string[]
{
Path.Combine(Path.Combine(RootDir, library.folder), "ctrl", "apax.yml"),
Path.Combine(Path.Combine(RootDir, library.folder), "ax", "apax.yml")
};
return paths.Where(Path.Exists);
}
public string GetApaxFile((string folder, string name, bool pack, bool app_run) library)
{
return Path.Combine(Path.Combine(RootDir, library.folder), "ctrl", "apax.yml");
}
public string GetApaxFile(string folder, string sub)
{
return Path.Combine(Path.Combine(RootDir, folder), sub, "apax.yml");
}
public string GetApaxFile(string folder)
{
return Path.Combine(Path.Combine(RootDir, folder), "apax.yml");
}
public string GetApplicationName(string yamlFilePath)
{
string appName = "";
// Load the YAML stream
var yaml = new YamlStream();
using (var reader = new StreamReader(yamlFilePath))
{
yaml.Load(reader);
}
// Assuming there's only one document in the YAML stream
var root = (YamlMappingNode)yaml.Documents[0].RootNode;
if (root.Children.TryGetValue(new YamlScalarNode("type"), out var typeNode) && typeNode is YamlScalarNode scalarNode && (scalarNode.Value == "app"))
{
if (root.Children.TryGetValue(new YamlScalarNode("name"), out var nameNode))
{
appName = nameNode.ToString();
}
}
return appName;
}
public string EnsureFolder(string path)
{
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
return path;
}
internal void ProvisionNodeJs()
{
// Check if node is available
var nodeCommand = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "node" : "node.exe";
var npmCommand = RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "npm" : "npm.cmd";
try
{
var nodeProcess = ProcessRunner.Start(nodeCommand, new ProcessSettings()
{
Arguments = "--version",
RedirectStandardOutput = true,
RedirectStandardError = true,
Silent = true
});
nodeProcess.WaitForExit();
if (nodeProcess.GetExitCode() == 0)
{
var version = string.Join("", nodeProcess.GetStandardOutput());
Log.Information($"Node.js is already installed: {version.Trim()}");
return;
}
}
catch (Exception ex)
{
Log.Warning($"Node.js not found or failed to execute: {ex.Message}");
}
// Node.js is not available, provision it
Log.Information("Node.js not found. Provisioning Node.js...");
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// Use winget to install Node.js on Windows
Log.Information("Attempting to install Node.js using winget...");
var wingetProcess = ProcessRunner.Start("winget", new ProcessSettings()
{
Arguments = "install OpenJS.NodeJS.LTS --accept-source-agreements --accept-package-agreements",
RedirectStandardOutput = false,
RedirectStandardError = false,
Silent = false
});
wingetProcess.WaitForExit();
if (wingetProcess.GetExitCode() != 0)
{
Log.Warning("winget installation failed. Trying Chocolatey...");
// Fallback to Chocolatey
var chocoProcess = ProcessRunner.Start("choco", new ProcessSettings()
{
Arguments = "install nodejs-lts -y",
RedirectStandardOutput = false,
RedirectStandardError = false,
Silent = false
});
chocoProcess.WaitForExit();
if (chocoProcess.GetExitCode() != 0)
{
throw new Exception("Failed to provision Node.js. Please install Node.js manually from https://nodejs.org/");
}
}
}
else
{
// Linux - use package manager or nvm
Log.Information("Attempting to install Node.js on Linux...");
// Try using apt (Debian/Ubuntu)
var aptProcess = ProcessRunner.Start("bash", new ProcessSettings()
{
Arguments = "-c \"curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash - && sudo apt-get install -y nodejs\"",
RedirectStandardOutput = false,
RedirectStandardError = false,
Silent = false
});
aptProcess.WaitForExit();
if (aptProcess.GetExitCode() != 0)
{
throw new Exception("Failed to provision Node.js on Linux. Please install Node.js manually.");
}
}
Log.Information("Node.js provisioning completed.");
}
}