Nuke.Cola
Loading...
Searching...
No Matches
XMakeTasks.cs
1using System.Runtime.InteropServices;
2using System.Text.RegularExpressions;
3using Nuke.Common;
4using Nuke.Common.IO;
5using Nuke.Common.Tooling;
6using Serilog;
7
9
10/// <summary>
11/// XMake is a versatile build tool for many languages https://xmake.io/#/?id=supported-languages
12/// scriptable in Lua
13/// </summary>
14public static partial class XMakeTasks
15{
16 public const string LatestVersion = "3.0.7";
17 internal static string GetBundleAppName(string version = LatestVersion)
18 => (plat: EnvironmentInfo.Platform, arch: RuntimeInformation.OSArchitecture) switch
19 {
20 (PlatformFamily.Windows, Architecture.X64) => $"xmake-bundle-v{version}.win64.exe",
21 (PlatformFamily.Windows, Architecture.X86) => $"xmake-bundle-v{version}.win32.exe",
22 (PlatformFamily.Windows, Architecture.Arm64) => $"xmake-bundle-v{version}.arm64.exe",
23 (PlatformFamily.Linux, Architecture.X64) => $"xmake-bundle-v{version}.linux.x86_64",
24 (PlatformFamily.OSX, Architecture.Arm64) => $"xmake-bundle-v{version}.macos.arm64",
25 (PlatformFamily.OSX, Architecture.X64) => $"xmake-bundle-v{version}.macos.x86_64",
26 var other => throw new Exception($"Trying to use XMake on an unsupported platform: {other.plat} {other.arch}")
27 };
28
29 [GeneratedRegex(
30 """
31 not found main script\:\s(?<PATH>.*)\/core.*\.lua
32 """,
33 RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture
34 )]
35 private static partial Regex ProblematicMainXMakeFolder();
36
37 internal static AbsolutePath? GetProblematicMainXMakeFolder(this Output line)
38 {
39 var text = line.Text.Replace('\\', '/').Replace("//", "/");
40 var result = text.Parse(ProblematicMainXMakeFolder(), forceNullOnWhitespce: true)("PATH");
41 return result.TryAsPath();
42 }
43
44 /// <summary>
45 /// Get XMake or an error if downloading it has failed.
46 /// </summary>
47 public static ValueOrError<ToolEx> TryGetXMake(string version = LatestVersion) => ErrorHandling.TryGet(() =>
48 {
49 var bundleAppName = GetBundleAppName(version);
50 var xmakePath = NukeBuild.TemporaryDirectory / bundleAppName;
51 if (!xmakePath.FileExists())
52 {
53 Log.Information("Downloading XMake {0}", bundleAppName);
54 HttpTasks.HttpDownloadFile(
55 $"https://github.com/xmake-io/xmake/releases/download/v{LatestVersion}/{bundleAppName}",
56 xmakePath
57 );
58 }
59 return ToolExResolver.GetTool(xmakePath)
60 .With(
61 retry: (tool, process, attempt) =>
62 {
63 if (process.ExitCode == 0 || attempt >= 2) return null;
64 foreach (var line in process.Output)
65 {
66 var mainXMakeFolder = line.GetProblematicMainXMakeFolder();
67 if (mainXMakeFolder != null)
68 {
69 Log.Information("A known issue has occured with XMake where it cannot use {0}", mainXMakeFolder);
70 Log.Information("Remove it and have another attempt {0}", attempt + 1);
71 mainXMakeFolder.Parent.DeleteDirectory();
72 return tool;
73 }
74 }
75 return null;
76 }
77 );
78 });
79
80 public static ValueOrError<ToolEx> EnsureXMake => TryGetXMake();
81
82 /// <summary>
83 /// Get XMake. It throws an exception if setup has failed.
84 /// </summary>
85 public static ToolEx XMake => EnsureXMake.Get();
86}
XMake is a versatile build tool for many languages https://xmake.io/#/?id=supported-languages scripta...
Definition XMakeTasks.cs:15
static ToolEx XMake
Get XMake. It throws an exception if setup has failed.
Definition XMakeTasks.cs:85
static ValueOrError< ToolEx > TryGetXMake(string version=LatestVersion)
Get XMake or an error if downloading it has failed.
delegate? IReadOnlyCollection< Output > ToolEx(ArgumentStringHandlerEx arguments=default, string? workingDirectory=null, IReadOnlyDictionary< string, string >? environmentVariables=null, int? timeout=null, bool? logOutput=null, bool? logInvocation=null, Action< OutputType, string >? logger=null, Action< IProcess >? exitHandler=null, Action< StreamWriter >? input=null, Encoding? standardOutputEncoding=null, Encoding? standardInputEncoding=null, ToolExRetry? retry=null)
Extended copy of Tool delegate of Nuke.