Skip to content

Commit

Permalink
fix: allow custom generator to pass through generate command
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp committed Mar 21, 2022
1 parent 0f86410 commit 9374ae6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
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"
}
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

0 comments on commit 9374ae6

Please sign in to comment.