22using System . CommandLine ;
33using AutoSDK . Extensions ;
44using AutoSDK . Generation ;
5+ using AutoSDK . Helpers ;
56using AutoSDK . Models ;
67using AutoSDK . Naming . Methods ;
78
@@ -168,6 +169,13 @@ internal sealed class GenerateCommand : Command
168169 Description = "Generate model classes, enums, and JSON converters. Set to false when referencing types from an existing namespace via --types-namespace." ,
169170 } ;
170171
172+ private Option < string > Language { get ; } = new (
173+ name : "--language" )
174+ {
175+ DefaultValueFactory = _ => "csharp" ,
176+ Description = "Generation backend. Currently supported: csharp." ,
177+ } ;
178+
171179 public GenerateCommand ( ) : base ( name : "generate" , description : "Generates client sdk using an OpenAPI or AsyncAPI spec." )
172180 {
173181 Arguments . Add ( Input ) ;
@@ -191,6 +199,7 @@ internal sealed class GenerateCommand : Command
191199 Options . Add ( JsonSerializerContextName ) ;
192200 Options . Add ( TypesNamespace ) ;
193201 Options . Add ( GenerateModels ) ;
202+ Options . Add ( Language ) ;
194203
195204 SetAction ( HandleAsync ) ;
196205 }
@@ -200,6 +209,7 @@ private async Task HandleAsync(ParseResult parseResult)
200209 string input = parseResult . GetRequiredValue ( Input ) ;
201210 string output = parseResult . GetRequiredValue ( Output ) ;
202211 bool singleFile = parseResult . GetRequiredValue ( SingleFile ) ;
212+ string language = parseResult . GetRequiredValue ( Language ) ;
203213
204214 var namespaceValue = parseResult . GetRequiredValue ( Namespace ) ;
205215 var contextName = parseResult . GetRequiredValue ( JsonSerializerContextName ) ;
@@ -268,104 +278,23 @@ private async Task HandleAsync(ParseResult parseResult)
268278 } ;
269279 }
270280
271- var data = Generation . Data . Prepare ( ( ( yaml , settings ) , GlobalSettings : settings ) ) ;
272- var files = settings . GenerateCli
273- ? data . Methods
274- . SelectMany ( x => new [ ]
275- {
276- Sources . Command ( x ) ,
277- } ) . Concat ( data . Methods . GroupBy ( x => x . Tag )
278- . SelectMany ( x => new [ ]
279- {
280- Sources . GroupCommand ( x . Key , x . ToImmutableArray ( ) ) ,
281- } ) )
282- . Concat ( [ Sources . MainCommand ( data . Tags ) ] )
283- . Concat ( [ Sources . AddCommands ( data . Methods , data . Tags ) ] )
284- . Where ( x => ! x . IsEmpty )
285- . ToArray ( )
286- : generateModels
287- ? data . Enums
288- . SelectMany ( x => new [ ]
289- {
290- Sources . Enum ( x ) ,
291- Sources . EnumJsonConverter ( x ) ,
292- Sources . EnumNullableJsonConverter ( x ) ,
293- } )
294- . Concat ( data . Classes
295- . SelectMany ( x => new [ ]
296- {
297- Sources . Class ( x ) ,
298- Sources . ClassJsonExtensions ( x ) ,
299- Sources . ClassValidation ( x ) ,
300- } ) )
301- . Concat ( data . Methods
302- . SelectMany ( x => new [ ]
303- {
304- Sources . Method ( x ) ,
305- Sources . MethodInterface ( x ) ,
306- } ) )
307- . Concat ( data . Clients
308- . SelectMany ( x => new [ ]
309- {
310- Sources . Client ( x ) ,
311- Sources . ClientInterface ( x ) ,
312- } ) )
313- . Concat ( data . Authorizations
314- . SelectMany ( x => new [ ]
315- {
316- Sources . Authorization ( x ) ,
317- Sources . AuthorizationInterface ( x ) ,
318- } ) )
319- . Concat ( [ Sources . MainAuthorizationConstructor ( data . Authorizations ) ] )
320- . Concat ( data . AnyOfs
321- . SelectMany ( x => new [ ]
322- {
323- Sources . AnyOf ( x ) ,
324- Sources . AnyOfJsonExtensions ( x ) ,
325- Sources . AnyOfJsonConverter ( x ) ,
326- //Sources.AnyOfJsonConverterFactory(x),
327- Sources . AnyOfValidation ( x ) ,
328- } ) )
329- . Concat ( [ Sources . JsonSerializerContext ( data . Converters , data . Types ) ] )
330- . Concat ( [ Sources . JsonSerializerContextTypes ( data . Types ) ] )
331- . Concat ( [ Sources . Polyfills ( settings ) ] )
332- . Concat ( [ Sources . Exceptions ( settings ) ] )
333- . Concat ( [ Sources . PathBuilder ( settings ) ] )
334- . Concat ( data . Methods . Any ( static x => x . RawStream )
335- ? [ Sources . ResponseStream ( data . Converters . Settings ) ]
336- : [ ] )
337- . Concat ( [ Sources . UnixTimestampJsonConverter ( settings ) ] )
338- // WebSocket client generation (from AsyncAPI specs)
339- . Concat ( data . WebSocketClients
340- . SelectMany ( x => new [ ]
341- {
342- Sources . WebSocketClient ( x ) ,
343- Sources . WebSocketReceiveMethod ( x ) ,
344- } ) )
345- // PathBuilder for WebSocket namespaces with query parameters
346- . Concat ( data . WebSocketClients
347- . Where ( x => x . QueryParameters . Length > 0 &&
348- x . Settings . Namespace != settings . Namespace )
349- . Select ( x => x . Settings )
350- . Distinct ( )
351- . Select ( s => Sources . PathBuilder ( s ) ) )
352- . Concat ( data . WebSocketOperations
353- . Where ( x => x . Direction == AutoSDK . Models . WebSocketDirection . Send )
354- . Select ( x => Sources . WebSocketSendMethod ( x ) ) )
355- . Where ( x => ! x . IsEmpty )
356- . ToArray ( )
357- // When generate-models is false, only output WebSocket client files
358- : data . WebSocketClients
359- . SelectMany ( x => new [ ]
360- {
361- Sources . WebSocketClient ( x ) ,
362- Sources . WebSocketReceiveMethod ( x ) ,
363- } )
364- . Concat ( data . WebSocketOperations
365- . Where ( x => x . Direction == AutoSDK . Models . WebSocketDirection . Send )
366- . Select ( x => Sources . WebSocketSendMethod ( x ) ) )
367- . Where ( x => ! x . IsEmpty )
368- . ToArray ( ) ;
281+ if ( ! string . Equals ( language , "csharp" , StringComparison . OrdinalIgnoreCase ) )
282+ {
283+ throw new NotSupportedException ( $ "Unsupported language '{ language } '. Currently only 'csharp' is supported.") ;
284+ }
285+
286+ var coreResult = CorePipeline . Prepare (
287+ ( ( yaml , settings ) , GlobalSettings : settings ) ,
288+ static ( document , currentSettings ) => document . GetSchemas ( ( CSharpSettings ) currentSettings ) ,
289+ CSharpPipeline . ApplyModelNaming ,
290+ static text => text . ToClassName ( ) ,
291+ static text => text . ToPropertyName ( ) ) ;
292+ var plugin = CSharpLanguagePlugin . Instance ;
293+ var data = plugin . Enrich ( coreResult ) ;
294+ var files = plugin
295+ . GenerateFiles ( data )
296+ . Where ( x => ! x . IsEmpty )
297+ . ToArray ( ) ;
369298
370299 Directory . CreateDirectory ( output ) ;
371300
0 commit comments