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

View File

@@ -1,6 +1,6 @@
import * as fs from "fs/promises";
import * as path from "path";
import { Uri, workspace } from "vscode";
import { RelativePattern, Uri, workspace, WorkspaceFolder } from "vscode";
import * as yaml from "yaml";
export interface Workflow {
@@ -17,33 +17,29 @@ export interface Job {
}
export class WorkflowsManager {
async getWorkflows(): Promise<Workflow[]> {
async getWorkflows(workspaceFolder: WorkspaceFolder): Promise<Workflow[]> {
const workflows: Workflow[] = [];
const workspaceFolders = workspace.workspaceFolders;
if (workspaceFolders && workspaceFolders.length > 0) {
const workflowFileUris = await workspace.findFiles(`.github/workflows/*.{yml,yaml}`);
const workflowFileUris = await workspace.findFiles(new RelativePattern(workspaceFolder, `.github/workflows/*.{yml,yaml}`));
for await (const workflowFileUri of workflowFileUris) {
let yamlContent: any | undefined;
for await (const workflowFileUri of workflowFileUris) {
let yamlContent: any | undefined;
try {
const fileContent = await fs.readFile(workflowFileUri.fsPath, 'utf8');
yamlContent = yaml.parse(fileContent);
try {
const fileContent = await fs.readFile(workflowFileUri.fsPath, 'utf8');
yamlContent = yaml.parse(fileContent);
workflows.push({
name: yamlContent.name || path.parse(workflowFileUri.fsPath).name,
uri: workflowFileUri,
fileContent: fileContent,
yaml: yaml.parse(fileContent)
});
} catch (error) {
workflows.push({
name: (yamlContent ? yamlContent.name : undefined) || path.parse(workflowFileUri.fsPath).name,
uri: workflowFileUri,
error: 'Failed to parse workflow file.'
});
}
workflows.push({
name: yamlContent.name || path.parse(workflowFileUri.fsPath).name,
uri: workflowFileUri,
fileContent: fileContent,
yaml: yaml.parse(fileContent)
});
} catch (error) {
workflows.push({
name: (yamlContent ? yamlContent.name : undefined) || path.parse(workflowFileUri.fsPath).name,
uri: workflowFileUri,
error: 'Failed to parse workflow file.'
});
}
}