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 various commands #5581

Merged
merged 6 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions vscode/microsoft-kiota/src/commands/closeDescriptionCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as vscode from 'vscode';

import { treeViewId } from "../constants";
import { OpenApiTreeProvider } from "../openApiTreeProvider";
import { updateTreeViewIcons } from '../util';
import { Command } from "./Command";

export class CloseDescriptionCommand extends Command {

public constructor(private _openApiTreeProvider: OpenApiTreeProvider) {
super();
}

public getName(): string {
return `${treeViewId}.closeDescription`;
}

public async execute(): Promise<void> {
const yesAnswer = vscode.l10n.t("Yes");
const response = await vscode.window.showInformationMessage(
vscode.l10n.t("Do you want to remove this API description?"),
yesAnswer,
vscode.l10n.t("No")
);
if (response === yesAnswer) {
this._openApiTreeProvider.closeDescription();
await updateTreeViewIcons(treeViewId, false);
}
}

}
19 changes: 19 additions & 0 deletions vscode/microsoft-kiota/src/commands/selectLockCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { extensionId } from "../constants";
import { OpenApiTreeProvider } from "../openApiTreeProvider";
import { loadWorkspaceFile } from "../utilities/file";
import { Command } from "./Command";

export class SelectLockCommand extends Command {
constructor(private _openApiTreeProvider: OpenApiTreeProvider) {
super();
}

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

public async execute(node: any): Promise<void> {
await loadWorkspaceFile(node, this._openApiTreeProvider);
}

}
24 changes: 24 additions & 0 deletions vscode/microsoft-kiota/src/commands/statusCommand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as vscode from 'vscode';

import { statusBarCommandId } from "../constants";
import { Command } from "./Command";

export class StatusCommand extends Command {

public getName(): string {
return statusBarCommandId;
}

public async execute(): Promise<void> {
const yesAnswer = vscode.l10n.t("Yes");
const response = await vscode.window.showInformationMessage(
vscode.l10n.t("Open installation instructions for kiota?"),
yesAnswer,
vscode.l10n.t("No")
);
if (response === yesAnswer) {
await vscode.env.openExternal(vscode.Uri.parse("https://aka.ms/get/kiota"));
}
}

}
91 changes: 13 additions & 78 deletions vscode/microsoft-kiota/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import TelemetryReporter from '@vscode/extension-telemetry';
import * as vscode from "vscode";

import { CodeLensProvider } from "./codelensProvider";
import { CloseDescriptionCommand } from './commands/closeDescriptionCommand';
import { EditPathsCommand } from './commands/editPathsCommand';
import { GenerateClientCommand } from './commands/generate/generateClientCommand';
import { displayGenerationResults } from './commands/generate/generation-util';
Expand All @@ -17,28 +18,26 @@ import { RemoveFromSelectedEndpointsCommand } from './commands/open-api-tree-vie
import { SearchOrOpenApiDescriptionCommand } from './commands/open-api-tree-view/search-or-open-api-description/searchOrOpenApiDescriptionCommand';
import { RegenerateButtonCommand } from './commands/regenerate/regenerateButtonCommand';
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 { DependenciesViewProvider } from "./dependenciesViewProvider";
import { getExtensionSettings } from "./extensionSettings";
import { GeneratedOutputState } from './GeneratedOutputState';
import { getKiotaVersion } from "./getKiotaVersion";
import { getGenerationConfiguration } from './handlers/configurationHandler';
import { getDeepLinkParams, setDeepLinkParams } from './handlers/deepLinkParamsHandler';
import { UriHandler } from './handlers/uriHandler';
import {
ClientOrPluginProperties
} from "./kiotaInterop";
import { checkForLockFileAndPrompt } from "./migrateFromLockFile";
import { OpenApiTreeNode, OpenApiTreeProvider } from "./openApiTreeProvider";
import { WorkspaceGenerationContext } from "./types/WorkspaceGenerationContext";
import { updateClients } from "./updateClients";
import {
updateTreeViewIcons
} from "./util";
import { IntegrationParams, validateDeepLinkQueryParams } from './utilities/deep-linking';
import { IntegrationParams } from './utilities/deep-linking';
import { loadWorkspaceFile } from './utilities/file';
import { exportLogsAndShowErrors } from './utilities/logging';
import { showUpgradeWarningMessage } from './utilities/messaging';
import { openTreeViewWithProgress } from './utilities/progress';
import { loadTreeView } from "./workspaceTreeProvider";

