Skip to content

Commit ad0b356

Browse files
authored
Refactor emitter options (#3105)
1 parent 68ec1a7 commit ad0b356

File tree

3 files changed

+102
-95
lines changed

3 files changed

+102
-95
lines changed

src/CADL.Extension/Emitter.Csharp/src/emitter.ts

Lines changed: 15 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
getSummary,
1313
ignoreDiagnostics,
1414
isErrorModel,
15-
JSONSchemaType,
1615
Model,
1716
ModelProperty,
1817
Operation,
@@ -73,15 +72,12 @@ import { getOperationLink } from "@azure-tools/cadl-azure-core";
7372
import fs from "fs";
7473
import path from "node:path";
7574
import { Configuration } from "./type/Configuration.js";
76-
import { dllFilePath } from "@autorest/csharp";
7775
import { execSync } from "child_process";
7876
import {
7977
Client,
8078
createDpgContext,
81-
DpgEmitterOptions,
8279
getConvenienceAPIName,
8380
isApiVersion,
84-
isOperationGroup,
8581
listClients,
8682
listOperationGroups,
8783
listOperationsInOperationGroup,
@@ -93,68 +89,12 @@ import { ClientKind } from "./type/ClientKind.js";
9389
import { getVersions } from "@cadl-lang/versioning";
9490
import { EmitContext } from "@cadl-lang/compiler/*";
9591
import { capitalize } from "./lib/utils.js";
96-
97-
export type NetEmitterOptions = {
98-
outputFile?: string;
99-
logFile?: string;
100-
namespace?: string;
101-
"library-name"?: string;
102-
"single-top-level-client"?: boolean;
103-
skipSDKGeneration?: boolean;
104-
"unreferenced-types-handling"?:
105-
| "removeOrInternalize"
106-
| "internalize"
107-
| "keepAll";
108-
"new-project"?: boolean;
109-
csharpGeneratorPath?: string;
110-
"clear-output-folder"?: boolean;
111-
"save-inputs"?: boolean;
112-
"model-namespace"?: boolean;
113-
} & DpgEmitterOptions;
114-
115-
const defaultOptions = {
116-
outputFile: "cadl.json",
117-
logFile: "log.json",
118-
skipSDKGeneration: false,
119-
"new-project": false,
120-
csharpGeneratorPath: dllFilePath,
121-
"clear-output-folder": false,
122-
"save-inputs": false,
123-
"generate-protocol-methods": true,
124-
"generate-convenience-methods": true,
125-
"package-name": undefined
126-
};
127-
128-
const NetEmitterOptionsSchema: JSONSchemaType<NetEmitterOptions> = {
129-
type: "object",
130-
additionalProperties: false,
131-
properties: {
132-
outputFile: { type: "string", nullable: true },
133-
logFile: { type: "string", nullable: true },
134-
namespace: { type: "string", nullable: true },
135-
"library-name": { type: "string", nullable: true },
136-
"single-top-level-client": { type: "boolean", nullable: true },
137-
skipSDKGeneration: { type: "boolean", default: false, nullable: true },
138-
"unreferenced-types-handling": {
139-
type: "string",
140-
enum: ["removeOrInternalize", "internalize", "keepAll"],
141-
nullable: true
142-
},
143-
"new-project": { type: "boolean", nullable: true },
144-
csharpGeneratorPath: {
145-
type: "string",
146-
default: dllFilePath,
147-
nullable: true
148-
},
149-
"clear-output-folder": { type: "boolean", nullable: true },
150-
"save-inputs": { type: "boolean", nullable: true },
151-
"model-namespace": { type: "boolean", nullable: true },
152-
"generate-protocol-methods": { type: "boolean", nullable: true },
153-
"generate-convenience-methods": { type: "boolean", nullable: true },
154-
"package-name": { type: "string", nullable: true }
155-
},
156-
required: []
157-
};
92+
import {
93+
NetEmitterOptions,
94+
NetEmitterOptionsSchema,
95+
resolveOptions,
96+
resolveOutputFolder
97+
} from "./options.js";
15898

15999
export const $lib = createCadlLibrary({
160100
name: "cadl-csharp",
@@ -166,26 +106,8 @@ export const $lib = createCadlLibrary({
166106

167107
export async function $onEmit(context: EmitContext<NetEmitterOptions>) {
168108
const program: Program = context.program;
169-
const emitterOptions = context.options;
170-
const emitterOutputDir = context.emitterOutputDir;
171-
const resolvedOptions = { ...defaultOptions, ...emitterOptions };
172-
const outputFolder = resolvePath(emitterOutputDir ?? "./cadl-output");
173-
const options: NetEmitterOptions = {
174-
outputFile: resolvePath(outputFolder, resolvedOptions.outputFile),
175-
logFile: resolvePath(
176-
emitterOutputDir ?? "./cadl-output",
177-
resolvedOptions.logFile
178-
),
179-
skipSDKGeneration: resolvedOptions.skipSDKGeneration,
180-
"unreferenced-types-handling":
181-
resolvedOptions["unreferenced-types-handling"],
182-
"new-project": resolvedOptions["new-project"],
183-
csharpGeneratorPath: resolvedOptions.csharpGeneratorPath,
184-
"clear-output-folder": resolvedOptions["clear-output-folder"],
185-
"save-inputs": resolvedOptions["save-inputs"],
186-
"model-namespace": resolvedOptions["model-namespace"]
187-
};
188-
109+
const options = resolveOptions(context);
110+
const outputFolder = resolveOutputFolder(context);
189111
if (!program.compilerOptions.noEmit && !program.hasError()) {
190112
// Write out the dotnet model to the output path
191113
const namespace = getServiceNamespaceString(program) || "";
@@ -199,12 +121,12 @@ export async function $onEmit(context: EmitContext<NetEmitterOptions>) {
199121
const resolvedSharedFolders: string[] = [];
200122
const sharedFolders = [
201123
resolvePath(
202-
options.csharpGeneratorPath ?? dllFilePath,
124+
options.csharpGeneratorPath,
203125
"..",
204126
"Generator.Shared"
205127
),
206128
resolvePath(
207-
options.csharpGeneratorPath ?? dllFilePath,
129+
options.csharpGeneratorPath,
208130
"..",
209131
"Azure.Core.Shared"
210132
)
@@ -231,14 +153,13 @@ export async function $onEmit(context: EmitContext<NetEmitterOptions>) {
231153
//emit configuration.json
232154
const configurations = {
233155
OutputFolder: ".",
234-
Namespace: resolvedOptions.namespace ?? namespace,
235-
LibraryName: resolvedOptions["library-name"] ?? null,
156+
Namespace: options.namespace ?? namespace,
157+
LibraryName: options["library-name"] ?? null,
236158
SharedSourceFolders: resolvedSharedFolders ?? [],
237-
SingleTopLevelClient:
238-
resolvedOptions["single-top-level-client"],
159+
SingleTopLevelClient: options["single-top-level-client"],
239160
"unreferenced-types-handling":
240161
options["unreferenced-types-handling"],
241-
"model-namespace": resolvedOptions["model-namespace"]
162+
"model-namespace": options["model-namespace"]
242163
} as Configuration;
243164

244165
await program.host.writeFile(
@@ -251,7 +172,7 @@ export async function $onEmit(context: EmitContext<NetEmitterOptions>) {
251172
? "--new-project"
252173
: "";
253174
const command = `dotnet --roll-forward Major ${resolvePath(
254-
options.csharpGeneratorPath ?? dllFilePath
175+
options.csharpGeneratorPath
255176
)} --project-path ${outputFolder} ${newProjectOption} --clear-output-folder ${
256177
options["clear-output-folder"]
257178
}`;
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { DpgEmitterOptions } from "@azure-tools/cadl-dpg";
2+
import { EmitContext, JSONSchemaType, resolvePath } from "@cadl-lang/compiler";
3+
import { dllFilePath } from "@autorest/csharp";
4+
5+
export type NetEmitterOptions = {
6+
outputFile?: string;
7+
logFile?: string;
8+
namespace?: string;
9+
"library-name"?: string;
10+
"single-top-level-client"?: boolean;
11+
skipSDKGeneration?: boolean;
12+
"unreferenced-types-handling"?:
13+
| "removeOrInternalize"
14+
| "internalize"
15+
| "keepAll";
16+
"new-project"?: boolean;
17+
csharpGeneratorPath?: string;
18+
"clear-output-folder"?: boolean;
19+
"save-inputs"?: boolean;
20+
"model-namespace"?: boolean;
21+
} & DpgEmitterOptions;
22+
23+
export const NetEmitterOptionsSchema: JSONSchemaType<NetEmitterOptions> = {
24+
type: "object",
25+
additionalProperties: false,
26+
properties: {
27+
outputFile: { type: "string", nullable: true },
28+
logFile: { type: "string", nullable: true },
29+
namespace: { type: "string", nullable: true },
30+
"library-name": { type: "string", nullable: true },
31+
"single-top-level-client": { type: "boolean", nullable: true },
32+
skipSDKGeneration: { type: "boolean", default: false, nullable: true },
33+
"unreferenced-types-handling": {
34+
type: "string",
35+
enum: ["removeOrInternalize", "internalize", "keepAll"],
36+
nullable: true
37+
},
38+
"new-project": { type: "boolean", nullable: true },
39+
csharpGeneratorPath: {
40+
type: "string",
41+
default: dllFilePath,
42+
nullable: true
43+
},
44+
"clear-output-folder": { type: "boolean", nullable: true },
45+
"save-inputs": { type: "boolean", nullable: true },
46+
"model-namespace": { type: "boolean", nullable: true },
47+
"generate-protocol-methods": { type: "boolean", nullable: true },
48+
"generate-convenience-methods": { type: "boolean", nullable: true },
49+
"package-name": { type: "string", nullable: true }
50+
},
51+
required: []
52+
};
53+
54+
const defaultOptions = {
55+
outputFile: "cadl.json",
56+
logFile: "log.json",
57+
skipSDKGeneration: false,
58+
"new-project": false,
59+
csharpGeneratorPath: dllFilePath,
60+
"clear-output-folder": false,
61+
"save-inputs": false,
62+
"generate-protocol-methods": true,
63+
"generate-convenience-methods": true,
64+
"package-name": undefined
65+
};
66+
67+
export function resolveOptions(context: EmitContext<NetEmitterOptions>) {
68+
const emitterOptions = context.options;
69+
const emitterOutputDir = context.emitterOutputDir;
70+
const resolvedOptions = { ...defaultOptions, ...emitterOptions };
71+
const outputFolder = resolveOutputFolder(context);
72+
return {
73+
...resolvedOptions,
74+
outputFile: resolvePath(outputFolder, resolvedOptions.outputFile),
75+
logFile: resolvePath(
76+
emitterOutputDir ?? "./cadl-output",
77+
resolvedOptions.logFile
78+
)
79+
};
80+
}
81+
82+
export function resolveOutputFolder(
83+
context: EmitContext<NetEmitterOptions>
84+
): string {
85+
return resolvePath(context.emitterOutputDir ?? "./cadl-output");
86+
}

src/CADL.Extension/Emitter.Csharp/test/Unit/utils/TestUtil.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { RestTestLibrary } from "@cadl-lang/rest/testing";
33
import { VersioningTestLibrary } from "@cadl-lang/versioning/testing";
44
import { AzureCoreTestLibrary } from "@azure-tools/cadl-azure-core/testing";
55
import { EmitContext, Program } from "@cadl-lang/compiler";
6-
import { NetEmitterOptions } from "../../../src/emitter";
6+
import { NetEmitterOptions } from "../../../src/options";
77

88
export async function createEmitterTestHost(): Promise<TestHost> {
99
return createTestHost({

0 commit comments

Comments
 (0)