Skip to content

Commit 3089895

Browse files
authored
Merge pull request #620 from algolia/master
fix(#439): allow custom generator to be passed to the generate command
2 parents 031b13a + 67dab73 commit 3089895

File tree

4 files changed

+52
-15
lines changed

4 files changed

+52
-15
lines changed

apps/generator-cli/src/app/services/generator.service.spec.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,11 @@ describe('GeneratorService', () => {
127127
command: `java -jar "/path/to/4.2.1.jar" generate ${appendix.join(' ')}`,
128128
});
129129

130+
const cmdWithCustomJar = (name: string, customJar: string, appendix: string[]) => ({
131+
name,
132+
command: `java -cp "/path/to/4.2.1.jar:${customJar}" org.openapitools.codegen.OpenAPIGenerator generate ${appendix.join(' ')}`,
133+
});
134+
130135
describe.each([
131136
['foo.json', [
132137
cmd('[angular] abc/app/pet.yaml', [
@@ -182,6 +187,18 @@ describe('GeneratorService', () => {
182187
'--some-bool',
183188
]),
184189
]],
190+
['bar.json', [
191+
cmdWithCustomJar('[bar] api/cat.yaml', '../some/custom.jar', [
192+
`--input-spec="${cwd}/api/cat.yaml"`,
193+
`--output="bar/cat"`,
194+
'--some-bool',
195+
]),
196+
cmdWithCustomJar('[bar] api/bird.json', '../some/custom.jar', [
197+
`--input-spec="${cwd}/api/bird.json"`,
198+
`--output="bar/bird"`,
199+
'--some-bool',
200+
]),
201+
], '../some/custom.jar'],
185202
['none.json', []],
186203
['also-none.json', []],
187204
['no-glob.json', [
@@ -199,13 +216,13 @@ describe('GeneratorService', () => {
199216
`--ext="json"`,
200217
]),
201218
]],
202-
])('%s', (filePath, expectedCommands) => {
219+
])('%s', (filePath, expectedCommands, customGenerator) => {
203220

204221
let returnValue: boolean
205222

206223
beforeEach(async () => {
207224
configGet.mockImplementation((path, defaultValue) => config[filePath] || defaultValue)
208-
returnValue = await fixture.generate()
225+
returnValue = await fixture.generate(customGenerator)
209226
})
210227

211228
it('calls the config get well', () => {

apps/generator-cli/src/app/services/generator.service.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class GeneratorService {
2929
) {
3030
}
3131

32-
public async generate(...keys: string[]) {
32+
public async generate(customGenerator?: string, ...keys: string[]) {
3333

3434
const cwd = this.configService.cwd
3535
const generators = Object.entries(this.configService.get<{ [name: string]: GeneratorConfig }>(this.configPath, {}))
@@ -54,7 +54,7 @@ export class GeneratorService {
5454
if (!globPattern) {
5555
return [{
5656
name: `[${name}] ${params.inputSpec}`,
57-
command: this.buildCommand(cwd, params)
57+
command: this.buildCommand(cwd, params, customGenerator)
5858
}]
5959
}
6060

@@ -66,7 +66,7 @@ export class GeneratorService {
6666

6767
return glob.sync(globPattern, {cwd}).map(spec => ({
6868
name: `[${name}] ${spec}`,
69-
command: this.buildCommand(cwd, params, spec)
69+
command: this.buildCommand(cwd, params, customGenerator, spec)
7070
}))
7171
}))
7272

@@ -95,7 +95,7 @@ export class GeneratorService {
9595
}).join('\n'))
9696
}
9797

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

101101
const command = Object.entries({
@@ -139,19 +139,26 @@ export class GeneratorService {
139139
ext: ext.split('.').slice(-1).pop()
140140
}
141141

142-
return this.cmd(Object.entries(placeholders)
142+
return this.cmd(customGenerator, Object.entries(placeholders)
143143
.filter(([, replacement]) => !!replacement)
144144
.reduce((cmd, [search, replacement]) => {
145145
return cmd.split(`#{${search}}`).join(replacement)
146146
}, command))
147147
}
148148

149-
private cmd = (appendix: string) => [
150-
'java',
151-
process.env['JAVA_OPTS'],
152-
`-jar "${this.versionManager.filePath()}"`,
153-
'generate',
154-
appendix,
155-
].filter(isString).join(' ');
149+
private cmd = (customGenerator: string | undefined, appendix: string) => {
150+
const cliPath = this.versionManager.filePath();
151+
const subCmd = customGenerator
152+
? `-cp "${[cliPath, customGenerator].join(this.isWin() ? ';' : ':')}" org.openapitools.codegen.OpenAPIGenerator`
153+
: `-jar "${cliPath}"`;
154+
return [
155+
'java',
156+
process.env['JAVA_OPTS'],
157+
subCmd,
158+
'generate',
159+
appendix,
160+
].filter(isString).join(' ');
161+
}
156162

163+
private isWin = () => process.platform === "win32"
157164
}

apps/generator-cli/src/app/services/pass-through.service.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,18 @@ describe('PassThroughService', () => {
189189
)
190190
})
191191

192+
if (name === 'generate') {
193+
it('can delegate with custom jar to generate command', async () => {
194+
await program.parseAsync([name, ...argv, '--generator-key=genKey', '--custom-generator=../some/custom.jar'], { from: 'user' })
195+
196+
expect(generate).toHaveBeenNthCalledWith(
197+
1,
198+
'../some/custom.jar',
199+
'genKey'
200+
)
201+
})
202+
}
203+
192204
// if (name === 'help') {
193205
// it('prints the help info and does not delegate, if args length = 0', async () => {
194206
// childProcess.spawn.mockReset()

apps/generator-cli/src/app/services/pass-through.service.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,12 @@ export class PassThroughService {
5656
.option("--generator-key <generator...>", "Run generator by key. Separate by comma to run many generators")
5757
.action(async (_, cmd) => {
5858
if (cmd.args.length === 0 || cmd.opts().generatorKey) {
59+
const customGenerator = this.program.opts()?.customGenerator;
5960
const generatorKeys = cmd.opts().generatorKey || [];
6061

6162
if (this.generatorService.enabled) {
6263
// @todo cover by unit test
63-
if (!await this.generatorService.generate(...generatorKeys)) {
64+
if (!await this.generatorService.generate(customGenerator, ...generatorKeys)) {
6465
this.logger.log(chalk.red('Code generation failed'));
6566
process.exit(1);
6667
}

0 commit comments

Comments
 (0)