swallowkit 1.0.0-beta.21 → 1.0.0-beta.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/README.ja.md +4 -4
  2. package/README.md +4 -4
  3. package/dist/cli/commands/dev.d.ts +11 -0
  4. package/dist/cli/commands/dev.d.ts.map +1 -1
  5. package/dist/cli/commands/dev.js +80 -7
  6. package/dist/cli/commands/dev.js.map +1 -1
  7. package/dist/cli/commands/init.d.ts.map +1 -1
  8. package/dist/cli/commands/init.js +17 -18
  9. package/dist/cli/commands/init.js.map +1 -1
  10. package/dist/cli/commands/scaffold.d.ts +0 -3
  11. package/dist/cli/commands/scaffold.d.ts.map +1 -1
  12. package/dist/cli/commands/scaffold.js +3 -172
  13. package/dist/cli/commands/scaffold.js.map +1 -1
  14. package/dist/cli/index.d.ts.map +1 -1
  15. package/dist/cli/index.js +37 -1
  16. package/dist/cli/index.js.map +1 -1
  17. package/dist/core/project/validation.js +2 -2
  18. package/dist/core/project/validation.js.map +1 -1
  19. package/dist/core/scaffold/model-parser.d.ts.map +1 -1
  20. package/dist/core/scaffold/model-parser.js +5 -6
  21. package/dist/core/scaffold/model-parser.js.map +1 -1
  22. package/dist/core/scaffold/native-schema-generator.d.ts +13 -0
  23. package/dist/core/scaffold/native-schema-generator.d.ts.map +1 -0
  24. package/dist/core/scaffold/native-schema-generator.js +667 -0
  25. package/dist/core/scaffold/native-schema-generator.js.map +1 -0
  26. package/package.json +1 -1
  27. package/src/__tests__/dev.test.ts +53 -1
  28. package/src/__tests__/model-parser.test.ts +44 -64
  29. package/src/__tests__/scaffold.test.ts +54 -26
  30. package/src/cli/commands/dev.ts +101 -8
  31. package/src/cli/commands/init.ts +26 -19
  32. package/src/cli/commands/scaffold.ts +3 -213
  33. package/src/cli/index.ts +4 -1
  34. package/src/core/project/validation.ts +2 -2
  35. package/src/core/scaffold/model-parser.ts +7 -7
  36. package/src/core/scaffold/native-schema-generator.ts +769 -0
