Skip to content

Commit b9bad9a

Browse files
Merge pull request #1181 from christianhelle/copilot/fix-1180
Add Java Runtime Check for OpenAPI Generator in VS Code Extension
2 parents e9f3f97 + e8397d7 commit b9bad9a

File tree

1 file changed

+64
-16
lines changed

1 file changed

+64
-16
lines changed

src/VSCode/src/extension.ts

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,20 @@ function isDotNetSdkInstalled(): boolean {
3737
}
3838
}
3939

40+
/**
41+
* Checks if Java runtime is installed and available
42+
* @returns true if Java is installed, false otherwise
43+
*/
44+
function isJavaRuntimeInstalled(): boolean {
45+
try {
46+
execSync('java -version', { encoding: 'utf-8' });
47+
return true;
48+
} catch (error) {
49+
console.error('Error checking Java runtime:', error);
50+
return false;
51+
}
52+
}
53+
4054
/**
4155
* Gets the installed Rapicgen .NET tool version
4256
* @returns The installed version as a string, or null if not installed or version cannot be determined
@@ -294,30 +308,46 @@ function getTypeScriptOutputDirectory(specificationFile: string, generator: stri
294308
* Available code generators with their command names and display names
295309
*/
296310
const generators = [
297-
{ command: 'nswag', displayName: 'NSwag' },
298-
{ command: 'refitter', displayName: 'Refitter' },
299-
{ command: 'openapi', displayName: 'OpenAPI Generator' },
300-
{ command: 'kiota', displayName: 'Microsoft Kiota' },
301-
{ command: 'swagger', displayName: 'Swagger Codegen CLI' },
302-
{ command: 'autorest', displayName: 'AutoREST' }
311+
{ command: 'nswag', displayName: 'NSwag', requiresJava: false },
312+
{ command: 'refitter', displayName: 'Refitter', requiresJava: false },
313+
{ command: 'openapi', displayName: 'OpenAPI Generator', requiresJava: true },
314+
{ command: 'kiota', displayName: 'Microsoft Kiota', requiresJava: false },
315+
{ command: 'swagger', displayName: 'Swagger Codegen CLI', requiresJava: true },
316+
{ command: 'autorest', displayName: 'AutoREST', requiresJava: false }
303317
];
304318

305319
/**
306320
* Available TypeScript generators with their command names and display names
307321
*/
308322
const typescriptGenerators = [
309-
{ command: 'angular', displayName: 'Angular' },
310-
{ command: 'aurelia', displayName: 'Aurelia' },
311-
{ command: 'axios', displayName: 'Axios' },
312-
{ command: 'fetch', displayName: 'Fetch' },
313-
{ command: 'inversify', displayName: 'Inversify' },
314-
{ command: 'jquery', displayName: 'JQuery' },
315-
{ command: 'nestjs', displayName: 'NestJS' },
316-
{ command: 'node', displayName: 'Node' },
317-
{ command: 'reduxquery', displayName: 'Redux Query' },
318-
{ command: 'rxjs', displayName: 'RxJS' }
323+
{ command: 'angular', displayName: 'Angular', requiresJava: true },
324+
{ command: 'aurelia', displayName: 'Aurelia', requiresJava: true },
325+
{ command: 'axios', displayName: 'Axios', requiresJava: true },
326+
{ command: 'fetch', displayName: 'Fetch', requiresJava: true },
327+
{ command: 'inversify', displayName: 'Inversify', requiresJava: true },
328+
{ command: 'jquery', displayName: 'JQuery', requiresJava: true },
329+
{ command: 'nestjs', displayName: 'NestJS', requiresJava: true },
330+
{ command: 'node', displayName: 'Node', requiresJava: true },
331+
{ command: 'reduxquery', displayName: 'Redux Query', requiresJava: true },
332+
{ command: 'rxjs', displayName: 'RxJS', requiresJava: true }
319333
];
320334

335+
/**
336+
* Checks if a generator requires Java runtime
337+
* @param generator The generator command name
338+
* @param isTypeScript Whether the generator is for TypeScript
339+
* @returns true if the generator requires Java, false otherwise
340+
*/
341+
function generatorRequiresJava(generator: string, isTypeScript = false): boolean {
342+
if (isTypeScript) {
343+
const typescriptGenerator = typescriptGenerators.find(g => g.command === generator);
344+
return typescriptGenerator?.requiresJava ?? false;
345+
} else {
346+
const csharpGenerator = generators.find(g => g.command === generator);
347+
return csharpGenerator?.requiresJava ?? false;
348+
}
349+
}
350+
321351
/**
322352
* Executes the Rapicgen tool with the given generator and parameters
323353
* @param generator The generator to use (nswag, refitter, etc.)
@@ -338,6 +368,15 @@ async function executeRapicgen(generator: string, specificationFilePath: string,
338368
return;
339369
}
340370

371+
// Check if Java is required and installed
372+
if (generatorRequiresJava(generator) && !isJavaRuntimeInstalled()) {
373+
vscode.window.showErrorMessage(
374+
'Java Runtime Environment (JRE) not found. The selected generator requires Java to be installed. ' +
375+
'Please install the latest version of Java from https://adoptium.net/ or https://www.oracle.com/java/technologies/downloads/'
376+
);
377+
return;
378+
}
379+
341380
// Ensure the Rapicgen tool is installed and up-to-date
342381
const rapicgenAvailable = await ensureRapicgenToolAvailable(context);
343382
if (!rapicgenAvailable) {
@@ -430,6 +469,15 @@ async function executeRapicgenTypeScript(generator: string, specificationFilePat
430469
return;
431470
}
432471

472+
// Check if Java is required and installed
473+
if (generatorRequiresJava(generator, true) && !isJavaRuntimeInstalled()) {
474+
vscode.window.showErrorMessage(
475+
'Java Runtime Environment (JRE) not found. The selected generator requires Java to be installed. ' +
476+
'Please install the latest version of Java from https://adoptium.net/ or https://www.oracle.com/java/technologies/downloads/'
477+
);
478+
return;
479+
}
480+
433481
// Ensure the Rapicgen tool is installed and up-to-date
434482
const rapicgenAvailable = await ensureRapicgenToolAvailable(context);
435483
if (!rapicgenAvailable) {

0 commit comments

Comments
 (0)