|
| 1 | +import { COMMAND_SERVER_EXTENSION_ID } from "@cursorless/vscode-common"; |
| 2 | +import { globSync } from "glob"; |
| 3 | +import * as fs from "node:fs"; |
| 4 | +import * as os from "node:os"; |
| 5 | +import * as path from "node:path"; |
| 6 | +import * as vscode from "vscode"; |
| 7 | + |
| 8 | +const STATE_KEY = "dontShowInstallationDependencies"; |
| 9 | + |
| 10 | +export class InstallationDependencies { |
| 11 | + private panel: vscode.WebviewPanel | undefined; |
| 12 | + |
| 13 | + constructor(private extensionContext: vscode.ExtensionContext) { |
| 14 | + this.show = this.show.bind(this); |
| 15 | + this.maybeShow = this.maybeShow.bind(this); |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * Shows the installation dependencies webview. |
| 20 | + */ |
| 21 | + show() { |
| 22 | + this.createWebview(); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * Shows the installation dependencies webview if there are missing dependencies. |
| 27 | + */ |
| 28 | + maybeShow() { |
| 29 | + const state = this.getState(); |
| 30 | + if (state.hasMissingDependencies && !state.dontShow) { |
| 31 | + this.createWebview(); |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + private getState() { |
| 36 | + const dependencies = getDependencies(); |
| 37 | + const hasMissingDependencies = Object.values(dependencies).some( |
| 38 | + (value) => !value, |
| 39 | + ); |
| 40 | + return { |
| 41 | + dontShow: !!this.extensionContext.globalState.get<boolean>(STATE_KEY), |
| 42 | + hasMissingDependencies, |
| 43 | + dependencies, |
| 44 | + }; |
| 45 | + } |
| 46 | + |
| 47 | + private createWebview() { |
| 48 | + if (this.panel != null) { |
| 49 | + this.panel.reveal(); |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + this.panel = vscode.window.createWebviewPanel( |
| 54 | + "cursorless.installationDependencies", |
| 55 | + "Cursorless dependencies", |
| 56 | + { |
| 57 | + viewColumn: vscode.ViewColumn.Active, |
| 58 | + }, |
| 59 | + { |
| 60 | + enableScripts: true, |
| 61 | + }, |
| 62 | + ); |
| 63 | + |
| 64 | + this.panel.webview.html = this.getWebviewContent(); |
| 65 | + |
| 66 | + const updateWebview = () => { |
| 67 | + this.panel?.webview.postMessage(this.getState()); |
| 68 | + }; |
| 69 | + |
| 70 | + this.panel.onDidChangeViewState(updateWebview); |
| 71 | + |
| 72 | + this.panel.webview.onDidReceiveMessage((message) => { |
| 73 | + if (message.type === "dontShow") { |
| 74 | + const checked = message.checked; |
| 75 | + this.extensionContext.globalState.update(STATE_KEY, checked); |
| 76 | + } else { |
| 77 | + console.error(`Unknown message: ${message}`); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + const interval = setInterval(updateWebview, 5000); |
| 82 | + |
| 83 | + this.panel.onDidDispose(() => { |
| 84 | + clearInterval(interval); |
| 85 | + this.panel = undefined; |
| 86 | + }); |
| 87 | + |
| 88 | + this.panel.webview.postMessage(this.getState()); |
| 89 | + } |
| 90 | + |
| 91 | + private getWebviewContent() { |
| 92 | + if (this.panel == null) { |
| 93 | + throw new Error("Panel not created yet"); |
| 94 | + } |
| 95 | + const htmlPath = this.getResourceUri("installationDependencies.html"); |
| 96 | + const jsUri = this.getResourceUri("installationDependencies.js"); |
| 97 | + const template = fs |
| 98 | + .readFileSync(htmlPath.fsPath, "utf8") |
| 99 | + .replace("META_CONTENT", `script-src ${this.panel.webview.cspSource};`) |
| 100 | + .replace("SCRIPT_SOURCE", jsUri.toString()); |
| 101 | + return template; |
| 102 | + } |
| 103 | + |
| 104 | + private getResourceUri(name: string): vscode.Uri { |
| 105 | + if (this.panel == null) { |
| 106 | + throw new Error("Panel not created yet"); |
| 107 | + } |
| 108 | + return this.panel.webview.asWebviewUri( |
| 109 | + vscode.Uri.joinPath( |
| 110 | + this.extensionContext.extensionUri, |
| 111 | + "resources", |
| 112 | + name, |
| 113 | + ), |
| 114 | + ); |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +function getDependencies(): Record<string, boolean> { |
| 119 | + return { |
| 120 | + talon: talonHomeExists(), |
| 121 | + cursorlessTalon: cursorlessTalonExists(), |
| 122 | + commandServer: commandServerInstalled(), |
| 123 | + }; |
| 124 | +} |
| 125 | + |
| 126 | +function talonHomeExists() { |
| 127 | + return fs.existsSync(getTalonHomePath()); |
| 128 | +} |
| 129 | + |
| 130 | +function cursorlessTalonExists() { |
| 131 | + const talonUserPath = path.join(getTalonHomePath(), "user"); |
| 132 | + const files = globSync("**/*/src/cursorless.talon", { |
| 133 | + cwd: talonUserPath, |
| 134 | + maxDepth: 3, |
| 135 | + }); |
| 136 | + return files.length > 0; |
| 137 | +} |
| 138 | + |
| 139 | +function commandServerInstalled() { |
| 140 | + const extension = vscode.extensions.getExtension(COMMAND_SERVER_EXTENSION_ID); |
| 141 | + return extension != null; |
| 142 | +} |
| 143 | + |
| 144 | +function getTalonHomePath() { |
| 145 | + return os.platform() === "win32" |
| 146 | + ? `${os.homedir()}\\AppData\\Roaming\\talon` |
| 147 | + : `${os.homedir()}/.talon`; |
| 148 | +} |
0 commit comments