@@ -0,0 +1,769 @@
1
+ import * as fs from "fs";
2
+ import * as path from "path";
3
+ import { spawn, spawnSync } from "child_process";
4
+ import { BackendLanguage } from "../../types";
5
+ import { ModelInfo, toKebabCase } from "./model-parser";
6
+ import { generateOpenApiDocument } from "./openapi-generator";
7
+
8
+ export const NSWAG_CONSOLECORE_VERSION = "14.7.1";
9
+ export const PYTHON_SCHEMA_CODEGEN_REQUIREMENT = "datamodel-code-generator>=0.44.0,<1.0.0";
10
+ const PYTHON_OUTPUT_MODEL_TYPE = "pydantic_v2.BaseModel";
11
+
12
+ type PythonLauncher = {
13
+ command: string;
14
+ argsPrefix: string[];
15
+ };
16
+
17
+ function getMachineAwareStdio(): "inherit" | "pipe" {
18
+ return process.env.SWALLOWKIT_MACHINE_OUTPUT === "1" ? "pipe" : "inherit";
19
+ }
20
+
21
+ function canRun(command: string, args: string[], cwd: string): boolean {
22
+ const result = spawnSync(command, args, {
23
+ cwd,
24
+ stdio: "ignore",
25
+ });
26
+
27
+ return !result.error && result.status === 0;
28
+ }
29
+
30
+ async function runCommand(command: string, args: string[], cwd: string, errorMessage: string): Promise<void> {
31
+ await new Promise<void>((resolve, reject) => {
32
+ const child = spawn(command, args, {
33
+ cwd,
34
+ stdio: getMachineAwareStdio(),
35
+ });
36
+
37
+ child.on("close", (code) => {
38
+ if (code === 0) {
39
+ resolve();
40
+ return;
41
+ }
42
+
43
+ reject(new Error(`${errorMessage} (${command} ${args.join(" ")}) exited with code ${code}`));
44
+ });
45
+
46
+ child.on("error", (error) => reject(new Error(`${errorMessage}: ${error.message}`)));
47
+ });
48
+ }
49
+
50
+ export function buildCSharpCodegenToolManifestSource(): string {
51
+ return `${JSON.stringify(
52
+ {
53
+ version: 1,
54
+ isRoot: true,
55
+ tools: {
56
+ "nswag.consolecore": {
57
+ version: NSWAG_CONSOLECORE_VERSION,
58
+ commands: ["nswag"],
59
+ },
60
+ },
61
+ },
62
+ null,
63
+ 2
64
+ )}\n`;
65
+ }
66
+
67
+ export function buildPythonCodegenRequirementsSource(): string {
68
+ return `${PYTHON_SCHEMA_CODEGEN_REQUIREMENT}\n`;
69
+ }
70
+
71
+ function toSnakeCase(value: string): string {
72
+ return value
73
+ .replace(/([a-z0-9])([A-Z])/g, "$1_$2")
74
+ .replace(/[-\s]+/g, "_")
75
+ .toLowerCase();
76
+ }
77
+
78
+ function toPascalIdentifier(value: string): string {
79
+ if (value.includes("-") || value.includes("_")) {
80
+ return value
81
+ .split(/[-_]/)
82
+ .filter(Boolean)
83
+ .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
84
+ .join("");
85
+ }
86
+
87
+ return value.charAt(0).toUpperCase() + value.slice(1);
88
+ }
89
+
90
+ function isDateLikeField(field: Pick<ModelInfo["fields"][number], "name" | "type">): boolean {
91
+ return field.type === "date" || (field.type === "string" && field.name.toLowerCase().endsWith("at"));
92
+ }
93
+
94
+ export function getCSharpSchemaModelPath(outputDir: string, modelName: string): string {
95
+ return path.join(outputDir, "src", "SwallowKitBackendModels", "Model", `${modelName}.cs`);
96
+ }
97
+
98
+ export function getCSharpSchemaOptionPath(outputDir: string): string {
99
+ return path.join(outputDir, "src", "SwallowKitBackendModels", "Client", "Option.cs");
100
+ }
101
+
102
+ export function getPythonSchemaModelPath(outputDir: string, modelName: string): string {
103
+ return path.join(outputDir, "backend_models", "models", `${toSnakeCase(modelName)}.py`);
104
+ }
105
+
106
+ export function getCSharpNativeGeneratorArgs(specPath: string, outputPath: string): string[] {
107
+ return [
108
+ "tool",
109
+ "run",
110
+ "nswag",
111
+ "openapi2csclient",
112
+ `/input:${specPath}`,
113
+ `/output:${outputPath}`,
114
+ "/namespace:SwallowKitBackendModels",
115
+ "/GenerateClientClasses:false",
116
+ "/GenerateClientInterfaces:false",
117
+ "/GenerateResponseClasses:false",
118
+ "/GenerateExceptionClasses:false",
119
+ "/GenerateDtoTypes:true",
120
+ "/GenerateNullableReferenceTypes:true",
121
+ "/GenerateOptionalPropertiesAsNullable:true",
122
+ "/JsonLibrary:SystemTextJson",
123
+ ];
124
+ }
125
+
126
+ export function getPythonNativeGeneratorArgs(specPath: string, outputPath: string): string[] {
127
+ return [
128
+ "-m",
129
+ "datamodel_code_generator",
130
+ "--input",
131
+ specPath,
132
+ "--input-file-type",
133
+ "openapi",
134
+ "--output",
135
+ outputPath,
136
+ "--output-model-type",
137
+ PYTHON_OUTPUT_MODEL_TYPE,
138
+ "--target-python-version",
139
+ "3.11",
140
+ "--disable-timestamp",
141
+ "--use-union-operator",
142
+ "--collapse-root-models",
143
+ ];
144
+ }
145
+
146
+ function getCSharpFieldBaseType(field: ModelInfo["fields"][number]): string {
147
+ if (field.isNestedSchema && field.nestedModelName) {
148
+ return field.nestedModelName;
149
+ }
150
+
151
+ if (field.enumValues?.length) {
152
+ return `${toPascalIdentifier(field.name)}Enum`;
153
+ }
154
+
155
+ if (field.isArray) {
156
+ return `List<${getCSharpArrayElementType(field)}>`;
157
+ }
158
+
159
+ switch (field.type) {
160
+ case "string":
161
+ return isDateLikeField(field) ? "DateTime" : "string";
162
+ case "number":
163
+ return "decimal";
164
+ case "boolean":
165
+ return "bool";
166
+ case "date":
167
+ return "DateTime";
168
+ case "object":
169
+ return "Dictionary<string, object>";
170
+ default:
171
+ return "object";
172
+ }
173
+ }
174
+
175
+ function getCSharpArrayElementType(field: ModelInfo["fields"][number]): string {
176
+ if (field.isNestedSchema && field.nestedModelName) {
177
+ return field.nestedModelName;
178
+ }
179
+
180
+ if (field.enumValues?.length) {
181
+ return `${toPascalIdentifier(field.name)}Enum`;
182
+ }
183
+
184
+ switch (field.type) {
185
+ case "string":
186
+ return isDateLikeField(field) ? "DateTime" : "string";
187
+ case "number":
188
+ return "decimal";
189
+ case "boolean":
190
+ return "bool";
191
+ case "date":
192
+ return "DateTime";
193
+ case "object":
194
+ return "Dictionary<string, object>";
195
+ default:
196
+ return "object";
197
+ }
198
+ }
199
+
200
+ function getCSharpPropertyType(field: ModelInfo["fields"][number]): string {
201
+ const baseType = getCSharpFieldBaseType(field);
202
+ return field.isOptional ? `${baseType}?` : baseType;
203
+ }
204
+
205
+ function getCSharpOptionType(field: ModelInfo["fields"][number]): string {
206
+ return `Option<${getCSharpPropertyType(field)}>`;
207
+ }
208
+
209
+ function generateLegacyCompatibleOptionSource(): string {
210
+ return `// <auto-generated>
211
+ // Minimal Option<T> for OpenAPI Generator model compatibility.
212
+ // Full client supporting files are excluded to avoid Polly version conflicts.
213
+ // </auto-generated>
214
+
215
+ #nullable enable
216
+
217
+ namespace SwallowKitBackendModels.Client
218
+ {
219
+ /// <summary>
220
+ /// A wrapper for nullable/optional properties generated by OpenAPI Generator.
221
+ /// Tracks whether a value has been explicitly set (distinguishing null from absent).
222
+ /// </summary>
223
+ public readonly struct Option<TValue>
224
+ {
225
+ /// <summary>Whether this option has been explicitly set.</summary>
226
+ public bool IsSet { get; }
227
+
228
+ /// <summary>The contained value (may be default if not set).</summary>
229
+ public TValue Value { get; }
230
+
231
+ /// <summary>Create an Option with an explicit value.</summary>
232
+ public Option(TValue value)
233
+ {
234
+ IsSet = true;
235
+ Value = value;
236
+ }
237
+
238
+ /// <summary>Implicit conversion from Option to its inner value.</summary>
239
+ public static implicit operator TValue(Option<TValue> option) => option.Value;
240
+ }
241
+ }
242
+ `;
243
+ }
244
+
245
+ function buildCSharpEnumMembers(values: string[]): string {
246
+ return values
247
+ .map((value, index) => ` ${toPascalIdentifier(value)} = ${index + 1}`)
248
+ .join(",\n\n");
249
+ }
250
+
251
+ function buildCSharpEnumFromStringCases(field: ModelInfo["fields"][number], nullable: boolean): string {
252
+ const enumType = `${toPascalIdentifier(field.name)}Enum`;
253
+ return field.enumValues!
254
+ .map((value) => ` if (value.Equals("${value}", StringComparison.Ordinal))\n return ${enumType}.${toPascalIdentifier(value)};`)
255
+ .join("\n\n") + (nullable ? `\n\n return null;` : `\n\n throw new NotImplementedException($"Could not convert value to type ${enumType}: '{value}'");`);
256
+ }
257
+
258
+ function buildCSharpEnumToJsonCases(field: ModelInfo["fields"][number]): string {
259
+ const enumType = `${toPascalIdentifier(field.name)}Enum`;
260
+ return field.enumValues!
261
+ .map((value) => ` if (value == ${enumType}.${toPascalIdentifier(value)})\n return "${value}";`)
262
+ .join("\n\n");
263
+ }
264
+
265
+ function generateLegacyCompatibleCSharpModelSource(model: ModelInfo): string {
266
+ const requiredFields = model.fields.filter((field) => !field.isOptional);
267
+ const optionalFields = model.fields.filter((field) => field.isOptional);
268
+
269
+ const constructorParams = [
270
+ ...requiredFields.map((field) => `${getCSharpFieldBaseType(field)} ${field.name}`),
271
+ ...optionalFields.map((field) => `${getCSharpOptionType(field)} ${field.name} = default`),
272
+ ].join(", ");
273
+
274
+ const constructorAssignments = [
275
+ ...requiredFields.map((field) => ` ${toPascalIdentifier(field.name)} = ${field.name};`),
276
+ ...optionalFields.map((field) => ` ${toPascalIdentifier(field.name)}Option = ${field.name};`),
277
+ " OnCreated();",
278
+ ].join("\n");
279
+
280
+ const enumBlocks = model.fields
281
+ .filter((field) => field.enumValues?.length)
282
+ .map((field) => {
283
+ const enumType = `${toPascalIdentifier(field.name)}Enum`;
284
+ return ` /// <summary>
285
+ /// Defines ${toPascalIdentifier(field.name)}
286
+ /// </summary>
287
+ [JsonConverter(typeof(JsonStringEnumConverter))]
288
+ public enum ${enumType}
289
+ {
290
+ ${buildCSharpEnumMembers(field.enumValues!)}
291
+ }
292
+
293
+ public static ${enumType} ${enumType}FromString(string value)
294
+ {
295
+ ${buildCSharpEnumFromStringCases(field, false)}
296
+ }
297
+
298
+ public static ${enumType}? ${enumType}FromStringOrDefault(string value)
299
+ {
300
+ ${buildCSharpEnumFromStringCases(field, true)}
301
+ }
302
+
303
+ public static string ${enumType}ToJsonValue(${enumType}? value)
304
+ {
305
+ ${buildCSharpEnumToJsonCases(field)}
306
+
307
+ throw new NotImplementedException($"Value could not be handled: '{value}'");
308
+ }`;
309
+ })
310
+ .join("\n\n");
311
+
312
+ const propertyBlocks = model.fields
313
+ .map((field) => {
314
+ const propertyName = toPascalIdentifier(field.name);
315
+ const propertyType = getCSharpPropertyType(field);
316
+
317
+ if (!field.isOptional) {
318
+ return ` /// <summary>
319
+ /// Gets or Sets ${propertyName}
320
+ /// </summary>
321
+ [JsonPropertyName("${field.name}")]
322
+ public ${propertyType} ${propertyName} { get; set; }`;
323
+ }
324
+
325
+ return ` /// <summary>
326
+ /// Used to track the state of ${propertyName}
327
+ /// </summary>
328
+ [JsonIgnore]
329
+ [global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
330
+ public ${getCSharpOptionType(field)} ${propertyName}Option { get; private set; }
331
+
332
+ /// <summary>
333
+ /// Gets or Sets ${propertyName}
334
+ /// </summary>
335
+ [JsonPropertyName("${field.name}")]
336
+ public ${propertyType} ${propertyName} { get { return this.${propertyName}Option; } set { this.${propertyName}Option = new(value); } }`;
337
+ })
338
+ .join("\n\n");
339
+
340
+ const toStringBody = model.fields
341
+ .map((field) => ` sb.Append(" ${toPascalIdentifier(field.name)}: ").Append(${toPascalIdentifier(field.name)}).Append("\\n");`)
342
+ .join("\n");
343
+
344
+ return `// <auto-generated>
345
+ /*
346
+ * ${model.name} API
347
+ *
348
+ * Generated from SwallowKit Zod model metadata.
349
+ *
350
+ * The version of the OpenAPI document: 1.0.0
351
+ * Generated by native SwallowKit schema compatibility layer
352
+ */
353
+
354
+ #nullable enable
355
+
356
+ using System;
357
+ using System.Collections.Generic;
358
+ using System.ComponentModel.DataAnnotations;
359
+ using System.Text;
360
+ using System.Text.Json.Serialization;
361
+ using SwallowKitBackendModels.Client;
362
+
363
+ namespace SwallowKitBackendModels.Model
364
+ {
365
+ /// <summary>
366
+ /// ${model.name}
367
+ /// </summary>
368
+ public partial class ${model.name} : IValidatableObject
369
+ {
370
+ [JsonConstructor]
371
+ public ${model.name}(${constructorParams})
372
+ {
373
+ ${constructorAssignments}
374
+ }
375
+
376
+ partial void OnCreated();
377
+ ${enumBlocks ? `\n\n${enumBlocks}` : ""}
378
+
379
+ ${propertyBlocks}
380
+
381
+ public override string ToString()
382
+ {
383
+ var sb = new StringBuilder();
384
+ sb.Append("class ${model.name} {\\n");
385
+ ${toStringBody}
386
+ sb.Append("}\\n");
387
+ return sb.ToString();
388
+ }
389
+
390
+ IEnumerable<ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
391
+ {
392
+ yield break;
393
+ }
394
+ }
395
+ }
396
+ `;
397
+ }
398
+
399
+ function getPythonTypeName(field: ModelInfo["fields"][number]): string {
400
+ if (field.isNestedSchema && field.nestedModelName) {
401
+ return field.nestedModelName;
402
+ }
403
+
404
+ if (field.isArray) {
405
+ return `List[${getPythonArrayElementType(field)}]`;
406
+ }
407
+
408
+ switch (field.type) {
409
+ case "string":
410
+ return isDateLikeField(field) ? "datetime" : "StrictStr";
411
+ case "number":
412
+ return "Union[StrictFloat, StrictInt]";
413
+ case "boolean":
414
+ return "StrictBool";
415
+ case "date":
416
+ return "datetime";
417
+ case "object":
418
+ return "Dict[str, Any]";
419
+ default:
420
+ return "Any";
421
+ }
422
+ }
423
+
424
+ function getPythonArrayElementType(field: ModelInfo["fields"][number]): string {
425
+ if (field.isNestedSchema && field.nestedModelName) {
426
+ return field.nestedModelName;
427
+ }
428
+
429
+ switch (field.type) {
430
+ case "string":
431
+ return isDateLikeField(field) ? "datetime" : "StrictStr";
432
+ case "number":
433
+ return "Union[StrictFloat, StrictInt]";
434
+ case "boolean":
435
+ return "StrictBool";
436
+ case "date":
437
+ return "datetime";
438
+ case "object":
439
+ return "Dict[str, Any]";
440
+ default:
441
+ return "Any";
442
+ }
443
+ }
444
+
445
+ function buildPythonFieldDeclaration(field: ModelInfo["fields"][number]): string {
446
+ const pythonName = toSnakeCase(field.name);
447
+ const typeName = field.isOptional ? `Optional[${getPythonTypeName(field)}]` : getPythonTypeName(field);
448
+ const aliasSuffix = pythonName !== field.name ? `, alias="${field.name}"` : "";
449
+
450
+ if (field.isOptional) {
451
+ return `${pythonName}: ${typeName} = Field(default=None${aliasSuffix})`;
452
+ }
453
+
454
+ if (aliasSuffix) {
455
+ return `${pythonName}: ${typeName} = Field(${aliasSuffix.slice(2)})`;
456
+ }
457
+
458
+ return `${pythonName}: ${typeName}`;
459
+ }
460
+
461
+ function buildPythonEnumValidators(model: ModelInfo): string {
462
+ return model.fields
463
+ .filter((field) => field.enumValues?.length)
464
+ .map((field) => {
465
+ const pythonName = toSnakeCase(field.name);
466
+ const enumSet = field.enumValues!.map((value) => `'${value}'`).join(", ");
467
+ return ` @field_validator('${pythonName}')
468
+ def ${pythonName}_validate_enum(cls, value):
469
+ """Validates the enum"""
470
+ if value is None:
471
+ return value
472
+
473
+ if value not in set([${enumSet}]):
474
+ raise ValueError("must be one of enum values (${field.enumValues!.map((value) => `'${value}'`).join(", ")})")
475
+ return value`;
476
+ })
477
+ .join("\n\n");
478
+ }
479
+
480
+ function buildPythonModelImports(model: ModelInfo): string {
481
+ const nestedImports = Array.from(
482
+ new Set(
483
+ model.fields
484
+ .filter((field) => field.isNestedSchema && field.nestedModelName)
485
+ .map((field) => field.nestedModelName!)
486
+ )
487
+ )
488
+ .map((modelName) => `from .${toSnakeCase(modelName)} import ${modelName}`)
489
+ .join("\n");
490
+
491
+ return nestedImports ? `${nestedImports}\n\n` : "";
492
+ }
493
+
494
+ function generateLegacyCompatiblePythonModelSource(model: ModelInfo): string {
495
+ const fieldDeclarations = model.fields.map((field) => ` ${buildPythonFieldDeclaration(field)}`).join("\n");
496
+ const propertyNames = model.fields.map((field) => `"${field.name}"`).join(", ");
497
+ const validators = buildPythonEnumValidators(model);
498
+ const dictAssignments = model.fields
499
+ .map((field) => ` "${field.name}": obj.get("${field.name}")`)
500
+ .join(",\n");
501
+
502
+ return `# coding: utf-8
503
+
504
+ """
505
+ ${model.name} API
506
+
507
+ Generated from SwallowKit Zod model metadata.
508
+
509
+ The version of the OpenAPI document: 1.0.0
510
+ Generated by native SwallowKit schema compatibility layer
511
+
512
+ Do not edit the class manually.
513
+ """ # noqa: E501
514
+
515
+
516
+ from __future__ import annotations
517
+ import pprint
518
+ import re # noqa: F401
519
+ import json
520
+
521
+ from datetime import datetime
522
+ from pydantic import BaseModel, ConfigDict, Field, StrictBool, StrictFloat, StrictInt, StrictStr, field_validator
523
+ from typing import Any, ClassVar, Dict, List, Optional, Set, Union
524
+ from typing_extensions import Self
525
+
526
+ ${buildPythonModelImports(model)}class ${model.name}(BaseModel):
527
+ """
528
+ ${model.name}
529
+ """ # noqa: E501
530
+ ${fieldDeclarations}
531
+ __properties: ClassVar[List[str]] = [${propertyNames}]
532
+ ${validators ? `\n\n${validators}` : ""}
533
+
534
+ model_config = ConfigDict(
535
+ populate_by_name=True,
536
+ validate_assignment=True,
537
+ protected_namespaces=(),
538
+ )
539
+
540
+
541
+ def to_str(self) -> str:
542
+ """Returns the string representation of the model using alias"""
543
+ return pprint.pformat(self.model_dump(by_alias=True))
544
+
545
+ def to_json(self) -> str:
546
+ """Returns the JSON representation of the model using alias"""
547
+ return json.dumps(self.to_dict())
548
+
549
+ @classmethod
550
+ def from_json(cls, json_str: str) -> Optional[Self]:
551
+ """Create an instance of ${model.name} from a JSON string"""
552
+ return cls.from_dict(json.loads(json_str))
553
+
554
+ def to_dict(self) -> Dict[str, Any]:
555
+ """Return the dictionary representation of the model using alias."""
556
+ excluded_fields: Set[str] = set([
557
+ ])
558
+
559
+ _dict = self.model_dump(
560
+ by_alias=True,
561
+ exclude=excluded_fields,
562
+ exclude_none=True,
563
+ )
564
+ return _dict
565
+
566
+ @classmethod
567
+ def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
568
+ """Create an instance of ${model.name} from a dict"""
569
+ if obj is None:
570
+ return None
571
+
572
+ if not isinstance(obj, dict):
573
+ return cls.model_validate(obj)
574
+
575
+ _obj = cls.model_validate({
576
+ ${dictAssignments}
577
+ })
578
+ return _obj
579
+
580
+
581
+ `;
582
+ }
583
+
584
+ function buildGeneratedPythonPackageInitSource(models: ModelInfo[]): string {
585
+ return models.map((model) => `from .models.${toSnakeCase(model.name)} import ${model.name}`).join("\n") + "\n";
586
+ }
587
+
588
+ function buildGeneratedPythonModelsInitSource(models: ModelInfo[]): string {
589
+ return models.map((model) => `from .${toSnakeCase(model.name)} import ${model.name}`).join("\n") + "\n";
590
+ }
591
+
592
+ function ensureCSharpCodegenProjectFiles(functionsRoot: string): void {
593
+ const toolManifestPath = path.join(functionsRoot, ".config", "dotnet-tools.json");
594
+ fs.mkdirSync(path.dirname(toolManifestPath), { recursive: true });
595
+ if (!fs.existsSync(toolManifestPath)) {
596
+ fs.writeFileSync(toolManifestPath, buildCSharpCodegenToolManifestSource(), "utf-8");
597
+ }
598
+ }
599
+
600
+ function ensurePythonCodegenProjectFiles(functionsRoot: string): string {
601
+ const requirementsPath = path.join(functionsRoot, "requirements.codegen.txt");
602
+ if (!fs.existsSync(requirementsPath)) {
603
+ fs.writeFileSync(requirementsPath, buildPythonCodegenRequirementsSource(), "utf-8");
604
+ }
605
+ return requirementsPath;
606
+ }
607
+
608
+ function getVirtualEnvPythonPath(venvDir: string): string {
609
+ return process.platform === "win32"
610
+ ? path.join(venvDir, "Scripts", "python.exe")
611
+ : path.join(venvDir, "bin", "python");
612
+ }
613
+
614
+ function detectSystemPythonLauncher(functionsRoot: string): PythonLauncher {
615
+ const candidates: PythonLauncher[] = [
616
+ { command: "python", argsPrefix: [] },
617
+ { command: "py", argsPrefix: ["-3"] },
618
+ { command: "python3", argsPrefix: [] },
619
+ ];
620
+
621
+ for (const candidate of candidates) {
622
+ if (canRun(candidate.command, [...candidate.argsPrefix, "--version"], functionsRoot)) {
623
+ return candidate;
624
+ }
625
+ }
626
+
627
+ throw new Error(
628
+ "Python 3.11+ is required to generate backend schema assets.\n" +
629
+ "Install Python and retry, or create functions/.codegen-venv manually."
630
+ );
631
+ }
632
+
633
+ async function ensurePythonCodegenEnvironment(functionsRoot: string): Promise<string> {
634
+ const requirementsPath = ensurePythonCodegenProjectFiles(functionsRoot);
635
+ const venvDir = path.join(functionsRoot, ".codegen-venv");
636
+ const venvPython = getVirtualEnvPythonPath(venvDir);
637
+
638
+ if (!fs.existsSync(venvPython)) {
639
+ const launcher = detectSystemPythonLauncher(functionsRoot);
640
+ await runCommand(
641
+ launcher.command,
642
+ [...launcher.argsPrefix, "-m", "venv", venvDir],
643
+ functionsRoot,
644
+ "Failed to create the Python schema code generation virtual environment."
645
+ );
646
+ }
647
+
648
+ if (!canRun(venvPython, ["-c", "import datamodel_code_generator"], functionsRoot)) {
649
+ await runCommand(
650
+ venvPython,
651
+ ["-m", "pip", "install", "--disable-pip-version-check", "-r", requirementsPath],
652
+ functionsRoot,
653
+ "Failed to install Python schema generation dependencies."
654
+ );
655
+ }
656
+
657
+ return venvPython;
658
+ }
659
+
660
+ async function generateCSharpSchemaArtifacts(
661
+ models: ModelInfo[],
662
+ specPath: string,
663
+ outputDir: string,
664
+ functionsRoot: string
665
+ ): Promise<void> {
666
+ ensureCSharpCodegenProjectFiles(functionsRoot);
667
+
668
+ if (!canRun("dotnet", ["--version"], functionsRoot)) {
669
+ throw new Error(
670
+ "The .NET SDK is required to generate C# backend schema assets.\n" +
671
+ "Install the .NET 8 SDK and retry."
672
+ );
673
+ }
674
+
675
+ const tempContractsPath = path.join(outputDir, ".native-temp", "Contracts.cs");
676
+ fs.mkdirSync(path.dirname(tempContractsPath), { recursive: true });
677
+
678
+ await runCommand(
679
+ "dotnet",
680
+ ["tool", "restore"],
681
+ functionsRoot,
682
+ "Failed to restore the NSwag dotnet tool."
683
+ );
684
+ await runCommand(
685
+ "dotnet",
686
+ getCSharpNativeGeneratorArgs(specPath, tempContractsPath),
687
+ functionsRoot,
688
+ "NSwag failed to generate C# backend schema assets."
689
+ );
690
+
691
+ fs.rmSync(path.dirname(tempContractsPath), { recursive: true, force: true });
692
+
693
+ const optionPath = getCSharpSchemaOptionPath(outputDir);
694
+ fs.mkdirSync(path.dirname(optionPath), { recursive: true });
695
+ fs.writeFileSync(optionPath, generateLegacyCompatibleOptionSource(), "utf-8");
696
+
697
+ for (const model of models) {
698
+ const modelPath = getCSharpSchemaModelPath(outputDir, model.name);
699
+ fs.mkdirSync(path.dirname(modelPath), { recursive: true });
700
+ fs.writeFileSync(modelPath, generateLegacyCompatibleCSharpModelSource(model), "utf-8");
701
+ }
702
+ }
703
+
704
+ async function generatePythonSchemaArtifacts(
705
+ models: ModelInfo[],
706
+ specPath: string,
707
+ outputDir: string,
708
+ functionsRoot: string
709
+ ): Promise<void> {
710
+ const pythonExecutable = await ensurePythonCodegenEnvironment(functionsRoot);
711
+ const tempModelsPath = path.join(outputDir, ".native-temp", "models.py");
712
+ fs.mkdirSync(path.dirname(tempModelsPath), { recursive: true });
713
+
714
+ await runCommand(
715
+ pythonExecutable,
716
+ getPythonNativeGeneratorArgs(specPath, tempModelsPath),
717
+ functionsRoot,
718
+ "datamodel-code-generator failed to generate Python backend schema assets."
719
+ );
720
+
721
+ fs.rmSync(path.dirname(tempModelsPath), { recursive: true, force: true });
722
+
723
+ const packageRoot = path.join(outputDir, "backend_models");
724
+ const modelsRoot = path.join(packageRoot, "models");
725
+ fs.mkdirSync(modelsRoot, { recursive: true });
726
+ fs.writeFileSync(path.join(packageRoot, "__init__.py"), buildGeneratedPythonPackageInitSource(models), "utf-8");
727
+ fs.writeFileSync(path.join(modelsRoot, "__init__.py"), buildGeneratedPythonModelsInitSource(models), "utf-8");
728
+
729
+ for (const model of models) {
730
+ const modelPath = getPythonSchemaModelPath(outputDir, model.name);
731
+ fs.mkdirSync(path.dirname(modelPath), { recursive: true });
732
+ fs.writeFileSync(modelPath, generateLegacyCompatiblePythonModelSource(model), "utf-8");
733
+ }
734
+ }
735
+
736
+ export async function generateLanguageSchemaArtifacts(
737
+ models: ModelInfo[],
738
+ rootModel: ModelInfo,
739
+ functionsDir: string,
740
+ backendLanguage: Exclude<BackendLanguage, "typescript">
741
+ ): Promise<void> {
742
+ console.log("\n🧬 Generating OpenAPI export and native schema assets...");
743
+
744
+ const projectRoot = process.cwd();
745
+ const functionsRoot = path.join(projectRoot, functionsDir);
746
+ const openApiDir = path.join(functionsRoot, "openapi");
747
+ fs.mkdirSync(openApiDir, { recursive: true });
748
+
749
+ const specPath = path.join(openApiDir, `${toKebabCase(rootModel.name)}.openapi.json`);
750
+ fs.writeFileSync(specPath, generateOpenApiDocument(models, rootModel), "utf-8");
751
+ console.log(`āœ… Created: ${specPath}`);
752
+
753
+ const outputDir = path.join(
754
+ functionsRoot,
755
+ "generated",
756
+ backendLanguage === "csharp" ? "csharp-models" : "python-models"
757
+ );
758
+
759
+ fs.rmSync(outputDir, { recursive: true, force: true });
760
+ fs.mkdirSync(outputDir, { recursive: true });
761
+
762
+ if (backendLanguage === "csharp") {
763
+ await generateCSharpSchemaArtifacts(models, specPath, outputDir, functionsRoot);
764
+ } else {
765
+ await generatePythonSchemaArtifacts(models, specPath, outputDir, functionsRoot);
766
+ }
767
+
768
+ console.log(`āœ… Generated ${backendLanguage} schema assets: ${outputDir}`);
769
+ }