|
5 | 5 | using System.Collections.Generic;
|
6 | 6 | using System.IO;
|
7 | 7 | using System.Linq;
|
| 8 | +using System.Runtime.CompilerServices; |
| 9 | +using System.Text.RegularExpressions; |
8 | 10 | using System.Threading.Tasks;
|
9 | 11 |
|
10 | 12 | namespace KubernetesWatchGenerator
|
@@ -101,6 +103,7 @@ static async Task Main(string[] args)
|
101 | 103 | Helpers.Register(nameof(GetApiVersion), GetApiVersion);
|
102 | 104 | Helpers.Register(nameof(GetKind), GetKind);
|
103 | 105 | Helpers.Register(nameof(GetPlural), GetPlural);
|
| 106 | + Helpers.Register(nameof(GetTuple), GetTuple); |
104 | 107 |
|
105 | 108 | // Generate the Watcher operations
|
106 | 109 | // We skip operations where the name of the class in the C# client could not be determined correctly.
|
@@ -134,10 +137,40 @@ static async Task Main(string[] args)
|
134 | 137 | .Select(x => x.Class)
|
135 | 138 | .ToHashSet();
|
136 | 139 |
|
137 |
| - Render.FileToFile("ModelExtensions.cs.template", definitions, |
138 |
| - Path.Combine(outputDirectory, "ModelExtensions.cs")); |
| 140 | + Render.FileToFile("ModelExtensions.cs.template", definitions, Path.Combine(outputDirectory, "ModelExtensions.cs")); |
| 141 | + |
| 142 | + // generate version converter maps |
| 143 | + var allGeneratedModelClassNames = Directory |
| 144 | + .EnumerateFiles(Path.Combine(outputDirectory, "Models")) |
| 145 | + .Select(Path.GetFileNameWithoutExtension) |
| 146 | + .ToList(); |
| 147 | + |
| 148 | + var versionRegex = @"(^V|v)[0-9]+((alpha|beta)[0-9]+)?"; |
| 149 | + var typePairs = allGeneratedModelClassNames |
| 150 | + .OrderBy(x => x) |
| 151 | + .Select(x => new { Version = Regex.Match(x, versionRegex).Value?.ToLower(), Kinda = Regex.Replace(x, versionRegex, string.Empty), Type = x }) |
| 152 | + .Where(x => !string.IsNullOrEmpty(x.Version)) |
| 153 | + .GroupBy(x => x.Kinda) |
| 154 | + .Where(x => x.Count() > 1) |
| 155 | + .SelectMany(x => x.SelectMany((value, index) => x.Skip(index + 1), (first, second) => new { first, second })) |
| 156 | + .OrderBy(x => x.first.Kinda) |
| 157 | + .ThenBy(x => x.first.Version) |
| 158 | + .Select(x => (ITuple)Tuple.Create(x.first.Type, x.second.Type)) |
| 159 | + .ToList(); |
| 160 | + |
| 161 | + var versionFile = File.ReadAllText(Path.Combine(outputDirectory, "..", "Versioning", "VersionConverter.cs")); |
| 162 | + var manualMaps = Regex.Matches(versionFile, @"\.CreateMap<(?<T1>.+?),\s?(?<T2>.+?)>") |
| 163 | + .Select(x => Tuple.Create(x.Groups["T1"].Value, x.Groups["T2"].Value)) |
| 164 | + .ToList(); |
| 165 | + var versionConverterPairs = typePairs.Except(manualMaps).ToList(); |
| 166 | + |
| 167 | + Render.FileToFile("VersionConverter.cs.template", versionConverterPairs, Path.Combine(outputDirectory, "VersionConverter.cs")); |
| 168 | + Render.FileToFile("ModelOperators.cs.template", typePairs, Path.Combine(outputDirectory, "ModelOperators.cs")); |
| 169 | + |
139 | 170 | }
|
140 | 171 |
|
| 172 | + |
| 173 | + |
141 | 174 | static void ToXmlDoc(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
|
142 | 175 | RenderBlock fn, RenderBlock inverse)
|
143 | 176 | {
|
@@ -166,6 +199,18 @@ static void ToXmlDoc(RenderContext context, IList<object> arguments, IDictionary
|
166 | 199 | }
|
167 | 200 | }
|
168 | 201 |
|
| 202 | + static void GetTuple(RenderContext context, IList<object> arguments, IDictionary<string, object> options, RenderBlock fn, RenderBlock inverse) |
| 203 | + { |
| 204 | + if (arguments != null && arguments.Count > 0 && arguments[0] is ITuple && options.TryGetValue("index", out var indexObj) && int.TryParse(indexObj?.ToString(), out var index)) |
| 205 | + { |
| 206 | + var pair = (ITuple)arguments[0]; |
| 207 | + var value = pair[index]; |
| 208 | + context.Write(value.ToString()); |
| 209 | + } |
| 210 | + } |
| 211 | + |
| 212 | + |
| 213 | + |
169 | 214 | static void GetClassName(RenderContext context, IList<object> arguments, IDictionary<string, object> options,
|
170 | 215 | RenderBlock fn, RenderBlock inverse)
|
171 | 216 | {
|
@@ -343,6 +388,7 @@ static void GetMethodName(RenderContext context, IList<object> arguments, IDicti
|
343 | 388 | }
|
344 | 389 | }
|
345 | 390 |
|
| 391 | + |
346 | 392 | static string GetMethodName(SwaggerOperation watchOperation)
|
347 | 393 | {
|
348 | 394 | var tag = watchOperation.Tags[0];
|
|
0 commit comments