Skip to content

Commit

Permalink
support JAVA_HOME environment variable (#756)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
mok-liee and adnbrownie authored Mar 3, 2024
1 parent 720b405 commit 3e949d8
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 27 deletions.
2 changes: 1 addition & 1 deletion apps/generator-cli/src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
11 changes: 11 additions & 0 deletions apps/generator-cli/src/app/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -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';
52 changes: 32 additions & 20 deletions apps/generator-cli/src/app/services/generator.service.spec.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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(
' '
)}`,
});
Expand All @@ -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"`,
Expand All @@ -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"`,
Expand All @@ -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',
Expand All @@ -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',
],
Expand All @@ -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"`,
Expand Down
4 changes: 3 additions & 1 deletion apps/generator-cli/src/app/services/generator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(' ');
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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',
Expand All @@ -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',
Expand All @@ -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`,
Expand Down Expand Up @@ -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 }
);
Expand Down
3 changes: 2 additions & 1 deletion apps/generator-cli/src/app/services/pass-through.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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(' ');
}
Expand Down

0 comments on commit 3e949d8

Please sign in to comment.