1+ using System . CommandLine ;
2+ using AutoSDK . Extensions ;
3+ using AutoSDK . Helpers ;
4+ using AutoSDK . Models ;
5+
6+ namespace AutoSDK . CLI . Commands ;
7+
8+ internal sealed class CliCommand : Command
9+ {
10+ private Argument < string > Input { get ; } = new (
11+ name : "input" )
12+ {
13+ DefaultValueFactory = _ => string . Empty ,
14+ Description = "Input file path" ,
15+ } ;
16+
17+ private Option < string > Output { get ; } = new (
18+ name : "--output" ,
19+ aliases : [ "-o" ] )
20+ {
21+ DefaultValueFactory = _ => "Testing" ,
22+ Description = "Output file path" ,
23+ } ;
24+
25+ private Option < bool > ExcludeDeprecated { get ; } = new (
26+ name : "--exclude-deprecated-operations" ,
27+ aliases : [ "-e" ] )
28+ {
29+ DefaultValueFactory = _ => Settings . Default . ExcludeDeprecatedOperations ,
30+ Description = "Exclude deprecated operations" ,
31+ } ;
32+
33+ private Option < bool > IgnoreOpenApiErrors { get ; } = new (
34+ name : "--ignore-openapi-errors" )
35+ {
36+ DefaultValueFactory = _ => Settings . Default . IgnoreOpenApiErrors ,
37+ Description = "Ignore OpenAPI errors" ,
38+ } ;
39+
40+ private Option < bool > IgnoreOpenApiWarnings { get ; } = new (
41+ name : "--ignore-openapi-warnings" )
42+ {
43+ DefaultValueFactory = _ => Settings . Default . IgnoreOpenApiWarnings ,
44+ Description = "Ignore OpenAPI warnings" ,
45+ } ;
46+
47+ public CliCommand ( ) : base ( name : "cli" , description : "Creates CLI .cs files for a OpenAPI spec." )
48+ {
49+ Arguments . Add ( Input ) ;
50+ Options . Add ( Output ) ;
51+ Options . Add ( ExcludeDeprecated ) ;
52+ Options . Add ( IgnoreOpenApiErrors ) ;
53+ Options . Add ( IgnoreOpenApiWarnings ) ;
54+
55+ SetAction ( HandleAsync ) ;
56+ }
57+
58+ private async Task HandleAsync ( ParseResult parseResult )
59+ {
60+ string input = parseResult . GetRequiredValue ( Input ) ;
61+ string output = parseResult . GetRequiredValue ( Output ) ;
62+ bool excludeDeprecatedOperations = parseResult . GetRequiredValue ( ExcludeDeprecated ) ;
63+ bool ignoreOpenApiErrors = parseResult . GetRequiredValue ( IgnoreOpenApiErrors ) ;
64+ bool ignoreOpenApiWarnings = parseResult . GetRequiredValue ( IgnoreOpenApiWarnings ) ;
65+
66+ Console . WriteLine ( $ "Loading { input } ...") ;
67+
68+ using var client = new HttpClient ( ) ;
69+ var yaml = input . StartsWith ( "http" , StringComparison . OrdinalIgnoreCase )
70+ ? await client . GetStringAsync ( new Uri ( input ) ) . ConfigureAwait ( false )
71+ : await File . ReadAllTextAsync ( input ) . ConfigureAwait ( false ) ;
72+
73+ Console . WriteLine ( "Creating..." ) ;
74+
75+ var settings = Settings . Default with
76+ {
77+ ExcludeDeprecatedOperations = excludeDeprecatedOperations ,
78+ IgnoreOpenApiErrors = ignoreOpenApiErrors ,
79+ IgnoreOpenApiWarnings = ignoreOpenApiWarnings ,
80+ } ;
81+ var openApiDocument = yaml . GetOpenApiDocument ( settings ) ;
82+ var schemas = openApiDocument . GetSchemas ( settings ) ;
83+ var operations = openApiDocument . GetOperations ( settings , globalSettings : settings , schemas ) ;
84+
85+ var files = new List < FileWithName >
86+ {
87+ new ( "http-client.env.json" , @$ "{{
88+ { openApiDocument . Servers . Select ( x => @$ "
89+ ""{ x . Description } "": {{
90+ ""host"": ""{ x . Url } "",
91+ ""token"": """"
92+ }}," ) . Inject ( ) . TrimEnd ( ',' ) }
93+ }}" )
94+ } ;
95+
96+ foreach ( var group in operations
97+ . SelectMany ( x => x . Tags . Select ( y => ( Tag : y , x ) ) )
98+ . GroupBy ( x => x . Tag ) )
99+ {
100+ var content = string . Empty ;
101+
102+ foreach ( var ( _, operation ) in group )
103+ {
104+ // "initialAmount": 1000.0,
105+ // "numberOfMonths": 36,
106+ // "startDate": "2025-02-25"
107+
108+ var requestSchema = operation . Schemas . FirstOrDefault ( y => y . Hint == Hint . Request ) ;
109+ content += $@ "
110+ ### { operation . OperationType . ToString ( "G" ) . ToUpperInvariant ( ) } { operation . OperationPath }
111+ { operation . OperationType . ToString ( "G" ) . ToUpperInvariant ( ) } {{{{host}}}}{ operation . OperationPath }
112+ { ( operation . GlobalSecurityRequirements . Any ( ) || operation . Operation . Security . Any ( ) ? @"
113+ Authorization: Bearer {{token}}" : " " ) }
114+ Content-Type: application/json
115+
116+ { ( requestSchema != null ? @$ "
117+ {{
118+ { requestSchema . Schema . Properties . Select ( x => @$ "
119+ ""{ x . Key } "": { x . Value . Example ? . ToString ( ) ?? "\" 1\" " } ," ) . Inject ( ) }
120+ }}
121+ " : " " ) }
122+
123+ " . RemoveBlankLinesWhereOnlyWhitespaces ( ) ;
124+ }
125+
126+ files . Add ( new FileWithName ( group . Key + ".http" , content ) ) ;
127+ }
128+
129+ Directory . CreateDirectory ( output ) ;
130+
131+ foreach ( var file in files )
132+ {
133+ await File . WriteAllTextAsync ( Path . Combine ( output , file . Name ) , file . Text ) . ConfigureAwait ( false ) ;
134+ }
135+
136+ Console . WriteLine ( "Done." ) ;
137+ }
138+ }
0 commit comments