11import fs from "node:fs" ;
22import Path from "node:path" ;
33import { 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" ;
65import type { Field , Identifier , RegularField } from "@typeschema/types" ;
76import { type ChoiceFieldInstance , isChoiceDeclarationField , type RegularTypeSchema } from "@typeschema/types.ts" ;
87import type { TypeSchemaIndex } from "@typeschema/utils.ts" ;
@@ -76,34 +75,28 @@ const isReservedTypeName = (name: string): boolean => RESERVED_TYPE_NAMES.includ
7675
7776const 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
8684interface 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
0 commit comments