2using System.Text.RegularExpressions;
4using Newtonsoft.Json.Linq;
7using Nuke.Common.Tooling;
8using Nuke.Common.Utilities;
9using Nuke.Common.Utilities.Collections;
13public partial record
class XRepoPackagePath(
14 AbsolutePath ParentFolder,
23 (?<PARENT>.*?[\/\\]packages[\/\\]\w)[\/\\]
26 (?<HASH>.+?)(?:[\/\\]|$)
28 RegexOptions.CultureInvariant
29 | RegexOptions.IgnoreCase
30 | RegexOptions.IgnorePatternWhitespace
32 private static partial Regex PathParserPattern();
39 public static XRepoPackagePath? Make(AbsolutePath from)
41 var parsed = from.ToString().Parse(PathParserPattern(), forceNullOnWhitespce:
true);
42 var parentFolder = parsed(
"PARENT");
43 var name = parsed(
"NAME");
44 var version = parsed(
"VERSION");
45 var hash = parsed(
"HASH");
46 if (parentFolder ==
null || name ==
null || version ==
null || hash ==
null)
51 return new(parentFolder, name, version, hash);
57 public AbsolutePath PackagesRoot => ParentFolder.Parent!;
62 public AbsolutePath PackageFolder => ParentFolder / Name / Version / Hash;
68[JsonObject(MemberSerialization.OptIn)]
71 [JsonProperty(PropertyName =
"commit")]
74 [JsonProperty(PropertyName =
"url")]
77 [JsonProperty(PropertyName =
"branch")]
80 [JsonProperty(PropertyName =
"name")]
87[JsonObject(MemberSerialization.OptIn)]
90 [JsonProperty(PropertyName =
"installdir")]
97[JsonObject(MemberSerialization.OptIn)]
100 [JsonProperty(PropertyName =
"version")]
103 [JsonProperty(PropertyName =
"license")]
104 string? License =
null,
106 [JsonProperty(PropertyName =
"links")]
108 List<string>? Links =
null,
110 [JsonProperty(PropertyName =
"syslinks")]
112 List<string>? SysLinks =
null,
114 [JsonProperty(PropertyName =
"static")]
115 bool? Static =
false,
117 [JsonProperty(PropertyName =
"shared")]
118 bool? Shared =
false,
120 [JsonProperty(PropertyName =
"linkdirs")]
121 AbsolutePath[]? LinkDirs =
null,
123 [JsonProperty(PropertyName =
"libfiles")]
124 AbsolutePath[]? LibFiles =
null,
126 [JsonProperty(PropertyName =
"includedirs")]
127 AbsolutePath[]? IncludeDirs =
null,
129 [JsonProperty(PropertyName =
"sysincludedirs")]
130 AbsolutePath[]? SysIncludeDirs =
null,
132 [JsonProperty(PropertyName =
"cxxflags")]
133 string? CxxFlags =
null,
135 [JsonProperty(PropertyName =
"defines")]
137 List<string>? Defines =
null,
140 [JsonProperty(PropertyName =
"name")]
143 [JsonProperty(PropertyName =
"program")]
144 AbsolutePath? Program =
null,
147 [JsonProperty(PropertyName =
"arch")]
150 [JsonProperty(PropertyName =
"configs")]
151 Dictionary<string, string>? Configs =
null,
153 [JsonProperty(PropertyName =
"description")]
154 string? Description =
null,
156 [JsonProperty(PropertyName =
"plat")]
159 [JsonProperty(PropertyName =
"envs")]
160 Dictionary<string, object>? Envs =
null,
162 [JsonProperty(PropertyName =
"repo")]
167 [JsonProperty(PropertyName =
"pathenvs")]
168 string[]? PathEnvs =
null,
170 [JsonProperty(PropertyName =
"kind")]
173 [JsonProperty(PropertyName =
"artifacts")]
176 [JsonProperty(PropertyName =
"mode")]
184 public XRepoPackagePath? GetPath()
186 foreach (var includeDir
in IncludeDirs ?? [])
188 var result = includeDir.InferXRepoPackage();
202 public string? InferredName => Name ?? GetPath()?.Name;
204 public bool IsSystemProgram => Program !=
null;
205 public bool IsEnvironmentProgram => !IsSystemProgram && (Kind?.EqualsOrdinalIgnoreCase(
"binary") ??
false);
206 public bool IsProgram => IsSystemProgram || IsEnvironmentProgram;
207 public bool IsLibrary => !IsProgram;
212 public bool IsHeaderOnly => (!IncludeDirs.IsNullOrEmpty() || !SysIncludeDirs.IsNullOrEmpty())
214 && LinkDirs.IsNullOrEmpty()
215 && LibFiles.IsNullOrEmpty()
218 public JObject? GetManifest()
220 var path = GetPath();
221 if (path ==
null)
return null;
222 var manifestJsonPath = path.PackageFolder /
"manifest.json";
223 manifestJsonPath.ExistingFile()?.Delete();
224 var tempLuaPath = path.PackageFolder /
"convert_manifest.lua";
225 tempLuaPath.WriteAllText(
227 import("core.base.json
")
228 local manifest = io.load("manifest.txt
")
229 json.savefile("manifest.json
", manifest)
232 XMakeTasks.
XMake(
"lua ./convert_manifest.lua", workingDirectory: path.PackageFolder);
233 Assert.FileExists(manifestJsonPath,
"XMake didn't generate a json version of the manifest file. It may have logged why.");
234 return manifestJsonPath.ReadJson();
240 public static XRepoPackagePath? InferXRepoPackage(
this AbsolutePath path) => XRepoPackagePath.Make(path);
249 => packages.Where(p => p.IsLibrary)
250 .FirstOrDefault(p => p.InferredName.EqualsOrdinalIgnoreCase(name));
259 => packages.Where(p => p.IsProgram)
260 .FirstOrDefault(p => p.InferredName.EqualsOrdinalIgnoreCase(name));
267 foreach (var line
in output)
269 if (line.Text.StartsWith(
"[{") && line.Text.EndsWith(
"]"))
271 return line.Text.GetJson<List<XRepoPackage>>();
A JSON converter for property which may have a single value or an array of values when de-serializing...