Refactor to use workspace folders

Signed-off-by: Sanjula Ganepola <sanjulagane@gmail.com>
This commit is contained in:
Sanjula Ganepola
2024-10-19 20:20:48 -04:00
parent f4a73316f4
commit 49e934e297
26 changed files with 515 additions and 206 deletions

76
src/historyManager.ts Normal file
View File

@@ -0,0 +1,76 @@
import { TaskExecution, window, workspace } from "vscode";
import { CommandArgs } from "./act";
import { act, historyTreeDataProvider } from "./extension";
import { StorageKey, StorageManager } from "./storageManager";
export interface History {
index: number,
name: string,
status: HistoryStatus,
date?: {
start: string,
end: string,
}
output?: string,
taskExecution?: TaskExecution,
commandArgs: CommandArgs
}
export enum HistoryStatus {
Running = 'Running',
Success = 'Success',
Failed = 'Failed',
Cancelled = 'Cancelled'
}
export class HistoryManager {
storageManager: StorageManager;
workspaceHistory: { [path: string]: History[] };
constructor(storageManager: StorageManager) {
this.storageManager = storageManager;
const workspaceHistory = this.storageManager.get<{ [path: string]: History[] }>(StorageKey.WorkspaceHistory) || {};
for (const [path, historyLogs] of Object.entries(workspaceHistory)) {
workspaceHistory[path] = historyLogs.map(history => {
if (history.status === HistoryStatus.Running) {
history.status = HistoryStatus.Cancelled;
}
return history;
});
}
this.workspaceHistory = workspaceHistory;
}
async clearAll() {
//TODO: Fix for multi workspace support
const workspaceFolders = workspace.workspaceFolders;
if (workspaceFolders && workspaceFolders.length > 0) {
for (const workspaceFolder of workspaceFolders) {
this.workspaceHistory[workspaceFolder.uri.fsPath] = [];
historyTreeDataProvider.refresh();
this.storageManager.update(StorageKey.WorkspaceHistory, this.workspaceHistory);
}
}
}
async viewOutput(history: History) {
await workspace.openTextDocument({ content: history.output }).then(async document => {
await window.showTextDocument(document);
})
}
async restart(history: History) {
await act.runCommand(history.commandArgs);
}
async stop(history: History) {
history.taskExecution?.terminate();
}
async remove(history: History) {
const historyIndex = this.workspaceHistory[history.commandArgs.workspaceFolder.uri.fsPath].findIndex(workspaceHistory => workspaceHistory.index === history.index)
this.workspaceHistory[history.commandArgs.workspaceFolder.uri.fsPath].splice(historyIndex, 1);
this.storageManager.update(StorageKey.WorkspaceHistory, this.workspaceHistory);
}
}