Skip to content

Commit

Permalink
introduce WorkspaceContentService to handle workspace file loading
Browse files Browse the repository at this point in the history
  • Loading branch information
thewahome committed Nov 22, 2024
1 parent ddecb7d commit 3217e24
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 54 deletions.
5 changes: 3 additions & 2 deletions vscode/microsoft-kiota/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { UriHandler } from './handlers/uriHandler';
import {
ClientOrPluginProperties
} from "./kiotaInterop";
import { WorkspaceContentService } from './modules/workspace';
import { CodeLensProvider } from './providers/codelensProvider';
import { DependenciesViewProvider } from "./providers/dependenciesViewProvider";
import { OpenApiTreeNode, OpenApiTreeProvider } from "./providers/openApiTreeProvider";
Expand All @@ -36,7 +37,6 @@ import { loadTreeView, WorkspaceTreeItem, WorkspaceTreeProvider } from './provid
import { getExtensionSettings } from "./types/extensionSettings";
import { GeneratedOutputState } from './types/GeneratedOutputState';
import { WorkspaceGenerationContext } from "./types/WorkspaceGenerationContext";
import { isKiotaWorkspaceFilePresent } from './util';
import { IntegrationParams } from './utilities/deep-linking';
import { loadWorkspaceFile } from './utilities/file';
import { updateStatusBarItem } from './utilities/status';
Expand All @@ -54,12 +54,13 @@ export async function activate(
log: true,
});
const sharedService = SharedService.getInstance();
const workspaceContentService = new WorkspaceContentService();
const openApiTreeProvider = new OpenApiTreeProvider(context, () => getExtensionSettings(extensionId), sharedService);
const dependenciesInfoProvider = new DependenciesViewProvider(
context.extensionUri
);
const reporter = new TelemetryReporter(context.extension.packageJSON.telemetryInstrumentationKey);
const workspaceTreeProvider = new WorkspaceTreeProvider(await isKiotaWorkspaceFilePresent(), sharedService);
const workspaceTreeProvider = new WorkspaceTreeProvider(workspaceContentService, sharedService);

const setWorkspaceGenerationContext = (params: Partial<WorkspaceGenerationContext>) => {
workspaceGenerationContext = { ...workspaceGenerationContext, ...params };
Expand Down
9 changes: 9 additions & 0 deletions vscode/microsoft-kiota/src/modules/workspace/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ClientOrPluginProperties } from "../../kiotaInterop";
import WorkspaceContentService from "./workspaceContentService";

export interface WorkspaceContent {
version: string;
clients: Record<string, ClientOrPluginProperties>;
plugins: Record<string, ClientOrPluginProperties>;
}
export { WorkspaceContentService };
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs';

import { WorkspaceContent } from ".";
import { KIOTA_WORKSPACE_FILE } from "../../constants";
import { getWorkspaceJsonPath } from '../../util';

class WorkspaceContentService {
constructor() { }

public async load(): Promise<WorkspaceContent | undefined> {
const isWorkspacePresent = await this.isKiotaWorkspaceFilePresent();
if (!isWorkspacePresent) {
return;
}
try {
const workspaceJson = vscode.workspace.textDocuments.find(doc => doc.fileName.endsWith(KIOTA_WORKSPACE_FILE));
if (!workspaceJson) {
throw new Error('Workspace file not found');
}
const content = workspaceJson.getText();
return JSON.parse(content);
} catch (error) {
console.error('Error loading workspace.json:', error);
}
}

async isKiotaWorkspaceFilePresent(): Promise<boolean> {
if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length === 0) {
return false;
}
const workspaceFileDir = path.resolve(getWorkspaceJsonPath());
try {
await fs.promises.access(workspaceFileDir);
} catch (error) {
return false;
}
return true;
}
}

export default WorkspaceContentService;
58 changes: 18 additions & 40 deletions vscode/microsoft-kiota/src/providers/workspaceTreeProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@ import * as vscode from 'vscode';

import { CLIENTS, KIOTA_WORKSPACE_FILE, PLUGINS } from '../constants';
import { ClientOrPluginProperties } from '../kiotaInterop';
import { getWorkspaceJsonPath, isClientType, isKiotaWorkspaceFilePresent, isPluginType } from '../util';
import { WorkspaceContent, WorkspaceContentService } from '../modules/workspace';
import { getWorkspaceJsonPath, isClientType, isPluginType } from '../util';
import { SharedService } from './sharedService';

