Skip to content
Merged
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
21 changes: 19 additions & 2 deletions apps/generator-cli/src/app/services/generator.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ describe('GeneratorService', () => {
command: `java -jar "/path/to/4.2.1.jar" generate ${appendix.join(' ')}`,
});

const cmdWithCustomJar = (name: string, customJar: string, appendix: string[]) => ({
name,
command: `java -cp "/path/to/4.2.1.jar:${customJar}" org.openapitools.codegen.OpenAPIGenerator generate ${appendix.join(' ')}`,
});

describe.each([
['foo.json', [
cmd('[angular] abc/app/pet.yaml', [
Expand Down Expand Up @@ -183,6 +188,18 @@ describe('GeneratorService', () => {
'--some-bool',
]),
]],
['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',
]),
], '../some/custom.jar'],
['none.json', []],
['also-none.json', []],
['no-glob.json', [
Expand All @@ -200,13 +217,13 @@ describe('GeneratorService', () => {
`--ext="json"`,
]),
]],
])('%s', (filePath, expectedCommands) => {
])('%s', (filePath, expectedCommands, customGenerator) => {

let returnValue: boolean

beforeEach(async () => {
configGet.mockImplementation((path, defaultValue) => config[filePath] || defaultValue)
returnValue = await fixture.generate()
returnValue = await fixture.generate(customGenerator)
})

it('calls the config get well', () => {
Expand Down
31 changes: 19 additions & 12 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(...keys: string[]) {
public async generate(customGenerator?: string, ...keys: string[]) {

const cwd = this.configService.cwd
const generators = Object.entries(this.configService.get<{ [name: string]: GeneratorConfig }>(this.configPath, {}))
Expand All @@ -54,7 +54,7 @@ export class GeneratorService {
if (!globPattern) {
return [{
name: `[${name}] ${params.inputSpec}`,
command: this.buildCommand(cwd, params)
command: this.buildCommand(cwd, params, customGenerator)
}]
}

Expand All @@ -66,7 +66,7 @@ export class GeneratorService {

return glob.sync(globPattern, {cwd}).map(spec => ({
name: `[${name}] ${spec}`,
command: this.buildCommand(cwd, params, spec)
command: this.buildCommand(cwd, params, customGenerator, spec)
}))
}))

Expand Down Expand Up @@ -95,7 +95,7 @@ export class GeneratorService {
}).join('\n'))
}

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

const command = Object.entries({
Expand Down Expand Up @@ -139,19 +139,26 @@ export class GeneratorService {
ext: ext.split('.').slice(-1).pop()
}

return this.cmd(Object.entries(placeholders)
return this.cmd(customGenerator, Object.entries(placeholders)
.filter(([, replacement]) => !!replacement)
.reduce((cmd, [search, replacement]) => {
return cmd.split(`#{${search}}`).join(replacement)
}, command))
}

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

private isWin = () => process.platform === "win32"
}
12 changes: 12 additions & 0 deletions apps/generator-cli/src/app/services/pass-through.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,18 @@ describe('PassThroughService', () => {
)
})

if (name === 'generate') {
it('can delegate with custom jar to generate command', async () => {
await program.parseAsync([name, ...argv, '--generator-key=genKey', '--custom-generator=../some/custom.jar'], { from: 'user' })

expect(generate).toHaveBeenNthCalledWith(
1,
'../some/custom.jar',
'genKey'
)
})
}

// if (name === 'help') {
// it('prints the help info and does not delegate, if args length = 0', async () => {
// childProcess.spawn.mockReset()
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 @@ -56,11 +56,12 @@ export class PassThroughService {
.option("--generator-key <generator...>", "Run generator by key. Separate by comma to run many generators")
.action(async (_, cmd) => {
if (cmd.args.length === 0 || cmd.opts().generatorKey) {
const customGenerator = this.program.opts()?.customGenerator;
const generatorKeys = cmd.opts().generatorKey || [];

if (this.generatorService.enabled) {
// @todo cover by unit test
if (!await this.generatorService.generate(...generatorKeys)) {
if (!await this.generatorService.generate(customGenerator, ...generatorKeys)) {
this.logger.log(chalk.red('Code generation failed'));
process.exit(1);
}
Expand Down