From 3e949d8839de87e985d7af4197eab54da2e1a8a3 Mon Sep 17 00:00:00 2001 From: mok-liee <6745620+mok-liee@users.noreply.github.com> Date: Sun, 3 Mar 2024 07:43:35 +0100 Subject: [PATCH] support JAVA_HOME environment variable (#756) * support JAVA_HOME environment variable fixes #661 * test(generator-cmd): add consideration of the JAVA_HOME variable * docs: add instructions for the use of JAVA_HOME * fix: add quotes to accept spaces in the JAVA_HOME path * fix: add quotation marks only for windows paths --------- Co-authored-by: Nils Braune <78608305+adnbrownie@users.noreply.github.com> --- apps/generator-cli/src/README.md | 2 +- apps/generator-cli/src/app/helpers/index.ts | 11 ++++ .../app/services/generator.service.spec.ts | 52 ++++++++++++------- .../src/app/services/generator.service.ts | 4 +- .../app/services/pass-through.service.spec.ts | 9 ++-- .../src/app/services/pass-through.service.ts | 3 +- 6 files changed, 54 insertions(+), 27 deletions(-) create mode 100644 apps/generator-cli/src/app/helpers/index.ts diff --git a/apps/generator-cli/src/README.md b/apps/generator-cli/src/README.md index a99441128..f30bf7b6f 100644 --- a/apps/generator-cli/src/README.md +++ b/apps/generator-cli/src/README.md @@ -10,7 +10,7 @@ OpenAPI Generator allows generation of API client libraries (SDK generation), se configuration automatically given an OpenAPI Spec (both 2.0 and 3.0 are supported). Please see [OpenAPITools/openapi-generator](https://github.com/OpenAPITools/openapi-generator). -The OpenAPI Generator is a Java project. `openapi-generator-cli` will download the appropriate JAR file and invoke the `java` executable to run the OpenAPI Generator. You must have the `java` binary executable available on your `PATH` for this to work. +The OpenAPI Generator is a Java project. `openapi-generator-cli` will download the appropriate JAR file and invoke the `java` executable to run the OpenAPI Generator. You must have the `java` binary executable available on your `PATH` or have set `JAVA_HOME` correctly for this to work. If you find this tool useful, please consider sponsoring this project financially via https://opencollective.com/openapi_generator or directly to [Kay Schecker](https://github.com/sponsors/kay-schecker) (the author of this tool) :pray: diff --git a/apps/generator-cli/src/app/helpers/index.ts b/apps/generator-cli/src/app/helpers/index.ts new file mode 100644 index 000000000..ec8c11f00 --- /dev/null +++ b/apps/generator-cli/src/app/helpers/index.ts @@ -0,0 +1,11 @@ +const isWin = () => process.platform === 'win32'; + +/** + * If JAVA_HOME is set, it returns `$JAVA_HOME/bin/java` + * otherwise it returns `java` and it has to be in the `PATH` + */ +export const javaCmd: string = process.env['JAVA_HOME'] + ? isWin() + ? `"${process.env['JAVA_HOME']}/bin/java"` + : `${process.env['JAVA_HOME']}/bin/java` + : 'java'; diff --git a/apps/generator-cli/src/app/services/generator.service.spec.ts b/apps/generator-cli/src/app/services/generator.service.spec.ts index 5a90c8375..e02e1eecc 100644 --- a/apps/generator-cli/src/app/services/generator.service.spec.ts +++ b/apps/generator-cli/src/app/services/generator.service.spec.ts @@ -1,6 +1,7 @@ import { Test } from '@nestjs/testing'; import { GeneratorService } from './generator.service'; import { LOGGER } from '../constants'; +import { javaCmd } from '../helpers'; import { VersionManagerService } from './version-manager.service'; import { ConfigService } from './config.service'; @@ -126,20 +127,21 @@ describe('GeneratorService', () => { }); }); - const cmd = (name, appendix: string[]) => ({ + const cmd = (name: string, javaCmd: string, appendix: string[]) => ({ name, - command: `java -jar "/path/to/4.2.1.jar" generate ${appendix.join( + command: `${javaCmd} -jar "/path/to/4.2.1.jar" generate ${appendix.join( ' ' )}`, }); const cmdWithCustomJar = ( name: string, + javaCmd: string, customJar: string, appendix: string[] ) => ({ name, - command: `java -cp "/path/to/4.2.1.jar:${customJar}" org.openapitools.codegen.OpenAPIGenerator generate ${appendix.join( + command: `${javaCmd} -cp "/path/to/4.2.1.jar:${customJar}" org.openapitools.codegen.OpenAPIGenerator generate ${appendix.join( ' ' )}`, }); @@ -148,19 +150,19 @@ describe('GeneratorService', () => { [ 'foo.json', [ - cmd('[angular] abc/app/pet.yaml', [ + cmd('[angular] abc/app/pet.yaml', javaCmd, [ `--input-spec="${cwd}/abc/app/pet.yaml"`, `--output="${cwd}/generated-sources/openapi/typescript-angular/pet"`, `--generator-name="typescript-angular"`, `--additional-properties="fileNaming=kebab-case,apiModulePrefix=Pet,npmName=petRestClient,supportsES6=true,withInterfaces=true"`, ]), - cmd('[angular] abc/app/car.yaml', [ + cmd('[angular] abc/app/car.yaml', javaCmd, [ `--input-spec="${cwd}/abc/app/car.yaml"`, `--output="${cwd}/generated-sources/openapi/typescript-angular/car"`, `--generator-name="typescript-angular"`, `--additional-properties="fileNaming=kebab-case,apiModulePrefix=Car,npmName=carRestClient,supportsES6=true,withInterfaces=true"`, ]), - cmd('[baz] def/app/pet.yaml', [ + cmd('[baz] def/app/pet.yaml', javaCmd, [ `--input-spec="${cwd}/def/app/pet.yaml"`, `--name="pet"`, `--name-uc-first="Pet"`, @@ -174,7 +176,7 @@ describe('GeneratorService', () => { '--some-bool', '--some-int=1', ]), - cmd('[baz] def/app/car.json', [ + cmd('[baz] def/app/car.json', javaCmd, [ `--input-spec="${cwd}/def/app/car.json"`, `--name="car"`, `--name-uc-first="Car"`, @@ -193,12 +195,12 @@ describe('GeneratorService', () => { [ 'bar.json', [ - cmd('[bar] api/cat.yaml', [ + cmd('[bar] api/cat.yaml', javaCmd, [ `--input-spec="${cwd}/api/cat.yaml"`, `--output="bar/cat"`, '--some-bool', ]), - cmd('[bar] api/bird.json', [ + cmd('[bar] api/bird.json', javaCmd, [ `--input-spec="${cwd}/api/bird.json"`, `--output="bar/bird"`, '--some-bool', @@ -208,16 +210,26 @@ describe('GeneratorService', () => { [ 'bar.json', [ - cmdWithCustomJar('[bar] api/cat.yaml', '../some/custom.jar', [ - `--input-spec="${cwd}/api/cat.yaml"`, - `--output="bar/cat"`, - '--some-bool', - ]), - cmdWithCustomJar('[bar] api/bird.json', '../some/custom.jar', [ - `--input-spec="${cwd}/api/bird.json"`, - `--output="bar/bird"`, - '--some-bool', - ]), + cmdWithCustomJar( + '[bar] api/cat.yaml', + javaCmd, + '../some/custom.jar', + [ + `--input-spec="${cwd}/api/cat.yaml"`, + `--output="bar/cat"`, + '--some-bool', + ] + ), + cmdWithCustomJar( + '[bar] api/bird.json', + javaCmd, + '../some/custom.jar', + [ + `--input-spec="${cwd}/api/bird.json"`, + `--output="bar/bird"`, + '--some-bool', + ] + ), ], '../some/custom.jar', ], @@ -226,7 +238,7 @@ describe('GeneratorService', () => { [ 'no-glob.json', [ - cmd('[noGlob] http://example.local/openapi.json', [ + cmd('[noGlob] http://example.local/openapi.json', javaCmd, [ `--input-spec="http://example.local/openapi.json"`, `--output="no-glob/openapi"`, `--name="openapi"`, diff --git a/apps/generator-cli/src/app/services/generator.service.ts b/apps/generator-cli/src/app/services/generator.service.ts index c94cf1b0b..327f1d4de 100644 --- a/apps/generator-cli/src/app/services/generator.service.ts +++ b/apps/generator-cli/src/app/services/generator.service.ts @@ -10,6 +10,7 @@ import * as os from 'os'; import { VersionManagerService } from './version-manager.service'; import { ConfigService } from './config.service'; import { LOGGER } from '../constants'; +import { javaCmd } from '../helpers'; interface GeneratorConfig { glob: string; @@ -241,7 +242,8 @@ export class GeneratorService { this.isWin() ? ';' : ':' )}" org.openapitools.codegen.OpenAPIGenerator` : `-jar "${cliPath}"`; - return ['java', process.env['JAVA_OPTS'], subCmd, 'generate', appendix] + + return [javaCmd, process.env['JAVA_OPTS'], subCmd, 'generate', appendix] .filter(isString) .join(' '); }; diff --git a/apps/generator-cli/src/app/services/pass-through.service.spec.ts b/apps/generator-cli/src/app/services/pass-through.service.spec.ts index 09f01ade7..f81ed1436 100644 --- a/apps/generator-cli/src/app/services/pass-through.service.spec.ts +++ b/apps/generator-cli/src/app/services/pass-through.service.spec.ts @@ -2,6 +2,7 @@ import { Test } from '@nestjs/testing'; import chalk from 'chalk'; import { Command, createCommand } from 'commander'; import { COMMANDER_PROGRAM, LOGGER } from '../constants'; +import { javaCmd } from '../helpers'; import { GeneratorService } from './generator.service'; import { PassThroughService } from './pass-through.service'; import { VersionManagerService } from './version-manager.service'; @@ -192,7 +193,7 @@ describe('PassThroughService', () => { await program.parseAsync([name, ...argv], { from: 'user' }); expect(childProcess.spawn).toHaveBeenNthCalledWith( 1, - 'java -jar "/some/path/to/4.2.1.jar"', + `${javaCmd} -jar "/some/path/to/4.2.1.jar"`, [name, ...argv], { stdio: 'inherit', @@ -206,7 +207,7 @@ describe('PassThroughService', () => { await program.parseAsync([name, ...argv], { from: 'user' }); expect(childProcess.spawn).toHaveBeenNthCalledWith( 1, - 'java java-opt-1=1 -jar "/some/path/to/4.2.1.jar"', + `${javaCmd} java-opt-1=1 -jar "/some/path/to/4.2.1.jar"`, [name, ...argv], { stdio: 'inherit', @@ -224,7 +225,7 @@ describe('PassThroughService', () => { expect(childProcess.spawn).toHaveBeenNthCalledWith( 1, - `java -cp "${[ + `${javaCmd} -cp "${[ '/some/path/to/4.2.1.jar', '../some/custom.jar', ].join(cpDelimiter)}" org.openapitools.codegen.OpenAPIGenerator`, @@ -303,7 +304,7 @@ describe('PassThroughService', () => { it('spawns the correct process', () => { expect(childProcess.spawn).toHaveBeenNthCalledWith( 1, - 'java -jar "/some/path/to/4.2.1.jar"', + `${javaCmd} -jar "/some/path/to/4.2.1.jar"`, cmd.split(' '), { stdio: 'inherit', shell: true } ); diff --git a/apps/generator-cli/src/app/services/pass-through.service.ts b/apps/generator-cli/src/app/services/pass-through.service.ts index 304ec4824..3d6825e5a 100644 --- a/apps/generator-cli/src/app/services/pass-through.service.ts +++ b/apps/generator-cli/src/app/services/pass-through.service.ts @@ -4,6 +4,7 @@ import { exec, spawn } from 'child_process'; import { Command } from 'commander'; import { isString, startsWith, trim } from 'lodash'; import { COMMANDER_PROGRAM, LOGGER } from '../constants'; +import { javaCmd } from '../helpers'; import { GeneratorService } from './generator.service'; import { VersionManagerService } from './version-manager.service'; import { ConfigService } from './config.service'; @@ -142,7 +143,7 @@ export class PassThroughService { )}" org.openapitools.codegen.OpenAPIGenerator` : `-jar "${cliPath}"`; - return ['java', process.env['JAVA_OPTS'], subCmd] + return [javaCmd, process.env['JAVA_OPTS'], subCmd] .filter(isString) .join(' '); }