1
+ import * as vscode from 'vscode' ;
2
+ import * as path from 'path' ;
3
+ import * as fs from 'fs' ;
4
+ import { IGenerator } from '../types/types' ;
5
+ import { validateSpecificationFile , getNamespace , getOutputFilePath , getTypeScriptOutputDirectory } from '../utils/file-utils' ;
6
+ import { validateDependencies } from '../utils/system-utils' ;
7
+ import { executeRapicgenCommand , ensureRapicgenToolAvailable } from './rapicgen-tool' ;
8
+
9
+ /**
10
+ * Available code generators with their command names and display names
11
+ */
12
+ export const generators : IGenerator [ ] = [
13
+ { command : 'nswag' , displayName : 'NSwag' , requiresJava : false } ,
14
+ { command : 'refitter' , displayName : 'Refitter' , requiresJava : false } ,
15
+ { command : 'openapi' , displayName : 'OpenAPI Generator' , requiresJava : true } ,
16
+ { command : 'kiota' , displayName : 'Microsoft Kiota' , requiresJava : false } ,
17
+ { command : 'swagger' , displayName : 'Swagger Codegen CLI' , requiresJava : true } ,
18
+ { command : 'autorest' , displayName : 'AutoREST' , requiresJava : false }
19
+ ] ;
20
+
21
+ /**
22
+ * Available TypeScript generators with their command names and display names
23
+ */
24
+ export const typescriptGenerators : IGenerator [ ] = [
25
+ { command : 'angular' , displayName : 'Angular' , requiresJava : true } ,
26
+ { command : 'aurelia' , displayName : 'Aurelia' , requiresJava : true } ,
27
+ { command : 'axios' , displayName : 'Axios' , requiresJava : true } ,
28
+ { command : 'fetch' , displayName : 'Fetch' , requiresJava : true } ,
29
+ { command : 'inversify' , displayName : 'Inversify' , requiresJava : true } ,
30
+ { command : 'jquery' , displayName : 'JQuery' , requiresJava : true } ,
31
+ { command : 'nestjs' , displayName : 'NestJS' , requiresJava : true } ,
32
+ { command : 'node' , displayName : 'Node' , requiresJava : true } ,
33
+ { command : 'reduxquery' , displayName : 'Redux Query' , requiresJava : true } ,
34
+ { command : 'rxjs' , displayName : 'RxJS' , requiresJava : true }
35
+ ] ;
36
+
37
+ /**
38
+ * Checks if a generator requires Java runtime
39
+ * @param generator The generator command name
40
+ * @param isTypeScript Whether the generator is for TypeScript
41
+ * @returns true if the generator requires Java, false otherwise
42
+ */
43
+ export function generatorRequiresJava ( generator : string , isTypeScript = false ) : boolean {
44
+ if ( isTypeScript ) {
45
+ const typescriptGenerator = typescriptGenerators . find ( g => g . command === generator ) ;
46
+ return typescriptGenerator ?. requiresJava ?? false ;
47
+ } else {
48
+ const csharpGenerator = generators . find ( g => g . command === generator ) ;
49
+ return csharpGenerator ?. requiresJava ?? false ;
50
+ }
51
+ }
52
+
53
+ /**
54
+ * Executes the Rapicgen tool to generate C# client code
55
+ * @param generator The generator to use (nswag, openapi, etc.)
56
+ * @param specificationFilePath The path to the OpenAPI/Swagger specification file
57
+ * @param context The extension context
58
+ */
59
+ export async function executeRapicgen ( generator : string , specificationFilePath : string , context : vscode . ExtensionContext ) : Promise < void > {
60
+ // Validate the specification file
61
+ if ( ! validateSpecificationFile ( specificationFilePath ) ) {
62
+ return ;
63
+ }
64
+
65
+ // Validate dependencies
66
+ if ( ! validateDependencies ( generator , generatorRequiresJava ) ) {
67
+ return ;
68
+ }
69
+
70
+ // Ensure the Rapicgen tool is installed and up-to-date
71
+ const rapicgenAvailable = await ensureRapicgenToolAvailable ( context ) ;
72
+ if ( ! rapicgenAvailable ) {
73
+ return ;
74
+ }
75
+
76
+ const namespace = getNamespace ( ) ;
77
+ const outputFile = getOutputFilePath ( specificationFilePath , generator ) ;
78
+
79
+ // Ensure output directory exists
80
+ const outputDir = path . dirname ( outputFile ) ;
81
+ if ( ! fs . existsSync ( outputDir ) ) {
82
+ try {
83
+ fs . mkdirSync ( outputDir , { recursive : true } ) ;
84
+ } catch ( err ) {
85
+ vscode . window . showErrorMessage ( `Failed to create output directory: ${ outputDir } ` ) ;
86
+ return ;
87
+ }
88
+ }
89
+
90
+ const command = `rapicgen csharp ${ generator } "${ specificationFilePath } " "${ namespace } " "${ outputFile } "` ;
91
+ const generatorDisplayName = generators . find ( g => g . command === generator ) ?. displayName || generator ;
92
+
93
+ await executeRapicgenCommand ( command , generatorDisplayName , outputFile ) ;
94
+ }
95
+
96
+ /**
97
+ * Executes the Rapicgen tool to generate TypeScript client code
98
+ * @param generator The TypeScript generator to use (angular, axios, etc.)
99
+ * @param specificationFilePath The path to the OpenAPI/Swagger specification file
100
+ * @param context The extension context
101
+ */
102
+ export async function executeRapicgenTypeScript ( generator : string , specificationFilePath : string , context : vscode . ExtensionContext ) : Promise < void > {
103
+ // Validate the specification file
104
+ if ( ! validateSpecificationFile ( specificationFilePath ) ) {
105
+ return ;
106
+ }
107
+
108
+ // Validate dependencies
109
+ if ( ! validateDependencies ( generator , generatorRequiresJava , true ) ) {
110
+ return ;
111
+ }
112
+
113
+ // Ensure the Rapicgen tool is installed and up-to-date
114
+ const rapicgenAvailable = await ensureRapicgenToolAvailable ( context ) ;
115
+ if ( ! rapicgenAvailable ) {
116
+ return ;
117
+ }
118
+
119
+ // For TypeScript, we get an output directory rather than a single file
120
+ const outputDir = getTypeScriptOutputDirectory ( specificationFilePath , generator ) ;
121
+
122
+ // Ensure output directory exists
123
+ if ( ! fs . existsSync ( outputDir ) ) {
124
+ try {
125
+ fs . mkdirSync ( outputDir , { recursive : true } ) ;
126
+ } catch ( err ) {
127
+ vscode . window . showErrorMessage ( `Failed to create output directory: ${ outputDir } ` ) ;
128
+ return ;
129
+ }
130
+ }
131
+
132
+ const command = `rapicgen typescript ${ generator } "${ specificationFilePath } " "${ outputDir } "` ;
133
+ const generatorDisplayName = typescriptGenerators . find ( g => g . command === generator ) ?. displayName || generator ;
134
+
135
+ await executeRapicgenCommand ( command , generatorDisplayName , outputDir , true ) ;
136
+ }
137
+
138
+ /**
139
+ * Executes the Rapicgen tool with Refitter using a settings file
140
+ * @param settingsFilePath The path to the .refitter settings file
141
+ * @param context The extension context
142
+ */
143
+ export async function executeRapicgenRefitterSettings ( settingsFilePath : string , context : vscode . ExtensionContext ) : Promise < void > {
144
+ // Validate the settings file
145
+ if ( ! validateSpecificationFile ( settingsFilePath ) ) {
146
+ return ;
147
+ }
148
+
149
+ // Validate dependencies
150
+ if ( ! validateDependencies ( 'refitter' , generatorRequiresJava ) ) {
151
+ return ;
152
+ }
153
+
154
+ // Ensure the Rapicgen tool is installed and up-to-date
155
+ const rapicgenAvailable = await ensureRapicgenToolAvailable ( context ) ;
156
+ if ( ! rapicgenAvailable ) {
157
+ return ;
158
+ }
159
+
160
+ const command = `rapicgen csharp refitter --settings-file "${ settingsFilePath } "` ;
161
+
162
+ await executeRapicgenCommand ( command , 'Refitter' , settingsFilePath , false , true ) ;
163
+ }
0 commit comments