Nuke.Unreal
Build Unreal apps in Style.
Loading...
Searching...
No Matches
UnrealToolArgument.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text.RegularExpressions;
5using System.Threading.Tasks;
6using Nuke.Common.Utilities;
7
8namespace Nuke.Unreal.Tools;
9
10/// <summary>
11/// Properties guiding how to process the argument
12/// </summary>
13/// <param name="Compatibility">
14/// Which major engine version this argument is compatible with
15/// </param>
16/// <param name="AllowMultiple">
17/// Allow multiple instances of this argument distinct by name only in the same command line
18/// output.
19/// </param>
20/// <param name="IsRawText">
21/// Argument is provided as-is in the Name field, do not parse it or escape it.
22/// </param>
23/// <returns></returns>
24public record class UnrealToolArgumentMeta(
25 UnrealCompatibility Compatibility = UnrealCompatibility.All,
26 bool AllowMultiple = false,
27 bool IsRawText = false
28);
29
30/// <summary>
31/// Argument for an Unreal tool
32/// </summary>
33/// <param name="Name">Argument name including optional starting dash (-MyArg)</param>
34/// <param name="Value">Flattened value of argument</param>
35/// <param name="Setter"></param>
36/// <param name="Meta">Properties guiding how to process the argument</param>
37public partial record class UnrealToolArgument(
38 string Name,
39 string? Value = null,
40 char Setter = '=',
41 UnrealToolArgumentMeta? Meta = null
42) {
43 /// <summary>
44 /// Render the command line without 'UE4' -&gt; 'Unreal' compatibility transformation
45 /// </summary>
46 public override string ToString()
47 {
48 if (GetMeta().IsRawText) return Name;
49
50 return string.IsNullOrWhiteSpace(Value)
51 ? Name : (Name + Setter + Value).DoubleQuoteIfNeeded();
52 }
53
54 [GeneratedRegex(@"^(?<NAME>.*?)((?<SETTER>[:=])(?<VALUE>.*))?$")]
55 private static partial Regex ParseRegex();
56
57 /// <summary>
58 /// Parse a (NAME)[=:]?(VALUE)? formatted argument
59 /// </summary>
60 /// <param name="input"></param>
61 /// <param name="meta">
62 /// Pass in new(IsRawText: true) to not parse it and take input at face value
63 /// </param>
64 /// <returns></returns>
65 public static UnrealToolArgument? Parse(string input, UnrealToolArgumentMeta? meta = null)
66 {
67 meta ??= new();
68 if (meta.IsRawText)
69 {
70 return new(
71 Name: input,
72 Meta: meta
73 );
74 }
75
76 var groups = ParseRegex().Match(input)?.Groups;
77 return groups?["NAME"] == null
78 ? null
79 : new(
80 groups?["NAME"]?.Value!,
81 groups?["VALUE"]?.Value,
82 (groups?["SETTER"]?.Value ?? "=").FirstOrDefault('='),
83 Meta: meta
84 );
85 }
86
87 /// <summary>
88 /// Render the command line with 'UE4' -&gt; 'Unreal' compatibility transformation
89 /// if we're gathering for UE5
90 /// </summary>
91 public string Gather(EngineVersion ueVersion)
92 {
93 if (GetMeta().IsRawText) return Name;
94
95 var compatibleName = ueVersion.SemanticalVersion.Major > 4
96 ? Name.Replace("UE4", "Unreal")
97 : Name;
98 return string.IsNullOrWhiteSpace(Value)
99 ? compatibleName : (compatibleName + Setter + Value).DoubleQuoteIfNeeded();
100 }
101
102 public UnrealToolArgumentMeta GetMeta() => Meta ?? new();
103}
High level representation of an Unreal Engine version.
partial record class UnrealToolArgument(string Name, string? Value=null, char Setter='=', UnrealToolArgumentMeta? Meta=null)
Argument for an Unreal tool.
record class UnrealToolArgumentMeta(UnrealCompatibility Compatibility=UnrealCompatibility.All, bool AllowMultiple=false, bool IsRawText=false)
Properties guiding how to process the argument.
UnrealCompatibility
A flag enum representation for checking the Unreal version compatibility of various features....