Skip to content

Commit a5813d1

Browse files
authored
Merge pull request #45 from atomic-ehr/test-for-generators
Tests for generators
2 parents 2093b82 + 3d5f006 commit a5813d1

11 files changed

Lines changed: 684 additions & 10 deletions

File tree

src/api/builder.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export type ProgressCallback = (phase: string, current: number, total: number, m
6565
export interface GenerationResult {
6666
success: boolean;
6767
outputDir: string;
68-
filesGenerated: string[];
68+
filesGenerated: Record<string, string>;
6969
errors: string[];
7070
warnings: string[];
7171
duration: number;
@@ -405,7 +405,7 @@ export class APIBuilder {
405405
const result: GenerationResult = {
406406
success: false,
407407
outputDir: this.options.outputDir,
408-
filesGenerated: [],
408+
filesGenerated: {},
409409
errors: [],
410410
warnings: [],
411411
duration: 0,
@@ -519,7 +519,9 @@ export class APIBuilder {
519519
try {
520520
await generator.generate(tsIndex);
521521
const fileBuffer: FileBuffer[] = generator.writtenFiles();
522-
result.filesGenerated.push(...fileBuffer.map((e) => e.absPath));
522+
fileBuffer.forEach((buf) => {
523+
result.filesGenerated[buf.relPath] = buf.content;
524+
});
523525
this.logger.info(`Generating ${type} finished successfully`);
524526
} catch (error) {
525527
result.errors.push(

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ export class CSharp extends Writer<CSharpGeneratorOptions> {
107107
tabSize: 4,
108108
withDebugComment: false,
109109
commentLinePrefix: "//",
110+
resolveAssets: options.resolveAssets ?? resolveCSharpAssets,
110111
...options,
111112
});
112113
}
@@ -357,9 +358,8 @@ export class CSharp extends Writer<CSharpGeneratorOptions> {
357358
}
358359

359360
private copyStaticFiles(): void {
360-
if (this.opts.inMemoryOnly) return;
361-
const sourceDir = resolveCSharpAssets("");
362-
fs.cpSync(sourceDir, this.opts.outputDir, { recursive: true });
361+
this.cp("Client.cs", "Client.cs");
362+
this.cp("Helper.cs", "Helper.cs");
363363
}
364364

365365
private generateHelperFile(): void {

src/api/writer-generator/python.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ export class Python extends Writer<PythonGeneratorOptions> {
150150
private tsIndex: TypeSchemaIndex | undefined;
151151

152152
constructor(options: PythonGeneratorOptions) {
153-
super(options);
153+
super({ ...options, resolveAssets: options.resolveAssets ?? resolvePyAssets });
154154
this.nameFormatFunction = this.getFieldFormatFunction(options.fieldFormat);
155155
}
156156

@@ -166,7 +166,7 @@ export class Python extends Writer<PythonGeneratorOptions> {
166166

167167
private generateRootPackages(groups: TypeSchemaPackageGroups): void {
168168
this.generateRootInitFile(groups);
169-
fs.cpSync(resolvePyAssets("requirements.txt"), Path.resolve(this.opts.outputDir, "requirements.txt"));
169+
this.cp(resolvePyAssets("requirements.txt"), "requirements.txt");
170170
}
171171

172172
private generateSDKPackages(groups: TypeSchemaPackageGroups): void {

src/api/writer-generator/writer.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export type FileSystemWriterOptions = {
77
outputDir: string;
88
inMemoryOnly?: boolean;
99
logger?: CodegenLogger;
10+
resolveAssets?: (fn: string) => string;
1011
};
1112

1213
export type WriterOptions = FileSystemWriterOptions & {
@@ -82,7 +83,11 @@ export abstract class FileSystemWriter<T extends FileSystemWriterOptions = FileS
8283

8384
this.logger()?.debug(`cat > '${relPath}'`);
8485
this.currentFile = { descriptor, relPath };
85-
this.writtenFilesBuffer[this.currentFile.relPath] = { relPath, absPath: Path.resolve(relPath), tokens: [] };
86+
this.writtenFilesBuffer[this.currentFile.relPath] = {
87+
relPath,
88+
absPath: Path.resolve(relPath),
89+
tokens: [],
90+
};
8691

8792
gen();
8893
} finally {
@@ -100,12 +105,25 @@ export abstract class FileSystemWriter<T extends FileSystemWriterOptions = FileS
100105
buf.tokens.push(str);
101106
}
102107

108+
cp(source: string, destination: string) {
109+
if (!this.opts.resolveAssets) throw new Error("resolveAssets is not defined");
110+
source = Path.resolve(this.opts.resolveAssets(source));
111+
destination = Path.normalize(`${this.currentDir ?? this.opts.outputDir}/${destination}`);
112+
const content = fs.readFileSync(source, "utf8");
113+
this.writtenFilesBuffer[destination] = {
114+
relPath: destination,
115+
absPath: Path.resolve(destination),
116+
tokens: [content],
117+
};
118+
fs.cpSync(source, destination);
119+
}
120+
103121
abstract generate(_tsIndex: TypeSchemaIndex): Promise<void>;
104122

105123
writtenFiles(): FileBuffer[] {
106124
return Object.values(this.writtenFilesBuffer)
107125
.map(({ relPath, absPath, tokens }) => {
108-
return { relPath, absPath, content: tokens.join() };
126+
return { relPath, absPath, content: tokens.join("") };
109127
})
110128
.sort((a, b) => a.relPath.localeCompare(b.relPath));
111129
}

0 commit comments

Comments
 (0)