From 091935496cd56d2724fe60560903fcabd2b0a13b Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Sun, 31 Dec 2023 12:00:16 -0800 Subject: [PATCH] Add prefixes. --- packages/vscode-extension/CHANGELOG.md | 1 + packages/vscode-extension/README.md | 10 ++-- packages/vscode-extension/package.json | 16 +++--- packages/vscode-extension/src/lastRunView.ts | 4 ++ packages/vscode-extension/src/workspace.ts | 56 ++++++++++++-------- 5 files changed, 54 insertions(+), 33 deletions(-) diff --git a/packages/vscode-extension/CHANGELOG.md b/packages/vscode-extension/CHANGELOG.md index 5b9a698..f744728 100644 --- a/packages/vscode-extension/CHANGELOG.md +++ b/packages/vscode-extension/CHANGELOG.md @@ -1,6 +1,7 @@ ## 0.8.0 - Removed support for moon < 1.0. +- Replaced `moon.workspaceRoot` setting with `moon.rootPrefixes`. - Refactored extension to support multiple VS Code workspace folders. - When you open a file in another workspace, the moon panels will refresh. diff --git a/packages/vscode-extension/README.md b/packages/vscode-extension/README.md index 74eab8f..01ba9bd 100644 --- a/packages/vscode-extension/README.md +++ b/packages/vscode-extension/README.md @@ -23,8 +23,7 @@ alt="Screenshot of projects view" width="300px" /> ### Last run view -Information about the last ran target will be displayed in a beautiful table with detailed stats. -Only tasks ran from the extension, or with `--report` on the command line will be displayed here. +Information about the last ran task will be displayed in a beautiful table with detailed stats. { + console.log( + new vscode.RelativePattern(folder.uri, workspace.getMoonDirPath('cache/runReport.json')), + ); + // When the report is changed, refresh view const watcher = vscode.workspace.createFileSystemWatcher( new vscode.RelativePattern(folder.uri, workspace.getMoonDirPath('cache/runReport.json')), diff --git a/packages/vscode-extension/src/workspace.ts b/packages/vscode-extension/src/workspace.ts index 6e1cc96..6e40dc8 100644 --- a/packages/vscode-extension/src/workspace.ts +++ b/packages/vscode-extension/src/workspace.ts @@ -24,6 +24,8 @@ export class Workspace { // Current moon workspace root root: string | null = null; + rootPrefix: string = ''; + onDidChangeWorkspaceEmitter: vscode.EventEmitter; disposables: vscode.Disposable[] = []; @@ -34,15 +36,15 @@ export class Workspace { // Find moon workspace from default editor if (vscode.window.activeTextEditor) { - void this.findRoot(vscode.window.activeTextEditor.document.uri); + this.findRoot(vscode.window.activeTextEditor.document.uri); } else { - void this.findDefaultRoot(); + this.findDefaultRoot(); } // When an editor is changed, attempt to find the moon workspace vscode.window.onDidChangeActiveTextEditor((editor) => { if (editor && editor.document.uri.scheme === 'file') { - void this.findRoot(editor.document.uri); + this.findRoot(editor.document.uri); } }); } @@ -68,13 +70,18 @@ export class Workspace { }); // Emit and add new watchers - this.onDidChangeWorkspaceEmitter.fire(this.folder); + const { folder } = this; + + // Run in a timeout to ensure listeners have been registered, + // otherwise this does nothing and the editor feels broken + setTimeout(() => { + this.onDidChangeWorkspaceEmitter.fire(folder); + }, 0); } - async findDefaultRoot() { + findDefaultRoot() { for (const folder of vscode.workspace.workspaceFolders ?? []) { - // eslint-disable-next-line no-await-in-loop - await this.findRoot(folder.uri); + this.findRoot(folder.uri); if (this.root) { break; @@ -82,7 +89,7 @@ export class Workspace { } } - async findRoot(openUri: vscode.Uri) { + findRoot(openUri: vscode.Uri) { if (openUri.fsPath === 'moonrepo.moon-console.moon') { return; } @@ -93,6 +100,7 @@ export class Workspace { this.folder = null; this.root = null; + this.rootPrefix = ''; this.binPath = null; this.logger.appendLine(`Attempting to find a VSC workspace folder for ${openUri.fsPath}`); @@ -105,21 +113,29 @@ export class Workspace { this.logger.appendLine(`Found workspace folder ${workspaceFolder.uri.fsPath}`); this.logger.appendLine('Attempting to find a moon installation'); - const files = await vscode.workspace.findFiles( - new vscode.RelativePattern(workspaceFolder.uri, this.getMoonDirPath('*.yml')), - ); + const rootPrefixes = vscode.workspace.getConfiguration('moon').get('rootPrefixes', ['.']); + let foundRoot = false; + + for (const prefix of rootPrefixes) { + const moonDir = path.join(workspaceFolder.uri.fsPath, prefix, '.moon'); - if (files.length > 0 && files[0].scheme === 'file') { - // Return folder containing the `.moon` folder - this.root = path.dirname(path.dirname(files[0].fsPath)); - this.binPath = this.findMoonBin(); + if (fs.existsSync(moonDir)) { + this.root = path.dirname(moonDir); + this.rootPrefix = prefix; + this.binPath = this.findMoonBin(); - this.logger.appendLine(`Found moon workspace root at ${this.root}`); + this.logger.appendLine(`Found moon workspace root at ${this.root}`); - if (this.binPath) { - this.logger.appendLine(`Found moon binary at ${this.binPath}`); + if (this.binPath) { + this.logger.appendLine(`Found moon binary at ${this.binPath}`); + } + + foundRoot = true; + break; } + } + if (foundRoot) { this.fireDidChangeWorkspace(); } else { this.logger.appendLine('Did not find a moon installation, disabling'); @@ -185,9 +201,7 @@ export class Workspace { } getMoonDirPath(file: string): string { - const rootPrefix = vscode.workspace.getConfiguration('moon').get('workspaceRoot', '.'); - - return path.join(rootPrefix, '.moon', file); + return path.join(this.rootPrefix, '.moon', file); } async getMoonVersion(): Promise {