Add storage support

Signed-off-by: Sanjula Ganepola <sanjulagane@gmail.com>
This commit is contained in:
Sanjula Ganepola
2024-10-17 22:25:20 -04:00
parent b274529707
commit 9bd8a8af0c
3 changed files with 35 additions and 36 deletions

View File

@@ -1,9 +1,10 @@
import * as child_process from 'child_process'; import * as child_process from 'child_process';
import * as path from "path"; import * as path from "path";
import { commands, CustomExecution, env, EventEmitter, Pseudoterminal, ShellExecution, TaskDefinition, TaskExecution, TaskGroup, TaskPanelKind, TaskRevealKind, tasks, TaskScope, TerminalDimensions, window, workspace, WorkspaceFolder } from "vscode"; import { commands, CustomExecution, env, EventEmitter, ExtensionContext, Pseudoterminal, ShellExecution, TaskDefinition, TaskExecution, TaskGroup, TaskPanelKind, TaskRevealKind, tasks, TaskScope, TerminalDimensions, window, workspace, WorkspaceFolder } from "vscode";
import { ComponentsManager } from "./componentsManager"; import { ComponentsManager } from "./componentsManager";
import { historyTreeDataProvider } from './extension'; import { historyTreeDataProvider } from './extension';
import { SettingsManager } from './settingsManager'; import { SettingsManager } from './settingsManager';
import { StorageKey, StorageManager } from './storageManager';
import { Workflow, WorkflowsManager } from "./workflowsManager"; import { Workflow, WorkflowsManager } from "./workflowsManager";
export enum Event { export enum Event {
@@ -49,32 +50,6 @@ export enum Option {
Json = "--json" Json = "--json"
} }
export interface RawLog {
dryrun: boolean,
job: string,
jobID: string,
level: string, //TODO: Could be an enum?
matrix: any,
msg: string,
time: string,
raw_output?: boolean,
stage?: string,
step?: string,
stepID?: string[],
stepResult?: string, //TODO: Could be an enum?
jobResult?: string, //TODO: Could be an enum?
}
export interface CommandArgs {
workspaceFolder: WorkspaceFolder,
options: string,
name: string,
typeText: string[]
}
export interface History { export interface History {
index: number, index: number,
name: string, name: string,
@@ -93,20 +68,29 @@ export enum HistoryStatus {
Cancelled = 'Cancelled' Cancelled = 'Cancelled'
} }
export interface CommandArgs {
workspaceFolder: WorkspaceFolder,
options: string,
name: string,
typeText: string[]
}
export class Act { export class Act {
private static base: string = 'act'; private static base: string = 'act';
workspaceHistory: { [path: string]: History[] };
componentsManager: ComponentsManager; componentsManager: ComponentsManager;
workflowsManager: WorkflowsManager; workflowsManager: WorkflowsManager;
settingsManager: SettingsManager; settingsManager: SettingsManager;
storageManager: StorageManager;
workspaceHistory: { [path: string]: History[] };
installationCommands: { [packageManager: string]: string }; installationCommands: { [packageManager: string]: string };
prebuiltExecutables: { [architecture: string]: string }; prebuiltExecutables: { [architecture: string]: string };
constructor() { constructor(context: ExtensionContext) {
this.workspaceHistory = {};
this.componentsManager = new ComponentsManager(); this.componentsManager = new ComponentsManager();
this.workflowsManager = new WorkflowsManager(); this.workflowsManager = new WorkflowsManager();
this.settingsManager = new SettingsManager(); this.settingsManager = new SettingsManager();
this.storageManager = new StorageManager(context);
this.workspaceHistory = this.storageManager.get<{ [path: string]: History[] }>(StorageKey.WorkspaceHistory) || {};
switch (process.platform) { switch (process.platform) {
case 'win32': case 'win32':
@@ -205,10 +189,11 @@ export class Act {
if (!this.workspaceHistory[commandArgs.workspaceFolder.uri.fsPath]) { if (!this.workspaceHistory[commandArgs.workspaceFolder.uri.fsPath]) {
this.workspaceHistory[commandArgs.workspaceFolder.uri.fsPath] = []; this.workspaceHistory[commandArgs.workspaceFolder.uri.fsPath] = [];
this.storageManager.update(StorageKey.WorkspaceHistory, this.workspaceHistory);
} }
const historyIndex = this.workspaceHistory[commandArgs.workspaceFolder.uri.fsPath].length; const historyIndex = this.workspaceHistory[commandArgs.workspaceFolder.uri.fsPath].length;
this.workspaceHistory[commandArgs.workspaceFolder.uri.fsPath].push({ this.workspaceHistory[commandArgs.workspaceFolder.uri.fsPath].unshift({
index: historyIndex, index: historyIndex,
name: `${commandArgs.name} #${this.workspaceHistory[commandArgs.workspaceFolder.uri.fsPath].length + 1}`, name: `${commandArgs.name} #${this.workspaceHistory[commandArgs.workspaceFolder.uri.fsPath].length + 1}`,
status: HistoryStatus.Running, status: HistoryStatus.Running,
@@ -216,6 +201,7 @@ export class Act {
commandArgs: commandArgs commandArgs: commandArgs
}); });
historyTreeDataProvider.refresh(); historyTreeDataProvider.refresh();
this.storageManager.update(StorageKey.WorkspaceHistory, this.workspaceHistory);
this.workspaceHistory[commandArgs.workspaceFolder.uri.fsPath][historyIndex].taskExecution = await tasks.executeTask({ this.workspaceHistory[commandArgs.workspaceFolder.uri.fsPath][historyIndex].taskExecution = await tasks.executeTask({
name: commandArgs.name, name: commandArgs.name,
@@ -246,6 +232,7 @@ export class Act {
} else { } else {
this.workspaceHistory[commandArgs.workspaceFolder.uri.fsPath][historyIndex].output += data; this.workspaceHistory[commandArgs.workspaceFolder.uri.fsPath][historyIndex].output += data;
} }
this.storageManager.update(StorageKey.WorkspaceHistory, this.workspaceHistory);
}); });
const exec = child_process.spawn(command, { cwd: commandArgs.workspaceFolder.uri.fsPath, shell: env.shell }); const exec = child_process.spawn(command, { cwd: commandArgs.workspaceFolder.uri.fsPath, shell: env.shell });
@@ -268,11 +255,11 @@ export class Act {
case 'failure': case 'failure':
this.workspaceHistory[commandArgs.workspaceFolder.uri.fsPath][historyIndex].status = HistoryStatus.Failed; this.workspaceHistory[commandArgs.workspaceFolder.uri.fsPath][historyIndex].status = HistoryStatus.Failed;
break; break;
// TODO: Handle cancelled
} }
} }
historyTreeDataProvider.refresh(); historyTreeDataProvider.refresh();
this.storageManager.update(StorageKey.WorkspaceHistory, this.workspaceHistory);
writeEmitter.fire(`${jsonLine.msg.trimEnd()}\r\n`); writeEmitter.fire(`${jsonLine.msg.trimEnd()}\r\n`);
} }
} }
@@ -288,6 +275,7 @@ export class Act {
} }
historyTreeDataProvider.refresh(); historyTreeDataProvider.refresh();
this.storageManager.update(StorageKey.WorkspaceHistory, this.workspaceHistory);
closeEmitter.fire(code || 0); closeEmitter.fire(code || 0);
}); });
@@ -314,6 +302,8 @@ export class Act {
} }
historyTreeDataProvider.refresh(); historyTreeDataProvider.refresh();
this.storageManager.update(StorageKey.WorkspaceHistory, this.workspaceHistory);
exec.stdout.destroy(); exec.stdout.destroy();
exec.stdin.destroy(); exec.stdin.destroy();
exec.stderr.destroy(); exec.stderr.destroy();
@@ -322,6 +312,7 @@ export class Act {
}; };
}) })
}); });
this.storageManager.update(StorageKey.WorkspaceHistory, this.workspaceHistory);
} }
async install(packageManager: string) { async install(packageManager: string) {
@@ -358,12 +349,15 @@ export class Act {
for (const workspaceFolder of workspaceFolders) { for (const workspaceFolder of workspaceFolders) {
this.workspaceHistory[workspaceFolder.uri.fsPath] = []; this.workspaceHistory[workspaceFolder.uri.fsPath] = [];
historyTreeDataProvider.refresh(); historyTreeDataProvider.refresh();
this.storageManager.update(StorageKey.WorkspaceHistory, this.workspaceHistory);
} }
} }
} }
async viewOutput(history: History) { async viewOutput(history: History) {
await workspace.openTextDocument({ content: history.output }); await workspace.openTextDocument({ content: history.output }).then(async document => {
await window.showTextDocument(document);
})
} }
async stop(history: History) { async stop(history: History) {
@@ -375,5 +369,6 @@ export class Act {
const historyIndex = this.workspaceHistory[history.commandArgs.workspaceFolder.uri.fsPath].findIndex(workspaceHistory => workspaceHistory.index === history.index) 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.workspaceHistory[history.commandArgs.workspaceFolder.uri.fsPath].splice(historyIndex, 1);
historyTreeDataProvider.refresh(); historyTreeDataProvider.refresh();
this.storageManager.update(StorageKey.WorkspaceHistory, this.workspaceHistory);
} }
} }

