Skip to content

Commit

Permalink
fix: fixes workspace loading when vscode does not have a folder open (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
baywet authored Jul 12, 2024
1 parent 4a3fe3d commit 62e57c4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 31 deletions.
4 changes: 2 additions & 2 deletions vscode/microsoft-kiota/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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({
Expand Down
65 changes: 36 additions & 29 deletions vscode/microsoft-kiota/src/workspaceTreeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<vscode.TreeItem> {

constructor(private context: vscode.ExtensionContext) {
void this.ensureKiotaDirectory();
constructor(public isWSPresent: boolean) {
}
async getChildren(element?: vscode.TreeItem): Promise<vscode.TreeItem[]> {
if (!element) {
Expand All @@ -19,36 +23,39 @@ export class WorkspaceTreeProvider implements vscode.TreeDataProvider<vscode.Tre

getTreeItem(element: vscode.TreeItem): vscode.TreeItem {
if (element) {
element.command = { command: 'kiota.workspace.openWorkspaceFile', title: vscode.l10n.t("Open File"), arguments: [vscode.Uri.file(workspaceJsonPath)], };
element.command = { command: 'kiota.workspace.openWorkspaceFile', title: vscode.l10n.t("Open File"), arguments: [vscode.Uri.file(getWorkspaceJsonPath())], };
element.contextValue = 'file';
}
return element;
}

private async ensureKiotaDirectory(): Promise<boolean> {
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<void> {
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<void> {
try{
await vscode.window.showTextDocument(resource);
} catch (error) {
await fs.promises.writeFile(workspaceJsonPath, Buffer.from('{}'));
await vscode.window.showTextDocument(resource);
}

}
async function isKiotaWorkspaceFilePresent(): Promise<boolean> {
const workspaceFileDir = path.resolve(getWorkspaceJsonPath());
try {
await fs.promises.access(workspaceFileDir);
} catch (error) {
return false;
}
}
return true;
}

export async function loadTreeView(context: vscode.ExtensionContext): Promise<void> {
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));
}

0 comments on commit 62e57c4

Please sign in to comment.