Skip to content

Commit 9c62645

Browse files
authored
Split Mgmt to its own plugin (Azure#48893)
1 parent 9703521 commit 9c62645

File tree

161 files changed

+23415
-12472
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

161 files changed

+23415
-12472
lines changed

eng/Packages.Data.props

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@
206206
</ItemGroup>
207207

208208
<ItemGroup Condition="'$(IsGeneratorLibrary)' == 'true'">
209-
<PackageReference Update="Microsoft.TypeSpec.Generator.ClientModel" Version="1.0.0-alpha.20250317.2" />
209+
<PackageReference Update="Microsoft.TypeSpec.Generator.ClientModel" Version="1.0.0-alpha.20250320.1" />
210+
<PackageReference Update="System.ClientModel" Version="1.3.0" />
210211
</ItemGroup>
211212

212213
<!--
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
dist/
2+
artifacts/
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"semi": true,
3+
"trailingComma": "none",
4+
"printWidth": 80,
5+
"endOfLine": "auto"
6+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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 { EmitContext } from "@typespec/compiler";
5+
import { DecoratorInfo } from "@azure-tools/typespec-client-generator-core";
6+
7+
import {
8+
CodeModel,
9+
CSharpEmitterOptions,
10+
InputModelType,
11+
setSDKContextOptions
12+
} from "@typespec/http-client-csharp";
13+
14+
import { $onEmit as $onAzureEmit } from "@azure-typespec/http-client-csharp";
15+
import { azureSDKContextOptions } from "./sdk-context-options.js";
16+
import { calculateResourceTypeFromPath } from "./resource-type.js";
17+
18+
const armResourceOperations = "Azure.ResourceManager.@armResourceOperations";
19+
const armResourceRead = "Azure.ResourceManager.@armResourceRead";
20+
const armResourceCreateOrUpdate =
21+
"Azure.ResourceManager.@armResourceCreateOrUpdate";
22+
const singleton = "Azure.ResourceManager.@singleton";
23+
const resourceMetadata = "Azure.ClientGenerator.Core.@resourceSchema";
24+
25+
export async function $onEmit(context: EmitContext<CSharpEmitterOptions>) {
26+
context.options["generator-name"] ??= "MgmtClientGenerator";
27+
context.options["update-code-model"] = updateCodeModel;
28+
context.options["emitter-extension-path"] ??= import.meta.url;
29+
context.options["sdk-context-options"] ??= azureSDKContextOptions;
30+
setSDKContextOptions(azureSDKContextOptions);
31+
await $onAzureEmit(context);
32+
}
33+
34+
function updateCodeModel(codeModel: CodeModel): CodeModel {
35+
for (const client of codeModel.clients) {
36+
// TODO: we can implement this decorator in TCGC until we meet the corner case
37+
// if the client has resourceMetadata decorator, it is a resource client and we don't need to add it again
38+
if (client.decorators?.some((d) => d.name == resourceMetadata)) {
39+
continue;
40+
}
41+
42+
// TODO: Once we have the ability to get resource hierarchy from TCGC directly, we can remove this implementation
43+
// A resource client should have decorator armResourceOperations and contains either a get operation(containing armResourceRead deocrator) or a put operation(containing armResourceCreateOrUpdate decorator)
44+
if (
45+
client.decorators?.some((d) => d.name == armResourceOperations) &&
46+
client.operations.some(
47+
(op) =>
48+
op.decorators?.some(
49+
(d) => d.name == armResourceRead || armResourceCreateOrUpdate
50+
)
51+
)
52+
) {
53+
let resourceModel: InputModelType | undefined = undefined;
54+
let isSingleton: boolean = false;
55+
let resourceType: string | undefined = undefined;
56+
// We will try to get resource metadata from put operation firstly, if not found, we will try to get it from get operation
57+
const putOperation = client.operations.find(
58+
(op) => op.decorators?.some((d) => d.name == armResourceCreateOrUpdate)
59+
);
60+
if (putOperation) {
61+
const path = putOperation.path;
62+
resourceType = calculateResourceTypeFromPath(path);
63+
resourceModel = putOperation.responses.filter((r) => r.bodyType)[0]
64+
.bodyType as InputModelType;
65+
isSingleton =
66+
resourceModel.decorators?.some((d) => d.name == singleton) ?? false;
67+
} else {
68+
const getOperation = client.operations.find(
69+
(op) => op.decorators?.some((d) => d.name == armResourceRead)
70+
);
71+
if (getOperation) {
72+
const path = getOperation.path;
73+
resourceType = calculateResourceTypeFromPath(path);
74+
resourceModel = getOperation.responses.filter((r) => r.bodyType)[0]
75+
.bodyType as InputModelType;
76+
isSingleton =
77+
resourceModel.decorators?.some((d) => d.name == singleton) ?? false;
78+
}
79+
}
80+
81+
const resourceMetadataDecorator: DecoratorInfo = {
82+
name: resourceMetadata,
83+
arguments: {}
84+
};
85+
resourceMetadataDecorator.arguments["resourceModel"] =
86+
resourceModel?.crossLanguageDefinitionId;
87+
resourceMetadataDecorator.arguments["isSingleton"] =
88+
isSingleton.toString();
89+
resourceMetadataDecorator.arguments["resourceType"] = resourceType;
90+
client.decorators.push(resourceMetadataDecorator);
91+
}
92+
}
93+
return codeModel;
94+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
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+
export * from "./emitter.js";

eng/packages/http-client-csharp/emitter/src/resource-type.ts renamed to eng/packages/http-client-csharp-mgmt/emitter/src/resource-type.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
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+
14
const ResourceGroupScopePrefix =
25
"/subscriptions/{subscriptionId}/resourceGroups";
36
const SubscriptionScopePrefix = "/subscriptions";
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"noEmit": false,
5+
"rootDir": "./src",
6+
"outDir": "../dist/emitter",
7+
"tsBuildInfoFile": "temp/tsconfig.tsbuildinfo"
8+
},
9+
"references": [],
10+
"include": ["src/"]
11+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"compilerOptions": {
3+
"noEmit": true,
4+
"composite": true,
5+
"alwaysStrict": true,
6+
"forceConsistentCasingInFileNames": true,
7+
"module": "Node16",
8+
"moduleResolution": "Node16",
9+
"esModuleInterop": true,
10+
"noImplicitAny": true,
11+
"noImplicitReturns": true,
12+
"noImplicitThis": true,
13+
"sourceMap": true,
14+
"declarationMap": true,
15+
"strict": true,
16+
"declaration": true,
17+
"stripInternal": true,
18+
"noEmitHelpers": false,
19+
"target": "ES2022",
20+
"types": ["node"],
21+
"lib": ["es2022", "DOM"],
22+
"experimentalDecorators": true,
23+
"newLine": "LF"
24+
},
25+
"include": [
26+
"src/**/*.ts",
27+
"test/**/*.ts",
28+
"vitest.config.ts",
29+
"**/*.ts",
30+
"**/*.tsx",
31+
"**/.storybook/**/*.ts",
32+
"**/.storybook/**/*.tsx"
33+
],
34+
"exclude": ["**/node_modules/", "**/dist/", "**/build/", "**/coverage/"]
35+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { defineConfig } from "vitest/config";
2+
3+
export default defineConfig({
4+
test: {
5+
environment: "node",
6+
isolate: false,
7+
coverage: {
8+
reporter: ["cobertura", "json", "text"]
9+
},
10+
outputFile: {
11+
junit: "./test-results.xml"
12+
},
13+
exclude: ["node_modules", "dist/test"]
14+
},
15+
server: {
16+
watch: {
17+
ignored: []
18+
}
19+
}
20+
});

0 commit comments

Comments
 (0)