Skip to content

Commit

Permalink
perf(spec-parser): update runtime if plugin file exists (#11287)
Browse files Browse the repository at this point in the history
* perf(spec-parser): update runtime if plugin file exists

* perf: update to make codecov happy

* fix: test cases

---------

Co-authored-by: turenlong <rentu@microsoft.com>
  • Loading branch information
SLdragon and SLdragon authored Apr 7, 2024
1 parent d2300f3 commit 968764c
Show file tree
Hide file tree
Showing 2 changed files with 667 additions and 29 deletions.
73 changes: 53 additions & 20 deletions packages/spec-parser/src/manifestUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ export class ManifestUpdater {
ManifestUpdater.updateManifestDescription(manifest, spec);

const specRelativePath = ManifestUpdater.getRelativePath(manifestPath, outputSpecPath);
const apiPlugin = ManifestUpdater.generatePluginManifestSchema(
const apiPlugin = await ManifestUpdater.generatePluginManifestSchema(
spec,
specRelativePath,
apiPluginFilePath,
appName,
options
);
Expand Down Expand Up @@ -92,12 +93,13 @@ export class ManifestUpdater {
return parameter;
}

static generatePluginManifestSchema(
static async generatePluginManifestSchema(
spec: OpenAPIV3.Document,
specRelativePath: string,
apiPluginFilePath: string,
appName: string,
options: ParseOptions
): PluginManifestSchema {
): Promise<PluginManifestSchema> {
const functions: FunctionObject[] = [];
const functionNames: string[] = [];

Expand Down Expand Up @@ -188,24 +190,55 @@ export class ManifestUpdater {
}
}

const apiPlugin: PluginManifestSchema = {
schema_version: "v2",
name_for_human: appName,
description_for_human: spec.info.description ?? "<Please add description of the plugin>",
functions: functions,
runtimes: [
{
type: "OpenApi",
auth: {
type: "none", // TODO, support auth in the future
},
spec: {
url: specRelativePath,
},
run_for_functions: functionNames,
let apiPlugin: PluginManifestSchema;
if (await fs.pathExists(apiPluginFilePath)) {
apiPlugin = await fs.readJSON(apiPluginFilePath);
} else {
apiPlugin = {
schema_version: "v2",
name_for_human: "",
description_for_human: "",
functions: [],
runtimes: [],
};
}

apiPlugin.functions = apiPlugin.functions || [];

for (const func of functions) {
const index = apiPlugin.functions?.findIndex((f) => f.name === func.name);
if (index === -1) {
apiPlugin.functions.push(func);
} else {
apiPlugin.functions[index] = func;
}
}

apiPlugin.runtimes = apiPlugin.runtimes || [];
const index = apiPlugin.runtimes.findIndex((runtime) => runtime.spec.url === specRelativePath);
if (index === -1) {
apiPlugin.runtimes.push({
type: "OpenApi",
auth: {
type: "none",
},
],
};
spec: {
url: specRelativePath,
},
run_for_functions: functionNames,
});
} else {
apiPlugin.runtimes[index].run_for_functions = functionNames;
}

if (!apiPlugin.name_for_human) {
apiPlugin.name_for_human = appName;
}

if (!apiPlugin.description_for_human) {
apiPlugin.description_for_human =
spec.info.description ?? "<Please add description of the plugin>";
}

return apiPlugin;
}
Expand Down
Loading

0 comments on commit 968764c

Please sign in to comment.