Skip to content

Commit

Permalink
Add prefixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Dec 31, 2023
1 parent 384b522 commit 0919354
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 33 deletions.
1 change: 1 addition & 0 deletions packages/vscode-extension/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
10 changes: 5 additions & 5 deletions packages/vscode-extension/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<img
src="https://raw.githubusercontent.com/moonrepo/dev/master/packages/vscode-extension/images/last-run-view.png"
Expand All @@ -48,21 +47,21 @@ about [installing and configuring moon](https://moonrepo.dev/docs/install)!

The following settings are available for this extension.

- `moon.workspaceRoot` - Relative path from the opened folder to moon's workspace root. Defaults to
"`.`". This is useful if moon is initialized in a sub-folder.
- `moon.binPath` - Relative path from moon's workspace root to the moon binary. Defaults to
`node_modules/@moonrepo/cli/moon`. _Windows will auto-suffix with `.exe`!_
- `moon.hideTasks` - List of tasks to hide in the projects view, using target syntax. Supports
fully-qualified targets (`app:build`) and partial targets (`:build` or `*:build`). Defaults to
`[]`.
- `moon.rootPrefixes` - List of relative paths from the editor root in which to find moon's
workspace root. Defaults to `['.']`. This is useful if moon is initialized in a sub-folder.

## Commands

The following commands are available in the command palette (typically `cmd + shift + p`), and are
prefixed with "moon".

- **Open settings** - Opens the settings page and filters to all moon applicable settings.
- **Run target** - Prompts the user for a target(s) and runs it in the terminal.
- **Run task** - Prompts the user for a task(s) and runs it in the terminal.
- **Refresh projects** - Refreshes the projects view. _This happens automatically when
`.moon/workspace.yml` changes!_
- **View action graph** - Opens a panel that renders an interactive action graph visualization.
Expand All @@ -84,3 +83,4 @@ prefixed with "moon".
- [ ] Auto-completion
- [ ] In-editor code generation
- [x] Graph visualizer
- [*] Multi-workspace support
16 changes: 9 additions & 7 deletions packages/vscode-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,19 @@
"moon.binPath": {
"type": "string",
"default": "node_modules/@moonrepo/cli/moon",
"markdownDescription": "Path to the moon binary, relative from the `moon.workspaceRoot` setting."
"markdownDescription": "Path to the moon binary, relative from the `moon.rootPrefixes` setting."
},
"moon.hideTasks": {
"type": "array",
"default": [],
"markdownDescription": "List of tasks to hide in the projects panel. Accepts fully-qualified or partial targets."
},
"moon.workspaceRoot": {
"type": "string",
"default": ".",
"markdownDescription": "Relative path from the editor root to moon's [workspace root](https://moonrepo.dev/docs/concepts/workspace)."
"moon.rootPrefixes": {
"type": "array",
"default": [
"."
],
"markdownDescription": "Relative paths from the editor root to find moon's [workspace root](https://moonrepo.dev/docs/concepts/workspace)."
}
}
},
Expand Down Expand Up @@ -232,7 +234,7 @@
"viewsWelcome": [
{
"view": "moonProjects",
"contents": "Unable to find the workspace root. Has moon been installed? Try opening a file.\n[Install moon](https://moonrepo.dev/docs/install)\nIf moon was installed in a sub-folder, update the [moon.workspaceRoot](command:moon.openSettings) setting.",
"contents": "Unable to find the workspace root. Has moon been installed? Try opening a file.\n[Install moon](https://moonrepo.dev/docs/install)\nIf moon was installed in a sub-folder, update the [moon.rootPrefixes](command:moon.openSettings) setting.",
"when": "!moon.inWorkspaceRoot"
},
{
Expand All @@ -242,7 +244,7 @@
},
{
"view": "moonProjectsExternal",
"contents": "Unable to find the workspace root. Has moon been installed? Try opening a file.\n[Install moon](https://moonrepo.dev/docs/install)\nIf moon was installed in a sub-folder, update the [moon.workspaceRoot](command:moon.openSettings) setting.",
"contents": "Unable to find the workspace root. Has moon been installed? Try opening a file.\n[Install moon](https://moonrepo.dev/docs/install)\nIf moon was installed in a sub-folder, update the [moon.rootPrefixes](command:moon.openSettings) setting.",
"when": "!moon.inWorkspaceRoot"
},
{
Expand Down
4 changes: 4 additions & 0 deletions packages/vscode-extension/src/lastRunView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ export class LastRunProvider implements vscode.WebviewViewProvider {
this.workspace = workspace;

workspace.onDidChangeWorkspace((folder) => {
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')),
Expand Down
56 changes: 35 additions & 21 deletions packages/vscode-extension/src/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export class Workspace {
// Current moon workspace root
root: string | null = null;

rootPrefix: string = '';

onDidChangeWorkspaceEmitter: vscode.EventEmitter<vscode.WorkspaceFolder>;

disposables: vscode.Disposable[] = [];
Expand All @@ -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);
}
});
}
Expand All @@ -68,21 +70,26 @@ 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;
}
}
}

async findRoot(openUri: vscode.Uri) {
findRoot(openUri: vscode.Uri) {
if (openUri.fsPath === 'moonrepo.moon-console.moon') {
return;
}
Expand All @@ -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}`);
Expand All @@ -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');
Expand Down Expand Up @@ -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<string> {
Expand Down

0 comments on commit 0919354

Please sign in to comment.