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
@@ -88,6 +90,7 @@ static async Task Main(string[] args)
88
90
Helpers . Register ( nameof ( GetApiVersion ) , GetApiVersion ) ;
89
91
Helpers . Register ( nameof ( GetKind ) , GetKind ) ;
90
92
Helpers . Register ( nameof ( GetPlural ) , GetPlural ) ;
93
+ Helpers . Register ( nameof ( GetTuple ) , GetTuple ) ;
91
94
92
95
// Generate the Watcher operations
93
96
// We skip operations where the name of the class in the C# client could not be determined correctly.
@@ -125,8 +128,39 @@ static async Task Main(string[] args)
125
128
. ToHashSet ( ) ;
126
129
127
130
Render . FileToFile ( "ModelExtensions.cs.template" , definitions , Path . Combine ( outputDirectory , "ModelExtensions.cs" ) ) ;
131
+
132
+ // generate version converter maps
133
+ var allGeneratedModelClassNames = Directory
134
+ . EnumerateFiles ( Path . Combine ( outputDirectory , "Models" ) )
135
+ . Select ( Path . GetFileNameWithoutExtension )
136
+ . ToList ( ) ;
137
+
138
+ var versionRegex = @"(^V|v)[0-9]+((alpha|beta)[0-9]+)?" ;
139
+ var typePairs = allGeneratedModelClassNames
140
+ . OrderBy ( x => x )
141
+ . Select ( x => new { Version = Regex . Match ( x , versionRegex ) . Value ? . ToLower ( ) , Kinda = Regex . Replace ( x , versionRegex , string . Empty ) , Type = x } )
142
+ . Where ( x => ! string . IsNullOrEmpty ( x . Version ) )
143
+ . GroupBy ( x => x . Kinda )
144
+ . Where ( x => x . Count ( ) > 1 )
145
+ . SelectMany ( x => x . SelectMany ( ( value , index ) => x . Skip ( index + 1 ) , ( first , second ) => new { first , second } ) )
146
+ . OrderBy ( x => x . first . Kinda )
147
+ . ThenBy ( x => x . first . Version )
148
+ . Select ( x => ( ITuple ) Tuple . Create ( x . first . Type , x . second . Type ) )
149
+ . ToList ( ) ;
150
+
151
+ var versionFile = File . ReadAllText ( Path . Combine ( outputDirectory , ".." , "Versioning" , "VersionConverter.cs" ) ) ;
152
+ var manualMaps = Regex . Matches ( versionFile , @"\.CreateMap<(?<T1>.+?),\s?(?<T2>.+?)>" )
153
+ . Select ( x => Tuple . Create ( x . Groups [ "T1" ] . Value , x . Groups [ "T2" ] . Value ) )
154
+ . ToList ( ) ;
155
+ var versionConverterPairs = typePairs . Except ( manualMaps ) . ToList ( ) ;
156
+
157
+ Render . FileToFile ( "VersionConverter.cs.template" , versionConverterPairs , Path . Combine ( outputDirectory , "VersionConverter.cs" ) ) ;
158
+ Render . FileToFile ( "ModelOperators.cs.template" , typePairs , Path . Combine ( outputDirectory , "ModelOperators.cs" ) ) ;
159
+
128
160
}
129
161
162
+
163
+
130
164
static void ToXmlDoc ( RenderContext context , IList < object > arguments , IDictionary < string , object > options , RenderBlock fn , RenderBlock inverse )
131
165
{
132
166
if ( arguments != null && arguments . Count > 0 && arguments [ 0 ] != null && arguments [ 0 ] is string )
@@ -153,6 +187,18 @@ static void ToXmlDoc(RenderContext context, IList<object> arguments, IDictionary
153
187
}
154
188
}
155
189
190
+ static void GetTuple ( RenderContext context , IList < object > arguments , IDictionary < string , object > options , RenderBlock fn , RenderBlock inverse )
191
+ {
192
+ if ( arguments != null && arguments . Count > 0 && arguments [ 0 ] is ITuple && options . TryGetValue ( "index" , out var indexObj ) && int . TryParse ( indexObj ? . ToString ( ) , out var index ) )
193
+ {
194
+ var pair = ( ITuple ) arguments [ 0 ] ;
195
+ var value = pair [ index ] ;
196
+ context . Write ( value . ToString ( ) ) ;
197
+ }
198
+ }
199
+
200
+
201
+
156
202
static void GetClassName ( RenderContext context , IList < object > arguments , IDictionary < string , object > options , RenderBlock fn , RenderBlock inverse )
157
203
{
158
204
if ( arguments != null && arguments . Count > 0 && arguments [ 0 ] != null && arguments [ 0 ] is SwaggerOperation )
@@ -309,6 +355,7 @@ static void GetMethodName(RenderContext context, IList<object> arguments, IDicti
309
355
}
310
356
}
311
357
358
+
312
359
static string GetMethodName ( SwaggerOperation watchOperation )
313
360
{
314
361
var tag = watchOperation . Tags [ 0 ] ;
0 commit comments