View File

@@ -16,7 +16,7 @@ export let settingsTreeDataProvider: SettingsTreeDataProvider;
export function activate(context: vscode.ExtensionContext) { export function activate(context: vscode.ExtensionContext) {
console.log('Congratulations, your extension "github-local-actions" is now active!'); console.log('Congratulations, your extension "github-local-actions" is now active!');
act = new Act(); act = new Act(context);
// Create tree views // Create tree views
const decorationProvider = new DecorationProvider(); const decorationProvider = new DecorationProvider();

View File

@@ -1,5 +1,9 @@
import { ExtensionContext } from "vscode"; import { ExtensionContext } from "vscode";
export enum StorageKey {
WorkspaceHistory = 'workspaceHistory'
}
export class StorageManager { export class StorageManager {
private context: ExtensionContext; private context: ExtensionContext;
private storageKey: string = 'githubLocalActions'; private storageKey: string = 'githubLocalActions';
@@ -12,11 +16,11 @@ export class StorageManager {
return this.context.globalState.keys(); return this.context.globalState.keys();
} }
get<T>(key: string): T | undefined { get<T>(key: StorageKey): T | undefined {
return this.context.globalState.get<T>(`${this.storageKey}.${key}`); return this.context.globalState.get<T>(`${this.storageKey}.${key}`);
} }
async update(key: string, value: any): Promise<void> { async update(key: StorageKey, value: any): Promise<void> {
await this.context.globalState.update(`${this.storageKey}.${key}`, value); await this.context.globalState.update(`${this.storageKey}.${key}`, value);
} }
} }