Skip to content

Commit 1d3302d

Browse files
committed
refactoring: rework WriterOptions passing from children
1 parent 24a32f4 commit 1d3302d

2 files changed

Lines changed: 22 additions & 27 deletions

File tree

src/api/writer-generator/csharp/csharp.ts

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import fs from "node:fs";
22
import Path from "node:path";
33
import { pascalCase, uppercaseFirstLetter, uppercaseFirstLetterOfEach } from "@root/api/writer-generator/utils.ts";
4-
import { Writer } from "@root/api/writer-generator/writer.ts";
5-
import type { CodegenLogger } from "@root/utils/codegen-logger.ts";
4+
import { Writer, type PartialBy, type WriterOptions } from "@root/api/writer-generator/writer.ts";
65
import type { Field, Identifier, RegularField } from "@typeschema/types";
76
import { type ChoiceFieldInstance, isChoiceDeclarationField, type RegularTypeSchema } from "@typeschema/types.ts";
87
import type { TypeSchemaIndex } from "@typeschema/utils.ts";
@@ -76,34 +75,28 @@ const isReservedTypeName = (name: string): boolean => RESERVED_TYPE_NAMES.includ
7675

7776
const prefixReservedTypeName = (name: string): string => (isReservedTypeName(name) ? `Resource${name}` : name);
7877

79-
interface CSharpGeneratorOptions {
78+
type CSharpGeneratorOptions = WriterOptions & {
8079
outputDir: string;
8180
staticSourceDir?: string;
8281
targetNamespace: string;
83-
logger?: CodegenLogger;
84-
}
82+
};
8583

8684
interface EnumRegistry {
8785
[packageName: string]: {
8886
[enumName: string]: string[];
8987
};
9088
}
9189

92-
export class CSharp extends Writer {
90+
export class CSharp extends Writer<CSharpGeneratorOptions> {
9391
private readonly enums: EnumRegistry = {};
94-
private readonly staticSourceDir?: string;
95-
private readonly targetNamespace: string;
9692

97-
constructor(options: CSharpGeneratorOptions) {
93+
constructor(options: PartialBy<CSharpGeneratorOptions, "tabSize" | "commentLinePrefix">) {
9894
super({
99-
outputDir: options.outputDir,
10095
tabSize: 4,
10196
withDebugComment: false,
10297
commentLinePrefix: "//",
103-
logger: options.logger,
98+
...options,
10499
});
105-
this.staticSourceDir = options.staticSourceDir;
106-
this.targetNamespace = options.targetNamespace;
107100
}
108101

109102
override generate(typeSchemaIndex: TypeSchemaIndex): void {
@@ -244,8 +237,8 @@ export class CSharp extends Writer {
244237
"CSharpSDK",
245238
"System.Text.Json",
246239
"System.Text.Json.Serialization",
247-
this.targetNamespace,
248-
...packages.map((pkg) => `${this.targetNamespace}.${pkg}`),
240+
this.opts.targetNamespace,
241+
...packages.map((pkg) => `${this.opts.targetNamespace}.${pkg}`),
249242
];
250243

251244
for (const using of globalUsings) this.lineSM("global", "using", using);
@@ -256,7 +249,7 @@ export class CSharp extends Writer {
256249
this.cat("base.cs", () => {
257250
this.generateDisclaimer();
258251
this.line();
259-
this.lineSM("namespace", this.targetNamespace);
252+
this.lineSM("namespace", this.opts.targetNamespace);
260253

261254
for (const schema of complexTypes) {
262255
const packageName = formatName(schema.identifier.package);
@@ -277,7 +270,7 @@ export class CSharp extends Writer {
277270
this.cat(`${schema.identifier.name}.cs`, () => {
278271
this.generateDisclaimer();
279272
this.line();
280-
this.lineSM("namespace", `${this.targetNamespace}.${packageName}`);
273+
this.lineSM("namespace", `${this.opts.targetNamespace}.${packageName}`);
281274
this.line();
282275
this.generateType(schema, packageName);
283276
});
@@ -305,7 +298,7 @@ export class CSharp extends Writer {
305298
private generateEnumFileContent(packageName: string, enums: Record<string, string[]>): void {
306299
this.lineSM("using", "System.ComponentModel");
307300
this.line();
308-
this.lineSM(`namespace ${this.targetNamespace}.${packageName}`);
301+
this.lineSM(`namespace ${this.opts.targetNamespace}.${packageName}`);
309302

310303
for (const [enumName, values] of Object.entries(enums)) {
311304
this.generateEnum(enumName, values);
@@ -332,7 +325,7 @@ export class CSharp extends Writer {
332325
this.cat(`${packageName}ResourceDictionary.cs`, () => {
333326
this.generateDisclaimer();
334327
this.line();
335-
this.lineSM(`namespace ${this.targetNamespace}`);
328+
this.lineSM(`namespace ${this.opts.targetNamespace}`);
336329
this.generateResourceDictionaryClass(packageName, packageResources);
337330
});
338331
}
@@ -352,8 +345,8 @@ export class CSharp extends Writer {
352345
}
353346

354347
private copyStaticFiles(): void {
355-
if (!this.staticSourceDir) return;
356-
const sourcePath = Path.resolve(this.staticSourceDir);
348+
if (!this.opts.staticSourceDir) return;
349+
const sourcePath = Path.resolve(this.opts.staticSourceDir);
357350
fs.cpSync(sourcePath, this.opts.outputDir, { recursive: true });
358351
}
359352

src/api/writer-generator/writer.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,24 @@ import * as Path from "node:path";
33
import type { TypeSchemaIndex } from "@root/typeschema/utils";
44
import type { CodegenLogger } from "@root/utils/codegen-logger";
55

6-
export interface WriterOptions {
6+
export type WriterOptions = {
77
outputDir: string;
88
tabSize: number;
99
withDebugComment?: boolean;
1010
commentLinePrefix: string;
1111
generateProfile?: boolean;
1212
logger?: CodegenLogger;
13-
}
13+
};
14+
15+
export type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
1416

15-
class FileSystemWriter {
16-
opts: WriterOptions;
17+
class FileSystemWriter<T extends WriterOptions = WriterOptions> {
18+
opts: T;
1719
currentDir: string;
1820
currentFileDescriptor?: number;
1921
writtenFilesSet: Set<string> = new Set();
2022

21-
constructor(opts: WriterOptions) {
23+
constructor(opts: T) {
2224
this.opts = opts;
2325
this.currentDir = opts.outputDir;
2426
}
@@ -74,7 +76,7 @@ class FileSystemWriter {
7476
}
7577
}
7678

77-
export class Writer extends FileSystemWriter {
79+
export class Writer<T extends WriterOptions = WriterOptions> extends FileSystemWriter<T> {
7880
currentIndent: number = 0;
7981

8082
private indent() {

0 commit comments

Comments
 (0)