Skip to content

Commit 39b8216

Browse files
kazrael2119MaryGaoCopilot
authored
Support snippets for Modular/RLC/HLC (#3052)
* support snippets for modular * update * support for rlc * support typespec rlc snippets gen * rlc smoke * update autorest rlcintegration * autorest rlc smoke * support snippets for hlc * update readme gen * fix * fix * smoke * format * fix ci * fix ci * fix ci * resolve comment * Update index.ts * Update buildReadmeFile.ts * Update buildReadmeFile.ts * update * Update packages/rlc-common/src/metadata/buildReadmeFile.ts Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Mary Gao <yanmeigao1210@gmail.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 85750fc commit 39b8216

255 files changed

Lines changed: 634 additions & 429 deletions

File tree

Some content is hidden

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

packages/autorest.typescript/src/generators/static/hlcREADME.md.hbs

Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -62,50 +62,62 @@ You will also need to **register a new AAD application and grant access to {{ se
6262
For more information about how to create an Azure AD Application check out [this guide](https://learn.microsoft.com/azure/active-directory/develop/howto-create-service-principal-portal).
6363

6464
{{#if azureArm}}
65-
```javascript
66-
const { {{ clientClassName }} } = require("{{ clientPackageName }}");
67-
const { DefaultAzureCredential } = require("@azure/identity");
68-
// For client-side applications running in the browser, use InteractiveBrowserCredential instead of DefaultAzureCredential. See https://aka.ms/azsdk/js/identity/examples for more details.
65+
Using Node.js and Node-like environments, you can use the `DefaultAzureCredential` class to authenticate the client.
66+
67+
```ts {{#if azureSdkForJs}}snippet:ReadmeSampleCreateClient_Node{{/if}}
68+
import { {{ clientClassName }} } from "{{ clientPackageName }}";
69+
import { DefaultAzureCredential } from "@azure/identity";
6970

7071
{{#if hasClientSubscriptionId}}
7172
const subscriptionId = "00000000-0000-0000-0000-000000000000";
7273
const client = new {{ clientClassName }}(new DefaultAzureCredential(), subscriptionId);
73-
74-
// For client-side applications running in the browser, use this code instead:
75-
// const credential = new InteractiveBrowserCredential({
76-
// tenantId: "<YOUR_TENANT_ID>",
77-
// clientId: "<YOUR_CLIENT_ID>"
78-
// });
79-
// const client = new {{ clientClassName }}(credential, subscriptionId);
8074
```
8175
{{else}}
8276
const client = new {{ clientClassName }}(new DefaultAzureCredential());
77+
```
78+
{{/if}}
8379

84-
// For client-side applications running in the browser, use this code instead:
85-
// const credential = new InteractiveBrowserCredential({
86-
// tenantId: "<YOUR_TENANT_ID>",
87-
// clientId: "<YOUR_CLIENT_ID>"
88-
// });
89-
// const client = new {{ clientClassName }}(credential);
80+
For browser environments, use the `InteractiveBrowserCredential` from the `@azure/identity` package to authenticate.
81+
82+
```ts {{#if azureSdkForJs}}snippet:ReadmeSampleCreateClient_Browser{{/if}}
83+
import { InteractiveBrowserCredential } from "@azure/identity";
84+
import { {{ clientClassName }} } from "{{ clientPackageName }}";
85+
86+
const credential = new InteractiveBrowserCredential({
87+
tenantId: "<YOUR_TENANT_ID>",
88+
clientId: "<YOUR_CLIENT_ID>"
89+
});
90+
{{#if hasClientSubscriptionId}}
91+
const client = new {{ clientClassName }}(credential, subscriptionId);
92+
```
93+
{{else}}
94+
const client = new {{ clientClassName }}(credential);
9095
```
9196
{{/if}}
9297
{{else}}
93-
```javascript
94-
const { {{ clientClassName }} } = require("{{ clientPackageName }}");
95-
const { DefaultAzureCredential } = require("@azure/identity");
96-
// For client-side applications running in the browser, use InteractiveBrowserCredential instead of DefaultAzureCredential. See https://aka.ms/azsdk/js/identity/examples for more details.
98+
Using Node.js and Node-like environments, you can use the `DefaultAzureCredential` class to authenticate the client.
99+
100+
```ts {{#if azureSdkForJs}}snippet:ReadmeSampleCreateClient_Node{{/if}}
101+
import { InteractiveBrowserCredential } from "@azure/identity";
102+
import { {{ clientClassName }} } from "{{ clientPackageName }}";
97103

98104
const client = new {{ clientClassName }}("<endpoint>", new DefaultAzureCredential());
99-
// For client-side applications running in the browser, use this code instead:
100-
// const credential = new InteractiveBrowserCredential({
101-
// tenantId: "<YOUR_TENANT_ID>",
102-
// clientId: "<YOUR_CLIENT_ID>"
103-
// });
104-
// const client = new {{ clientClassName }}("<endpoint>", credential);
105+
```
106+
107+
For browser environments, use the `InteractiveBrowserCredential` from the `@azure/identity` package to authenticate.
108+
109+
```ts {{#if azureSdkForJs}}snippet:ReadmeSampleCreateClient_Browser{{/if}}
110+
import { InteractiveBrowserCredential } from "@azure/identity";
111+
import { {{ clientClassName }} } from "{{ clientPackageName }}";
112+
113+
const credential = new InteractiveBrowserCredential({
114+
tenantId: "<YOUR_TENANT_ID>",
115+
clientId: "<YOUR_CLIENT_ID>"
116+
});
117+
const client = new {{ clientClassName }}("<endpoint>", credential);
105118
```
106119
{{/if}}
107120
{{/if}}{{/if}}
108-
109121
### JavaScript Bundle
110122
To use this client library in the browser, first you need to use a bundler. For details on how to do this, please refer to our [bundling documentation](https://aka.ms/AzureSDKBundling).
111123

@@ -122,8 +134,9 @@ To use this client library in the browser, first you need to use a bundler. For
122134

123135
Enabling logging may help uncover useful information about failures. In order to see a log of HTTP requests and responses, set the `AZURE_LOG_LEVEL` environment variable to `info`. Alternatively, logging can be enabled at runtime by calling `setLogLevel` in the `@azure/logger`:
124136

125-
```javascript
126-
const { setLogLevel } = require("@azure/logger");
137+
```ts {{#if azureSdkForJs}}snippet:SetLogLevel{{/if}}
138+
import { setLogLevel } from "@azure/logger";
139+
127140
setLogLevel("info");
128141
```
129142

packages/autorest.typescript/src/generators/static/packageFileGenerator.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ function regularAutorestPackage(
186186
"npm run clean && tsc && dev-tool run bundle && npm run minify && dev-tool run vendored mkdirp ./review && npm run extract-api";
187187
packageInfo.scripts["clean"] = "dev-tool run vendored rimraf --glob dist dist-browser dist-esm test-dist temp types *.tgz *.log";
188188
packageInfo.scripts["extract-api"] = "dev-tool run extract-api";
189-
packageInfo.scripts["update-snippets"] = "echo skipped";
189+
packageInfo.scripts["update-snippets"] = "dev-tool run update-snippets";
190190
packageInfo.scripts["minify"] = `dev-tool run vendored uglifyjs -c -m --comments --source-map "content='./dist/index.js.map'" -o ./dist/index.min.js ./dist/index.js`;
191191
} else {
192192
packageInfo.devDependencies["@rollup/plugin-commonjs"] = "^24.0.0";
@@ -201,6 +201,7 @@ function regularAutorestPackage(
201201
if (generateTest) {
202202
packageInfo.module = `./dist-esm/src/index.js`;
203203
packageInfo.devDependencies["@azure/identity"] = "^4.2.1";
204+
packageInfo.devDependencies["@azure/logger"] = "^1.1.4";
204205
packageInfo.devDependencies["@azure-tools/test-recorder"] = "^3.0.0";
205206
packageInfo.devDependencies["@azure-tools/test-credential"] = "^1.1.0";
206207
packageInfo.devDependencies["mocha"] = "^11.0.2";

packages/autorest.typescript/src/generators/static/readmeFileGenerator.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ interface Metadata {
6464
hasMultiClients?: boolean;
6565
/** Indicates if we have a client-level subscription id paramter */
6666
hasClientSubscriptionId?: boolean;
67+
/** Indicates if the package is generted to azure-sdk-for-js repo */
68+
azureSdkForJs?: boolean;
6769
}
6870

6971
/**
@@ -83,7 +85,8 @@ function createMetadata(
8385
productDocLink,
8486
dependencyInfo,
8587
multiClient,
86-
batch
88+
batch,
89+
azureSdkForJs
8790
} = getAutorestOptions();
8891
const { addCredentials } = getSecurityInfoFromModel(codeModel.security);
8992

@@ -149,8 +152,8 @@ function createMetadata(
149152
: packageSourceURL && `${packageSourceURL}/samples`,
150153
impressionURL: azureHuh
151154
? packageParentDirectoryName &&
152-
packageDirectoryName &&
153-
`https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-js%2Fsdk%2F${packageParentDirectoryName}%2F${packageDirectoryName}%2FREADME.png`
155+
packageDirectoryName &&
156+
`https://azure-sdk-impressions.azurewebsites.net/api/impressions/azure-sdk-for-js%2Fsdk%2F${packageParentDirectoryName}%2F${packageDirectoryName}%2FREADME.png`
154157
: undefined,
155158
clientDescriptiveName: `${serviceName} client`,
156159
description: codeModel.info?.description,
@@ -168,7 +171,8 @@ function createMetadata(
168171
dependencyDescription: dependencyInfo?.description,
169172
dependencyLink: dependencyInfo?.link,
170173
hasMultiClients: multiClient && batch && batch.length > 1,
171-
hasClientSubscriptionId: hasClientSubscriptionId(clientDetails?.samples)
174+
hasClientSubscriptionId: hasClientSubscriptionId(clientDetails?.samples),
175+
azureSdkForJs: azureSdkForJs
172176
};
173177
}
174178

@@ -192,7 +196,7 @@ export function generateReadmeFile(
192196
});
193197
}
194198

195-
function hasClientSubscriptionId(samples?: SampleGroup[]) {
199+
export function hasClientSubscriptionId(samples?: SampleGroup[]) {
196200
if (!samples || samples.length === 0) {
197201
// have the subscription id parameter in constructor if no samples
198202
return true;
@@ -204,7 +208,7 @@ function hasClientSubscriptionId(samples?: SampleGroup[]) {
204208
});
205209
}
206210

207-
function getClientAndServiceName(
211+
export function getClientAndServiceName(
208212
codeModelLanguage: Languages,
209213
codeModelInfo: Info
210214
) {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
import { {{ clientClassName }} } from "../src/index.js";
5+
import { DefaultAzureCredential, InteractiveBrowserCredential } from "@azure/identity";
6+
import { setLogLevel } from "@azure/logger";
7+
import { describe, it } from "vitest";
8+
9+
describe("snippets", () => {
10+
it("ReadmeSampleCreateClient_Node", async () => {
11+
{{#if azureArm}}
12+
{{#if hasClientSubscriptionId}}
13+
const subscriptionId = "00000000-0000-0000-0000-000000000000";
14+
const client = new {{ clientClassName }}(new DefaultAzureCredential(), subscriptionId);
15+
{{else}}
16+
const client = new {{ clientClassName }}(new DefaultAzureCredential());
17+
{{/if}}
18+
{{else}}
19+
const client = new {{ clientClassName }}("<endpoint>", new DefaultAzureCredential());
20+
{{/if}}
21+
});
22+
23+
it("ReadmeSampleCreateClient_Browser", async () => {
24+
{{#if azureArm}}
25+
{{#if hasClientSubscriptionId}}
26+
const subscriptionId = "00000000-0000-0000-0000-000000000000";
27+
const credential = new InteractiveBrowserCredential({
28+
tenantId: "<YOUR_TENANT_ID>",
29+
clientId: "<YOUR_CLIENT_ID>",
30+
});
31+
const client = new {{ clientClassName }}(credential, subscriptionId);
32+
{{else}}
33+
const credential = new InteractiveBrowserCredential({
34+
tenantId: "<YOUR_TENANT_ID>",
35+
clientId: "<YOUR_CLIENT_ID>",
36+
});
37+
const client = new {{ clientClassName }}(credential);
38+
{{/if}}
39+
{{else}}
40+
const credential = new InteractiveBrowserCredential({
41+
tenantId: "<YOUR_TENANT_ID>",
42+
clientId: "<YOUR_CLIENT_ID>",
43+
});
44+
const client = new {{ clientClassName }}("<endpoint>", credential);
45+
{{/if}}
46+
});
47+
48+
it("SetLogLevel", async () => {
49+
setLogLevel("info");
50+
});
51+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { Project } from "ts-morph";
2+
import * as path from "path";
3+
import * as fs from "fs";
4+
import * as hbs from "handlebars";
5+
import { CodeModel } from "@autorest/codemodel";
6+
import { ClientDetails } from "../../models/clientDetails";
7+
import { getAutorestOptions } from "../../autorestSession";
8+
import { getClientAndServiceName, hasClientSubscriptionId } from "../static/readmeFileGenerator";
9+
10+
export function generateSnippetsFile(
11+
codeModel: CodeModel,
12+
project: Project,
13+
clientDetails?: ClientDetails
14+
) {
15+
const { generateTest, azureSdkForJs, azureArm } = getAutorestOptions();
16+
if (!generateTest || !azureSdkForJs) {
17+
return;
18+
}
19+
const { clientClassName } = getClientAndServiceName(
20+
codeModel.language,
21+
codeModel.info
22+
);
23+
const metadata = {
24+
clientClassName: clientClassName,
25+
azureArm: azureArm,
26+
hasClientSubscriptionId: hasClientSubscriptionId(clientDetails?.samples)
27+
};
28+
const file = fs.readFileSync(path.join(__dirname, "snippets.ts.hbs"), {
29+
encoding: "utf-8"
30+
});
31+
const readmeFileContents = hbs.compile(file, { noEscape: true });
32+
project.createSourceFile("test/snippets.spec.ts", readmeFileContents(metadata), {
33+
overwrite: true
34+
});
35+
}

packages/autorest.typescript/src/restLevelClient/generateRestLevel.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ import {
3232
buildSerializeHelper,
3333
buildLogger,
3434
buildSamples,
35-
updatePackageFile
35+
updatePackageFile,
36+
buildSnippets
3637
} from "@azure-tools/rlc-common";
3738
import {
3839
generateFileByBuilder,
@@ -87,6 +88,8 @@ export async function generateRestLevelClient() {
8788
generateFileByBuilder(project, buildRecordedClientFile, rlcModels);
8889
// buildSampleTest
8990
generateFileByBuilder(project, buildSampleTest, rlcModels);
91+
// buildSnippets
92+
generateFileByBuilder(project, buildSnippets, rlcModels);
9093
}
9194

9295
// buildResponseTypes

packages/autorest.typescript/src/typescriptGenerator.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { generatePagingFiles } from "./generators/pagingHelperGenerator";
3030
import { generateTracingFile } from "./generators/tracingFileGenerator";
3131
import { getAutorestOptions } from "./autorestSession";
3232
import { conflictResolver } from "./conflictResolver";
33+
import { generateSnippetsFile } from "./generators/test/snippetsGenerator";
3334

3435
const prettierTypeScriptOptions: prettier.Options = {
3536
parser: "typescript",
@@ -82,6 +83,7 @@ export async function generateTypeScriptLibrary(
8283
generateLicenseFile(project);
8384
generateReadmeFile(codeModel, project, clientDetails);
8485
if (generateTest) {
86+
generateSnippetsFile(codeModel, project, clientDetails);
8587
generateSampleTestFile(project);
8688
}
8789
generateTsConfig(project);

packages/autorest.typescript/test/integration/generated/additionalProperties/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ See our [support policy](https://github.com/Azure/azure-sdk-for-js/blob/main/SUP
1919

2020

2121

22-
2322
### JavaScript Bundle
2423
To use this client library in the browser, first you need to use a bundler. For details on how to do this, please refer to our [bundling documentation](https://aka.ms/AzureSDKBundling).
2524

packages/autorest.typescript/test/integration/generated/additionalProperties/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"rollup-plugin-sourcemaps": "^0.6.3",
3030
"uglify-js": "^3.4.9",
3131
"@azure/identity": "^4.2.1",
32+
"@azure/logger": "^1.1.4",
3233
"@azure-tools/test-recorder": "^3.0.0",
3334
"@azure-tools/test-credential": "^1.1.0",
3435
"mocha": "^11.0.2",

packages/autorest.typescript/test/integration/generated/appconfiguration/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ See our [support policy](https://github.com/Azure/azure-sdk-for-js/blob/main/SUP
1919

2020

2121

22-
2322
### JavaScript Bundle
2423
To use this client library in the browser, first you need to use a bundler. For details on how to do this, please refer to our [bundling documentation](https://aka.ms/AzureSDKBundling).
2524

0 commit comments

Comments
 (0)