let kiotaStatusBarItem: vscode.StatusBarItem;
Expand All @@ -63,6 +62,7 @@ export async function activate(
workspaceGenerationContext = { ...workspaceGenerationContext, ...params };
};

const uriHandler = new UriHandler(context, openApiTreeProvider);
const migrateFromLockFileCommand = new MigrateFromLockFileCommand(context);
const addAllToSelectedEndpointsCommand = new AddAllToSelectedEndpointsCommand(openApiTreeProvider);
const addToSelectedEndpointsCommand = new AddToSelectedEndpointsCommand(openApiTreeProvider);
Expand All @@ -75,54 +75,19 @@ export async function activate(
const generateClientCommand = new GenerateClientCommand(openApiTreeProvider, context, dependenciesInfoProvider, setWorkspaceGenerationContext);
const regenerateCommand = new RegenerateCommand(context, openApiTreeProvider);
const regenerateButtonCommand = new RegenerateButtonCommand(context, openApiTreeProvider);
const closeDescriptionCommand = new CloseDescriptionCommand(openApiTreeProvider);
const statusCommand = new StatusCommand();
const selectLockCommand = new SelectLockCommand(openApiTreeProvider);

await loadTreeView(context);
await checkForLockFileAndPrompt(context);
let codeLensProvider = new CodeLensProvider();
context.subscriptions.push(
vscode.window.registerUriHandler({
handleUri: async (uri: vscode.Uri) => {
if (uri.path === "/") {
return;
}
const queryParameters = getQueryParameters(uri);
if (uri.path.toLowerCase() === "/opendescription") {
let [params, errorsArray] = validateDeepLinkQueryParams(queryParameters);
setDeepLinkParams(params);
reporter.sendTelemetryEvent("DeepLink.OpenDescription initialization status", {
"queryParameters": JSON.stringify(queryParameters),
"validationErrors": errorsArray.join(", ")
});

let deepLinkParams = getDeepLinkParams();
if (deepLinkParams.descriptionurl) {
await openTreeViewWithProgress(() => openApiTreeProvider.setDescriptionUrl(deepLinkParams.descriptionurl!));
return;
}
}
void vscode.window.showErrorMessage(
vscode.l10n.t("Invalid URL, please check the documentation for the supported URLs")
);
}
}),

vscode.window.registerUriHandler({ handleUri: async (uri: vscode.Uri) => await uriHandler.handleUri(uri) }),
vscode.languages.registerCodeLensProvider('json', codeLensProvider),
reporter,
registerCommandWithTelemetry(reporter,
`${extensionId}.selectLock`,
(x) => loadWorkspaceFile(x, openApiTreeProvider)
),
registerCommandWithTelemetry(reporter, statusBarCommandId, async () => {
const yesAnswer = vscode.l10n.t("Yes");
const response = await vscode.window.showInformationMessage(
vscode.l10n.t("Open installation instructions for kiota?"),
yesAnswer,
vscode.l10n.t("No")
);
if (response === yesAnswer) {
await vscode.env.openExternal(vscode.Uri.parse("https://aka.ms/get/kiota"));
}
}),
registerCommandWithTelemetry(reporter, selectLockCommand.getName(), async (x) => await loadWorkspaceFile(x, openApiTreeProvider)),
registerCommandWithTelemetry(reporter, statusCommand.getName(), async () => await statusCommand.execute()),
vscode.window.registerWebviewViewProvider(
dependenciesInfo,
dependenciesInfoProvider
Expand All @@ -147,19 +112,7 @@ export async function activate(
registerCommandWithTelemetry(reporter, searchOrOpenApiDescriptionCommand.getName(),
async (searchParams: Partial<IntegrationParams> = {}) => await searchOrOpenApiDescriptionCommand.execute(searchParams)
),
registerCommandWithTelemetry(reporter, `${treeViewId}.closeDescription`, async () => {
const yesAnswer = vscode.l10n.t("Yes");
const response = await vscode.window.showInformationMessage(
vscode.l10n.t("Do you want to remove this API description?"),
yesAnswer,
vscode.l10n.t("No")
);
if (response === yesAnswer) {
openApiTreeProvider.closeDescription();
await updateTreeViewIcons(treeViewId, false);
}
}
),
registerCommandWithTelemetry(reporter, closeDescriptionCommand.getName(), async () => await closeDescriptionCommand.execute()),
registerCommandWithTelemetry(reporter, filterDescriptionCommand.getName(), async () => await filterDescriptionCommand.execute()),
registerCommandWithTelemetry(reporter, editPathsCommand.getName(), async (clientOrPluginKey: string, clientOrPluginObject: ClientOrPluginProperties, generationType: string) => {
setWorkspaceGenerationContext({ clientOrPluginKey, clientOrPluginObject, generationType });
Expand Down Expand Up @@ -246,10 +199,6 @@ function registerCommandWithTelemetry(reporter: TelemetryReporter, command: stri
}, thisArg);
}

async function loadEditPaths(clientOrPluginKey: string, clientObject: any, openApiTreeProvider: OpenApiTreeProvider): Promise<void> {
await openTreeViewWithProgress(() => openApiTreeProvider.loadEditPaths(clientOrPluginKey, clientObject));
}

async function updateStatusBarItem(context: vscode.ExtensionContext): Promise<void> {
try {
const version = await getKiotaVersion(context, kiotaOutputChannel);
Expand All @@ -268,19 +217,5 @@ async function updateStatusBarItem(context: vscode.ExtensionContext): Promise<vo
kiotaStatusBarItem.show();
}

function getQueryParameters(uri: vscode.Uri): Record<string, string> {
const query = uri.query;
if (!query) {
return {};
}
const queryParameters = (query.startsWith('?') ? query.substring(1) : query).split("&");
const parameters = {} as Record<string, string>;
queryParameters.forEach((element) => {
const keyValue = element.split("=");
parameters[keyValue[0].toLowerCase()] = decodeURIComponent(keyValue[1]);
});
return parameters;
}

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

import { OpenApiTreeProvider } from "../openApiTreeProvider";
import { validateDeepLinkQueryParams } from '../utilities/deep-linking';
import { openTreeViewWithProgress } from '../utilities/progress';
import { setDeepLinkParams, getDeepLinkParams } from './deepLinkParamsHandler';

export class UriHandler {
constructor(private context: vscode.ExtensionContext, private openApiTreeProvider: OpenApiTreeProvider) { }

async handleUri(uri: vscode.Uri) {
if (uri.path === "/") {
return;
}
const queryParameters = this.getQueryParameters(uri);
if (uri.path.toLowerCase() === "/opendescription") {
let [params, errorsArray] = validateDeepLinkQueryParams(queryParameters);
setDeepLinkParams(params);

const reporter = new TelemetryReporter(this.context.extension.packageJSON.telemetryInstrumentationKey);
reporter.sendTelemetryEvent("DeepLink.OpenDescription initialization status", {
"queryParameters": JSON.stringify(queryParameters),
"validationErrors": errorsArray.join(", ")
});

let deepLinkParams = getDeepLinkParams();
if (deepLinkParams.descriptionurl) {
await openTreeViewWithProgress(() => this.openApiTreeProvider.setDescriptionUrl(deepLinkParams.descriptionurl!));
return;
}
}
void vscode.window.showErrorMessage(
vscode.l10n.t("Invalid URL, please check the documentation for the supported URLs")
);
}

private getQueryParameters(uri: vscode.Uri): Record<string, string> {
const query = uri.query;
if (!query) {
return {};
}
const queryParameters = (query.startsWith('?') ? query.substring(1) : query).split("&");
const parameters = {} as Record<string, string>;
queryParameters.forEach((element) => {
const keyValue = element.split("=");
parameters[keyValue[0].toLowerCase()] = decodeURIComponent(keyValue[1]);
});
return parameters;
}
}
Loading