Skip to content

Commit

Permalink
connect to kiota rpc functions
Browse files Browse the repository at this point in the history
  • Loading branch information
thewahome committed Nov 19, 2024
1 parent 4ee59e7 commit 0a6763f
Showing 1 changed file with 42 additions and 62 deletions.
104 changes: 42 additions & 62 deletions vscode/microsoft-kiota/src/commands/deleteWorkspaceItemCommand.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import * as path from 'path';
import * as vscode from "vscode";
import * as rpc from "vscode-jsonrpc/node";

import TelemetryReporter from "@vscode/extension-telemetry";
import { extensionId } from "../constants";
import { ClientObjectProperties, ConsumerOperation, generationLanguageToString, getLogEntriesForLevel, KiotaLogEntry, LogLevel, PluginObjectProperties } from "../kiotaInterop";
import { GenerateState } from "../modules/steps/generateSteps";
import { connectToKiota, getLogEntriesForLevel, KiotaLogEntry, LogLevel } from "../kiotaInterop";
import { WorkspaceTreeItem, WorkspaceTreeProvider } from "../providers/workspaceTreeProvider";
import { KiotaGenerationLanguage, KiotaPluginType } from "../types/enums";
import { getWorkspaceJsonDirectory, isPluginType, parseGenerationLanguage, parsePluginType } from "../util";
import { isPluginType } from "../util";
import { exportLogsAndShowErrors } from "../utilities/logging";
import { Command } from "./Command";
import { generateClient } from "./generate/generateClient";
import { generatePlugin } from "./generate/generatePlugin";
import { checkForSuccess } from "./generate/generation-util";

export class DeleteWorkspaceItemCommand extends Command {
Expand All @@ -37,54 +33,30 @@ export class DeleteWorkspaceItemCommand extends Command {
}

private async deleteItem(type: string, workspaceTreeItem: WorkspaceTreeItem): Promise<KiotaLogEntry[] | undefined> {
const outputPath = workspaceTreeItem.properties?.outputPath!;
const descriptionUrl = workspaceTreeItem.properties?.descriptionLocation!;

const config: Partial<GenerateState> = {
workingDirectory: getWorkspaceJsonDirectory(),
};
const absoluteOutputPath = path.join(config.workingDirectory!, outputPath);

if (type === "plugin") {
const properties = workspaceTreeItem.properties as PluginObjectProperties;
config.pluginTypes = properties.types;
config.pluginName = workspaceTreeItem.label;
return await this.removePlugin(config, absoluteOutputPath, descriptionUrl);
return await this.deletePlugin(workspaceTreeItem.label);
} else {
const properties = workspaceTreeItem.properties as ClientObjectProperties;
config.clientClassName = workspaceTreeItem.label;
config.clientNamespaceName = properties.clientNamespaceName;
return await this.removeClient(config, absoluteOutputPath, descriptionUrl);
return await this.deleteClient(workspaceTreeItem.label);
}
}

private async removePlugin(config: Partial<GenerateState>, outputPath: string, descriptionLocation: string): Promise<KiotaLogEntry[] | undefined> {
const pluginTypes = Array.isArray(config.pluginTypes) ? parsePluginType(config.pluginTypes) : [KiotaPluginType.ApiPlugin];
private async deletePlugin(pluginName: string): Promise<KiotaLogEntry[] | undefined> {
const result = await vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
cancellable: false,
title: vscode.l10n.t("Removing plugin...")
}, async (progress, _) => {
const start = performance.now();
const result = await generatePlugin(
const result = await removePlugin(
this._context,
descriptionLocation,
outputPath,
pluginTypes,
[],
[],
config.pluginName!,
true,
pluginName!,
false,
[],
ConsumerOperation.Remove,
config.workingDirectory
);
const duration = performance.now() - start;
const errorsCount = result ? getLogEntriesForLevel(result, LogLevel.critical, LogLevel.error).length : 0;
const reporter = new TelemetryReporter(this._context.extension.packageJSON.telemetryInstrumentationKey);
reporter.sendRawTelemetryEvent(`${extensionId}.removePlugin.completed`, {
"pluginType": pluginTypes.toString(),
"pluginType": pluginName,
"errorsCount": errorsCount.toString(),
}, {
"duration": duration,
Expand All @@ -93,43 +65,23 @@ export class DeleteWorkspaceItemCommand extends Command {
});
return result;
}
private async removeClient(config: Partial<GenerateState>, outputPath: string, descriptionLocation: string): Promise<KiotaLogEntry[] | undefined> {
const language =
typeof config.language === "string"
? parseGenerationLanguage(config.language)
: KiotaGenerationLanguage.CSharp;
private async deleteClient(clientName: string): Promise<KiotaLogEntry[] | undefined> {
const result = await vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
cancellable: false,
title: vscode.l10n.t("Removing client...")
}, async (progress, _) => {
const start = performance.now();
const result = await generateClient(
const result = await removeClient(
this._context,
descriptionLocation,
outputPath,
language,
[],
[],
config.clientClassName!,
config.clientNamespaceName! as string,
false,
true,
false,
false,
[],
[],
[],
[],
clientName,
false,
ConsumerOperation.Remove,
config.workingDirectory
);
const duration = performance.now() - start;
const errorsCount = result ? getLogEntriesForLevel(result, LogLevel.critical, LogLevel.error).length : 0;
const reporter = new TelemetryReporter(this._context.extension.packageJSON.telemetryInstrumentationKey);
reporter.sendRawTelemetryEvent(`${extensionId}.removeClient.completed`, {
"language": generationLanguageToString(language),
"client": clientName,
"errorsCount": errorsCount.toString(),
}, {
"duration": duration,
Expand All @@ -138,4 +90,32 @@ export class DeleteWorkspaceItemCommand extends Command {
});
return result;
}
}
}

export function removePlugin(context: vscode.ExtensionContext, pluginName: string, cleanOutput: boolean): Promise<KiotaLogEntry[] | undefined> {
return connectToKiota(context, async (connection) => {
const request = new rpc.RequestType2<string, boolean, KiotaLogEntry[], void>(
"RemovePlugin"
);
const result = await connection.sendRequest(
request,
pluginName,
cleanOutput
);
return result;
});
};

export function removeClient(context: vscode.ExtensionContext, clientName: string, cleanOutput: boolean): Promise<KiotaLogEntry[] | undefined> {
return connectToKiota(context, async (connection) => {
const request = new rpc.RequestType2<string, boolean, KiotaLogEntry[], void>(
"RemoveClient"
);
const result = await connection.sendRequest(
request,
clientName,
cleanOutput
);
return result;
});
};

0 comments on commit 0a6763f

Please sign in to comment.