Skip to content

Commit 70b9da5

Browse files
committed
chore: refactor
1 parent 4cf08f3 commit 70b9da5

File tree

3 files changed

+31
-33
lines changed

3 files changed

+31
-33
lines changed

src/features/terminal/startup/powershellStartup.ts

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,16 @@
11
import * as fs from 'fs-extra';
22
import * as path from 'path';
3-
import * as cp from 'child_process';
43
import { isWindows } from '../../../common/utils/platformUtils';
54
import { ShellStartupProvider } from './startupProvider';
65
import { EnvironmentVariableCollection } from 'vscode';
76
import { PythonCommandRunConfiguration, PythonEnvironment, TerminalShellType } from '../../../api';
87
import { getActivationCommandForShell } from '../../common/activation';
98
import { quoteArgs } from '../../execution/execUtils';
109
import { traceInfo, traceVerbose } from '../../../common/logging';
10+
import { runCommand } from './utils';
1111

1212
const pwshActivationEnvVarKey = 'VSCODE_PWSH_ACTIVATE';
1313

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-
2714
interface PowerShellInfo {
2815
shell: 'powershell' | 'pwsh';
2916
profilePath: string;
@@ -167,30 +154,26 @@ export class PowershellStartupProvider implements ShellStartupProvider {
167154

168155
async updateEnvVariables(collection: EnvironmentVariableCollection, env: PythonEnvironment): Promise<void> {
169156
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);
185162
}
186163
}
187164

188165
async removeEnvVariables(envCollection: EnvironmentVariableCollection): Promise<void> {
189166
envCollection.delete(pwshActivationEnvVarKey);
190167
}
191168

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+
}
195178
}
196179
}

src/features/terminal/startup/startupProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ export interface ShellStartupProvider {
88
teardownScripts(): Promise<boolean>;
99
updateEnvVariables(envVars: EnvironmentVariableCollection, env: PythonEnvironment): Promise<void>;
1010
removeEnvVariables(envVars: EnvironmentVariableCollection): Promise<void>;
11-
getEnvVariables(env: PythonEnvironment): Promise<Map<string, string> | undefined>;
11+
getEnvVariables(env?: PythonEnvironment): Promise<Map<string, string | undefined> | undefined>;
1212
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import * as cp from 'child_process';
2+
import { traceVerbose } from '../../../common/logging';
3+
4+
export async function runCommand(command: string): Promise<string | undefined> {
5+
return new Promise((resolve) => {
6+
cp.exec(command, (err, stdout) => {
7+
if (err) {
8+
traceVerbose(`Error running command: ${command}`, err);
9+
resolve(undefined);
10+
} else {
11+
resolve(stdout?.trim());
12+
}
13+
});
14+
});
15+
}

0 commit comments

Comments
 (0)