From 498c84207f139cbdf553ac9be730653b8685a911 Mon Sep 17 00:00:00 2001 From: ElinorW Date: Tue, 2 Jul 2024 22:54:39 +0300 Subject: [PATCH 01/17] remove workspace warnings --- vscode/microsoft-kiota/src/workspaceTreeProvider.ts | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/vscode/microsoft-kiota/src/workspaceTreeProvider.ts b/vscode/microsoft-kiota/src/workspaceTreeProvider.ts index ab590f4535..a5ded234e1 100644 --- a/vscode/microsoft-kiota/src/workspaceTreeProvider.ts +++ b/vscode/microsoft-kiota/src/workspaceTreeProvider.ts @@ -25,20 +25,13 @@ export class WorkspaceTreeProvider implements vscode.TreeDataProvider { - 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; - } + private async ensureKiotaDirectory(): Promise { const kiotaDir = path.dirname(workspaceJsonPath); try { await fs.promises.access(kiotaDir); + return true; } catch (error) { - await vscode.window.showErrorMessage( - vscode.l10n.t("Kiota directory not found") - ); + return false; } } } From 28fd20caf422b3b6b9fc816c5fb9018e9f299cf1 Mon Sep 17 00:00:00 2001 From: ElinorW Date: Tue, 2 Jul 2024 22:55:21 +0300 Subject: [PATCH 02/17] update default output path if no workspace is present --- vscode/microsoft-kiota/src/steps.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/vscode/microsoft-kiota/src/steps.ts b/vscode/microsoft-kiota/src/steps.ts index 4a652552c3..9f2266ee75 100644 --- a/vscode/microsoft-kiota/src/steps.ts +++ b/vscode/microsoft-kiota/src/steps.ts @@ -1,6 +1,9 @@ import { QuickPickItem, window, Disposable, QuickInputButton, QuickInput, QuickInputButtons, workspace, l10n, Uri, OpenDialogOptions } from 'vscode'; import { allGenerationLanguages, generationLanguageToString, KiotaSearchResultItem, LanguagesInformation, maturityLevelToString } from './kiotaInterop'; import * as vscode from 'vscode'; +import * as os from 'os'; +import * as path from 'path'; +import * as fs from 'fs'; export async function filterSteps(existingFilter: string, filterCallback: (searchQuery: string) => void) { const state = {} as Partial; @@ -108,7 +111,10 @@ export async function generateSteps(existingConfiguration: Partial Date: Tue, 2 Jul 2024 23:15:12 +0300 Subject: [PATCH 03/17] update state after generation --- vscode/microsoft-kiota/src/extension.ts | 87 ++++++++++++++----------- 1 file changed, 49 insertions(+), 38 deletions(-) diff --git a/vscode/microsoft-kiota/src/extension.ts b/vscode/microsoft-kiota/src/extension.ts index b34d05802a..1c3927cd06 100644 --- a/vscode/microsoft-kiota/src/extension.ts +++ b/vscode/microsoft-kiota/src/extension.ts @@ -38,6 +38,12 @@ let clientOrPluginKey: string; let clientOrPluginObject: ClientOrPluginProperties; let workspaceGenerationType: string; +interface GeneratedOutputState { + outputPath: string; + config: Partial; + clientClassName: string; +} + // This method is called when your extension is activated // Your extension is activated the very first time the command is executed export async function activate( @@ -46,7 +52,6 @@ export async function activate( kiotaOutputChannel = vscode.window.createOutputChannel("Kiota", { log: true, }); - const workspaceJsonPath = path.join(vscode.workspace.workspaceFolders?.map(folder => folder.uri.fsPath).join('') || '', KIOTA_DIRECTORY, KIOTA_WORKSPACE_FILE); const openApiTreeProvider = new OpenApiTreeProvider(context, () => getExtensionSettings(extensionId)); const dependenciesInfoProvider = new DependenciesViewProvider( context.extensionUri @@ -127,15 +132,6 @@ export async function activate( ); return; } - 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; - } let languagesInformation = await getLanguageInformation(context); const config = await generateSteps( { @@ -159,23 +155,48 @@ export async function activate( } const settings = getExtensionSettings(extensionId); + let result; switch (generationType) { case GenerationType.Client: - await generateClientAndRefreshUI(config, settings, outputPath, selectedPaths); + result = await generateClientAndRefreshUI(config, settings, outputPath, selectedPaths); break; case GenerationType.Plugin: - await generatePluginAndRefreshUI(config, settings, outputPath, selectedPaths); + result = await generatePluginAndRefreshUI(config, settings, outputPath, selectedPaths); break; case GenerationType.ApiManifest: - await generateManifestAndRefreshUI(config, settings, outputPath, selectedPaths); + result = await generateManifestAndRefreshUI(config, settings, outputPath, selectedPaths); default: await vscode.window.showErrorMessage( vscode.l10n.t("Invalid generation type") ); break; } + + if (result && getLogEntriesForLevel(result, LogLevel.critical, LogLevel.error).length === 0) { + // Save state before opening the new window + void context.workspaceState.update('generatedOutput', { + outputPath, + config, + clientClassName: config.clientClassName || config.pluginName + } as GeneratedOutputState); + if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length === 0) { + await vscode.window.showInformationMessage('Opening a new window with the output folder as workspace.'); + await vscode.commands.executeCommand('vscode.openFolder', vscode.Uri.file(outputPath), true); + } else { + await displayGenerationResults(context, openApiTreeProvider, config, outputPath); + } + } } ), + vscode.workspace.onDidChangeWorkspaceFolders(async () => { + const generatedOutput = context.workspaceState.get('generatedOutput'); + if (generatedOutput) { + const { outputPath, config } = generatedOutput; + await displayGenerationResults(context, openApiTreeProvider, config, outputPath); + // Clear the state + void context.workspaceState.update('generatedOutput', undefined); + } + }), registerCommandWithTelemetry(reporter, `${treeViewId}.searchOrOpenApiDescription`, async () => { @@ -265,7 +286,7 @@ export async function activate( }), ); - async function generateManifestAndRefreshUI(config: Partial, settings: ExtensionSettings, outputPath: string, selectedPaths: string[]):Promise { + async function generateManifestAndRefreshUI(config: Partial, settings: ExtensionSettings, outputPath: string, selectedPaths: string[]):Promise { const pluginTypes = KiotaPluginType.ApiManifest; const result = await vscode.window.withProgress({ location: vscode.ProgressLocation.Notification, @@ -300,15 +321,11 @@ export async function activate( }); if (result) { - await checkForSuccess(result); - openApiTreeProvider.refreshView(); - openApiTreeProvider.setSelectionChanged(false); - await loadLockFile({fsPath: workspaceJsonPath}, openApiTreeProvider, config.pluginName); - await updateTreeViewIcons(treeViewId, false, true); await exportLogsAndShowErrors(result); } + return result; } - async function generatePluginAndRefreshUI(config: Partial, settings: ExtensionSettings, outputPath: string, selectedPaths: string[]):Promise { + async function generatePluginAndRefreshUI(config: Partial, settings: ExtensionSettings, outputPath: string, selectedPaths: string[]):Promise { const pluginTypes = typeof config.pluginTypes === 'string' ? parsePluginType(config.pluginTypes) : KiotaPluginType.ApiPlugin; const result = await vscode.window.withProgress({ location: vscode.ProgressLocation.Notification, @@ -343,15 +360,11 @@ export async function activate( }); if (result) { - await checkForSuccess(result); - openApiTreeProvider.refreshView(); - openApiTreeProvider.setSelectionChanged(false); - await loadLockFile({fsPath: workspaceJsonPath}, openApiTreeProvider, config.pluginName); - await updateTreeViewIcons(treeViewId, false, true); await exportLogsAndShowErrors(result); } + return result; } - async function generateClientAndRefreshUI(config: Partial, settings: ExtensionSettings, outputPath: string, selectedPaths: string[]):Promise { + async function generateClientAndRefreshUI(config: Partial, settings: ExtensionSettings, outputPath: string, selectedPaths: string[]):Promise { const language = typeof config.language === "string" ? parseGenerationLanguage(config.language) @@ -406,22 +419,20 @@ export async function activate( dependenciesInfoProvider.update(languagesInformation, language); await vscode.commands.executeCommand(treeViewFocusCommand); } - if (typeof config.outputPath === "string" && !openApiTreeProvider.isLockFileLoaded && - vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0 && - result && getLogEntriesForLevel(result, LogLevel.critical, LogLevel.error).length === 0) { - const WORKSPACE_FOLDER = vscode.workspace.workspaceFolders[0].uri.fsPath; - const KIOTA_WORKSPACE_PATH = path.join(WORKSPACE_FOLDER, KIOTA_DIRECTORY, KIOTA_WORKSPACE_FILE); - await openApiTreeProvider.loadLockFile(KIOTA_WORKSPACE_PATH, config.clientClassName); - } if (result) { - await checkForSuccess(result); - openApiTreeProvider.refreshView(); - openApiTreeProvider.setSelectionChanged(false); - await loadLockFile({fsPath: workspaceJsonPath}, openApiTreeProvider, config.clientClassName); - await updateTreeViewIcons(treeViewId, false, true); await exportLogsAndShowErrors(result); } + return result; + } + + async function displayGenerationResults(context: vscode.ExtensionContext, openApiTreeProvider: OpenApiTreeProvider, config: any, outputPath: string) { + const clientNameOrPluginName = config.clientClassName || config.pluginName; + openApiTreeProvider.refreshView(); + openApiTreeProvider.setSelectionChanged(false); + const workspaceJsonPath = path.join(vscode.workspace.workspaceFolders?.map(folder => folder.uri.fsPath).join('') || '', KIOTA_DIRECTORY, KIOTA_WORKSPACE_FILE); + await loadLockFile({fsPath: workspaceJsonPath}, openApiTreeProvider, clientNameOrPluginName ); + await updateTreeViewIcons(treeViewId, false, true); } async function regenerateClient(clientKey: string, clientObject:any, settings: ExtensionSettings, selectedPaths?: string[]): Promise { const language = From fda85e124524ab32950a9fb20febb67f8df9a125 Mon Sep 17 00:00:00 2001 From: ElinorW Date: Thu, 11 Jul 2024 10:46:28 +0300 Subject: [PATCH 04/17] fix switch case --- vscode/microsoft-kiota/src/extension.ts | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/vscode/microsoft-kiota/src/extension.ts b/vscode/microsoft-kiota/src/extension.ts index 1c3927cd06..be0565f46b 100644 --- a/vscode/microsoft-kiota/src/extension.ts +++ b/vscode/microsoft-kiota/src/extension.ts @@ -132,6 +132,7 @@ export async function activate( ); return; } + let languagesInformation = await getLanguageInformation(context); const config = await generateSteps( { @@ -153,23 +154,24 @@ export async function activate( ); return; } - + const settings = getExtensionSettings(extensionId); let result; switch (generationType) { case GenerationType.Client: result = await generateClientAndRefreshUI(config, settings, outputPath, selectedPaths); - break; + break; case GenerationType.Plugin: result = await generatePluginAndRefreshUI(config, settings, outputPath, selectedPaths); - break; + break; case GenerationType.ApiManifest: result = await generateManifestAndRefreshUI(config, settings, outputPath, selectedPaths); + break; default: await vscode.window.showErrorMessage( vscode.l10n.t("Invalid generation type") ); - break; + return; } if (result && getLogEntriesForLevel(result, LogLevel.critical, LogLevel.error).length === 0) { @@ -180,7 +182,6 @@ export async function activate( clientClassName: config.clientClassName || config.pluginName } as GeneratedOutputState); if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length === 0) { - await vscode.window.showInformationMessage('Opening a new window with the output folder as workspace.'); await vscode.commands.executeCommand('vscode.openFolder', vscode.Uri.file(outputPath), true); } else { await displayGenerationResults(context, openApiTreeProvider, config, outputPath); From b4f501fec9fd586c6db0e2aa5f1505de6499c906 Mon Sep 17 00:00:00 2001 From: ElinorW Date: Thu, 11 Jul 2024 15:28:02 +0300 Subject: [PATCH 05/17] add workspace path function --- vscode/microsoft-kiota/src/kiotaInterop.ts | 3 ++- vscode/microsoft-kiota/src/steps.ts | 6 ++---- vscode/microsoft-kiota/src/util.ts | 10 +++++++++- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/vscode/microsoft-kiota/src/kiotaInterop.ts b/vscode/microsoft-kiota/src/kiotaInterop.ts index 01a862cd72..055cde439b 100644 --- a/vscode/microsoft-kiota/src/kiotaInterop.ts +++ b/vscode/microsoft-kiota/src/kiotaInterop.ts @@ -2,12 +2,13 @@ import * as vscode from "vscode"; import * as cp from 'child_process'; import * as rpc from 'vscode-jsonrpc/node'; import { ensureKiotaIsPresent, getKiotaPath } from './kiotaInstall'; +import { getKiotaWorkspacePath } from "./util"; export async function connectToKiota(context: vscode.ExtensionContext, callback:(connection: rpc.MessageConnection) => Promise): Promise { const kiotaPath = getKiotaPath(context); await ensureKiotaIsPresent(context); const childProcess = cp.spawn(kiotaPath, ["rpc"],{ - cwd: vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0 ? vscode.workspace.workspaceFolders[0].uri.fsPath : undefined, + cwd: getKiotaWorkspacePath(), env: { ...process.env, // eslint-disable-next-line @typescript-eslint/naming-convention diff --git a/vscode/microsoft-kiota/src/steps.ts b/vscode/microsoft-kiota/src/steps.ts index 9f2266ee75..f552cf10fd 100644 --- a/vscode/microsoft-kiota/src/steps.ts +++ b/vscode/microsoft-kiota/src/steps.ts @@ -1,9 +1,7 @@ import { QuickPickItem, window, Disposable, QuickInputButton, QuickInput, QuickInputButtons, workspace, l10n, Uri, OpenDialogOptions } from 'vscode'; import { allGenerationLanguages, generationLanguageToString, KiotaSearchResultItem, LanguagesInformation, maturityLevelToString } from './kiotaInterop'; -import * as vscode from 'vscode'; -import * as os from 'os'; -import * as path from 'path'; import * as fs from 'fs'; +import { getKiotaWorkspacePath } from './util'; export async function filterSteps(existingFilter: string, filterCallback: (searchQuery: string) => void) { const state = {} as Partial; @@ -111,7 +109,7 @@ export async function generateSteps(existingConfiguration: Partial 0 + ? vscode.workspace.workspaceFolders[0].uri.fsPath + : path.join(os.homedir(), 'kiota'); + } \ No newline at end of file From 4c4ede7eb74ccd106ac7160e87e7ced81d78309b Mon Sep 17 00:00:00 2001 From: ElinorW Date: Thu, 11 Jul 2024 23:16:43 +0300 Subject: [PATCH 06/17] return deleted line --- vscode/microsoft-kiota/src/extension.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/vscode/microsoft-kiota/src/extension.ts b/vscode/microsoft-kiota/src/extension.ts index c813838454..8ea304af5b 100644 --- a/vscode/microsoft-kiota/src/extension.ts +++ b/vscode/microsoft-kiota/src/extension.ts @@ -155,6 +155,7 @@ export async function activate( } const settings = getExtensionSettings(extensionId); + workspaceGenerationType = config.generationType as string; let result; switch (generationType) { case GenerationType.Client: From 62e57c4f78862707159c8eba6fc0c6df4e599cd6 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Fri, 12 Jul 2024 09:16:27 -0400 Subject: [PATCH 07/17] fix: fixes workspace loading when vscode does not have a folder open (#4963) --- vscode/microsoft-kiota/src/extension.ts | 4 +- .../src/workspaceTreeProvider.ts | 65 ++++++++++--------- 2 files changed, 38 insertions(+), 31 deletions(-) diff --git a/vscode/microsoft-kiota/src/extension.ts b/vscode/microsoft-kiota/src/extension.ts index c813838454..88427ff8b3 100644 --- a/vscode/microsoft-kiota/src/extension.ts +++ b/vscode/microsoft-kiota/src/extension.ts @@ -26,7 +26,7 @@ import { getLanguageInformation, getLanguageInformationForDescription } from "./ import { DependenciesViewProvider } from "./dependenciesViewProvider"; import { updateClients } from "./updateClients"; import { ExtensionSettings, getExtensionSettings } from "./extensionSettings"; -import { KiotaWorkspace } from "./workspaceTreeProvider"; +import { loadTreeView } from "./workspaceTreeProvider"; import { generatePlugin } from "./generatePlugin"; import { CodeLensProvider } from "./codelensProvider"; import { CLIENT, CLIENTS, KIOTA_DIRECTORY, KIOTA_WORKSPACE_FILE, PLUGIN, PLUGINS, dependenciesInfo, extensionId, statusBarCommandId, treeViewFocusCommand, treeViewId } from "./constants"; @@ -56,7 +56,7 @@ export async function activate( context.extensionUri ); const reporter = new TelemetryReporter(context.extension.packageJSON.telemetryInstrumentationKey); - new KiotaWorkspace(context); + await loadTreeView(context); let codeLensProvider = new CodeLensProvider(); context.subscriptions.push( vscode.window.registerUriHandler({ diff --git a/vscode/microsoft-kiota/src/workspaceTreeProvider.ts b/vscode/microsoft-kiota/src/workspaceTreeProvider.ts index a5ded234e1..2b2238f32e 100644 --- a/vscode/microsoft-kiota/src/workspaceTreeProvider.ts +++ b/vscode/microsoft-kiota/src/workspaceTreeProvider.ts @@ -3,12 +3,16 @@ import * as path from 'path'; import * as fs from 'fs'; import { KIOTA_DIRECTORY, KIOTA_WORKSPACE_FILE } from './constants'; -const workspaceJsonPath = path.join(vscode.workspace.workspaceFolders?.map(folder => folder.uri.fsPath).join('') || '', KIOTA_DIRECTORY, KIOTA_WORKSPACE_FILE); +export function getWorkspaceJsonPath(): string { + return path.join(vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0 ? + vscode.workspace.workspaceFolders[0].uri.fsPath : + process.env.HOME ?? process.env.USERPROFILE ?? process.cwd(), + KIOTA_DIRECTORY, + KIOTA_WORKSPACE_FILE); +}; export class WorkspaceTreeProvider implements vscode.TreeDataProvider { - - constructor(private context: vscode.ExtensionContext) { - void this.ensureKiotaDirectory(); + constructor(public isWSPresent: boolean) { } async getChildren(element?: vscode.TreeItem): Promise { if (!element) { @@ -19,36 +23,39 @@ export class WorkspaceTreeProvider implements vscode.TreeDataProvider { - const kiotaDir = path.dirname(workspaceJsonPath); - try { - await fs.promises.access(kiotaDir); - return true; - } catch (error) { - return false; - } - } } -export class KiotaWorkspace { - constructor(context: vscode.ExtensionContext) { - const treeDataProvider = new WorkspaceTreeProvider(context); - context.subscriptions.push(vscode.window.createTreeView('kiota.workspace', { treeDataProvider })); - vscode.commands.registerCommand('kiota.workspace.openWorkspaceFile', async (resource) => await this.openResource(resource)); +async function openResource(resource: vscode.Uri): Promise { + const workspaceJsonPath = getWorkspaceJsonPath(); + try{ + await vscode.window.showTextDocument(resource); + } catch (error) { + const dirPath = path.dirname(workspaceJsonPath); + await fs.promises.mkdir(dirPath, { recursive: true }); + await fs.promises.writeFile(workspaceJsonPath, Buffer.from('{}')); + await vscode.window.showTextDocument(resource); } - private async openResource(resource: vscode.Uri): Promise { - try{ - await vscode.window.showTextDocument(resource); - } catch (error) { - await fs.promises.writeFile(workspaceJsonPath, Buffer.from('{}')); - await vscode.window.showTextDocument(resource); - } - +} +async function isKiotaWorkspaceFilePresent(): Promise { + const workspaceFileDir = path.resolve(getWorkspaceJsonPath()); + try { + await fs.promises.access(workspaceFileDir); + } catch (error) { + return false; } -} \ No newline at end of file + return true; +} + +export async function loadTreeView(context: vscode.ExtensionContext): Promise { + const treeDataProvider = new WorkspaceTreeProvider(await isKiotaWorkspaceFilePresent()); + context.subscriptions.push(vscode.workspace.onDidChangeWorkspaceFolders(async () => { + treeDataProvider.isWSPresent = await isKiotaWorkspaceFilePresent(); + })); + context.subscriptions.push(vscode.window.createTreeView('kiota.workspace', { treeDataProvider })); + context.subscriptions.push(vscode.commands.registerCommand('kiota.workspace.openWorkspaceFile', openResource)); +} From f5749db3fb9ebdea828f14d868c0b528138472fd Mon Sep 17 00:00:00 2001 From: ElinorW Date: Mon, 15 Jul 2024 16:41:36 +0300 Subject: [PATCH 08/17] udapte workspace provider --- vscode/microsoft-kiota/src/steps.ts | 4 +- vscode/microsoft-kiota/src/util.ts | 23 +++++++---- .../src/workspaceTreeProvider.ts | 41 +++++++++---------- 3 files changed, 37 insertions(+), 31 deletions(-) diff --git a/vscode/microsoft-kiota/src/steps.ts b/vscode/microsoft-kiota/src/steps.ts index f552cf10fd..6a4574f50f 100644 --- a/vscode/microsoft-kiota/src/steps.ts +++ b/vscode/microsoft-kiota/src/steps.ts @@ -1,7 +1,7 @@ import { QuickPickItem, window, Disposable, QuickInputButton, QuickInput, QuickInputButtons, workspace, l10n, Uri, OpenDialogOptions } from 'vscode'; import { allGenerationLanguages, generationLanguageToString, KiotaSearchResultItem, LanguagesInformation, maturityLevelToString } from './kiotaInterop'; import * as fs from 'fs'; -import { getKiotaWorkspacePath } from './util'; +import { getWorkspaceJsonDirectory } from './util'; export async function filterSteps(existingFilter: string, filterCallback: (searchQuery: string) => void) { const state = {} as Partial; @@ -109,7 +109,7 @@ export async function generateSteps(existingConfiguration: Partial 0 - ? vscode.workspace.workspaceFolders[0].uri.fsPath - : path.join(os.homedir(), 'kiota'); - } \ No newline at end of file +export function getWorkspaceJsonPath(): string { + return path.join(vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0 ? + vscode.workspace.workspaceFolders[0].uri.fsPath : + process.env.HOME ?? process.env.USERPROFILE ?? process.cwd(), + KIOTA_DIRECTORY, + KIOTA_WORKSPACE_FILE); +}; + +export function getWorkspaceJsonDirectory(): string { + return path.join( + vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0 ? + vscode.workspace.workspaceFolders[0].uri.fsPath : + process.env.HOME ?? process.env.USERPROFILE ?? process.cwd(), + 'kiota'); +} \ No newline at end of file diff --git a/vscode/microsoft-kiota/src/workspaceTreeProvider.ts b/vscode/microsoft-kiota/src/workspaceTreeProvider.ts index 2b2238f32e..74f5a05a43 100644 --- a/vscode/microsoft-kiota/src/workspaceTreeProvider.ts +++ b/vscode/microsoft-kiota/src/workspaceTreeProvider.ts @@ -1,29 +1,30 @@ import * as vscode from 'vscode'; import * as path from 'path'; import * as fs from 'fs'; -import { KIOTA_DIRECTORY, KIOTA_WORKSPACE_FILE } from './constants'; +import { KIOTA_WORKSPACE_FILE } from './constants'; +import { getWorkspaceJsonPath } from './util'; -export function getWorkspaceJsonPath(): string { - return path.join(vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0 ? - vscode.workspace.workspaceFolders[0].uri.fsPath : - process.env.HOME ?? process.env.USERPROFILE ?? process.cwd(), - KIOTA_DIRECTORY, - KIOTA_WORKSPACE_FILE); -}; export class WorkspaceTreeProvider implements vscode.TreeDataProvider { constructor(public isWSPresent: boolean) { } async getChildren(element?: vscode.TreeItem): Promise { - if (!element) { - return [new vscode.TreeItem(KIOTA_WORKSPACE_FILE, vscode.TreeItemCollapsibleState.None)]; - } + if (!this.isWSPresent) { return []; + } + if (!element) { + return [new vscode.TreeItem(KIOTA_WORKSPACE_FILE, vscode.TreeItemCollapsibleState.None)]; + } + return []; } getTreeItem(element: vscode.TreeItem): vscode.TreeItem { if (element) { - element.command = { command: 'kiota.workspace.openWorkspaceFile', title: vscode.l10n.t("Open File"), arguments: [vscode.Uri.file(getWorkspaceJsonPath())], }; + element.command = { + command: 'kiota.workspace.openWorkspaceFile', + title: vscode.l10n.t("Open File"), + arguments: [vscode.Uri.file(getWorkspaceJsonPath())] + }; element.contextValue = 'file'; } return element; @@ -31,15 +32,7 @@ export class WorkspaceTreeProvider implements vscode.TreeDataProvider { - const workspaceJsonPath = getWorkspaceJsonPath(); - try{ - await vscode.window.showTextDocument(resource); - } catch (error) { - const dirPath = path.dirname(workspaceJsonPath); - await fs.promises.mkdir(dirPath, { recursive: true }); - await fs.promises.writeFile(workspaceJsonPath, Buffer.from('{}')); await vscode.window.showTextDocument(resource); - } } async function isKiotaWorkspaceFilePresent(): Promise { const workspaceFileDir = path.resolve(getWorkspaceJsonPath()); @@ -54,8 +47,12 @@ async function isKiotaWorkspaceFilePresent(): Promise { export async function loadTreeView(context: vscode.ExtensionContext): Promise { const treeDataProvider = new WorkspaceTreeProvider(await isKiotaWorkspaceFilePresent()); context.subscriptions.push(vscode.workspace.onDidChangeWorkspaceFolders(async () => { - treeDataProvider.isWSPresent = await isKiotaWorkspaceFilePresent(); - })); + treeDataProvider.isWSPresent = await isKiotaWorkspaceFilePresent(); + void vscode.commands.executeCommand('kiota.workspace.refresh'); // Refresh the tree view when workspace folders change + })); context.subscriptions.push(vscode.window.createTreeView('kiota.workspace', { treeDataProvider })); context.subscriptions.push(vscode.commands.registerCommand('kiota.workspace.openWorkspaceFile', openResource)); + context.subscriptions.push(vscode.commands.registerCommand('kiota.workspace.refresh', async () => { + treeDataProvider.isWSPresent = await isKiotaWorkspaceFilePresent(); + })); // Register refresh command } From 65228b0a918667049cfff4fdc9f85470bc236711 Mon Sep 17 00:00:00 2001 From: ElinorW Date: Mon, 15 Jul 2024 16:41:59 +0300 Subject: [PATCH 09/17] uddate cwd --- vscode/microsoft-kiota/src/kiotaInterop.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vscode/microsoft-kiota/src/kiotaInterop.ts b/vscode/microsoft-kiota/src/kiotaInterop.ts index 7cb58467b5..0e3f539cf7 100644 --- a/vscode/microsoft-kiota/src/kiotaInterop.ts +++ b/vscode/microsoft-kiota/src/kiotaInterop.ts @@ -2,13 +2,13 @@ import * as vscode from "vscode"; import * as cp from 'child_process'; import * as rpc from 'vscode-jsonrpc/node'; import { ensureKiotaIsPresent, getKiotaPath } from './kiotaInstall'; -import { getKiotaWorkspacePath } from "./util"; +import { getWorkspaceJsonDirectory } from "./util"; export async function connectToKiota(context: vscode.ExtensionContext, callback:(connection: rpc.MessageConnection) => Promise): Promise { const kiotaPath = getKiotaPath(context); await ensureKiotaIsPresent(context); const childProcess = cp.spawn(kiotaPath, ["rpc"],{ - cwd: getKiotaWorkspacePath(), + cwd: vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0 ? vscode.workspace.workspaceFolders[0].uri.fsPath : getWorkspaceJsonDirectory(), env: { ...process.env, // eslint-disable-next-line @typescript-eslint/naming-convention From cae8b1c544dce43d35c94e10a5904807322bf2dd Mon Sep 17 00:00:00 2001 From: ElinorW Date: Mon, 15 Jul 2024 18:01:12 +0300 Subject: [PATCH 10/17] update directory function --- vscode/microsoft-kiota/src/kiotaInterop.ts | 3 ++- vscode/microsoft-kiota/src/steps.ts | 5 +---- vscode/microsoft-kiota/src/util.ts | 13 ++++++++----- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/vscode/microsoft-kiota/src/kiotaInterop.ts b/vscode/microsoft-kiota/src/kiotaInterop.ts index 0e3f539cf7..20fe72c4f9 100644 --- a/vscode/microsoft-kiota/src/kiotaInterop.ts +++ b/vscode/microsoft-kiota/src/kiotaInterop.ts @@ -7,8 +7,9 @@ import { getWorkspaceJsonDirectory } from "./util"; export async function connectToKiota(context: vscode.ExtensionContext, callback:(connection: rpc.MessageConnection) => Promise): Promise { const kiotaPath = getKiotaPath(context); await ensureKiotaIsPresent(context); + const workspaceDirectory = getWorkspaceJsonDirectory(); const childProcess = cp.spawn(kiotaPath, ["rpc"],{ - cwd: vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0 ? vscode.workspace.workspaceFolders[0].uri.fsPath : getWorkspaceJsonDirectory(), + cwd: vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0 ? vscode.workspace.workspaceFolders[0].uri.fsPath : workspaceDirectory, env: { ...process.env, // eslint-disable-next-line @typescript-eslint/naming-convention diff --git a/vscode/microsoft-kiota/src/steps.ts b/vscode/microsoft-kiota/src/steps.ts index 6a4574f50f..fd1f6cef30 100644 --- a/vscode/microsoft-kiota/src/steps.ts +++ b/vscode/microsoft-kiota/src/steps.ts @@ -1,6 +1,5 @@ import { QuickPickItem, window, Disposable, QuickInputButton, QuickInput, QuickInputButtons, workspace, l10n, Uri, OpenDialogOptions } from 'vscode'; import { allGenerationLanguages, generationLanguageToString, KiotaSearchResultItem, LanguagesInformation, maturityLevelToString } from './kiotaInterop'; -import * as fs from 'fs'; import { getWorkspaceJsonDirectory } from './util'; export async function filterSteps(existingFilter: string, filterCallback: (searchQuery: string) => void) { @@ -110,9 +109,7 @@ export async function generateSteps(existingConfiguration: Partial 0 ? - vscode.workspace.workspaceFolders[0].uri.fsPath : - process.env.HOME ?? process.env.USERPROFILE ?? process.cwd(), - 'kiota'); + const workspaceFolder = path.join(vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0 ? + vscode.workspace.workspaceFolders[0].uri.fsPath : + process.env.HOME ?? process.env.USERPROFILE ?? process.cwd(),'kiota'); + if (!fs.existsSync(workspaceFolder)) { + fs.mkdirSync(workspaceFolder, { recursive: true }); +} + return workspaceFolder; } \ No newline at end of file From 6561c88e96063f474025e500ca96e555a5049373 Mon Sep 17 00:00:00 2001 From: ElinorW Date: Tue, 16 Jul 2024 15:11:39 +0300 Subject: [PATCH 11/17] update get workspace directory --- vscode/microsoft-kiota/src/kiotaInterop.ts | 3 +-- vscode/microsoft-kiota/src/util.ts | 6 +----- vscode/microsoft-kiota/src/workspaceTreeProvider.ts | 4 ++-- 3 files changed, 4 insertions(+), 9 deletions(-) diff --git a/vscode/microsoft-kiota/src/kiotaInterop.ts b/vscode/microsoft-kiota/src/kiotaInterop.ts index 20fe72c4f9..dae8a4a3d8 100644 --- a/vscode/microsoft-kiota/src/kiotaInterop.ts +++ b/vscode/microsoft-kiota/src/kiotaInterop.ts @@ -7,9 +7,8 @@ import { getWorkspaceJsonDirectory } from "./util"; export async function connectToKiota(context: vscode.ExtensionContext, callback:(connection: rpc.MessageConnection) => Promise): Promise { const kiotaPath = getKiotaPath(context); await ensureKiotaIsPresent(context); - const workspaceDirectory = getWorkspaceJsonDirectory(); const childProcess = cp.spawn(kiotaPath, ["rpc"],{ - cwd: vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0 ? vscode.workspace.workspaceFolders[0].uri.fsPath : workspaceDirectory, + cwd: getWorkspaceJsonDirectory(), env: { ...process.env, // eslint-disable-next-line @typescript-eslint/naming-convention diff --git a/vscode/microsoft-kiota/src/util.ts b/vscode/microsoft-kiota/src/util.ts index 92fca8b73d..c593ace9c6 100644 --- a/vscode/microsoft-kiota/src/util.ts +++ b/vscode/microsoft-kiota/src/util.ts @@ -22,11 +22,7 @@ export async function updateTreeViewIcons(treeViewId: string, showIcons: boolean } export function getWorkspaceJsonPath(): string { - return path.join(vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0 ? - vscode.workspace.workspaceFolders[0].uri.fsPath : - process.env.HOME ?? process.env.USERPROFILE ?? process.cwd(), - KIOTA_DIRECTORY, - KIOTA_WORKSPACE_FILE); + return path.join(getWorkspaceJsonDirectory(),KIOTA_DIRECTORY, KIOTA_WORKSPACE_FILE); }; export function getWorkspaceJsonDirectory(): string { diff --git a/vscode/microsoft-kiota/src/workspaceTreeProvider.ts b/vscode/microsoft-kiota/src/workspaceTreeProvider.ts index 74f5a05a43..a82efd3c27 100644 --- a/vscode/microsoft-kiota/src/workspaceTreeProvider.ts +++ b/vscode/microsoft-kiota/src/workspaceTreeProvider.ts @@ -48,11 +48,11 @@ export async function loadTreeView(context: vscode.ExtensionContext): Promise { treeDataProvider.isWSPresent = await isKiotaWorkspaceFilePresent(); - void vscode.commands.executeCommand('kiota.workspace.refresh'); // Refresh the tree view when workspace folders change + await vscode.commands.executeCommand('kiota.workspace.refresh'); // Refresh the tree view when workspace folders change })); context.subscriptions.push(vscode.window.createTreeView('kiota.workspace', { treeDataProvider })); context.subscriptions.push(vscode.commands.registerCommand('kiota.workspace.openWorkspaceFile', openResource)); context.subscriptions.push(vscode.commands.registerCommand('kiota.workspace.refresh', async () => { treeDataProvider.isWSPresent = await isKiotaWorkspaceFilePresent(); - })); // Register refresh command + })); } From 90199f571291c4007ab035b445cd21e451fa3641 Mon Sep 17 00:00:00 2001 From: ElinorW Date: Tue, 16 Jul 2024 16:21:51 +0300 Subject: [PATCH 12/17] update workspace function --- vscode/microsoft-kiota/src/util.ts | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/vscode/microsoft-kiota/src/util.ts b/vscode/microsoft-kiota/src/util.ts index c593ace9c6..272ca411d9 100644 --- a/vscode/microsoft-kiota/src/util.ts +++ b/vscode/microsoft-kiota/src/util.ts @@ -26,9 +26,11 @@ export function getWorkspaceJsonPath(): string { }; export function getWorkspaceJsonDirectory(): string { - const workspaceFolder = path.join(vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0 ? + const baseDir = path.join(vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0 ? vscode.workspace.workspaceFolders[0].uri.fsPath : - process.env.HOME ?? process.env.USERPROFILE ?? process.cwd(),'kiota'); + process.env.HOME ?? process.env.USERPROFILE ?? process.cwd()); + const workspaceFolder = !vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length === 0 ? + path.join(baseDir, 'kiota') : baseDir ; if (!fs.existsSync(workspaceFolder)) { fs.mkdirSync(workspaceFolder, { recursive: true }); } From 51f6b4494c815fd2d6b72ec72ff9b4d48c07c811 Mon Sep 17 00:00:00 2001 From: ElinorW Date: Tue, 16 Jul 2024 16:22:10 +0300 Subject: [PATCH 13/17] correct return types --- vscode/microsoft-kiota/src/extension.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/vscode/microsoft-kiota/src/extension.ts b/vscode/microsoft-kiota/src/extension.ts index 080f5601ef..35ec43b30a 100644 --- a/vscode/microsoft-kiota/src/extension.ts +++ b/vscode/microsoft-kiota/src/extension.ts @@ -173,7 +173,6 @@ export async function activate( ); return; } - if (result && getLogEntriesForLevel(result, LogLevel.critical, LogLevel.error).length === 0) { // Save state before opening the new window void context.workspaceState.update('generatedOutput', { @@ -296,7 +295,7 @@ export async function activate( }), ); - async function generateManifestAndRefreshUI(config: Partial, settings: ExtensionSettings, outputPath: string, selectedPaths: string[]):Promise { + async function generateManifestAndRefreshUI(config: Partial, settings: ExtensionSettings, outputPath: string, selectedPaths: string[]):Promise { const pluginTypes = KiotaPluginType.ApiManifest; const result = await vscode.window.withProgress({ location: vscode.ProgressLocation.Notification, @@ -335,7 +334,7 @@ export async function activate( } return result; } - async function generatePluginAndRefreshUI(config: Partial, settings: ExtensionSettings, outputPath: string, selectedPaths: string[]):Promise { + async function generatePluginAndRefreshUI(config: Partial, settings: ExtensionSettings, outputPath: string, selectedPaths: string[]):Promise { const pluginTypes = Array.isArray(config.pluginTypes) ? parsePluginType(config.pluginTypes) : [KiotaPluginType.ApiPlugin]; const result = await vscode.window.withProgress({ location: vscode.ProgressLocation.Notification, @@ -374,7 +373,7 @@ export async function activate( } return result; } - async function generateClientAndRefreshUI(config: Partial, settings: ExtensionSettings, outputPath: string, selectedPaths: string[]):Promise { + async function generateClientAndRefreshUI(config: Partial, settings: ExtensionSettings, outputPath: string, selectedPaths: string[]):Promise { const language = typeof config.language === "string" ? parseGenerationLanguage(config.language) From a6ab934c0a6ca7d90bc5bc91eab7888615c3ce77 Mon Sep 17 00:00:00 2001 From: ElinorW Date: Wed, 17 Jul 2024 15:02:12 +0300 Subject: [PATCH 14/17] add appPackage as default output path --- vscode/microsoft-kiota/src/steps.ts | 12 +++++--- vscode/microsoft-kiota/src/util.ts | 44 ++++++++++++++++++++++++----- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/vscode/microsoft-kiota/src/steps.ts b/vscode/microsoft-kiota/src/steps.ts index fd1f6cef30..bf527e5417 100644 --- a/vscode/microsoft-kiota/src/steps.ts +++ b/vscode/microsoft-kiota/src/steps.ts @@ -1,6 +1,6 @@ import { QuickPickItem, window, Disposable, QuickInputButton, QuickInput, QuickInputButtons, workspace, l10n, Uri, OpenDialogOptions } from 'vscode'; import { allGenerationLanguages, generationLanguageToString, KiotaSearchResultItem, LanguagesInformation, maturityLevelToString } from './kiotaInterop'; -import { getWorkspaceJsonDirectory } from './util'; +import { findAppPackageDirectory, getWorkspaceJsonDirectory } from './util'; export async function filterSteps(existingFilter: string, filterCallback: (searchQuery: string) => void) { const state = {} as Partial; @@ -107,10 +107,14 @@ export async function generateSteps(existingConfiguration: Partial 0 ? - vscode.workspace.workspaceFolders[0].uri.fsPath : - process.env.HOME ?? process.env.USERPROFILE ?? process.cwd()); - const workspaceFolder = !vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length === 0 ? - path.join(baseDir, 'kiota') : baseDir ; - if (!fs.existsSync(workspaceFolder)) { + const baseDir = path.join( + vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0 + ? vscode.workspace.workspaceFolders[0].uri.fsPath + : process.env.HOME ?? process.env.USERPROFILE ?? process.cwd() + ); + + let workspaceFolder = !vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length === 0 + ? path.join(baseDir, 'kiota') + : baseDir; + +if (!fs.existsSync(workspaceFolder)) { fs.mkdirSync(workspaceFolder, { recursive: true }); -} + } return workspaceFolder; +} + +export function findAppPackageDirectory(directory: string): string | null { + if (!fs.existsSync(directory)) { + return null; + } + + const entries = fs.readdirSync(directory, { withFileTypes: true }); + + for (const entry of entries) { + const fullPath = path.join(directory, entry.name); + + if (entry.isDirectory()) { + if (entry.name === 'appPackage') { + return fullPath; + } + + const subDirectory = findAppPackageDirectory(fullPath); + if (subDirectory) { + return subDirectory; + } + } + } + + return null; } \ No newline at end of file From da954ed479645c9bbf2f0f827051d43c3f2a5de5 Mon Sep 17 00:00:00 2001 From: Elinor Date: Thu, 18 Jul 2024 14:44:21 +0300 Subject: [PATCH 15/17] update indentation Co-authored-by: Andrew Omondi --- vscode/microsoft-kiota/src/util.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vscode/microsoft-kiota/src/util.ts b/vscode/microsoft-kiota/src/util.ts index db1a33e479..8b99dc5f39 100644 --- a/vscode/microsoft-kiota/src/util.ts +++ b/vscode/microsoft-kiota/src/util.ts @@ -36,7 +36,7 @@ export function getWorkspaceJsonDirectory(): string { ? path.join(baseDir, 'kiota') : baseDir; -if (!fs.existsSync(workspaceFolder)) { + if (!fs.existsSync(workspaceFolder)) { fs.mkdirSync(workspaceFolder, { recursive: true }); } return workspaceFolder; From 0118d783519adc5e20d99800c0482e145e3d5238 Mon Sep 17 00:00:00 2001 From: ElinorW Date: Thu, 18 Jul 2024 18:54:09 +0300 Subject: [PATCH 16/17] modify outputPath --- vscode/microsoft-kiota/src/extension.ts | 6 +++--- vscode/microsoft-kiota/src/steps.ts | 16 +++++++++++++++- vscode/microsoft-kiota/src/util.ts | 4 ++-- 3 files changed, 20 insertions(+), 6 deletions(-) diff --git a/vscode/microsoft-kiota/src/extension.ts b/vscode/microsoft-kiota/src/extension.ts index 9cb6bd2125..c084db20c8 100644 --- a/vscode/microsoft-kiota/src/extension.ts +++ b/vscode/microsoft-kiota/src/extension.ts @@ -29,7 +29,7 @@ import { loadTreeView } from "./workspaceTreeProvider"; import { generatePlugin } from "./generatePlugin"; import { CodeLensProvider } from "./codelensProvider"; import { KIOTA_DIRECTORY, KIOTA_WORKSPACE_FILE, dependenciesInfo, extensionId, statusBarCommandId, treeViewFocusCommand, treeViewId } from "./constants"; -import { isClientType, isPluginType, updateTreeViewIcons } from "./util"; +import { getWorkspaceJsonDirectory, getWorkspaceJsonPath, isClientType, isPluginType, updateTreeViewIcons } from "./util"; let kiotaStatusBarItem: vscode.StatusBarItem; let kiotaOutputChannel: vscode.LogOutputChannel; @@ -181,7 +181,7 @@ export async function activate( clientClassName: config.clientClassName || config.pluginName } as GeneratedOutputState); if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length === 0) { - await vscode.commands.executeCommand('vscode.openFolder', vscode.Uri.file(outputPath), true); + await vscode.commands.executeCommand('vscode.openFolder', vscode.Uri.file(getWorkspaceJsonDirectory()), true); } else { await displayGenerationResults(context, openApiTreeProvider, config, outputPath); } @@ -439,7 +439,7 @@ export async function activate( const clientNameOrPluginName = config.clientClassName || config.pluginName; openApiTreeProvider.refreshView(); openApiTreeProvider.setSelectionChanged(false); - const workspaceJsonPath = path.join(vscode.workspace.workspaceFolders?.map(folder => folder.uri.fsPath).join('') || '', KIOTA_DIRECTORY, KIOTA_WORKSPACE_FILE); + const workspaceJsonPath = getWorkspaceJsonPath(); await loadLockFile({fsPath: workspaceJsonPath}, openApiTreeProvider, clientNameOrPluginName ); await updateTreeViewIcons(treeViewId, false, true); } diff --git a/vscode/microsoft-kiota/src/steps.ts b/vscode/microsoft-kiota/src/steps.ts index bf527e5417..6d5fe0689b 100644 --- a/vscode/microsoft-kiota/src/steps.ts +++ b/vscode/microsoft-kiota/src/steps.ts @@ -1,3 +1,4 @@ +import * as vscode from 'vscode'; import { QuickPickItem, window, Disposable, QuickInputButton, QuickInput, QuickInputButtons, workspace, l10n, Uri, OpenDialogOptions } from 'vscode'; import { allGenerationLanguages, generationLanguageToString, KiotaSearchResultItem, LanguagesInformation, maturityLevelToString } from './kiotaInterop'; import { findAppPackageDirectory, getWorkspaceJsonDirectory } from './util'; @@ -119,10 +120,20 @@ export async function generateSteps(existingConfiguration: Partial) { const items = [l10n.t('Generate an API client'), l10n.t('Generate a plugin'), l10n.t('Generate an API manifest')]; const option = await input.showQuickPick({ @@ -158,6 +169,7 @@ export async function generateSteps(existingConfiguration: Partial inputClientNamespaceName(input, state); } async function inputClientNamespaceName(input: MultiStepInput, state: Partial) { @@ -238,6 +250,7 @@ export async function generateSteps(existingConfiguration: Partial inputPluginType(input, state); } async function inputPluginType(input: MultiStepInput, state: Partial) { @@ -298,6 +311,7 @@ export async function generateSteps(existingConfiguration: Partial inputManifestOutputPath(input, state); } async function inputManifestOutputPath(input: MultiStepInput, state: Partial) { diff --git a/vscode/microsoft-kiota/src/util.ts b/vscode/microsoft-kiota/src/util.ts index db1a33e479..d33424f16f 100644 --- a/vscode/microsoft-kiota/src/util.ts +++ b/vscode/microsoft-kiota/src/util.ts @@ -25,7 +25,7 @@ export function getWorkspaceJsonPath(): string { return path.join(getWorkspaceJsonDirectory(),KIOTA_DIRECTORY, KIOTA_WORKSPACE_FILE); }; -export function getWorkspaceJsonDirectory(): string { +export function getWorkspaceJsonDirectory(clientNameOrPluginName?: string): string { const baseDir = path.join( vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0 ? vscode.workspace.workspaceFolders[0].uri.fsPath @@ -33,7 +33,7 @@ export function getWorkspaceJsonDirectory(): string { ); let workspaceFolder = !vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length === 0 - ? path.join(baseDir, 'kiota') + ? path.join(baseDir, 'kiota', clientNameOrPluginName ?? '') : baseDir; if (!fs.existsSync(workspaceFolder)) { From 927286a0d814b778048fa521a22028592cb6d43b Mon Sep 17 00:00:00 2001 From: ElinorW Date: Fri, 26 Jul 2024 09:32:45 +0300 Subject: [PATCH 17/17] add kiota files for selected output path --- vscode/microsoft-kiota/src/extension.ts | 11 ++++++---- vscode/microsoft-kiota/src/generateClient.ts | 6 +++-- vscode/microsoft-kiota/src/generatePlugin.ts | 6 +++-- vscode/microsoft-kiota/src/kiotaInterop.ts | 4 ++-- vscode/microsoft-kiota/src/steps.ts | 23 ++++++++++++++++---- 5 files changed, 36 insertions(+), 14 deletions(-) diff --git a/vscode/microsoft-kiota/src/extension.ts b/vscode/microsoft-kiota/src/extension.ts index c084db20c8..cc06ad07f8 100644 --- a/vscode/microsoft-kiota/src/extension.ts +++ b/vscode/microsoft-kiota/src/extension.ts @@ -181,7 +181,7 @@ export async function activate( clientClassName: config.clientClassName || config.pluginName } as GeneratedOutputState); if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length === 0) { - await vscode.commands.executeCommand('vscode.openFolder', vscode.Uri.file(getWorkspaceJsonDirectory()), true); + await vscode.commands.executeCommand('vscode.openFolder', vscode.Uri.file(config.workingDirectory ?? getWorkspaceJsonDirectory()), true); } else { await displayGenerationResults(context, openApiTreeProvider, config, outputPath); } @@ -316,7 +316,8 @@ export async function activate( settings.clearCache, settings.cleanOutput, settings.disableValidationRules, - ConsumerOperation.Add + ConsumerOperation.Add, + config.workingDirectory ); const duration = performance.now() - start; const errorsCount = result ? getLogEntriesForLevel(result, LogLevel.critical, LogLevel.error).length : 0; @@ -355,7 +356,8 @@ export async function activate( settings.clearCache, settings.cleanOutput, settings.disableValidationRules, - ConsumerOperation.Add + ConsumerOperation.Add, + config.workingDirectory ); const duration = performance.now() - start; const errorsCount = result ? getLogEntriesForLevel(result, LogLevel.critical, LogLevel.error).length : 0; @@ -406,7 +408,8 @@ export async function activate( settings.languagesSerializationConfiguration[language].deserializers, settings.structuredMimeTypes, settings.includeAdditionalData, - ConsumerOperation.Add + ConsumerOperation.Add, + config.workingDirectory ); const duration = performance.now() - start; const errorsCount = result ? getLogEntriesForLevel(result, LogLevel.critical, LogLevel.error).length : 0; diff --git a/vscode/microsoft-kiota/src/generateClient.ts b/vscode/microsoft-kiota/src/generateClient.ts index 4c69f2c4bb..97475b8747 100644 --- a/vscode/microsoft-kiota/src/generateClient.ts +++ b/vscode/microsoft-kiota/src/generateClient.ts @@ -1,6 +1,7 @@ import { connectToKiota, ConsumerOperation, GenerationConfiguration, KiotaGenerationLanguage, KiotaLogEntry } from "./kiotaInterop"; import * as rpc from "vscode-jsonrpc/node"; import * as vscode from "vscode"; +import { getWorkspaceJsonDirectory } from "./util"; export function generateClient(context: vscode.ExtensionContext, descriptionPath: string, @@ -19,7 +20,8 @@ export function generateClient(context: vscode.ExtensionContext, deserializers: string[], structuredMimeTypes: string[], includeAdditionalData: boolean, - operation: ConsumerOperation + operation: ConsumerOperation, + workingDirectory: string = getWorkspaceJsonDirectory() ): Promise { return connectToKiota(context, async (connection) => { const request = new rpc.RequestType1( @@ -47,5 +49,5 @@ export function generateClient(context: vscode.ExtensionContext, operation: operation } as GenerationConfiguration, ); - }); + }, workingDirectory); }; \ No newline at end of file diff --git a/vscode/microsoft-kiota/src/generatePlugin.ts b/vscode/microsoft-kiota/src/generatePlugin.ts index 609a6de073..8e9948bcf0 100644 --- a/vscode/microsoft-kiota/src/generatePlugin.ts +++ b/vscode/microsoft-kiota/src/generatePlugin.ts @@ -1,6 +1,7 @@ import { connectToKiota, ConsumerOperation, GenerationConfiguration, KiotaGenerationLanguage, KiotaLogEntry, KiotaPluginType } from "./kiotaInterop"; import * as rpc from "vscode-jsonrpc/node"; import * as vscode from "vscode"; +import { getWorkspaceJsonDirectory } from "./util"; export function generatePlugin(context: vscode.ExtensionContext, descriptionPath: string, @@ -12,7 +13,8 @@ export function generatePlugin(context: vscode.ExtensionContext, clearCache: boolean, cleanOutput: boolean, disableValidationRules: string[], - operation: ConsumerOperation ): Promise { + operation: ConsumerOperation, + workingDirectory: string = getWorkspaceJsonDirectory() ): Promise { return connectToKiota(context, async (connection) => { const request = new rpc.RequestType1( "GeneratePlugin" @@ -32,5 +34,5 @@ export function generatePlugin(context: vscode.ExtensionContext, operation: operation } as GenerationConfiguration, ); - }); + }, workingDirectory); }; \ No newline at end of file diff --git a/vscode/microsoft-kiota/src/kiotaInterop.ts b/vscode/microsoft-kiota/src/kiotaInterop.ts index dae8a4a3d8..3aea7cc663 100644 --- a/vscode/microsoft-kiota/src/kiotaInterop.ts +++ b/vscode/microsoft-kiota/src/kiotaInterop.ts @@ -4,11 +4,11 @@ import * as rpc from 'vscode-jsonrpc/node'; import { ensureKiotaIsPresent, getKiotaPath } from './kiotaInstall'; import { getWorkspaceJsonDirectory } from "./util"; -export async function connectToKiota(context: vscode.ExtensionContext, callback:(connection: rpc.MessageConnection) => Promise): Promise { +export async function connectToKiota(context: vscode.ExtensionContext, callback:(connection: rpc.MessageConnection) => Promise, workingDirectory:string = getWorkspaceJsonDirectory()): Promise { const kiotaPath = getKiotaPath(context); await ensureKiotaIsPresent(context); const childProcess = cp.spawn(kiotaPath, ["rpc"],{ - cwd: getWorkspaceJsonDirectory(), + cwd: workingDirectory, env: { ...process.env, // eslint-disable-next-line @typescript-eslint/naming-convention diff --git a/vscode/microsoft-kiota/src/steps.ts b/vscode/microsoft-kiota/src/steps.ts index 6d5fe0689b..6901dde732 100644 --- a/vscode/microsoft-kiota/src/steps.ts +++ b/vscode/microsoft-kiota/src/steps.ts @@ -2,6 +2,7 @@ import * as vscode from 'vscode'; import { QuickPickItem, window, Disposable, QuickInputButton, QuickInput, QuickInputButtons, workspace, l10n, Uri, OpenDialogOptions } from 'vscode'; import { allGenerationLanguages, generationLanguageToString, KiotaSearchResultItem, LanguagesInformation, maturityLevelToString } from './kiotaInterop'; import { findAppPackageDirectory, getWorkspaceJsonDirectory } from './util'; +import * as path from 'path'; export async function filterSteps(existingFilter: string, filterCallback: (searchQuery: string) => void) { const state = {} as Partial; @@ -109,6 +110,7 @@ export async function generateSteps(existingConfiguration: Partial inputPluginOutputPath(input, state); } async function inputPluginOutputPath(input: MultiStepInput, state: Partial) { @@ -293,6 +298,9 @@ export async function generateSteps(existingConfiguration: Partial inputGenerationType(input, state), () => step-=2); + if(!state.workingDirectory){ + state.workingDirectory = state.outputPath as string; + } return state; } @@ -385,12 +399,13 @@ type QuickSearchPickItem = QuickPickItem & SearchItem; export interface GenerateState extends BaseStepsState { generationType: QuickPickItem | string; - pluginTypes: QuickPickItem | string; + pluginTypes: QuickPickItem | string[]; pluginName:string; clientClassName: string; clientNamespaceName: QuickPickItem | string; language: QuickPickItem | string; outputPath: QuickPickItem | string; + workingDirectory: string; } export enum GenerationType { // eslint-disable-next-line @typescript-eslint/naming-convention