|
1 | | -import { Disposable, GlobalEnvironmentVariableCollection } from 'vscode'; |
2 | | -import { onDidChangeConfiguration } from '../../../common/workspace.apis'; |
3 | | -import { registerCommand } from '../../../common/command.api'; |
4 | | -import { getAutoActivationType } from '../utils'; |
| 1 | +import { ConfigurationChangeEvent, Disposable, GlobalEnvironmentVariableCollection } from 'vscode'; |
| 2 | +import { getWorkspaceFolder, getWorkspaceFolders, onDidChangeConfiguration } from '../../../common/workspace.apis'; |
| 3 | +import { getAutoActivationType, setAutoActivationType } from '../utils'; |
| 4 | +import { ShellStartupProvider } from './startupProvider'; |
| 5 | +import { DidChangeEnvironmentEventArgs } from '../../../api'; |
5 | 6 | import { EnvironmentManagers } from '../../../internal.api'; |
| 7 | +import { traceError, traceInfo } from '../../../common/logging'; |
| 8 | +import { ShellStartupActivationStrings } from '../../../common/localize'; |
| 9 | +import { showInformationMessage } from '../../../common/window.apis'; |
6 | 10 |
|
7 | | -export interface ActivateUsingShellStartup extends Disposable {} |
| 11 | +export interface ShellStartupActivationManager extends Disposable { |
| 12 | + initialize(): Promise<void>; |
| 13 | + updateStartupScripts(): Promise<void>; |
| 14 | + cleanupStartupScripts(): Promise<void>; |
| 15 | +} |
8 | 16 |
|
9 | | -class ActivateUsingShellStartupImpl implements ActivateUsingShellStartup { |
| 17 | +export class ShellStartupActivationManagerImpl implements ShellStartupActivationManager { |
10 | 18 | private readonly disposables: Disposable[] = []; |
11 | 19 | constructor( |
12 | 20 | private readonly envCollection: GlobalEnvironmentVariableCollection, |
| 21 | + private readonly shellStartupProviders: ShellStartupProvider[], |
13 | 22 | private readonly em: EnvironmentManagers, |
14 | 23 | ) { |
15 | 24 | this.disposables.push( |
16 | | - onDidChangeConfiguration((e) => { |
| 25 | + onDidChangeConfiguration((e: ConfigurationChangeEvent) => { |
17 | 26 | this.handleConfigurationChange(e); |
18 | 27 | }), |
| 28 | + this.em.onDidChangeEnvironmentFiltered((e: DidChangeEnvironmentEventArgs) => { |
| 29 | + this.handleEnvironmentChange(e); |
| 30 | + }), |
19 | 31 | ); |
20 | 32 | } |
21 | 33 |
|
22 | | - private handleConfigurationChange(e) { |
23 | | - if (e.affectsConfiguration('python.terminal.autoActivationType')) { |
| 34 | + private async handleConfigurationChange(e: ConfigurationChangeEvent) { |
| 35 | + if (e.affectsConfiguration('python-envs.terminal.autoActivationType')) { |
24 | 36 | const autoActType = getAutoActivationType(); |
25 | 37 | if (autoActType === 'shellStartup') { |
26 | | - s; |
| 38 | + await this.initialize(); |
27 | 39 | } else { |
| 40 | + // remove any contributed environment variables |
| 41 | + const workspaces = getWorkspaceFolders() ?? []; |
| 42 | + if (workspaces.length > 0) { |
| 43 | + // User has one or more workspaces open |
| 44 | + const promises: Promise<void>[] = []; |
| 45 | + workspaces.forEach((workspace) => { |
| 46 | + const collection = this.envCollection.getScoped({ workspaceFolder: workspace }); |
| 47 | + promises.push( |
| 48 | + ...this.shellStartupProviders.map((provider) => provider.removeEnvVariables(collection)), |
| 49 | + ); |
| 50 | + }); |
| 51 | + await Promise.all(promises); |
| 52 | + } else { |
| 53 | + // User has no workspaces open |
| 54 | + await Promise.all( |
| 55 | + this.shellStartupProviders.map((provider) => provider.removeEnvVariables(this.envCollection)), |
| 56 | + ); |
| 57 | + } |
28 | 58 | } |
29 | 59 | } |
30 | 60 | } |
31 | 61 |
|
32 | | - private async addActivationVariables(): Promise<void> {} |
33 | | - |
34 | | - private async removeActivationVariables(): Promise<void> {} |
| 62 | + private async handleEnvironmentChange(e: DidChangeEnvironmentEventArgs) { |
| 63 | + const autoActType = getAutoActivationType(); |
| 64 | + if (autoActType !== 'shellStartup') { |
| 65 | + return; |
| 66 | + } |
35 | 67 |
|
36 | | - dispose() { |
37 | | - this.disposables.forEach((disposable) => disposable.dispose()); |
| 68 | + if (e.uri) { |
| 69 | + const wf = getWorkspaceFolder(e.uri); |
| 70 | + if (wf) { |
| 71 | + const envVars = this.envCollection.getScoped({ workspaceFolder: wf }); |
| 72 | + if (envVars) { |
| 73 | + await Promise.all( |
| 74 | + this.shellStartupProviders.map(async (provider) => { |
| 75 | + if (e.new) { |
| 76 | + await provider.updateEnvVariables(envVars, e.new); |
| 77 | + } else { |
| 78 | + await provider.removeEnvVariables(envVars); |
| 79 | + } |
| 80 | + }), |
| 81 | + ); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
38 | 85 | } |
39 | | -} |
40 | | - |
41 | | -export async function checkAndUpdateStartupScripts(): Promise<void> { |
42 | | - // Implement the logic to check startup scripts |
43 | | - return Promise.resolve(); |
44 | | -} |
45 | 86 |
|
46 | | -export async function removeAllStartupScripts(): Promise<void> { |
47 | | - // Implement the logic to remove all startup scripts |
48 | | - return Promise.resolve(); |
49 | | -} |
| 87 | + private async isSetupRequired(): Promise<boolean> { |
| 88 | + const results = await Promise.all(this.shellStartupProviders.map((provider) => provider.isSetup())); |
| 89 | + return results.some((result) => !result); |
| 90 | + } |
50 | 91 |
|
51 | | -export function registerActivateUsingShellStartup( |
52 | | - disposables: Disposable[], |
53 | | - environmentVariableCollection: GlobalEnvironmentVariableCollection, |
54 | | - em: EnvironmentManagers, |
55 | | -) { |
56 | | - let activateUsingShellStartup: ActivateUsingShellStartup | undefined; |
| 92 | + public async initialize(): Promise<void> { |
| 93 | + const autoActType = getAutoActivationType(); |
| 94 | + if (autoActType === 'shellStartup') { |
| 95 | + if (await this.isSetupRequired()) { |
| 96 | + const result = await showInformationMessage( |
| 97 | + ShellStartupActivationStrings.shellStartupScriptEditPrompt, |
| 98 | + { modal: true }, |
| 99 | + ShellStartupActivationStrings.updateScript, |
| 100 | + ); |
57 | 101 |
|
58 | | - disposables.push( |
59 | | - onDidChangeConfiguration((e) => { |
60 | | - if (e.affectsConfiguration('python.terminal.autoActivationType')) { |
61 | | - const autoActType = getAutoActivationType(); |
62 | | - if (autoActType === 'shellStartup') { |
63 | | - if (!activateUsingShellStartup) { |
64 | | - activateUsingShellStartup = new ActivateUsingShellStartupImpl(environmentVariableCollection); |
65 | | - } |
| 102 | + if (ShellStartupActivationStrings.updateScript === result) { |
| 103 | + await this.updateStartupScripts(); |
66 | 104 | } else { |
67 | | - activateUsingShellStartup?.dispose(); |
68 | | - activateUsingShellStartup = undefined; |
| 105 | + traceError('User declined to edit shell startup scripts. See <doc-link> for more information.'); |
| 106 | + traceInfo('Setting `python-envs.terminal.autoActivationType` to `command`.'); |
| 107 | + setAutoActivationType('command'); |
| 108 | + return; |
69 | 109 | } |
70 | 110 | } |
71 | | - }), |
72 | | - new Disposable(() => activateUsingShellStartup?.dispose()), |
73 | | - registerCommand('python-envs.removeStartupScripts', async () => { |
74 | | - await removeAllStartupScripts(); |
75 | | - }), |
76 | | - ); |
77 | 111 |
|
78 | | - const autoActType = getAutoActivationType(); |
79 | | - if (autoActType === 'shellStartup') { |
80 | | - activateUsingShellStartup = new ActivateUsingShellStartupImpl(environmentVariableCollection); |
81 | | - setImmediate(async () => { |
82 | | - await checkAndUpdateStartupScripts(); |
83 | | - }); |
| 112 | + const workspaces = getWorkspaceFolders() ?? []; |
| 113 | + |
| 114 | + if (workspaces.length > 0) { |
| 115 | + const promises: Promise<void>[] = []; |
| 116 | + workspaces.forEach((workspace) => { |
| 117 | + const collection = this.envCollection.getScoped({ workspaceFolder: workspace }); |
| 118 | + promises.push( |
| 119 | + ...this.shellStartupProviders.map(async (provider) => { |
| 120 | + const env = await this.em.getEnvironment(workspace.uri); |
| 121 | + if (env) { |
| 122 | + await provider.updateEnvVariables(collection, env); |
| 123 | + } else { |
| 124 | + await provider.removeEnvVariables(collection); |
| 125 | + } |
| 126 | + }), |
| 127 | + ); |
| 128 | + }); |
| 129 | + await Promise.all(promises); |
| 130 | + } else { |
| 131 | + await Promise.all( |
| 132 | + this.shellStartupProviders.map(async (provider) => { |
| 133 | + const env = await this.em.getEnvironment(undefined); |
| 134 | + if (env) { |
| 135 | + await provider.updateEnvVariables(this.envCollection, env); |
| 136 | + } else { |
| 137 | + await provider.removeEnvVariables(this.envCollection); |
| 138 | + } |
| 139 | + }), |
| 140 | + ); |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + public async updateStartupScripts(): Promise<void> { |
| 146 | + await Promise.all(this.shellStartupProviders.map((provider) => provider.setupScripts())); |
| 147 | + } |
| 148 | + |
| 149 | + public async cleanupStartupScripts(): Promise<void> { |
| 150 | + await Promise.all(this.shellStartupProviders.map((provider) => provider.teardownScripts())); |
| 151 | + setAutoActivationType('command'); |
| 152 | + showInformationMessage(ShellStartupActivationStrings.revertToCommandActivation); |
| 153 | + } |
| 154 | + |
| 155 | + dispose() { |
| 156 | + this.disposables.forEach((disposable) => disposable.dispose()); |
84 | 157 | } |
85 | 158 | } |
0 commit comments