interface WorkspaceContent {
version: string;
clients: Record<string, ClientOrPluginProperties>;
plugins: Record<string, ClientOrPluginProperties>;
}

export class WorkspaceTreeItem extends vscode.TreeItem {
constructor(
public readonly label: string,
Expand All @@ -26,25 +21,28 @@ export class WorkspaceTreeItem extends vscode.TreeItem {
}

export class WorkspaceTreeProvider implements vscode.TreeDataProvider<WorkspaceTreeItem> {
public isWorkspacePresent: boolean;
private _onDidChangeTreeData: vscode.EventEmitter<WorkspaceTreeItem | undefined | null | void> = new vscode.EventEmitter<WorkspaceTreeItem | undefined | null | void>();
readonly onDidChangeTreeData: vscode.Event<WorkspaceTreeItem | undefined | null | void> = this._onDidChangeTreeData.event;
private workspaceContent: WorkspaceContent | null = null;
private sharedService: SharedService;
private workspaceContent: WorkspaceContent | undefined = undefined;

constructor(isWSPresent: boolean, _sharedService: SharedService) {
this.isWorkspacePresent = isWSPresent;
this.sharedService = _sharedService;
this.loadWorkspaceContent();
constructor(
private workspaceContentService: WorkspaceContentService,
private sharedService: SharedService
) {
void this.loadContent();
}

async refreshView(): Promise<void> {
this.loadWorkspaceContent();
this._onDidChangeTreeData.fire();
this.workspaceContent = await this.workspaceContentService.load();
this._onDidChangeTreeData.fire();
}

async loadContent(): Promise<void> {
this.workspaceContent = await this.workspaceContentService.load();
}

async getChildren(element?: WorkspaceTreeItem): Promise<WorkspaceTreeItem[]> {
if (!this.isWorkspacePresent) {
if (!this.workspaceContent) {
return [];
}

Expand Down Expand Up @@ -89,9 +87,9 @@ export class WorkspaceTreeProvider implements vscode.TreeDataProvider<WorkspaceT

getProperties(name: string, category: string): ClientOrPluginProperties | undefined {
if (category && category === CLIENTS) {
return this.workspaceContent?.plugins[name];
return this.workspaceContent?.clients[name];
}
return this.workspaceContent?.clients[name];
return this.workspaceContent?.plugins[name];
}

getTreeItem(element: WorkspaceTreeItem): WorkspaceTreeItem {
Expand Down Expand Up @@ -120,22 +118,6 @@ export class WorkspaceTreeProvider implements vscode.TreeDataProvider<WorkspaceT
return element;
}

public loadWorkspaceContent(): void {
if (!this.isWorkspacePresent) {
this.workspaceContent = null;
return;
}
try {
const workspaceJson = vscode.workspace.textDocuments.find(doc => doc.fileName.endsWith(KIOTA_WORKSPACE_FILE));
if (!workspaceJson) {
throw new Error('Workspace file not found');
}
const content = workspaceJson.getText();
this.workspaceContent = JSON.parse(content);
} catch (error) {
console.error('Error loading workspace.json:', error);
}
}
}

async function openResource(resource: vscode.Uri): Promise<void> {
Expand All @@ -144,13 +126,11 @@ async function openResource(resource: vscode.Uri): Promise<void> {

export async function loadTreeView(context: vscode.ExtensionContext, treeDataProvider: WorkspaceTreeProvider): Promise<void> {
context.subscriptions.push(vscode.workspace.onDidChangeWorkspaceFolders(async () => {
treeDataProvider.isWorkspacePresent = await isKiotaWorkspaceFilePresent();
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.isWorkspacePresent = await isKiotaWorkspaceFilePresent();
await treeDataProvider.refreshView();
}));
context.subscriptions.push(
Expand All @@ -159,6 +139,4 @@ export async function loadTreeView(context: vscode.ExtensionContext, treeDataPro
await vscode.commands.executeCommand('kiota.editPaths', label, properties, category);
})
);

}
;
};
13 changes: 1 addition & 12 deletions vscode/microsoft-kiota/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,4 @@ export function isValidUrl(url: string): boolean {
}
}

export async function isKiotaWorkspaceFilePresent(): Promise<boolean> {
if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length === 0) {
return false;
}
const workspaceFileDir = path.resolve(getWorkspaceJsonPath());
try {
await fs.promises.access(workspaceFileDir);
} catch (error) {
return false;
}
return true;
}

0 comments on commit 3217e24

Please sign in to comment.