Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the sample generation into rlc-common & Generate samples with mock content #2002

Merged
merged 94 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
d1b84c1
Refactor the sample generation into rlc-common
MaryGao Sep 4, 2023
ddaac2f
update the reference
MaryGao Sep 4, 2023
dcf5cd3
Build a generated fake content method
MaryGao Sep 4, 2023
9882ad3
update linter issue
MaryGao Sep 4, 2023
5416961
Update the js suffixes
MaryGao Sep 4, 2023
4226999
update the config
MaryGao Sep 4, 2023
dbe739b
adjust the position
MaryGao Sep 4, 2023
304d595
refresh with generatesample=true
MaryGao Sep 4, 2023
cf92a7d
Merge branch 'main' into rlc-sample-refactoring
MaryGao Sep 5, 2023
62a31e9
build the generation framework
MaryGao Sep 5, 2023
b0ecf78
Can moke primitive data
MaryGao Sep 5, 2023
a9774b3
update the cases
MaryGao Sep 5, 2023
1a7479f
Update the samples in anomaly detector
MaryGao Sep 6, 2023
288e259
Support more types for sample type
MaryGao Sep 6, 2023
8b25749
Update all samples in smoke testing
MaryGao Sep 7, 2023
2125522
update the sample codes
MaryGao Sep 7, 2023
ecd6f29
Merge remote-tracking branch 'origin/main' into rlc-sample-refactoring
MaryGao Sep 11, 2023
76e66ff
Merge remote-tracking branch 'mary/rlc-sample-refactoring' into rlc-s…
MaryGao Sep 11, 2023
d2da2dd
Fix union bugs
MaryGao Sep 11, 2023
29c01c2
Update the normalization bug
MaryGao Sep 11, 2023
92f80ba
Remove debugging info
MaryGao Sep 11, 2023
c39367c
temp
MaryGao Sep 11, 2023
7a41d0b
update autorest samples for rlc
MaryGao Sep 11, 2023
5e55897
Update the sample gen in autorest rlc
MaryGao Sep 11, 2023
9a21c3f
Update the sample generation
MaryGao Sep 11, 2023
81ce67d
Fix union issues
MaryGao Sep 11, 2023
afcad65
Fix the fixed enum issues
MaryGao Sep 11, 2023
06ef3d0
Update the samples
MaryGao Sep 11, 2023
5a64391
Merge remote-tracking branch 'origin/main' into rlc-sample-refactoring
MaryGao Sep 11, 2023
f86af2f
Update the samples
MaryGao Sep 11, 2023
4bccbef
Update the sample gen logic for smoke test
MaryGao Sep 12, 2023
0f6f0ef
Fix boolean issue
MaryGao Sep 12, 2023
3e85fe8
Update the path
MaryGao Sep 12, 2023
f7bb325
Update the config to remove multi client settings
MaryGao Sep 12, 2023
a3665b2
Update the media types rlc samples
MaryGao Sep 12, 2023
2d57e97
Update the inherientance cases
MaryGao Sep 12, 2023
1835509
Update the test cases
MaryGao Sep 12, 2023
70e6056
Update the smoke testings
MaryGao Sep 12, 2023
e5f7a83
Update the samples in autorest side
MaryGao Sep 12, 2023
dfaf637
remove the debug tag
MaryGao Sep 12, 2023
85a9f19
Update the samples
MaryGao Sep 12, 2023
be57d21
Disable some sample testing in integration testing
MaryGao Sep 13, 2023
c9d32f9
Disable sample gen in smoke testing
MaryGao Sep 13, 2023
27f6269
Update the smoke files
MaryGao Sep 13, 2023
1b65a8d
Merge branch 'main' into rlc-sample-refactoring
MaryGao Sep 18, 2023
4306797
Update the csv info
MaryGao Sep 18, 2023
9ab23d9
Refactor the code for building import simpler
MaryGao Sep 18, 2023
285513d
Update to operation type
MaryGao Sep 18, 2023
ac4d2bf
Fix the key issues
MaryGao Sep 18, 2023
b563add
Regenerate the samples
MaryGao Sep 18, 2023
a4bed65
Merge branch 'main' into rlc-sample-refactoring
MaryGao Oct 7, 2023
d11c2c0
Refactor sample codes
MaryGao Oct 9, 2023
14ef2a0
Update the test cases for type utils
MaryGao Oct 9, 2023
720394b
Update the test cases for typeUtil.ts
MaryGao Oct 9, 2023
f25e58c
Fix lint issue in rlc-common
MaryGao Oct 9, 2023
f470f89
Add more test cases
MaryGao Oct 9, 2023
707b1f0
Merge branch 'main' into rlc-sample-refactoring
MaryGao Oct 11, 2023
483c5f5
Regenerate the samples
MaryGao Oct 11, 2023
30786a8
Merge remote-tracking branch 'origin/main' into rlc-sample-refactoring
MaryGao Oct 12, 2023
658cd53
Update the UT for getSchemaFromType
MaryGao Oct 12, 2023
5dc8c87
Revert format property
MaryGao Oct 12, 2023
9c6d253
Update the mock UT from typespec
MaryGao Oct 12, 2023
81331f4
Update our UTs
MaryGao Oct 12, 2023
d4acf33
UPdate changes
MaryGao Oct 12, 2023
806edb5
Update the samples
MaryGao Oct 12, 2023
ed00759
Fix linter issues
MaryGao Oct 12, 2023
280ed48
Fix extensible enum case
MaryGao Oct 12, 2023
4ee6f5a
Refresh the extensible enum value
MaryGao Oct 12, 2023
cc1f697
Refresh with fixes
MaryGao Oct 12, 2023
1c52847
Merge remote-tracking branch 'origin/main' into rlc-sample-refactoring
MaryGao Oct 13, 2023
b9dff8a
Update the UT for unions
MaryGao Oct 13, 2023
c42c57f
Refactor code to getallparents to getallproperties
MaryGao Oct 13, 2023
1bf35c9
Fix the inheritance case issue
MaryGao Oct 13, 2023
fc375d1
Move generation logic to transform layer
MaryGao Oct 13, 2023
81deac8
Update the ut issues
MaryGao Oct 13, 2023
94cd3a5
Refactor the code
MaryGao Oct 13, 2023
07e8a90
Update the comments
MaryGao Oct 13, 2023
63052ba
Update the generations
MaryGao Oct 13, 2023
0226909
Update the sample in swagger
MaryGao Oct 13, 2023
001e228
Update the file name
MaryGao Oct 13, 2023
9fd3c11
Remove duplicated lisence header in sampleTemplate
MaryGao Oct 16, 2023
11bb227
Update the csv info in swagger way
MaryGao Oct 16, 2023
742a79b
Refresh the generation
MaryGao Oct 16, 2023
c267c11
update the lisence header
MaryGao Oct 16, 2023
67ef7fb
Generate the code again
MaryGao Oct 16, 2023
eb44ef8
Regenerate the samples
MaryGao Oct 16, 2023
2ad11ac
Update the sample in autorest smoke
MaryGao Oct 17, 2023
455afb6
Update the more extension in eslint
MaryGao Oct 17, 2023
b9b5786
update ut
MaryGao Oct 17, 2023
2b0b22a
update more ut in rlc-common
MaryGao Oct 17, 2023
463629b
remove the type info
MaryGao Oct 17, 2023
73da3f1
Update the ut
MaryGao Oct 17, 2023
2d38a23
Merge remote-tracking branch 'origin/main' into rlc-sample-refactoring
MaryGao Oct 17, 2023
27be215
Resolve comments
MaryGao Oct 17, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@
"${workspaceFolder}/packages/*/dist-dev/**/*.js"
]
},
{
"name": "[Autorest] Run Debug Integration Test",
"request": "launch",
"cwd": "${workspaceFolder}/packages/autorest.typescript",
"runtimeArgs": [
"ts-node",
"./test/commands/test-swagger-gen.ts",
"--",
"rlc"
],
"runtimeExecutable": "npx",
"skipFiles": ["<node_internals>/**"],
"type": "pwa-node",
"outFiles": [
"${workspaceFolder}/packages/*/dist/**/*.js",
"${workspaceFolder}/packages/*/dist-dev/**/*.js"
]
},
{
"type": "node",
"request": "launch",
Expand Down Expand Up @@ -84,7 +102,7 @@
{
"name": "[TypeSpec] Smoke Test Debug",
"request": "launch",
"cwd": "${workspaceFolder}/packages/typespec-test/test/openai_modular",
"cwd": "${workspaceFolder}/packages/typespec-test/test/customWrapper",
"runtimeArgs": ["tsp", "compile", "./spec"],
"runtimeExecutable": "npx",
"skipFiles": ["<node_internals>/**"],
Expand All @@ -98,7 +116,7 @@
"runtimeArgs": [
"ts-node",
"./test/commands/gen-cadl-ranch.ts",
"--tag=modular",
"--tag=rlc",
"--debug"
],
"runtimeExecutable": "npx",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,86 +2,53 @@ import {
ExampleParameter,
TestCodeModel
} from "@autorest/testmodeler/dist/src/core/model";
import { Project } from "ts-morph";
import { getAutorestOptions, getSession } from "../../autorestSession";
import * as fs from "fs";
import * as path from "path";
import * as hbs from "handlebars";
import { NameType, normalizeName } from "../../utils/nameUtils";
import { getLanguageMetadata } from "../../utils/languageHelpers";
import { transformBaseUrl } from "../../transforms/urlTransforms";
import {
RLCSampleDetail,
RLCSampleGroup,
SampleParameter,
SampleParameters,
TestSampleParameters,
OperationMethod
} from "../../restLevelClient/interfaces";
import { camelCase } from "@azure-tools/codegen";
import { Operation, ParameterLocation } from "@autorest/codemodel";
import { isLongRunningOperation } from "../../restLevelClient/helpers/hasPollingOperations";
import { isPagingOperation } from "../../utils/extractPaginationDetails";
import { getSecurityInfoFromModel } from "../../utils/schemaHelpers";
import { getParameterAssignment } from "../../utils/valueHelpers";
import { Paths, PathMetadata } from "@azure-tools/rlc-common";
import {
Paths,
PathMetadata,
RLCSampleGroup,
RLCSampleDetail,
SampleParameter,
SampleParameters,
OperationMethod,
SampleParameterPosition
} from "@azure-tools/rlc-common";
import { transformPaths } from "../../restLevelClient/transforms/transformPaths";

type TestSampleParameters = Record<SampleParameterPosition, ExampleParameter[]>;

const tokenCredentialPackage = "@azure/identity";
const apiKeyCredentialPackage = "@azure/core-auth";

export let hasRLCSamplesGenerated = false;

export function generateRLCSamples(model: TestCodeModel, project: Project) {
export function transformRLCSampleData(
model: TestCodeModel
): RLCSampleGroup[] | undefined {
const { generateSample, multiClient } = getAutorestOptions();
const session = getSession();
if (!generateSample || !model?.testModel?.mockTest?.exampleGroups) {
return;
}
const session = getSession();
// Currently only support single client
if (multiClient) {
session.info(
"Not support to generate samples for multi-clients and return directly"
);
return;
}
const sampleGroups: RLCSampleGroup[] = transformRLCSampleData(model);
if (sampleGroups.length > 0) {
hasRLCSamplesGenerated = true;
}
for (const sampleGroup of sampleGroups) {
try {
const file = fs.readFileSync(path.join(__dirname, "rlcSamples.ts.hbs"), {
encoding: "utf-8"
});
const sampleGroupFileContents = hbs.compile(file, { noEscape: true });
project.createSourceFile(
`samples-dev/${sampleGroup.filename}.ts`,
sampleGroupFileContents(sampleGroup),
{
overwrite: true
}
);
} catch (error) {
session.error(
"An error was encountered while handling sample generation",
[sampleGroup.filename]
);
session.error(
"Stop generating samples and please inform the developers of codegen the detailed errors",
[]
);
}
}
}

export function transformRLCSampleData(model: TestCodeModel): RLCSampleGroup[] {
const rlcSampleGroups: RLCSampleGroup[] = [];
if (!model?.testModel?.mockTest?.exampleGroups) {
return rlcSampleGroups;
}
// Get all paths
const session = getSession();
const paths: Paths = transformPaths(model);
const clientName = getLanguageMetadata(model.language).name;
const clientInterfaceName = clientName.endsWith("Client")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@ import { prettierJSONOptions, prettierTypeScriptOptions } from "./config";
import * as path from "path";
import * as fsextra from "fs-extra";
import { generateSampleEnv } from "../generators/samples/sampleEnvGenerator";
import {
generateRLCSamples,
hasRLCSamplesGenerated
} from "../generators/samples/rlcSampleGenerator";
import { transform } from "./transforms/transform";
import {
buildApiExtractorConfig,
Expand All @@ -36,13 +32,15 @@ import {
buildLicenseFile,
buildReadmeFile,
buildSerializeHelper,
buildLogger
buildLogger,
buildSamples
} from "@azure-tools/rlc-common";
import {
generateFileByBuilder,
generateSchemaTypes,
generateTopLevelIndexFile
} from "./helpers/generatorHelpers";
import { buildSamplesOnMockContent } from "@azure-tools/rlc-common";

/**
* Generates a Rest Level Client library
Expand Down Expand Up @@ -70,6 +68,14 @@ export async function generateRestLevelClient() {

// then transform CodeModel to RLCModel
const rlcModels = transform(model);
if (
generateSample === true &&
generateMetadata === true &&
(rlcModels.sampleGroups ?? []).length === 0
) {
rlcModels.sampleGroups = buildSamplesOnMockContent(rlcModels);
}
const hasSampleGenerated = (rlcModels.sampleGroups ?? []).length > 0;

if (generateMetadata) {
// buildReadmeFile
Expand Down Expand Up @@ -118,31 +124,18 @@ export async function generateRestLevelClient() {
generateFileByBuilder(project, buildSerializeHelper, rlcModels);
generateFileByBuilder(project, buildLogger, rlcModels);
generateTopLevelIndexFile(rlcModels, project);
if (generateSample && generateMetadata) {
generateRLCSamples(model, project);
if (hasSampleGenerated && generateMetadata) {
generateFileByBuilder(project, buildSamples, rlcModels);
}
if (
((generateSample && hasRLCSamplesGenerated) || generateTest) &&
generateMetadata
) {
if ((hasSampleGenerated || generateTest) && generateMetadata) {
generateSampleEnv(project);
}

if (generateMetadata) {
// buildPackageFile
generateFileByBuilder(
project,
buildPackageFile,
rlcModels,
hasRLCSamplesGenerated
);
generateFileByBuilder(project, buildPackageFile, rlcModels);
MaryGao marked this conversation as resolved.
Show resolved Hide resolved
// buildTsConfig
generateFileByBuilder(
project,
buildTsConfig,
rlcModels,
hasRLCSamplesGenerated
);
generateFileByBuilder(project, buildTsConfig, rlcModels);
}

// Save the source files to the virtual filesystem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,13 @@ import { buildSchemaTypes } from "@azure-tools/rlc-common";
export function generateFileByBuilder(
project: Project,
buildFnOrList: ContentBuilder | ContentBuilder[],
rlcModels: RLCModel,
hasSampleGenerated?: boolean
rlcModels: RLCModel
) {
if (!Array.isArray(buildFnOrList)) {
buildFnOrList = [buildFnOrList];
}
for (const buildFn of buildFnOrList) {
const preparedFile: RLCFile | undefined = buildFn(
rlcModels,
hasSampleGenerated
);
generateFile(preparedFile, project);
generateFile(buildFn(rlcModels), project);
}
}

Expand All @@ -30,8 +25,17 @@ export function generateSchemaTypes(project: Project, rlcModels: RLCModel) {
generateFile(outputModelFile, project);
}

function generateFile(file: RLCFile | undefined, project: Project) {
if (file) {
function generateFile(
files: RLCFile[] | RLCFile | undefined,
project: Project
) {
if (!files) {
return;
}
if (!Array.isArray(files)) {
files = [files];
}
for (const file of files) {
project.createSourceFile(file.path, file.content, {
overwrite: true
});
Expand Down
96 changes: 0 additions & 96 deletions packages/autorest.typescript/src/restLevelClient/interfaces.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
import { transformPaths } from "./transformPaths";
import { transformResponseTypes } from "./transformResponseTypes";
import { transformSchemas } from "./transformSchemas";
import { transformRLCSampleData } from "../../generators/samples/rlcSampleGenerator";

export function transform(model: CodeModel): RLCModel {
const { srcPath } = getAutorestOptions();
Expand All @@ -53,7 +54,8 @@ export function transform(model: CodeModel): RLCModel {
parameters: transformParameterTypes(model, importDetails),
helperDetails: transformHelperDetails(model),
urlInfo: transformUrlInfo(model),
apiVersionInfo: transformApiVersion(model, urlInfo)
apiVersionInfo: transformApiVersion(model, urlInfo),
sampleGroups: transformRLCSampleData(model)
};
return rlcModel;
}
Expand Down
Loading
Loading