Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task: create update clients command #5586

Merged
merged 10 commits into from
Oct 14, 2024
72 changes: 72 additions & 0 deletions vscode/microsoft-kiota/src/commands/updateClientsCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import * as vscode from 'vscode';

import { API_MANIFEST_FILE, extensionId } from "../constants";
import { getExtensionSettings } from '../extensionSettings';
import { updateClients } from '../updateClients';
import { exportLogsAndShowErrors } from '../utilities/logging';
import { showUpgradeWarningMessage } from '../utilities/messaging';
import { updateStatusBarItem } from '../utilities/status';
import { Command } from "./Command";

interface UpdateClientsCommandProps {
kiotaOutputChannel: vscode.LogOutputChannel;
kiotaStatusBarItem: vscode.StatusBarItem;
}

export class UpdateClientsCommand extends Command {

constructor(private context: vscode.ExtensionContext) {
super();
}

public getName(): string {
return `${extensionId}.updateClients`;
}

public async execute({ kiotaOutputChannel, kiotaStatusBarItem }: UpdateClientsCommandProps): Promise<void> {
if (
!vscode.workspace.workspaceFolders ||
vscode.workspace.workspaceFolders.length === 0
) {
await vscode.window.showErrorMessage(
vscode.l10n.t("No workspace folder found, open a folder first")
);
return;
}
const existingApiManifestFileUris = await vscode.workspace.findFiles(`**/${API_MANIFEST_FILE}`);
if (existingApiManifestFileUris.length > 0) {
await Promise.all(existingApiManifestFileUris.map(uri => showUpgradeWarningMessage(uri, null, null, this.context)));
}
await updateStatusBarItem(this.context, kiotaOutputChannel, kiotaStatusBarItem);
try {
kiotaOutputChannel.clear();
kiotaOutputChannel.show();
kiotaOutputChannel.info(
vscode.l10n.t("updating client with path {path}", {
path: vscode.workspace.workspaceFolders[0].uri.fsPath,
})
);
const res = await vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
cancellable: false,
title: vscode.l10n.t("Updating clients...")
}, (progress, _) => {
const settings = getExtensionSettings(extensionId);
return updateClients(this.context, settings.cleanOutput, settings.clearCache);
});
if (res) {
await exportLogsAndShowErrors(res);
}
} catch (error) {
kiotaOutputChannel.error(
vscode.l10n.t("error updating the clients {error}"),
thewahome marked this conversation as resolved.
Show resolved Hide resolved
error
);
await vscode.window.showErrorMessage(
vscode.l10n.t("error updating the clients {error}"),
error as string
);
}
}

}
80 changes: 6 additions & 74 deletions vscode/microsoft-kiota/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ import { RegenerateButtonCommand } from './commands/regenerate/regenerateButtonC
import { RegenerateCommand } from './commands/regenerate/regenerateCommand';
import { SelectLockCommand } from './commands/selectLockCommand';
import { StatusCommand } from './commands/statusCommand';
import { API_MANIFEST_FILE, dependenciesInfo, extensionId, statusBarCommandId, treeViewId } from "./constants";
import { UpdateClientsCommand } from './commands/updateClientsCommand';
import { dependenciesInfo, extensionId, statusBarCommandId, treeViewId } from "./constants";
import { DependenciesViewProvider } from "./dependenciesViewProvider";
import { getExtensionSettings } from "./extensionSettings";
import { GeneratedOutputState } from './GeneratedOutputState';
import { getKiotaVersion } from "./getKiotaVersion";
import { getGenerationConfiguration } from './handlers/configurationHandler';
import { UriHandler } from './handlers/uriHandler';
import {
Expand All @@ -33,11 +33,9 @@ import {
import { checkForLockFileAndPrompt } from "./migrateFromLockFile";
import { OpenApiTreeNode, OpenApiTreeProvider } from "./openApiTreeProvider";
import { WorkspaceGenerationContext } from "./types/WorkspaceGenerationContext";
import { updateClients } from "./updateClients";
import { IntegrationParams } from './utilities/deep-linking';
import { loadWorkspaceFile } from './utilities/file';
import { exportLogsAndShowErrors } from './utilities/logging';
import { showUpgradeWarningMessage } from './utilities/messaging';
import { updateStatusBarItem } from './utilities/status';
import { loadTreeView } from "./workspaceTreeProvider";

let kiotaStatusBarItem: vscode.StatusBarItem;
Expand Down Expand Up @@ -78,6 +76,7 @@ export async function activate(
const closeDescriptionCommand = new CloseDescriptionCommand(openApiTreeProvider);
const statusCommand = new StatusCommand();
const selectLockCommand = new SelectLockCommand(openApiTreeProvider);
const updateClientsCommand = new UpdateClientsCommand(context);

await loadTreeView(context);
await checkForLockFileAndPrompt(context);
Expand Down Expand Up @@ -137,57 +136,8 @@ export async function activate(
context.subscriptions.push(kiotaStatusBarItem);

// update status bar item once at start
await updateStatusBarItem(context);
let disposable = vscode.commands.registerCommand(
`${extensionId}.updateClients`,
async () => {
if (
!vscode.workspace.workspaceFolders ||
vscode.workspace.workspaceFolders.length === 0
) {
await vscode.window.showErrorMessage(
vscode.l10n.t("No workspace folder found, open a folder first")
);
return;
}
const existingApiManifestFileUris = await vscode.workspace.findFiles(`**/${API_MANIFEST_FILE}`);
if (existingApiManifestFileUris.length > 0) {
await Promise.all(existingApiManifestFileUris.map(uri => showUpgradeWarningMessage(uri, null, null, context)));
}
await updateStatusBarItem(context);
try {
kiotaOutputChannel.clear();
kiotaOutputChannel.show();
kiotaOutputChannel.info(
vscode.l10n.t("updating client with path {path}", {
path: vscode.workspace.workspaceFolders[0].uri.fsPath,
})
);
const res = await vscode.window.withProgress({
location: vscode.ProgressLocation.Notification,
cancellable: false,
title: vscode.l10n.t("Updating clients...")
}, (progress, _) => {
const settings = getExtensionSettings(extensionId);
return updateClients(context, settings.cleanOutput, settings.clearCache);
});
if (res) {
await exportLogsAndShowErrors(res);
}
} catch (error) {
kiotaOutputChannel.error(
vscode.l10n.t("error updating the clients {error}"),
error
);
await vscode.window.showErrorMessage(
vscode.l10n.t("error updating the clients {error}"),
error as string
);
}
}
);

context.subscriptions.push(disposable);
await updateStatusBarItem(context, kiotaOutputChannel, kiotaStatusBarItem);
context.subscriptions.push(vscode.commands.registerCommand(updateClientsCommand.getName(), async () => await updateClientsCommand.execute({ kiotaOutputChannel, kiotaStatusBarItem })));
}

function registerCommandWithTelemetry(reporter: TelemetryReporter, command: string, callback: (...args: any[]) => any, thisArg?: any): vscode.Disposable {
Expand All @@ -199,23 +149,5 @@ function registerCommandWithTelemetry(reporter: TelemetryReporter, command: stri
}, thisArg);
}

async function updateStatusBarItem(context: vscode.ExtensionContext): Promise<void> {
try {
const version = await getKiotaVersion(context, kiotaOutputChannel);
if (!version) {
throw new Error("kiota not found");
}
kiotaStatusBarItem.text = `$(extensions-info-message) kiota ${version}`;
} catch (error) {
kiotaStatusBarItem.text = `$(extensions-warning-message) kiota ${vscode.l10n.t(
"not found"
)}`;
kiotaStatusBarItem.backgroundColor = new vscode.ThemeColor(
"statusBarItem.errorBackground"
);
}
kiotaStatusBarItem.show();
}

// This method is called when your extension is deactivated
export function deactivate() { }
23 changes: 23 additions & 0 deletions vscode/microsoft-kiota/src/utilities/status.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as vscode from 'vscode';

import { getKiotaVersion } from '../getKiotaVersion';

async function updateStatusBarItem(context: vscode.ExtensionContext, kiotaOutputChannel: vscode.LogOutputChannel, kiotaStatusBarItem: vscode.StatusBarItem): Promise<void> {
try {
const version = await getKiotaVersion(context, kiotaOutputChannel);
if (!version) {
throw new Error("kiota not found");
}
kiotaStatusBarItem.text = `$(extensions-info-message) kiota ${version}`;
} catch (error) {
kiotaStatusBarItem.text = `$(extensions-warning-message) kiota ${vscode.l10n.t(
"not found"
)}`;
kiotaStatusBarItem.backgroundColor = new vscode.ThemeColor(
"statusBarItem.errorBackground"
);
}
kiotaStatusBarItem.show();
}

export { updateStatusBarItem };
Loading