-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBuildContext.cs
More file actions
434 lines (356 loc) · 16.8 KB
/
BuildContext.cs
File metadata and controls
434 lines (356 loc) · 16.8 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
// 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
public IEnumerable<(string folder, string name, bool pack, bool app_run, bool test)> Libraries { get; } = new[]
{
("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),
("abstractions", "axopen.abstractions", true, true, true),
("timers", "axopen.timers", true, false, true),
("simatic1500", "axopen.simatic1500", true, false, true),
("utils", "axopen.utils", true, false, true),
("core", "axopen.core", true, true, true),
("data", "axopen.data", true, true, true),
("probers", "axopen.probers", true, true, false),
("inspectors", "axopen.inspectors", true, true, true),
("components.abstractions", "axopen.components.abstractions", true, true, true),
("components.elements", "axopen.components.elements", true, true, true),
("io", "axopen.io", true, true, false),
("components.cognex.vision", "axopen.components.cognex.vision", true, true, true),
("components.pneumatics", "axopen.components.pneumatics", true, true, true),
("components.drives", "axopen.components.drives", true, true, true),
("components.rexroth.drives", "axopen.components.rexroth.drives", true, true, true),
("components.rexroth.press", "axopen.components.rexroth.press", true, true, true),
("components.festo.drives", "axopen.components.festo.drives", true, true, true),
("components.desoutter.tightening", "axopen.components.desoutter.tightening", true, true, true),
("components.robotics", "axopen.components.robotics", true, true, true),
("components.abb.robotics", "axopen.components.abb.robotics", true, true, true),
("components.mitsubishi.robotics", "axopen.components.mitsubishi.robotics", true, true, true),
("components.ur.robotics", "axopen.components.ur.robotics", true, true, true),
("components.kuka.robotics", "axopen.components.kuka.robotics", true, true, true),
("components.siem.identification", "axopen.components.siem.identification", true, true, true),
("components.siem.communication", "axopen.components.siem.communication", true, true, true),
("components.balluff.identification", "axopen.components.balluff.identification", true, true, true),
("components.keyence.vision", "axopen.components.keyence.vision", true, true, true),
("components.rexroth.tightening", "axopen.components.rexroth.tightening", true, true, true),
("components.dukane.welders", "axopen.components.dukane.welders", true, true, true),
("components.zebra.vision", "axopen.components.zebra.vision", true, true, true),
("integrations", "ix.integrations", false,false, false),
("template.axolibrary", "template.axolibrary", false, true, false)
};
#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), "app"),
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), "app"),
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), "app", "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.");
}
}