|
| 1 | +import * as vscode from "vscode"; |
| 2 | +import * as path from "path"; |
| 3 | + |
| 4 | +import * as output from "./output"; |
| 5 | +import { getWorkspaceSettings } from "./settings"; |
| 6 | +import { State, StateChange } from "./lsp"; |
| 7 | + |
| 8 | +// Manages the `PATH` environment variable contribution we make to integrated |
| 9 | +// terminals. |
| 10 | +export class PathEnvironmentVariableManager implements vscode.Disposable { |
| 11 | + private executableDirectory: string | null = null; |
| 12 | + private workspaceFolder: vscode.WorkspaceFolder | null = null; |
| 13 | + |
| 14 | + private subscriptions: vscode.Disposable[] = []; |
| 15 | + |
| 16 | + constructor( |
| 17 | + private collection: vscode.EnvironmentVariableCollection, |
| 18 | + onStateChange: vscode.Event<StateChange>, |
| 19 | + ) { |
| 20 | + // When the LSP fully starts up, sync our shared state and update `PATH` contribution. |
| 21 | + // When the LSP shuts down (restart or deactivation), clear shared state and unset `PATH` contribution. |
| 22 | + this.subscriptions.push( |
| 23 | + onStateChange((change) => { |
| 24 | + switch (change.state) { |
| 25 | + case State.Started: { |
| 26 | + this.executableDirectory = path.dirname( |
| 27 | + change.binaryPath, |
| 28 | + ); |
| 29 | + this.workspaceFolder = change.workspaceFolder; |
| 30 | + this.update(); |
| 31 | + break; |
| 32 | + } |
| 33 | + case State.Stopped: { |
| 34 | + this.executableDirectory = null; |
| 35 | + this.workspaceFolder = null; |
| 36 | + this.delete(); |
| 37 | + break; |
| 38 | + } |
| 39 | + } |
| 40 | + }), |
| 41 | + ); |
| 42 | + |
| 43 | + // When the user changes `air.addExecutableToPath`, reflect this immediately in new terminals |
| 44 | + this.subscriptions.push( |
| 45 | + vscode.workspace.onDidChangeConfiguration((event) => { |
| 46 | + if (event.affectsConfiguration("air.addExecutableToPath")) { |
| 47 | + this.update(); |
| 48 | + } |
| 49 | + }), |
| 50 | + ); |
| 51 | + } |
| 52 | + |
| 53 | + private update(): void { |
| 54 | + if (!this.workspaceFolder) { |
| 55 | + return; |
| 56 | + } |
| 57 | + if (!this.executableDirectory) { |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + const workspaceSettings = getWorkspaceSettings( |
| 62 | + "air", |
| 63 | + this.workspaceFolder, |
| 64 | + ); |
| 65 | + |
| 66 | + if (workspaceSettings.addExecutableToPath) { |
| 67 | + this.prepend(this.executableDirectory); |
| 68 | + } else { |
| 69 | + this.delete(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // Prepend a directory to the `PATH` |
| 74 | + // |
| 75 | + // `applyAtProcessCreation` and `applyAtShellIntegration` are really |
| 76 | + // mutually exclusive: |
| 77 | + // |
| 78 | + // - `applyAtProcessCreation` would modify the `PATH` as early as possible, |
| 79 | + // before any shell startup scripts run (like zshrc, for example). This |
| 80 | + // could allow shell scripts to overwrite our modifications. |
| 81 | + // - `applyAtShellIntegration` is the newer mechanism and applies the |
| 82 | + // modification via a VS Code managed shell integration script after any |
| 83 | + // shell startup scripts have run. |
| 84 | + private prepend(directory: string): void { |
| 85 | + this.collection.prepend("PATH", directory + path.delimiter, { |
| 86 | + applyAtProcessCreation: false, |
| 87 | + applyAtShellIntegration: true, |
| 88 | + }); |
| 89 | + output.log(`Added \`${directory}\` to the terminal PATH.`); |
| 90 | + } |
| 91 | + |
| 92 | + private delete(): void { |
| 93 | + this.collection.delete("PATH"); |
| 94 | + output.log(`Removed terminal PATH mutation.`); |
| 95 | + } |
| 96 | + |
| 97 | + dispose() { |
| 98 | + for (const subscription of this.subscriptions) { |
| 99 | + subscription.dispose(); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments