|
| 1 | +// import path from "node:path"; |
| 2 | + |
| 3 | +// import * as Mustache from "mustache"; |
| 4 | +// import { Generator, type GeneratorOptions } from "../../generator"; |
| 5 | +// import { ViewModelCache, ViewModelFactory } from "@mustache/generator/ViewModelFactory"; |
| 6 | +// import { Rendering } from "@mustache/types/Rendering"; |
| 7 | +// import * as util from "node:util"; |
| 8 | +// import { TemplateFileCache } from "@mustache/generator/TemplateFileCache"; |
| 9 | +// import { NamedViewModel } from "@mustache/types/NamedViewModel"; |
| 10 | +// import { DistinctNameConfigurationType, NameGenerator, NameTransformation } from "@mustache/generator/NameGenerator"; |
| 11 | +// import { SchemaLoaderFacade } from "@mustache/generator/SchemaLoaderFacade"; |
| 12 | +// import { View } from "@mustache/types/View"; |
| 13 | +// import { LambdaMixinProvider } from "@mustache/generator/LambdaMixinProvider"; |
| 14 | +// import { PrimitiveType } from "@mustache/types/PrimitiveType"; |
| 15 | +// import { DebugMixinProvider } from "@mustache/generator/DebugMixinProvider"; |
| 16 | +// import { spawn } from "child_process"; |
| 17 | +// import { TypeViewModel } from "@mustache/types/TypeViewModel"; |
| 18 | +// import { HookType } from "@mustache/types/HookType"; |
| 19 | +// import { ViewModel } from "@mustache/types/ViewModel"; |
| 20 | +// import { FilterType } from "@mustache/types/FilterType"; |
| 21 | + |
| 22 | +// export interface MustacheGeneratorOptions extends GeneratorOptions { |
| 23 | +// debug: "OFF" | "FORMATTED" | "COMPACT"; |
| 24 | +// hooks: { |
| 25 | +// afterGenerate?: HookType[]; |
| 26 | +// }; |
| 27 | +// meta: { |
| 28 | +// timestamp?: string; |
| 29 | +// generator?: string; |
| 30 | +// }; |
| 31 | +// filters: { |
| 32 | +// resource?: FilterType; |
| 33 | +// complexType?: FilterType; |
| 34 | +// }; |
| 35 | +// sources: { |
| 36 | +// templateSource: string; |
| 37 | +// staticSource: string; |
| 38 | +// }; |
| 39 | +// typeMap: Partial<Record<PrimitiveType, string>>; |
| 40 | +// unsaveCharacterPattern: string | RegExp; |
| 41 | +// nameTransformations: DistinctNameConfigurationType<NameTransformation[]>; |
| 42 | +// renderings: { |
| 43 | +// utility: Rendering[]; |
| 44 | +// resource: Rendering[]; |
| 45 | +// complexType: Rendering[]; |
| 46 | +// }; |
| 47 | +// } |
| 48 | + |
| 49 | +// export class MustacheGenerator extends Generator { |
| 50 | +// private static runCommand(cmd, args: string[] = [], options = {}) { |
| 51 | +// return new Promise((resolve, reject) => { |
| 52 | +// const child = spawn(cmd, args, { |
| 53 | +// stdio: "inherit", |
| 54 | +// ...options, |
| 55 | +// }); |
| 56 | +// child.on("error", reject); |
| 57 | +// child.on("close", (code) => { |
| 58 | +// if (code === 0) resolve(code); |
| 59 | +// else reject(new Error(`Prozess beendet mit Fehlercode ${code}`)); |
| 60 | +// }); |
| 61 | +// }); |
| 62 | +// } |
| 63 | + |
| 64 | +// private readonly templateFileCache: TemplateFileCache; |
| 65 | +// private readonly mustacheGeneratorOptions: MustacheGeneratorOptions; |
| 66 | +// private readonly nameGenerator: NameGenerator; |
| 67 | +// private readonly lambdaMixinProvider: LambdaMixinProvider; |
| 68 | +// private readonly debugMixinProvider?: DebugMixinProvider; |
| 69 | + |
| 70 | +// constructor(opts: MustacheGeneratorOptions) { |
| 71 | +// super({ |
| 72 | +// ...opts, |
| 73 | +// typeMap: opts.typeMap, |
| 74 | +// staticDir: path.resolve(opts.sources.staticSource), |
| 75 | +// }); |
| 76 | +// this.mustacheGeneratorOptions = opts; |
| 77 | +// this.nameGenerator = new NameGenerator( |
| 78 | +// opts.keywords ?? new Set<string>(), |
| 79 | +// opts.typeMap, |
| 80 | +// opts.nameTransformations, |
| 81 | +// opts.unsaveCharacterPattern, |
| 82 | +// ); |
| 83 | +// this.templateFileCache = new TemplateFileCache(opts.sources.templateSource); |
| 84 | +// this.lambdaMixinProvider = new LambdaMixinProvider(this.nameGenerator); |
| 85 | +// this.debugMixinProvider = opts.debug != "OFF" ? new DebugMixinProvider(opts.debug) : undefined; |
| 86 | +// } |
| 87 | +// public async generate() { |
| 88 | +// this.clear(); |
| 89 | +// const schemaLoaderFacade = new SchemaLoaderFacade(this.loader, this.mustacheGeneratorOptions.filters); |
| 90 | +// const modelFactory = new ViewModelFactory(schemaLoaderFacade, this.nameGenerator); |
| 91 | + |
| 92 | +// const cache: ViewModelCache = { |
| 93 | +// resourcesByUri: {}, |
| 94 | +// complexTypesByUri: {}, |
| 95 | +// }; |
| 96 | +// schemaLoaderFacade |
| 97 | +// .getComplexTypes() |
| 98 | +// .map((typeRef) => modelFactory.createComplexType(typeRef, cache)) |
| 99 | +// .forEach(this._renderComplexType.bind(this)); |
| 100 | + |
| 101 | +// schemaLoaderFacade |
| 102 | +// .getResources() |
| 103 | +// .map((typeRef) => modelFactory.createResource(typeRef, cache)) |
| 104 | +// .forEach(this._renderResource.bind(this)); |
| 105 | + |
| 106 | +// this._renderUtility(modelFactory.createUtility()); |
| 107 | + |
| 108 | +// this.copyStaticFiles(); |
| 109 | + |
| 110 | +// await this._runHooks(this.mustacheGeneratorOptions.hooks.afterGenerate); |
| 111 | +// } |
| 112 | + |
| 113 | +// private async _runHooks(hooks?: HookType[]) { |
| 114 | +// for (const hook of hooks ?? []) { |
| 115 | +// console.info(`Running hook: ${hook.cmd} ${hook.args?.join(" ")}`); |
| 116 | +// await MustacheGenerator.runCommand(hook.cmd, hook.args ?? [], { |
| 117 | +// cwd: this.mustacheGeneratorOptions.outputDir, |
| 118 | +// }); |
| 119 | +// console.info(`Completed hook: ${hook.cmd} ${hook.args?.join(" ")}`); |
| 120 | +// } |
| 121 | +// } |
| 122 | + |
| 123 | +// private _checkRenderingFilter(model: TypeViewModel, rendering: Rendering): boolean { |
| 124 | +// if (!rendering.filter?.whitelist?.length && !rendering.filter?.blacklist?.length) { |
| 125 | +// return true; |
| 126 | +// } |
| 127 | +// if ((rendering.filter?.blacklist ?? []).find((v) => model.name.match(v))) { |
| 128 | +// return false; |
| 129 | +// } |
| 130 | +// if ((rendering.filter?.whitelist ?? []).find((v) => model.name.match(v))) { |
| 131 | +// return true; |
| 132 | +// } |
| 133 | +// return !rendering.filter.whitelist?.length; |
| 134 | +// } |
| 135 | + |
| 136 | +// private _renderUtility(model: ViewModel) { |
| 137 | +// this.mustacheGeneratorOptions.renderings.utility.forEach((rendering) => { |
| 138 | +// this.dir(rendering.path, () => { |
| 139 | +// this.file(rendering.fileNameFormat, () => { |
| 140 | +// this.write(this._render(model, rendering)); |
| 141 | +// }); |
| 142 | +// }); |
| 143 | +// }); |
| 144 | +// } |
| 145 | + |
| 146 | +// private _renderResource(model: TypeViewModel) { |
| 147 | +// this.mustacheGeneratorOptions.renderings.resource |
| 148 | +// .filter((rendering) => this._checkRenderingFilter(model, rendering)) |
| 149 | +// .forEach((rendering) => { |
| 150 | +// this.dir(rendering.path, () => { |
| 151 | +// this.file(this._calculateFilename(model, rendering), () => { |
| 152 | +// this.write(this._render(model, rendering)); |
| 153 | +// }); |
| 154 | +// }); |
| 155 | +// }); |
| 156 | +// } |
| 157 | + |
| 158 | +// private _renderComplexType(model: TypeViewModel) { |
| 159 | +// this.mustacheGeneratorOptions.renderings.complexType |
| 160 | +// .filter((rendering) => this._checkRenderingFilter(model, rendering)) |
| 161 | +// .forEach((rendering) => { |
| 162 | +// this.dir(rendering.path, () => { |
| 163 | +// this.file(this._calculateFilename(model, rendering), () => { |
| 164 | +// this.write(this._render(model, rendering)); |
| 165 | +// }); |
| 166 | +// }); |
| 167 | +// }); |
| 168 | +// } |
| 169 | + |
| 170 | +// private _calculateFilename(model: NamedViewModel, rendering: Rendering): string { |
| 171 | +// return util.format(rendering.fileNameFormat, model.saveName); |
| 172 | +// } |
| 173 | + |
| 174 | +// private _render<T extends ViewModel>(model: T, rendering: Rendering): string { |
| 175 | +// let view: View<T> = this.lambdaMixinProvider.apply({ |
| 176 | +// meta: { |
| 177 | +// timestamp: this.mustacheGeneratorOptions.meta.timestamp ?? new Date().toISOString(), |
| 178 | +// generator: this.mustacheGeneratorOptions.meta.generator ?? "FHIR-Schema Mustache Generator", |
| 179 | +// }, |
| 180 | +// model: model, |
| 181 | +// properties: rendering.properties ?? {}, |
| 182 | +// }); |
| 183 | +// if (this.debugMixinProvider) { |
| 184 | +// view = this.debugMixinProvider.apply(view); |
| 185 | +// } |
| 186 | +// return Mustache.render(this.templateFileCache.read(rendering), view, (partialName) => |
| 187 | +// this.templateFileCache.readTemplate(partialName), |
| 188 | +// ); |
| 189 | +// } |
| 190 | +// } |
0 commit comments