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

Generator service support custom #414

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 16 additions & 1 deletion apps/generator-cli/src/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,22 @@ is automatically used to generate your code. 🎉

## Custom Generators

Custom generators can be used by passing the `--custom-generator=/my/custom-generator.jar` argument.
Custom generators can be used by passing the `--custom-generator=my/custom-generator.jar` argument. Alternatively, use a `customJarPath` property in *openapitools.json* as shown here.

```json5
{
...
"generators": {
...
"myCustom": {
"generatorName": "my-custom-generator",
"customJarPath": "my/custom-generator.jar",
...
}
}
...
}
```

## Further Documentation

Expand Down
32 changes: 28 additions & 4 deletions apps/generator-cli/src/app/services/generator.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ describe('GeneratorService', () => {
someBool: false,
},
},
['bar-custom-generator.json']: {
bar: {
glob: 'bar/abc/**/*.yaml',
output: 'bar/#{name}',
customJarPath: 'path/to/custom-generators.jar',
},
},
['no-glob.json']: {
noGlob: {
inputSpec: 'http://example.local/openapi.json',
Expand Down Expand Up @@ -123,10 +130,17 @@ describe('GeneratorService', () => {
})
})

const cmd = (name, appendix: string[]) => ({
name,
command: `java -jar "/path/to/4.2.1.jar" generate ${appendix.join(' ')}`,
});
const cmd = (name, appendix: string[], customJarPath?: string) => {
const cliPath = '/path/to/4.2.1.jar'
const cpDelimiter = process.platform === "win32" ? ';' : ':';
const subCmd = customJarPath
? `-cp "${[cliPath, customJarPath].join(cpDelimiter)}" org.openapitools.codegen.OpenAPIGenerator`
: `-jar "${cliPath}"`
return {
name,
command: `java ${subCmd} generate ${appendix.join(' ')}`,
}
};

describe.each([
['foo.json', [
Expand Down Expand Up @@ -183,6 +197,16 @@ describe('GeneratorService', () => {
'--some-bool',
]),
]],
['bar-custom-generator.json', [
cmd('[bar] api/cat.yaml', [
`--input-spec="${cwd}/api/cat.yaml"`,
`--output="bar/cat"`,
], 'path/to/custom-generators.jar'),
cmd('[bar] api/bird.json', [
`--input-spec="${cwd}/api/bird.json"`,
`--output="bar/bird"`,
], 'path/to/custom-generators.jar'),
]],
['none.json', []],
['also-none.json', []],
['no-glob.json', [
Expand Down
36 changes: 25 additions & 11 deletions apps/generator-cli/src/app/services/generator.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class GeneratorService {
) {
}

public async generate() {
public async generate(customJarPath?: string) {

const cwd = this.configService.cwd
const generators = Object.entries(this.configService.get<{ [name: string]: GeneratorConfig }>(this.configPath, {}))
Expand Down Expand Up @@ -85,7 +85,7 @@ export class GeneratorService {
}).join('\n'))
}

private buildCommand(cwd: string, params: Record<string, unknown>, specFile?: string) {
private buildCommand(cwd: string, params: Record<string, unknown>, specFile?: string, customJarPath?: string) {
const absoluteSpecPath = specFile ? path.resolve(cwd, specFile) : String(params.inputSpec)

const command = Object.entries({
Expand All @@ -94,6 +94,12 @@ export class GeneratorService {
}).map(([k, v]) => {

const key = kebabCase(k)

if (key === 'custom-jar-path') {
customJarPath = v
return ''
}

const value = (() => {
switch (typeof v) {
case 'object':
Expand All @@ -109,7 +115,7 @@ export class GeneratorService {
})()

return value === undefined ? `--${key}` : `--${key}=${value}`
}).join(' ')
}).filter(arg => arg).join(' ')

const ext = path.extname(absoluteSpecPath)
const name = path.basename(absoluteSpecPath, ext)
Expand All @@ -133,15 +139,23 @@ export class GeneratorService {
.filter(([, replacement]) => !!replacement)
.reduce((cmd, [search, replacement]) => {
return cmd.split(`#{${search}}`).join(replacement)
}, command))
}, command), customJarPath)
}

private cmd = (appendix: string) => [
'java',
process.env['JAVA_OPTS'],
`-jar "${this.versionManager.filePath()}"`,
'generate',
appendix,
].filter(isString).join(' ');
private cmd = (appendix: string, customJarPath?: string) => {
const cliPath = this.versionManager.filePath();
const cpDelimiter = process.platform === "win32" ? ';' : ':';
const subCmd = customJarPath
? `-cp "${[cliPath, customJarPath].join(cpDelimiter)}" org.openapitools.codegen.OpenAPIGenerator`
: `-jar "${cliPath}"`;

return [
'java',
process.env['JAVA_OPTS'],
subCmd,
'generate',
appendix,
].filter(isString).join(' ');
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ describe('PassThroughService', () => {
cmdMock.args = []
commandMock.commands[cmd].action(cmdMock)
expect(childProcess.spawn).toBeCalledTimes(0)
expect(generate).toHaveBeenNthCalledWith(1)
expect(generate).toBeCalledTimes(1);
})
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class PassThroughService {
return;
case 'generate':
if (this.generatorService.enabled) {
if (!await this.generatorService.generate()) {
if (!await this.generatorService.generate(cmd.opts().customGenerator)) {
this.logger.log(chalk.red('Code generation failed'));
process.exit(1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { mocked } from 'ts-jest/utils';
import { LOGGER } from '../constants';
import * as chalk from 'chalk';
import { ConfigService } from './config.service';
import { resolve } from 'path';
import { resolve, join } from 'path';
import * as os from 'os';
import { TestingModule } from '@nestjs/testing/testing-module';

Expand Down Expand Up @@ -442,7 +442,7 @@ describe('VersionManagerService', () => {


it('creates a temporary directory', () => {
expect(fs.mkdtempSync).toHaveBeenNthCalledWith(1, '/tmp/generator-cli-');
expect(fs.mkdtempSync).toHaveBeenNthCalledWith(1, join(os.tmpdir(), 'generator-cli-'));
});

it('creates the correct write stream', () => {
Expand Down
3 changes: 3 additions & 0 deletions apps/generator-cli/src/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@
"type": "boolean",
"default": false
},
"customJarPath": {
"type": "string"
},
"generatorName": {
"description": "generator to use (see list command for list)",
"anyOf": [
Expand Down