From 1d3c12e5478ee06cdf23364c1366f01db0c47ded Mon Sep 17 00:00:00 2001 From: Bowen Song Date: Mon, 4 Nov 2024 13:27:15 +0800 Subject: [PATCH] perf: rename api plugin manifest and spec file for kiota integration (#12644) * perf: rename api plugin manifest and spec file for kiota integration * test: update ut --- .../teamsApp/utils/CopilotGptManifestUtils.ts | 31 +++++++++++++++-- .../teamsApp/utils/PluginManifestUtils.ts | 34 +++++++++++++++++-- packages/fx-core/src/core/FxCore.ts | 6 ++-- packages/fx-core/tests/core/FxCore.test.ts | 20 +++++++---- 4 files changed, 76 insertions(+), 15 deletions(-) diff --git a/packages/fx-core/src/component/driver/teamsApp/utils/CopilotGptManifestUtils.ts b/packages/fx-core/src/component/driver/teamsApp/utils/CopilotGptManifestUtils.ts index bf34e679cb..81f43c4709 100644 --- a/packages/fx-core/src/component/driver/teamsApp/utils/CopilotGptManifestUtils.ts +++ b/packages/fx-core/src/component/driver/teamsApp/utils/CopilotGptManifestUtils.ts @@ -302,19 +302,44 @@ export class CopilotGptManifestUtils { public async getDefaultNextAvailablePluginManifestPath( folder: string, - pluginManifestFileName = DefaultPluginManifestFileName + pluginManifestFileName = DefaultPluginManifestFileName, + isKiotaIntegration = false ) { if (!(await fs.pathExists(path.join(folder, pluginManifestFileName)))) { return path.join(folder, pluginManifestFileName); } const pluginManifestNamePrefix = pluginManifestFileName.split(".")[0]; let pluginFileNameSuffix = 1; - let pluginManifestName = `${pluginManifestNamePrefix}_${pluginFileNameSuffix}.json`; + let pluginManifestName = this.getPluginManifestFileName( + pluginManifestNamePrefix, + pluginFileNameSuffix, + isKiotaIntegration + ); while (await fs.pathExists(path.join(folder, pluginManifestName))) { - pluginManifestName = `${pluginManifestNamePrefix}_${++pluginFileNameSuffix}.json`; + pluginFileNameSuffix++; + pluginManifestName = this.getPluginManifestFileName( + pluginManifestNamePrefix, + pluginFileNameSuffix, + isKiotaIntegration + ); } return path.join(folder, pluginManifestName); } + + getPluginManifestFileName( + pluginManifestNamePrefix: string, + pluginFileNameSuffix: number, + isKiotaIntegration: boolean + ) { + let pluginManifestName; + if (isKiotaIntegration) { + const pluginManifestNameSplit = pluginManifestNamePrefix.split("-"); + pluginManifestName = `${pluginManifestNameSplit[0]}_${pluginFileNameSuffix}-${pluginManifestNameSplit[1]}.json`; + } else { + pluginManifestName = `${pluginManifestNamePrefix}_${pluginFileNameSuffix}.json`; + } + return pluginManifestName; + } } export const copilotGptManifestUtils = new CopilotGptManifestUtils(); diff --git a/packages/fx-core/src/component/driver/teamsApp/utils/PluginManifestUtils.ts b/packages/fx-core/src/component/driver/teamsApp/utils/PluginManifestUtils.ts index 86008941c7..3205a87546 100644 --- a/packages/fx-core/src/component/driver/teamsApp/utils/PluginManifestUtils.ts +++ b/packages/fx-core/src/component/driver/teamsApp/utils/PluginManifestUtils.ts @@ -176,7 +176,8 @@ export class PluginManifestUtils { public async getDefaultNextAvailableApiSpecPath( apiSpecPath: string, apiSpecFolder: string, - apiSpecFileName?: string + apiSpecFileName?: string, + isKiotaIntegration = false ) { let isYaml = false; try { @@ -193,16 +194,43 @@ export class PluginManifestUtils { const openApiSpecFileNamePrefix = openApiSpecFileName.split(".")[0]; const openApiSpecFileType = openApiSpecFileName.split(".")[1]; let apiSpecFileNameSuffix = 1; - openApiSpecFileName = `${openApiSpecFileNamePrefix}_${apiSpecFileNameSuffix}.${openApiSpecFileType}`; + openApiSpecFileName = this.getApiSpecFileName( + openApiSpecFileNamePrefix, + openApiSpecFileType, + apiSpecFileNameSuffix, + isKiotaIntegration + ); while (await fs.pathExists(path.join(apiSpecFolder, openApiSpecFileName))) { - openApiSpecFileName = `${openApiSpecFileNamePrefix}_${++apiSpecFileNameSuffix}.${openApiSpecFileType}`; + apiSpecFileNameSuffix++; + openApiSpecFileName = this.getApiSpecFileName( + openApiSpecFileNamePrefix, + openApiSpecFileType, + apiSpecFileNameSuffix, + isKiotaIntegration + ); } const openApiSpecFilePath = path.join(apiSpecFolder, openApiSpecFileName); return openApiSpecFilePath; } + getApiSpecFileName( + openApiSpecFileNamePrefix: string, + openApiSpecFileType: string, + apiSpecFileNameSuffix: number, + isKiotaIntegration: boolean + ): string { + let openApiSpecFileName; + if (isKiotaIntegration) { + const apiSpecNameSplit = openApiSpecFileNamePrefix.split("-"); + openApiSpecFileName = `${apiSpecNameSplit[0]}_${apiSpecFileNameSuffix}-${apiSpecNameSplit[1]}.${openApiSpecFileType}`; + } else { + openApiSpecFileName = `${openApiSpecFileNamePrefix}_${apiSpecFileNameSuffix}.${openApiSpecFileType}`; + } + return openApiSpecFileName; + } + async getApiSpecFilePathFromPlugin( plugin: PluginManifestSchema, pluginPath: string diff --git a/packages/fx-core/src/core/FxCore.ts b/packages/fx-core/src/core/FxCore.ts index a2d031cac0..a0c047b001 100644 --- a/packages/fx-core/src/core/FxCore.ts +++ b/packages/fx-core/src/core/FxCore.ts @@ -1957,12 +1957,14 @@ export class FxCore { appPackageFolder, isKiotaIntegration ? path.basename(inputs[QuestionNames.ApiPluginManifestPath]) - : undefined + : undefined, + isKiotaIntegration ); const destinationApiSpecPath = await pluginManifestUtils.getDefaultNextAvailableApiSpecPath( url, path.join(appPackageFolder, isKiotaIntegration ? "" : DefaultApiSpecFolderName), - isKiotaIntegration ? path.basename(inputs[QuestionNames.ApiSpecLocation]) : undefined + isKiotaIntegration ? path.basename(inputs[QuestionNames.ApiSpecLocation]) : undefined, + isKiotaIntegration ); const specParser = new SpecParser(url, getParserOptions(ProjectType.Copilot, true)); const generateRes = await generateFromApiSpec( diff --git a/packages/fx-core/tests/core/FxCore.test.ts b/packages/fx-core/tests/core/FxCore.test.ts index 176ad2d9c3..456fb37665 100644 --- a/packages/fx-core/tests/core/FxCore.test.ts +++ b/packages/fx-core/tests/core/FxCore.test.ts @@ -5566,9 +5566,9 @@ describe("addPlugin", async () => { [QuestionNames.Folder]: os.tmpdir(), [QuestionNames.ApiPluginType]: ApiPluginStartOptions.apiSpec().id, [QuestionNames.TeamsAppManifestFilePath]: "manifest.json", - [QuestionNames.ApiPluginManifestPath]: "ai-plugin.json", - [QuestionNames.ApiSpecLocation]: "spec.yaml", - [QuestionNames.ApiOperation]: "ai-plugin.json", + [QuestionNames.ApiPluginManifestPath]: "aiplugin-apiplugin.json", + [QuestionNames.ApiSpecLocation]: "spec-apimanifest.yaml", + [QuestionNames.ApiOperation]: "aiplugin-apiplugin.json", projectPath: path.join(os.tmpdir(), appName), }; @@ -5598,10 +5598,16 @@ describe("addPlugin", async () => { if (path.endsWith("ai-plugin_2.json")) { return false; } - if (path.endsWith("spec.yaml")) { + if (path.endsWith("aiplugin-apiplugin.json")) { + return true; + } + if (path.endsWith("spec-apimanifest.yaml")) { + return true; + } + if (path.endsWith("aiplugin_1-apiplugin.json")) { return false; } - if (path.endsWith("ai-plugin.json")) { + if (path.endsWith("spec_1-apimanifest.yaml")) { return false; } return true; @@ -5627,8 +5633,8 @@ describe("addPlugin", async () => { projectType, outputFilePath ) => { - assert.isTrue(outputFilePath.destinationApiSpecFilePath.includes("spec.yaml")); - assert.isTrue(outputFilePath.pluginManifestFilePath?.includes("ai-plugin.json")); + assert.isTrue(outputFilePath.destinationApiSpecFilePath.includes("apimanifest.yaml")); + assert.isTrue(outputFilePath.pluginManifestFilePath?.includes("apiplugin.json")); return ok({ warnings: [] }); } );