|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +import { UsageFlags } from "@azure-tools/typespec-client-generator-core"; |
| 5 | +import { resolvePath } from "@typespec/compiler"; |
| 6 | +import { configurationFileName, tspOutputFileName } from "./constants.js"; |
| 7 | +import { CSharpEmitterContext } from "./sdk-context.js"; |
| 8 | +import { CodeModel } from "./type/code-model.js"; |
| 9 | +import { Configuration } from "./type/configuration.js"; |
| 10 | + |
| 11 | +/** |
| 12 | + * Writes the code model to the output folder. Should only be used by autorest.csharp. |
| 13 | + * @param context - The CSharp emitter context |
| 14 | + * @param codeModel - The code model to write |
| 15 | + * @param outputFolder - The output folder to write the code model to |
| 16 | + * @beta |
| 17 | + */ |
| 18 | +export async function writeCodeModel( |
| 19 | + context: CSharpEmitterContext, |
| 20 | + codeModel: CodeModel, |
| 21 | + outputFolder: string |
| 22 | +) { |
| 23 | + await context.program.host.writeFile( |
| 24 | + resolvePath(outputFolder, tspOutputFileName), |
| 25 | + prettierOutput( |
| 26 | + JSON.stringify( |
| 27 | + buildJson(context, codeModel), |
| 28 | + transformJSONProperties, |
| 29 | + 2 |
| 30 | + ) |
| 31 | + ) |
| 32 | + ); |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * This function builds a json from code model with refs and ids in it. |
| 37 | + * @param context - The CSharp emitter context |
| 38 | + * @param codeModel - The code model to build |
| 39 | + */ |
| 40 | +function buildJson(context: CSharpEmitterContext, codeModel: CodeModel): any { |
| 41 | + const objectsIds = new Map<any, string>(); |
| 42 | + const stack: any[] = []; |
| 43 | + |
| 44 | + return doBuildJson(codeModel, stack); |
| 45 | + |
| 46 | + function doBuildJson(obj: any, stack: any[]): any { |
| 47 | + // check if this is a primitive type or null or undefined |
| 48 | + if (!obj || typeof obj !== "object") { |
| 49 | + return obj; |
| 50 | + } |
| 51 | + // we switch here for object, arrays and primitives |
| 52 | + if (Array.isArray(obj)) { |
| 53 | + // array types |
| 54 | + return obj.map((item) => doBuildJson(item, stack)); |
| 55 | + } else { |
| 56 | + // this is an object |
| 57 | + if (shouldHaveRef(obj)) { |
| 58 | + // we will add the $id property to the object if this is the first time we see it |
| 59 | + // or returns a $ref if we have seen it before |
| 60 | + let id = objectsIds.get(obj); |
| 61 | + if (id) { |
| 62 | + // we have seen this object before |
| 63 | + return { |
| 64 | + $ref: id |
| 65 | + }; |
| 66 | + } else { |
| 67 | + // this is the first time we see this object |
| 68 | + id = (objectsIds.size + 1).toString(); |
| 69 | + objectsIds.set(obj, id); |
| 70 | + return handleObject(obj, id, stack); |
| 71 | + } |
| 72 | + } else { |
| 73 | + // this is not an object to ref |
| 74 | + return handleObject(obj, undefined, stack); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + function handleObject(obj: any, id: string | undefined, stack: any[]): any { |
| 80 | + if (stack.includes(obj)) { |
| 81 | + // we have a cyclical reference, we should not continue |
| 82 | + context.logger.warn( |
| 83 | + `Cyclical reference detected in the code model (id: ${id}).` |
| 84 | + ); |
| 85 | + return undefined; |
| 86 | + } |
| 87 | + |
| 88 | + const result: any = id === undefined ? {} : { $id: id }; |
| 89 | + stack.push(obj); |
| 90 | + |
| 91 | + for (const property in obj) { |
| 92 | + if (property === "__raw") { |
| 93 | + continue; // skip __raw property |
| 94 | + } |
| 95 | + const v = obj[property]; |
| 96 | + result[property] = doBuildJson(v, stack); |
| 97 | + } |
| 98 | + |
| 99 | + stack.pop(); |
| 100 | + return result; |
| 101 | + } |
| 102 | + |
| 103 | + function shouldHaveRef(obj: any): boolean { |
| 104 | + // we only add reference to those types with a crossLanguageDefinitionId or a kind property. |
| 105 | + // TODO -- crossLanguageDefinitionId should be enough but there is something that should be referenced but does not have it. |
| 106 | + return "crossLanguageDefinitionId" in obj || "kind" in obj; |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +export async function writeConfiguration( |
| 111 | + context: CSharpEmitterContext, |
| 112 | + configurations: Configuration, |
| 113 | + outputFolder: string |
| 114 | +) { |
| 115 | + await context.program.host.writeFile( |
| 116 | + resolvePath(outputFolder, configurationFileName), |
| 117 | + prettierOutput(JSON.stringify(configurations, null, 2)) |
| 118 | + ); |
| 119 | +} |
| 120 | + |
| 121 | +function transformJSONProperties(this: any, key: string, value: any): any { |
| 122 | + // convertUsageNumbersToStrings |
| 123 | + if (key === "usage" && typeof value === "number") { |
| 124 | + if (value === 0) { |
| 125 | + return "None"; |
| 126 | + } |
| 127 | + const result: string[] = []; |
| 128 | + for (const prop in UsageFlags) { |
| 129 | + if (!isNaN(Number(prop))) { |
| 130 | + if ((value & Number(prop)) !== 0) { |
| 131 | + result.push(UsageFlags[prop]); |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + return result.join(","); |
| 136 | + } |
| 137 | + |
| 138 | + // skip __raw if there is one |
| 139 | + if (key === "__raw") { |
| 140 | + return undefined; |
| 141 | + } |
| 142 | + |
| 143 | + return value; |
| 144 | +} |
| 145 | + |
| 146 | +function prettierOutput(output: string) { |
| 147 | + return output + "\n"; |
| 148 | +} |
0 commit comments