Skip to content

Commit

Permalink
Add mise file watcher
Browse files Browse the repository at this point in the history
  • Loading branch information
hverlin committed Nov 12, 2024
1 parent 53ff35e commit a2f82c3
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"displayName": "Mise VSCode",
"publisher": "hverlin",
"description": "VSCode extension for mise (manged dev tools, tasks and environment variables)",
"version": "0.0.19",
"version": "0.0.20",
"repository": {
"type": "git",
"url": "https://github.com/hverlin/mise-vscode"
Expand Down
7 changes: 7 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as vscode from "vscode";
import { MiseFileWatcher } from "./miseFileWatcher";
import { MiseService } from "./miseService";
import { MiseEnvsProvider } from "./providers/envProvider";
import { MiseRunCodeLensProvider } from "./providers/miseRunCodeLensProvider";
Expand Down Expand Up @@ -132,6 +133,12 @@ export async function activate(context: vscode.ExtensionContext) {
});

context.subscriptions.push(taskProvider);

const miseWatcher = new MiseFileWatcher(context, miseService, async (uri) => {
logger.info(`Mise configuration file changed: ${uri}`);
await vscode.commands.executeCommand("mise.refreshEntry");
});
context.subscriptions.push(miseWatcher);
}

export function deactivate() {
Expand Down
74 changes: 74 additions & 0 deletions src/miseFileWatcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import * as vscode from "vscode";
import type { MiseService } from "./miseService";
import { logger } from "./utils/logger";

const misePatterns = [
".config/mise/config.toml",
"mise/config.toml",
"mise.toml",
".mise/config.toml",
".mise.toml",
".config/mise/config.local.toml",
"mise/config.local.toml",
"mise.local.toml",
".mise/config.local.toml",
".mise.local.toml",
".config/mise/config.*.toml",
"mise/config.*.toml",
"mise.*.toml",
".mise/config.*.toml",
".mise.*.toml",
".config/mise/config.*.local.toml",
"mise/config.*.local.toml",
".mise/config.*.local.toml",
".mise.*.local.toml",
].join(",");

export class MiseFileWatcher {
private fileWatcher: vscode.FileSystemWatcher | undefined;
private context: vscode.ExtensionContext;
private miseService: MiseService;
private readonly onConfigChangeCallback: (uri: vscode.Uri) => Promise<void>;

constructor(
context: vscode.ExtensionContext,
miseService: MiseService,
onConfigChangeCallback: (uri: vscode.Uri) => Promise<void>,
) {
this.context = context;
this.miseService = miseService;
this.onConfigChangeCallback = onConfigChangeCallback;
this.initializeFileWatcher();
}

private initializeFileWatcher() {
const rootFolder = vscode.workspace.workspaceFolders?.[0];
if (!rootFolder) {
return;
}
const pattern = new vscode.RelativePattern(rootFolder, `{${misePatterns}}`);
this.fileWatcher = vscode.workspace.createFileSystemWatcher(pattern);

this.context.subscriptions.push(this.fileWatcher);

this.fileWatcher.onDidChange(this.handleFileChange.bind(this));
this.fileWatcher.onDidCreate(this.handleFileChange.bind(this));
this.fileWatcher.onDidDelete(this.handleFileChange.bind(this));
}

private async handleFileChange(uri: vscode.Uri) {
try {
const { stdout } = await this.miseService.execMiseCommand("tasks ls");

if (stdout) {
await this.onConfigChangeCallback(uri);
}
} catch (error) {
logger.info(`Error while handling file change ${error}`);
}
}

public dispose() {
this.fileWatcher?.dispose();
}
}

0 comments on commit a2f82c3

Please sign in to comment.