Skip to content

Commit

Permalink
reuse item generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles Wahome committed Nov 19, 2024
1 parent 711294c commit ab673e5
Showing 1 changed file with 133 additions and 17 deletions.
150 changes: 133 additions & 17 deletions vscode/microsoft-kiota/src/commands/deleteWorkspaceItemCommand.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import * as vscode from "vscode";
import * as rpc from "vscode-jsonrpc/node";

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

export class DeleteWorkspaceItemCommand extends Command {
constructor(private _context: vscode.ExtensionContext) {
Expand All @@ -17,20 +26,127 @@ export class DeleteWorkspaceItemCommand extends Command {
}

public async execute(workspaceTreeItem: WorkspaceTreeItem): Promise<void> {
const result = await deleteItem(this._context, isPluginType(workspaceTreeItem.category!) ? "plugin" : "client", workspaceTreeItem.label);
const result = await this.deleteItem(isPluginType(workspaceTreeItem.category!) ? "plugin" : "client", workspaceTreeItem);
vscode.window.showInformationMessage(`Delete item: ${workspaceTreeItem.label}`);
}
}

export function deleteItem(context: vscode.ExtensionContext, workspaceItemType: string, key: string): Promise<KiotaLogEntry[] | undefined> {
return connectToKiota(context, async (connection) => {
const request = new rpc.RequestType1<string, KiotaLogEntry[], void>(
workspaceItemType
);
const result = await connection.sendRequest(
request,
key
);
private async generatePluginAndRefreshUI(config: Partial<GenerateState>, outputPath: string, descriptionLocation: string): Promise<KiotaLogEntry[] | undefined> {
const pluginTypes = Array.isArray(config.pluginTypes) ? parsePluginType(config.pluginTypes) : [KiotaPluginType.ApiPlugin];
const result = await vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
cancellable: false,
title: vscode.l10n.t("Generating plugin...")
}, async (progress, _) => {
const start = performance.now();
const result = await generatePlugin(
this._context,
descriptionLocation,
outputPath,
pluginTypes,
[],
[],
typeof config.pluginName === "string"
? config.pluginName
: "ApiClient",
true,
true,
[],
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}.generatePlugin.completed`, {
"pluginType": pluginTypes.toString(),
"errorsCount": errorsCount.toString(),
}, {
"duration": duration,
});
return result;
});
if (result) {
const isSuccess = await checkForSuccess(result);
if (!isSuccess) {
await exportLogsAndShowErrors(result);
}
const deepLinkParams = getDeepLinkParams();
const isttkIntegration = deepLinkParams.source?.toLowerCase() === 'ttk';
if (!isttkIntegration) {
void vscode.window.showInformationMessage(vscode.l10n.t('Plugin generated successfully.'));
}
}
return result;
});
};
}
private async generateClientAndRefreshUI(config: Partial<GenerateState>, outputPath: string, descriptionLocation: string): Promise<KiotaLogEntry[] | undefined> {
const language =
typeof config.language === "string"
? parseGenerationLanguage(config.language)
: KiotaGenerationLanguage.CSharp;
const result = await vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
cancellable: false,
title: vscode.l10n.t("Generating client...")
}, async (progress, _) => {
const start = performance.now();
const result = await generateClient(
this._context,
descriptionLocation,
outputPath,
language,
[],
[],
typeof config.clientClassName === "string"
? config.clientClassName
: "ApiClient",
typeof config.clientNamespaceName === "string"
? config.clientNamespaceName
: "ApiSdk",
true,
true,
true,
true,
[],
[],
[],
[],
true,
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}.generateClient.completed`, {
"language": generationLanguageToString(language),
"errorsCount": errorsCount.toString(),
}, {
"duration": duration,
});
return result;
});

if (result) {
const isSuccess = await checkForSuccess(result);
if (!isSuccess) {
await exportLogsAndShowErrors(result);
}
void vscode.window.showInformationMessage(vscode.l10n.t('Generation completed successfully.'));
}
return result;
}

private async deleteItem(type: string, workspaceTreeItem: WorkspaceTreeItem): Promise<KiotaLogEntry[] | undefined> {
const outputPath = workspaceTreeItem.properties?.outputPath!;
const descriptionUrl = workspaceTreeItem.properties?.descriptionLocation!;
const config: Partial<GenerateState> = {
pluginTypes: [workspaceTreeItem.properties?.types!],
workingDirectory: getWorkspaceJsonDirectory(),
};
if (type === "plugin") {
return await this.generatePluginAndRefreshUI(config, outputPath, descriptionUrl);
} else {
return await this.generateClientAndRefreshUI(config, outputPath, descriptionUrl);
}
}
}

0 comments on commit ab673e5

Please sign in to comment.