Skip to content

Commit

Permalink
perf: rename api plugin manifest and spec file for kiota integration (#…
Browse files Browse the repository at this point in the history
…12644)

* perf: rename api plugin manifest and spec file for kiota integration

* test: update ut
  • Loading branch information
KennethBWSong authored Nov 4, 2024
1 parent 2f545e5 commit 1d3c12e
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ export class PluginManifestUtils {
public async getDefaultNextAvailableApiSpecPath(
apiSpecPath: string,
apiSpecFolder: string,
apiSpecFileName?: string
apiSpecFileName?: string,
isKiotaIntegration = false
) {
let isYaml = false;
try {
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions packages/fx-core/src/core/FxCore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
20 changes: 13 additions & 7 deletions packages/fx-core/tests/core/FxCore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};

Expand Down Expand Up @@ -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;
Expand All @@ -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: [] });
}
);
Expand Down

0 comments on commit 1d3c12e

Please sign in to comment.