From e06a3d605f8df5e02251502d36670a5ce99e72cf Mon Sep 17 00:00:00 2001 From: John Kaster Date: Thu, 1 Jul 2021 00:43:52 +0000 Subject: [PATCH] chore: noStreams skips tally headers in generated source When the `-n` option is used for the code generator, the tally comments at the top of each generated source file are also removed. Consider it "no comment" as well as "no streams" --- packages/sdk-codegen-scripts/src/sdkGen.ts | 10 +-- .../sdk-codegen-scripts/src/sdkGenerator.ts | 63 ++++++++++--------- 2 files changed, 37 insertions(+), 36 deletions(-) diff --git a/packages/sdk-codegen-scripts/src/sdkGen.ts b/packages/sdk-codegen-scripts/src/sdkGen.ts index 89193462f..2f71bfe39 100644 --- a/packages/sdk-codegen-scripts/src/sdkGen.ts +++ b/packages/sdk-codegen-scripts/src/sdkGen.ts @@ -103,20 +103,20 @@ export const writeCodeFile = (fileName: string, content: string): string => { if (gen.useFunctions) { log(`generating ${api} functions ...`) const s = new FunctionGenerator(apiModel, gen) - const output = s.render(gen.indentStr) + const output = s.render(gen.indentStr, noStreams) writeCodeFile(gen.sdkFileName(`funcs`), output) } if (gen.useInterfaces) { log(`generating ${api} interfaces ...`) const s = new InterfaceGenerator(apiModel, gen) - const output = s.render(gen.indentStr) + const output = s.render(gen.indentStr, noStreams) writeCodeFile(gen.sdkFileName(`methodsInterface`), output) } // Generate standard method declarations const sdk = new MethodGenerator(apiModel, gen) - let output = sdk.render(gen.indentStr) + let output = sdk.render(gen.indentStr, noStreams) writeCodeFile(gen.sdkFileName(`methods`), output) if (gen.willItStream) { @@ -126,14 +126,14 @@ export const writeCodeFile = (fileName: string, content: string): string => { // Generate streaming method declarations log(`generating ${api} streaming methods ...`) const s = new StreamGenerator(apiModel, gen) - const output = s.render(gen.indentStr) + const output = s.render(gen.indentStr, noStreams) writeCodeFile(gen.sdkFileName(`streams`), output) } } log(`generating ${api} models ...`) const types = new TypeGenerator(apiModel, gen) - output = types.render('') + output = types.render('', noStreams) writeCodeFile(gen.sdkFileName(`models`), output) if (api === lastApi) { formatter.versionStamp(gen) diff --git a/packages/sdk-codegen-scripts/src/sdkGenerator.ts b/packages/sdk-codegen-scripts/src/sdkGenerator.ts index 6c6392afc..176f5b5d0 100644 --- a/packages/sdk-codegen-scripts/src/sdkGenerator.ts +++ b/packages/sdk-codegen-scripts/src/sdkGenerator.ts @@ -53,27 +53,27 @@ export abstract class Generator { this.codeFormatter = formatter } - // convenience function that calls render for each item in the list - // and collects their output in the buffer - each( - list: Array, - ctor: IGeneratorCtor, - indent = '', - delimiter?: string - ): this { - const strs = list.map((model) => { - // eslint-disable-next-line new-cap - return new ctor(model, this.codeFormatter).render(indent) - }) - if (delimiter) { - this.p(strs.join(delimiter)) - } else { - this.p(strs) - } - return this - } + // // convenience function that calls render for each item in the list + // // and collects their output in the buffer + // each( + // list: Array, + // ctor: IGeneratorCtor, + // indent = '', + // delimiter?: string + // ): this { + // const strs = list.map((model) => { + // // eslint-disable-next-line new-cap + // return new ctor(model, this.codeFormatter).render(indent) + // }) + // if (delimiter) { + // this.p(strs.join(delimiter)) + // } else { + // this.p(strs) + // } + // return this + // } - abstract render(indent: string): string + abstract render(indent: string, noStreams: boolean): string // Add one or more strings to the internal buffer // if the string is not empty or undefined @@ -84,13 +84,6 @@ export abstract class Generator { return this } - // pIf(expr: any, str?: string | string[]): this { - // if (expr) { - // this.p(str) - // } - // return this - // } - toString(indent: string): string { return indent + this.buf.join('\n' + indent) } @@ -111,7 +104,7 @@ export class MethodGenerator extends Generator { return this.codeFormatter.methodsEpilogue(indent) } - render(indent: string) { + render(indent: string, noStreams = false) { this.codeFormatter.reset() const items: string[] = [] // reset refcounts for ALL types so dynamic import statement will work @@ -145,8 +138,12 @@ export class MethodGenerator extends Generator { ) } }) - const tally = `${items.length - tagCount} API methods` + let tally = `${items.length - tagCount} API methods` success(tally) + if (noStreams) { + // Minimize code changes, don't include the tally when not streaming + tally = '' + } return this.p(this.codeFormatter.commentHeader('', licenseText, noComment)) .p(this.codeFormatter.commentHeader('', tally)) .p(this.prologue(indent)) @@ -199,7 +196,7 @@ export class FunctionGenerator extends MethodGenerator { } export class TypeGenerator extends Generator { - render(indent: string) { + render(indent: string, noStreams = false) { this.codeFormatter.reset() const items: string[] = [] Object.values(this.model.types).forEach((type) => { @@ -214,8 +211,12 @@ export class TypeGenerator extends Generator { }) const counts = this.typeTally(this.model.types) - const tally = `${counts.total} API models: ${counts.standard} Spec, ${counts.request} Request, ${counts.write} Write, ${counts.enums} Enum` + let tally = `${counts.total} API models: ${counts.standard} Spec, ${counts.request} Request, ${counts.write} Write, ${counts.enums} Enum` success(tally) + if (noStreams) { + // Minimize code changes, don't include the tally when not streaming + tally = '' + } return this.p(this.codeFormatter.commentHeader('', licenseText, noComment)) .p(this.codeFormatter.commentHeader('', tally)) .p(this.codeFormatter.modelsPrologue(indent))