|
1 | 1 | import * as fs from 'fs-extra'; |
2 | 2 | import * as path from 'path'; |
3 | | -import * as cp from 'child_process'; |
4 | 3 | import { isWindows } from '../../../common/utils/platformUtils'; |
5 | 4 | import { ShellStartupProvider } from './startupProvider'; |
6 | 5 | import { EnvironmentVariableCollection } from 'vscode'; |
7 | 6 | import { PythonCommandRunConfiguration, PythonEnvironment, TerminalShellType } from '../../../api'; |
8 | 7 | import { getActivationCommandForShell } from '../../common/activation'; |
9 | 8 | import { quoteArgs } from '../../execution/execUtils'; |
10 | 9 | import { traceInfo, traceVerbose } from '../../../common/logging'; |
| 10 | +import { runCommand } from './utils'; |
11 | 11 |
|
12 | 12 | const pwshActivationEnvVarKey = 'VSCODE_PWSH_ACTIVATE'; |
13 | 13 |
|
14 | | -async function runCommand(command: string): Promise<string | undefined> { |
15 | | - return new Promise((resolve) => { |
16 | | - cp.exec(command, (err, stdout) => { |
17 | | - if (err) { |
18 | | - traceVerbose(`Error running command: ${command}`, err); |
19 | | - resolve(undefined); |
20 | | - } else { |
21 | | - resolve(stdout?.trim()); |
22 | | - } |
23 | | - }); |
24 | | - }); |
25 | | -} |
26 | | - |
27 | 14 | interface PowerShellInfo { |
28 | 15 | shell: 'powershell' | 'pwsh'; |
29 | 16 | profilePath: string; |
@@ -167,30 +154,26 @@ export class PowershellStartupProvider implements ShellStartupProvider { |
167 | 154 |
|
168 | 155 | async updateEnvVariables(collection: EnvironmentVariableCollection, env: PythonEnvironment): Promise<void> { |
169 | 156 | const pwshActivation = getActivationCommandForShell(env, TerminalShellType.powershell); |
170 | | - |
171 | | - const curValue = collection.get(pwshActivationEnvVarKey); |
172 | | - if (curValue) { |
173 | | - if (pwshActivation) { |
174 | | - const command = getCommandAsString(pwshActivation); |
175 | | - if (curValue.value !== command) { |
176 | | - collection.replace(pwshActivationEnvVarKey, command, { applyAtProcessCreation: true }); |
177 | | - } |
178 | | - } else { |
179 | | - collection.delete(pwshActivationEnvVarKey); |
180 | | - } |
181 | | - } else if (pwshActivation) { |
182 | | - collection.replace(pwshActivationEnvVarKey, getCommandAsString(pwshActivation), { |
183 | | - applyAtProcessCreation: true, |
184 | | - }); |
| 157 | + if (pwshActivation) { |
| 158 | + const command = getCommandAsString(pwshActivation); |
| 159 | + collection.replace(pwshActivationEnvVarKey, command); |
| 160 | + } else { |
| 161 | + collection.delete(pwshActivationEnvVarKey); |
185 | 162 | } |
186 | 163 | } |
187 | 164 |
|
188 | 165 | async removeEnvVariables(envCollection: EnvironmentVariableCollection): Promise<void> { |
189 | 166 | envCollection.delete(pwshActivationEnvVarKey); |
190 | 167 | } |
191 | 168 |
|
192 | | - async getEnvVariables(env: PythonEnvironment): Promise<Map<string, string> | undefined> { |
193 | | - const pwshActivation = getActivationCommandForShell(env, TerminalShellType.powershell); |
194 | | - return pwshActivation ? new Map([[pwshActivationEnvVarKey, getCommandAsString(pwshActivation)]]) : undefined; |
| 169 | + async getEnvVariables(env?: PythonEnvironment): Promise<Map<string, string | undefined> | undefined> { |
| 170 | + if (env) { |
| 171 | + const pwshActivation = getActivationCommandForShell(env, TerminalShellType.powershell); |
| 172 | + return pwshActivation |
| 173 | + ? new Map([[pwshActivationEnvVarKey, getCommandAsString(pwshActivation)]]) |
| 174 | + : undefined; |
| 175 | + } else { |
| 176 | + return new Map([[pwshActivationEnvVarKey, undefined]]); |
| 177 | + } |
195 | 178 | } |
196 | 179 | } |
0 commit comments