Skip to content

Commit 0a34d77

Browse files
committed
py: fix & refactor way of work with python assets
1 parent 13c844a commit 0a34d77

5 files changed

Lines changed: 16 additions & 15 deletions

File tree

src/api/writer-generator/python/static-files/requirements.txt renamed to assets/api/writer-generator/python/requirements.txt

File renamed without changes.

src/api/writer-generator/python/resource_family_validator.py renamed to assets/api/writer-generator/python/resource_family_validator.py

File renamed without changes.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
},
2626
"preferGlobal": true,
2727
"files": [
28-
"dist"
28+
"dist",
29+
"assets"
2930
],
3031
"scripts": {
3132
"test": "bun test",

src/api/builder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import * as afs from "node:fs/promises";
1010
import * as Path from "node:path";
1111
import { CanonicalManager } from "@atomic-ehr/fhir-canonical-manager";
1212
import { CSharp } from "@root/api/writer-generator/csharp/csharp.ts";
13-
import { Python, type PythonGeneratorOptions } from "@root/api/writer-generator/python/python.ts";
13+
import { Python, type PythonGeneratorOptions } from "@root/api/writer-generator/python";
1414
import { generateTypeSchemas } from "@root/typeschema";
1515
import { registerFromManager } from "@root/typeschema/register";
1616
import { mkTypeSchemaIndex, type TreeShake, type TypeSchemaIndex, treeShake } from "@root/typeschema/utils";
Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import assert from "node:assert";
22
import fs from "node:fs";
33
import * as Path from "node:path";
4+
import { fileURLToPath } from "node:url";
45
import {
56
camelCase,
67
capitalCase,
@@ -139,21 +140,26 @@ const deriveResourceName = (id: Identifier): string => {
139140
return pascalCase(id.name);
140141
};
141142

143+
const resolvePyAssets = (fn: string) => {
144+
const __dirname = Path.dirname(fileURLToPath(import.meta.url));
145+
if (__filename.endsWith("dist/index.js")) {
146+
return Path.resolve(__dirname, "..", "assets", "api", "writer-generator", "python", fn);
147+
} else {
148+
return Path.resolve(__dirname, "../../..", "assets", "api", "writer-generator", "python", fn);
149+
}
150+
};
151+
142152
type TypeSchemaPackageGroups = {
143153
groupedResources: Record<string, RegularTypeSchema[]>;
144154
groupedComplexTypes: Record<string, RegularTypeSchema[]>;
145155
};
146156

147157
export class Python extends Writer<PythonGeneratorOptions> {
148-
private readonly staticDir: string | undefined;
149158
private readonly nameFormatFunction: (name: string) => string;
150159
private tsIndex: TypeSchemaIndex | undefined;
151160

152161
constructor(options: PythonGeneratorOptions) {
153-
super({
154-
...options,
155-
});
156-
this.staticDir = options.staticDir || undefined;
162+
super(options);
157163
this.nameFormatFunction = this.getFieldFormatFunction(options.fieldFormat);
158164
}
159165

@@ -169,7 +175,7 @@ export class Python extends Writer<PythonGeneratorOptions> {
169175

170176
private generateRootPackages(groups: TypeSchemaPackageGroups): void {
171177
this.generateRootInitFile(groups);
172-
this.copyStaticFiles();
178+
fs.cpSync(resolvePyAssets("requirements.txt"), Path.resolve(this.opts.outputDir, "requirements.txt"));
173179
}
174180

175181
private generateSDKPackages(groups: TypeSchemaPackageGroups): void {
@@ -628,8 +634,7 @@ export class Python extends Writer<PythonGeneratorOptions> {
628634
}
629635

630636
private includeResourceFamilyValidator(): void {
631-
const path = "src/api/writer-generator/python/resource_family_validator.py";
632-
const content = fs.readFileSync(path, "utf-8");
637+
const content = fs.readFileSync(resolvePyAssets("resource_family_validator.py"), "utf-8");
633638
this.line(content);
634639
}
635640

@@ -683,11 +688,6 @@ export class Python extends Writer<PythonGeneratorOptions> {
683688
return this.pyFhirPackage(identifier);
684689
}
685690

686-
copyStaticFiles(): void {
687-
if (!this.staticDir) return;
688-
fs.cpSync(Path.resolve(this.staticDir), this.opts.outputDir, { recursive: true });
689-
}
690-
691691
getFieldFormatFunction(format: StringFormatKey): (name: string) => string {
692692
if (!AVAILABLE_STRING_FORMATS[format]) {
693693
this.logger()?.warn(`Unknown field format '${format}'. Defaulting to SnakeCase.`);

0 commit comments

Comments
 (0)