From e8f3f6c673a39a9da4b3ec627c22933da343b4fb Mon Sep 17 00:00:00 2001 From: Sanjula Ganepola <32170854+SanjulaGanepola@users.noreply.github.com> Date: Sun, 23 Mar 2025 18:27:30 -0400 Subject: [PATCH] Add setting to change workflow directory (#188) Signed-off-by: Sanjula Ganepola --- package.json | 5 ++ src/act.ts | 10 ++-- src/configurationManager.ts | 17 ++++-- src/extension.ts | 56 +++++++++++++------ .../workflows/workflowsTreeDataProvider.ts | 5 +- src/workflowsManager.ts | 14 +++-- 6 files changed, 74 insertions(+), 33 deletions(-) diff --git a/package.json b/package.json index ed707cc..0e65c51 100644 --- a/package.json +++ b/package.json @@ -786,6 +786,11 @@ "type": "string", "default": "act" }, + "githubLocalActions.workflowsDirectory": { + "markdownDescription": "The relative path to the directory containing your workflows. By default, this will be `.github/workflows`.", + "type": "string", + "default": ".github/workflows" + }, "githubLocalActions.dockerDesktopPath": { "markdownDescription": "The path to your Docker Desktop executable (used for Windows and MacOS). To start Docker Engine from the `Components` view, this application will be launched. Refer to the default path based on OS:\n\n* **Windows**: `C:/Program Files/Docker/Docker/Docker Desktop.exe`\n\n* **MacOS**: `/Applications/Docker.app`", "type": "string", diff --git a/src/act.ts b/src/act.ts index a0eb37a..d5dc5fb 100644 --- a/src/act.ts +++ b/src/act.ts @@ -273,11 +273,12 @@ export class Act { } async runWorkflow(workspaceFolder: WorkspaceFolder, workflow: Workflow) { + const workflowsDirectory = WorkflowsManager.getWorkflowsDirectory(); return await this.runCommand({ path: workspaceFolder.uri.fsPath, workflow: workflow, options: [ - `${Option.Workflows} "${WorkflowsManager.WORKFLOWS_DIRECTORY}/${path.parse(workflow.uri.fsPath).base}"` + `${Option.Workflows} "${workflowsDirectory}/${path.parse(workflow.uri.fsPath).base}"` ], name: workflow.name, extraHeader: [ @@ -287,11 +288,12 @@ export class Act { } async runJob(workspaceFolder: WorkspaceFolder, workflow: Workflow, job: Job) { + const workflowsDirectory = WorkflowsManager.getWorkflowsDirectory(); return await this.runCommand({ path: workspaceFolder.uri.fsPath, workflow: workflow, options: [ - `${Option.Workflows} "${WorkflowsManager.WORKFLOWS_DIRECTORY}/${path.parse(workflow.uri.fsPath).base}"`, + `${Option.Workflows} "${workflowsDirectory}/${path.parse(workflow.uri.fsPath).base}"`, `${Option.Job} "${job.id}"` ], name: `${workflow.name}/${job.name}`, @@ -304,7 +306,7 @@ export class Act { async runEvent(workspaceFolder: WorkspaceFolder, event: Event) { let eventExists: boolean = false; - + const workflowsDirectory = WorkflowsManager.getWorkflowsDirectory(); const workflows = await this.workflowsManager.getWorkflows(workspaceFolder); if (workflows.length > 0) { for (const workflow of workflows) { @@ -314,7 +316,7 @@ export class Act { path: workspaceFolder.uri.fsPath, workflow: workflow, options: [ - `${event} ${Option.Workflows} "${WorkflowsManager.WORKFLOWS_DIRECTORY}/${path.parse(workflow.uri.fsPath).base}"` + `${event} ${Option.Workflows} "${workflowsDirectory}/${path.parse(workflow.uri.fsPath).base}"` ], name: `${workflow.name} (${event})`, extraHeader: [ diff --git a/src/configurationManager.ts b/src/configurationManager.ts index 4d38acd..3d4679f 100644 --- a/src/configurationManager.ts +++ b/src/configurationManager.ts @@ -1,5 +1,6 @@ import { ConfigurationTarget, workspace } from 'vscode'; import { Act } from './act'; +import { WorkflowsManager } from './workflowsManager'; export enum Platform { windows = 'win32', @@ -9,6 +10,7 @@ export enum Platform { export enum Section { actCommand = 'actCommand', + workflowsDirectory = 'workflowsDirectory', dockerDesktopPath = 'dockerDesktopPath' } @@ -17,6 +19,16 @@ export namespace ConfigurationManager { export const searchPrefix: string = '@ext:sanjulaganepola.github-local-actions'; export async function initialize(): Promise { + let actCommand = ConfigurationManager.get(Section.actCommand); + if (!actCommand) { + await ConfigurationManager.set(Section.actCommand, Act.defaultActCommand); + } + + let workflowsDirectory = ConfigurationManager.get(Section.workflowsDirectory); + if (!workflowsDirectory) { + await ConfigurationManager.set(Section.workflowsDirectory, WorkflowsManager.defaultWorkflowsDirectory); + } + let dockerDesktopPath = ConfigurationManager.get(Section.dockerDesktopPath); if (!dockerDesktopPath) { switch (process.platform) { @@ -32,11 +44,6 @@ export namespace ConfigurationManager { await ConfigurationManager.set(Section.dockerDesktopPath, dockerDesktopPath); } - - let actCommand = ConfigurationManager.get(Section.actCommand); - if (!actCommand) { - await ConfigurationManager.set(Section.actCommand, Act.defaultActCommand); - } } export function getSearchTerm(section: Section): string { diff --git a/src/extension.ts b/src/extension.ts index b49d6d7..7e95ae7 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,7 +1,6 @@ -import * as vscode from 'vscode'; -import { commands, env, TreeCheckboxChangeEvent, Uri, window, workspace } from 'vscode'; +import { commands, env, ExtensionContext, TreeCheckboxChangeEvent, Uri, window, workspace } from 'vscode'; import { Act } from './act'; -import { ConfigurationManager } from './configurationManager'; +import { ConfigurationManager, Section } from './configurationManager'; import { IssueHandler } from './issueHandler'; import ComponentsTreeDataProvider from './views/components/componentsTreeDataProvider'; import { DecorationProvider } from './views/decorationProvider'; @@ -18,7 +17,7 @@ export let workflowsTreeDataProvider: WorkflowsTreeDataProvider; export let historyTreeDataProvider: HistoryTreeDataProvider; export let settingsTreeDataProvider: SettingsTreeDataProvider; -export function activate(context: vscode.ExtensionContext) { +export function activate(context: ExtensionContext) { console.log('Congratulations, your extension "github-local-actions" is now active!'); act = new Act(context); @@ -38,26 +37,28 @@ export function activate(context: vscode.ExtensionContext) { }); // Create file watcher - const workflowsFileWatcher = workspace.createFileSystemWatcher(`**/${WorkflowsManager.WORKFLOWS_DIRECTORY}/*.{${WorkflowsManager.YML_EXTENSION},${WorkflowsManager.YAML_EXTENSION}}`); - workflowsFileWatcher.onDidCreate(() => { - workflowsTreeDataProvider.refresh(); - settingsTreeDataProvider.refresh(); - }); - workflowsFileWatcher.onDidChange(() => { - workflowsTreeDataProvider.refresh(); - settingsTreeDataProvider.refresh(); - }); - workflowsFileWatcher.onDidDelete(() => { - workflowsTreeDataProvider.refresh(); - settingsTreeDataProvider.refresh(); - }); + let workflowsFileWatcher = setupFileWatcher(context); // Initialize configurations ConfigurationManager.initialize(); workspace.onDidChangeConfiguration(async event => { if (event.affectsConfiguration(ConfigurationManager.group)) { await ConfigurationManager.initialize(); - componentsTreeDataProvider.refresh(); + + if (event.affectsConfiguration(`${ConfigurationManager.group}.${Section.actCommand}`) || + event.affectsConfiguration(`${ConfigurationManager.group}.${Section.dockerDesktopPath}`)) { + componentsTreeDataProvider.refresh(); + } + + if (event.affectsConfiguration(`${ConfigurationManager.group}.${Section.workflowsDirectory}`)) { + workflowsTreeDataProvider.refresh(); + settingsTreeDataProvider.refresh(); + + if (workflowsFileWatcher) { + workflowsFileWatcher.dispose(); + workflowsFileWatcher = setupFileWatcher(context); + } + } } }); @@ -77,4 +78,23 @@ export function activate(context: vscode.ExtensionContext) { ); } +function setupFileWatcher(context: ExtensionContext) { + const workflowsDirectory = WorkflowsManager.getWorkflowsDirectory(); + const workflowsFileWatcher = workspace.createFileSystemWatcher(`**/${workflowsDirectory}/*.{${WorkflowsManager.ymlExtension},${WorkflowsManager.yamlExtension}}`); + workflowsFileWatcher.onDidCreate(() => { + workflowsTreeDataProvider.refresh(); + settingsTreeDataProvider.refresh(); + }); + workflowsFileWatcher.onDidChange(() => { + workflowsTreeDataProvider.refresh(); + settingsTreeDataProvider.refresh(); + }); + workflowsFileWatcher.onDidDelete(() => { + workflowsTreeDataProvider.refresh(); + settingsTreeDataProvider.refresh(); + }); + + return workflowsFileWatcher; +} + export function deactivate() { } diff --git a/src/views/workflows/workflowsTreeDataProvider.ts b/src/views/workflows/workflowsTreeDataProvider.ts index 05c0967..d87fbfa 100644 --- a/src/views/workflows/workflowsTreeDataProvider.ts +++ b/src/views/workflows/workflowsTreeDataProvider.ts @@ -61,7 +61,8 @@ export default class WorkflowsTreeDataProvider implements TreeDataProvider(Section.workflowsDirectory) || WorkflowsManager.defaultWorkflowsDirectory; + } async getWorkflows(workspaceFolder: WorkspaceFolder): Promise { const workflows: Workflow[] = []; - const workflowFileUris = await workspace.findFiles(new RelativePattern(workspaceFolder, `${WorkflowsManager.WORKFLOWS_DIRECTORY}/*.{${WorkflowsManager.YAML_EXTENSION},${WorkflowsManager.YML_EXTENSION}}`)); + const workflowsDirectory = WorkflowsManager.getWorkflowsDirectory(); + const workflowFileUris = await workspace.findFiles(new RelativePattern(workspaceFolder, `${workflowsDirectory}/*.{${WorkflowsManager.yamlExtension},${WorkflowsManager.ymlExtension}}`)); for await (const workflowFileUri of workflowFileUris) { let yamlContent: any | undefined;