From 9374ae623099593e5b2f006d35240c12b144b17a Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Mon, 21 Mar 2022 15:11:11 +0100 Subject: [PATCH 1/2] fix: allow custom generator to pass through generate command --- .../src/app/services/generator.service.ts | 31 ++++++++++++------- .../src/app/services/pass-through.service.ts | 3 +- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/apps/generator-cli/src/app/services/generator.service.ts b/apps/generator-cli/src/app/services/generator.service.ts index c0b8e42ca..4b6328a15 100644 --- a/apps/generator-cli/src/app/services/generator.service.ts +++ b/apps/generator-cli/src/app/services/generator.service.ts @@ -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, {})) @@ -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) }] } @@ -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) })) })) @@ -95,7 +95,7 @@ export class GeneratorService { }).join('\n')) } - private buildCommand(cwd: string, params: Record, specFile?: string) { + private buildCommand(cwd: string, params: Record, customGenerator?: string, specFile?: string) { const absoluteSpecPath = specFile ? path.resolve(cwd, specFile) : String(params.inputSpec) const command = Object.entries({ @@ -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" } 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 9c1e45b86..510ddafd5 100644 --- a/apps/generator-cli/src/app/services/pass-through.service.ts +++ b/apps/generator-cli/src/app/services/pass-through.service.ts @@ -56,11 +56,12 @@ export class PassThroughService { .option("--generator-key ", "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); } From 67dab73cc5114595f4599ae08d3db296d342114a Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Tue, 22 Mar 2022 10:28:49 +0100 Subject: [PATCH 2/2] test(custom-jar): add unit tests --- .../app/services/generator.service.spec.ts | 21 +++++++++++++++++-- .../app/services/pass-through.service.spec.ts | 12 +++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) 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 470d7ab2b..31b70f6c6 100644 --- a/apps/generator-cli/src/app/services/generator.service.spec.ts +++ b/apps/generator-cli/src/app/services/generator.service.spec.ts @@ -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', [ @@ -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', [ @@ -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', () => { 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 b9e6bb6fe..205c1f07d 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 @@ -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()