Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#758): add path.join to build os-dependent java binary path #759

Open
wants to merge 66 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
dbb010b
support JAVA_HOME environment variable
adnbrownie Jan 6, 2023
b430560
Merge branch 'master' into patch-2
mok-liee Feb 28, 2024
ac4a81c
test(generator-cmd): add consideration of the JAVA_HOME variable
mok-liee Feb 28, 2024
f96430c
docs: add instructions for the use of JAVA_HOME
mok-liee Feb 28, 2024
ce2ac4c
fix: add quotes to accept spaces in the JAVA_HOME path
mok-liee Feb 28, 2024
a5724ae
fix: add quotation marks only for windows paths
mok-liee Feb 29, 2024
00fda13
ci: add windows-latest to build workflow
mok-liee Mar 1, 2024
25fb817
ci: add windows condition to test script
mok-liee Mar 1, 2024
2371c0a
fix: use path.join for generating java bin path
mok-liee Mar 5, 2024
a708d4b
Merge branch 'master' into patch-2
mok-liee Mar 5, 2024
5f0dbf8
fix: add surrounding quotation marks for the whole concurrently command
mok-liee Mar 5, 2024
8c3eb47
fix: remove quotation marks for non-windows systems
mok-liee Mar 5, 2024
7538ffa
Merge branch 'patch-2' into feature/add-windows-to-build
mok-liee Mar 5, 2024
15956c1
chore(windows-build): try powershell commands
mok-liee Mar 5, 2024
9a23c8a
chore(windows-build): try powershell commands
mok-liee Mar 5, 2024
c39ef8a
chore(windows-build): switch to Java 17 on windows build
mok-liee Mar 5, 2024
960b21e
chore(windows-build): add test case without JAVA_HOME
mok-liee Mar 5, 2024
15ac43e
chore(config.service): use process-cwd() for windows
mok-liee Mar 6, 2024
2a9aef9
Revert "support JAVA_HOME environment variable (#756)" (#761)
wing328 Mar 6, 2024
7e43a4e
feat(release): trigger a release
wing328 Mar 6, 2024
805bb0b
Merge pull request #2 from mok-liee/feature/add-windows-to-build
mok-liee Mar 6, 2024
04007fd
chore(windows-build): remove JAVA_HOME env var correctly
mok-liee Mar 6, 2024
34dd0de
chore(deps): update dependency prettier to v3 (#749)
renovate[bot] Mar 12, 2024
4042a6e
chore(deps): update dependency @types/jest to v29.5.12 (#755)
renovate[bot] Mar 12, 2024
2092e90
chore(deps): update dependency jest-environment-jsdom to v29 (#748)
renovate[bot] Mar 12, 2024
b115451
chore(deps): update dependency ts-jest to v29.1.2 (#764)
renovate[bot] Mar 12, 2024
fcb2f83
chore(deps): update commitlint monorepo to v19 (#744)
renovate[bot] Mar 12, 2024
ad97182
feat(release): trigger a release
wing328 Mar 12, 2024
f980618
chore(deps): update dependency dotenv to v16.4.5 (#767)
renovate[bot] Mar 12, 2024
bcd1ff6
chore(deps): update dependency eslint to ~8.57.0 (#768)
renovate[bot] Mar 12, 2024
2178d3c
support JAVA_HOME environment variable (#756)
mok-liee Mar 3, 2024
583bde9
chore(deps): update dependency @types/lodash to v4.17.0 (#769)
renovate[bot] Mar 13, 2024
bc85db4
chore(deps): update nrwl monorepo to v17.3.2 (#771)
renovate[bot] Mar 17, 2024
4b058b6
chore(deps): update dependency @commitlint/cli to v19.2.0 (#772)
renovate[bot] Mar 18, 2024
f6dcb95
fix(deps): update dependency reflect-metadata to v0.2.1 (#773)
renovate[bot] Mar 19, 2024
7f41abc
chore(deps): update dependency husky to v9 (#774)
renovate[bot] Mar 19, 2024
c042887
chore(deps): update dependency type-fest to v4 (#775)
renovate[bot] Mar 19, 2024
1d2a1a2
feat(#644): add cli option for openapitools.json (#754)
nicholasphair Mar 19, 2024
fb01b0b
Revert "fix(deps): update dependency reflect-metadata to v0.2.1 (#773…
wing328 Mar 19, 2024
7787f07
fix(app): configure http with proxy url if present in environment (#762)
jontze Apr 16, 2024
5dff424
fix(deps): update dependency axios to v1.6.8 (#766)
renovate[bot] Apr 21, 2024
5f00242
chore(deps): update commitlint monorepo to v19.2.2 (#778)
renovate[bot] Apr 21, 2024
37597a2
fix(deps): update dependency @nestjs/axios to v3.0.2 (#765)
renovate[bot] Apr 22, 2024
76dfb19
Update README.md with JDK version required
wing328 Jun 28, 2024
6b76bbb
Update README.md with openjdk
wing328 Jun 28, 2024
18abce4
fix(deps): update dependency axios to v1.7.4 [security] (#789)
renovate[bot] Aug 20, 2024
67f6457
update versions mentioned in Readme (#795)
wing328 Sep 4, 2024
45e1633
docs: how to use local JARs and `SNAPSHOT`builds. (#794)
joscha Sep 4, 2024
41585e7
support JAVA_HOME environment variable
adnbrownie Sep 5, 2024
bc9352d
test(generator-cmd): add consideration of the JAVA_HOME variable
mok-liee Sep 5, 2024
1a94743
docs: add instructions for the use of JAVA_HOME
mok-liee Sep 5, 2024
26cde5d
fix: add quotes to accept spaces in the JAVA_HOME path
mok-liee Sep 5, 2024
47b0abb
fix: add quotation marks only for windows paths
mok-liee Sep 5, 2024
55ad5be
ci: add windows-latest to build workflow
mok-liee Sep 5, 2024
aadd8ed
ci: add windows condition to test script
mok-liee Sep 5, 2024
2dc0c8e
fix: use path.join for generating java bin path
mok-liee Sep 5, 2024
5eb3b82
fix: add surrounding quotation marks for the whole concurrently command
mok-liee Sep 5, 2024
9d8c2f6
fix: remove quotation marks for non-windows systems
mok-liee Sep 5, 2024
4c35653
chore(windows-build): try powershell commands
mok-liee Sep 5, 2024
29f99a6
chore(windows-build): try powershell commands
mok-liee Sep 5, 2024
adc00ea
chore(windows-build): switch to Java 17 on windows build
mok-liee Sep 5, 2024
f08c18f
chore(windows-build): add test case without JAVA_HOME
mok-liee Sep 5, 2024
f17d771
chore(config.service): use process-cwd() for windows
mok-liee Sep 5, 2024
78e6f06
chore(windows-build): remove JAVA_HOME env var correctly
mok-liee Sep 5, 2024
55be5d0
support JAVA_HOME environment variable (#756)
mok-liee Sep 5, 2024
4aba432
Merge branch 'patch-2' of https://github.com/mok-liee/openapi-generat…
mok-liee Sep 5, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 39 additions & 8 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@ on:
- master
- renovate/**
pull_request:
types: [ assigned, opened, synchronize, reopened ]
types: [assigned, opened, synchronize, reopened]
jobs:
build:
name: Build
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [ 18.x ]
node-version: [18.x]

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Use Node.js ${{matrix.node-version }}
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{matrix.node-version }}
node-version: ${{ matrix.node-version }}

- name: Install
run: yarn --frozen-lockfile
Expand All @@ -48,13 +48,13 @@ jobs:
path: dist/apps/package.tgz

e2e:
name: "E2E local: (${{ matrix.os }})"
name: 'E2E local: (${{ matrix.os }})'
needs: build
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ ubuntu-latest, macos-latest ]
node-version: [ 18.x ]
os: [ubuntu-latest, macos-latest, windows-latest]
node-version: [18.x]

steps:
- name: Checkout
Expand All @@ -69,7 +69,35 @@ jobs:
with:
name: package.tgz

- name: Test
- if: ${{matrix.os == 'windows-latest'}}
name: Test Windows
run: |
$env:JAVA_HOME = $env:JAVA_HOME_11_X64
cd .\examples
yarn global add json; $yarn_global_bin=yarn global bin; $env:PATH += ";$yarn_global_bin"
yarn cache clean; yarn add $env:GITHUB_WORKSPACE\package.tgz
npm run oa version
npm run oa completion
npm run oa help
npm run oa help generate
npm run oa version-manager help
npm run oa generate -- -g ruby -i https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/modules/openapi-generator/src/test/resources/3_0/petstore.yaml -o $env:GITHUB_WORKSPACE\tmp\ruby-client
npm run oa version-manager set 3.0.0; $version=npm run oa version; if (-not ($version.Contains('3.0.0'))) { exit 1 }
npm run oa version-manager set 4.0; $version=npm run oa version; if (-not ($version.Contains('4.0.3'))) { exit 1 }
npm run oa version-manager set 4.3.1; $version=npm run oa version; if (-not ($version.Contains('4.3.1'))) { exit 1 }
npm run oa version-manager set 4.3.1; $version=npm run oa version; if (-not ($version.Contains('4.3.1'))) { exit 1 }
json -I -f openapitools.json -e "this['generator-cli']['storageDir']='./my/storage/'"
npm run oa version-manager set 4.3.0; $version=npm run oa version; if (-not ($version.Contains('4.3.0'))) { exit 1 }
if (-not (Test-Path .\my\storage\4.3.0.jar)) { exit 1 }
json -I -f openapitools.json -e "this['generator-cli']['storageDir']='~/my/storage/'"
npm run oa version-manager set 4.3.1; $version=npm run oa version; if (-not ($version.Contains('4.3.1'))) { exit 1 }
if (-not (Test-Path ~\my\storage\4.3.1.jar)) { exit 1 }
npm run oa:generate; mkdir .\foo; cd .\foo; npm run oa:generate
cd ..
ri env:JAVA_HOME
npm run oa:generate; mkdir .\bar; cd .\bar; npm run oa:generate
- if: ${{matrix.os != 'windows-latest'}}
name: Test Ubuntu/MacOS
run: |
cd ./examples
yarn global add json && export PATH="$(yarn global bin):$PATH"
Expand All @@ -91,6 +119,9 @@ jobs:
(npm run oa version-manager set 4.3.1 && npm run oa version | grep -q '4.3.1') || exit 1
test -f ~/my/storage/4.3.1.jar || exit 1
npm run oa:generate && mkdir ./foo && cd ./foo && npm run oa:generate
cd ..
unset JAVA_HOME
npm run oa:generate && mkdir ./bar && cd ./bar && npm run oa:generate

# release:
# if: github.event.pull_request.merged == 'true'
Expand Down
50 changes: 44 additions & 6 deletions 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` or have set `JAVA_HOME` correctly 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. (JDK 11 is the minimal version supported. To install OpenJDK, please visit https://adoptium.net/)

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 Expand Up @@ -115,7 +115,7 @@ Initially the file has the following content:
"$schema": "node_modules/@openapitools/openapi-generator-cli/config.schema.json",
"spaces": 2,
"generator-cli": {
"version": "4.3.1" // or the current latest version ;)
"version": "7.8.0" // or the current latest version ;)
}
}
```
Expand All @@ -124,7 +124,7 @@ This configuration indicates the following:

- the json file shall be formatted using **2 spaces**
- the jar files shall be downloaded to *./my/custom/storage/dir*
- the generator-cli version 4.3.1 is used
- the generator-cli version 7.8.0 is used

Further it is also possible to configure generators, for example:

Expand All @@ -133,7 +133,7 @@ Further it is also possible to configure generators, for example:
"$schema": "node_modules/@openapitools/openapi-generator-cli/config.schema.json",
"spaces": 2,
"generator-cli": {
"version": "4.3.1",
"version": "7.8.0",
"storageDir": "~/my/custom/storage/dir", // optional
"generators": { // optional
"v2.0": { // any name you like (just printed to the console log or reference it using --generator-key)
Expand Down Expand Up @@ -185,7 +185,7 @@ If you're using a private maven registry you can configure the `downloadUrl` and
"$schema": "node_modules/@openapitools/openapi-generator-cli/config.schema.json",
"spaces": 2,
"generator-cli": {
"version": "5.3.0",
"version": "7.8.0",
"repository": {
"queryUrl": "https://private.maven.intern/solrsearch/select?q=g:${group.id}+AND+a:${artifact.id}&core=gav&start=0&rows=200",
"downloadUrl": "https://private.maven.intern/maven2/${groupId}/${artifactId}/${versionName}/${artifactId}-${versionName}.jar"
Expand All @@ -196,6 +196,44 @@ If you're using a private maven registry you can configure the `downloadUrl` and

If the `version` property param is set it is not necessary to configure the `queryUrl`.

### Use locally built JAR
In order to use a locally built jar of the generator CLI, you can copy the jar from your local build (i.e. if you were to `build` the [OpenAPITools/openapi-generator](https://github.com/OpenAPITools/openapi-generator) repository it would be in `~/openapi-generator/modules/openapi-generator-cli/target/openapi-generator-cli.jar`) into `./node_modules/@openapitools/openapi-generator-cli/versions/` and change the `version` in the `openapitools.json` file to the base name of the jar file.
E.g.:
```sh
cd openapi-generator
./mvnw clean package
cp ./modules/openapi-generator-cli/target/openapi-generator-cli.jar /your/project/node_modules/@openapitools/openapi-generator-cli/versions/my-local-snapshot.jar
```
and then:
```json
{
"$schema": "./node_modules/@openapitools/openapi-generator-cli/config.schema.json",
"spaces": 2,
"generator-cli": {
"version": "my-local-snapshot",
}
}
```

### Use nightly `SNAPSHOT` build
Change your `openapitools.json` to:

```json
{
"$schema": "node_modules/@openapitools/openapi-generator-cli/config.schema.json",
"spaces": 2,
"generator-cli": {
"version": "7.9.0-20240829.123431-22",
"repository": {
"downloadUrl": "https://oss.sonatype.org/content/repositories/snapshots/org/openapitools/openapi-generator-cli/7.9.0-SNAPSHOT/openapi-generator-cli-${versionName}.jar"
}
}
}
```

Example is with a snapshot of `7.9.0`, please change the `version` and `downloadUrl` accordingly.
You can find all snapshots [here](https://oss.sonatype.org/content/repositories/snapshots/org/openapitools/openapi-generator-cli/).

## Run specific generators

| cmd | v3.0 runs | v2.0 runs |
Expand Down Expand Up @@ -231,7 +269,7 @@ more information about the possible arguments and a detailed usage manual of the

```sh
npm install @openapitools/openapi-generator-cli@previous
npm i @openapitools/openapi-generator-cli@1.0.18-4.3.1
npm i @openapitools/openapi-generator-cli@1.0.18-4.3.1
```

or using yarn
Expand Down
60 changes: 40 additions & 20 deletions apps/generator-cli/src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
import {Inject, Module, OnApplicationBootstrap} from '@nestjs/common';
import {HttpModule} from '@nestjs/axios';
import {Command} from 'commander';
import { Inject, Module, OnApplicationBootstrap } from '@nestjs/common';
import { HttpModule, HttpModuleOptions } from '@nestjs/axios';
import { Command } from 'commander';

import {COMMANDER_PROGRAM, LOGGER} from './constants';
import {VersionManagerController} from './controllers/version-manager.controller';
import {ConfigService, GeneratorService, PassThroughService, UIService, VersionManagerService} from './services';
import { COMMANDER_PROGRAM, LOGGER } from './constants';
import { VersionManagerController } from './controllers/version-manager.controller';
import {
ConfigService,
GeneratorService,
PassThroughService,
UIService,
VersionManagerService,
} from './services';
import { HttpsProxyAgent } from 'https-proxy-agent';

const proxyUrl = process.env.HTTP_PROXY || process.env.HTTPS_PROXY;
const httpModuleConfig: HttpModuleOptions = {};

if (proxyUrl) {
httpModuleConfig.proxy = false;
httpModuleConfig.httpsAgent = new HttpsProxyAgent(proxyUrl);
}

@Module({
imports: [HttpModule],
controllers: [
VersionManagerController
imports: [
HttpModule.register({
...httpModuleConfig,
}),
],
controllers: [VersionManagerController],
providers: [
UIService,
ConfigService,
Expand All @@ -19,34 +36,37 @@ import {ConfigService, GeneratorService, PassThroughService, UIService, VersionM
VersionManagerService,
{
provide: COMMANDER_PROGRAM,
useValue: new Command('openapi-generator-cli').helpOption(false).usage('<command> [<args>]')
useValue: new Command('openapi-generator-cli')
.helpOption(false)
.usage('<command> [<args>]')
.option(
'--openapitools <openapitools.json>',
'Use the specified openapi-generator-cli configuration file',
),
},
{provide: LOGGER, useValue: console}
]
{ provide: LOGGER, useValue: console },
],
})
export class AppModule implements OnApplicationBootstrap {

constructor(
@Inject(COMMANDER_PROGRAM) private readonly program: Command,
private readonly versionManager: VersionManagerService,
private readonly passThroughService: PassThroughService
) {
}
private readonly passThroughService: PassThroughService,
) {}

onApplicationBootstrap = async () => {

let selectedVersion = this.versionManager.getSelectedVersion();

if (!selectedVersion) {
const [{version}] = await this.versionManager.search(['latest']).toPromise();
const [{ version }] = await this.versionManager
.search(['latest'])
.toPromise();
await this.versionManager.setSelectedVersion(version);
selectedVersion = version;
}

await this.versionManager.downloadIfNeeded(selectedVersion);
await this.passThroughService.init();
this.program.parse(process.argv);

};

}
8 changes: 5 additions & 3 deletions apps/generator-cli/src/app/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const isWin = () => process.platform === 'win32';
import path from 'path';

export 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`
? `"${path.join(process.env['JAVA_HOME'], 'bin', 'java')}"`
: `${path.join(process.env['JAVA_HOME'], 'bin', 'java')}`
: 'java';
47 changes: 45 additions & 2 deletions apps/generator-cli/src/app/services/config.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
import { Test } from '@nestjs/testing';
import { Command, createCommand } from 'commander';
import { ConfigService } from './config.service';
import { LOGGER } from '../constants';
import { LOGGER, COMMANDER_PROGRAM } from '../constants';

jest.mock('fs-extra');
// eslint-disable-next-line @typescript-eslint/no-var-requires
const fs = jest.mocked(require('fs-extra'));

describe('ConfigService', () => {
let fixture: ConfigService;
let program: Command;

const log = jest.fn();

beforeEach(async () => {
program = createCommand();
jest.spyOn(program, 'helpInformation');

const moduleRef = await Test.createTestingModule({
providers: [ConfigService, { provide: LOGGER, useValue: { log } }],
providers: [
ConfigService,
{ provide: LOGGER, useValue: { log } },
{ provide: COMMANDER_PROGRAM, useValue: program },
],
}).compile();

fixture = moduleRef.get(ConfigService);
Expand Down Expand Up @@ -141,5 +150,39 @@ describe('ConfigService', () => {
);
});
});

describe('configFileOrDefault()', () => {
describe('--openapitools set', () => {
beforeEach(async () => {
program = createCommand();
program.opts().openapitools = '/tmp/myopenapitools.json';

const moduleRef = await Test.createTestingModule({
providers: [
ConfigService,
{ provide: LOGGER, useValue: { log } },
{ provide: COMMANDER_PROGRAM, useValue: program },
],
}).compile();

fixture = moduleRef.get(ConfigService);
fs.writeJSONSync.mockReset();
fs.readJSONSync.mockReset();
fs.ensureFileSync.mockReset();
});
it('returns path set at cli, if openapitools argument provided', () => {
expect(fixture.configFile).toEqual('/tmp/myopenapitools.json');
});
});
describe('--openapitools not set', () => {
it('returns default path, if openapitools argument not provided', () => {
expect(
fixture.configFile.endsWith(
'openapi-generator-cli/openapitools.json'
)
).toBeTruthy();
});
});
});
});
});
Loading
Loading