Skip to content

Commit e08c49d

Browse files
committed
feat: initial powershell startup provider
1 parent 0b2fd63 commit e08c49d

4 files changed

Lines changed: 91 additions & 4 deletions

File tree

src/features/terminal/startup/activateUsingShellStartup.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,36 @@ import { Disposable, GlobalEnvironmentVariableCollection } from 'vscode';
22
import { onDidChangeConfiguration } from '../../../common/workspace.apis';
33
import { registerCommand } from '../../../common/command.api';
44
import { getAutoActivationType } from '../utils';
5+
import { EnvironmentManagers } from '../../../internal.api';
56

67
export interface ActivateUsingShellStartup extends Disposable {}
78

89
class ActivateUsingShellStartupImpl implements ActivateUsingShellStartup {
910
private readonly disposables: Disposable[] = [];
10-
constructor(private readonly envCollection: GlobalEnvironmentVariableCollection) {}
11+
constructor(
12+
private readonly envCollection: GlobalEnvironmentVariableCollection,
13+
private readonly em: EnvironmentManagers,
14+
) {
15+
this.disposables.push(
16+
onDidChangeConfiguration((e) => {
17+
this.handleConfigurationChange(e);
18+
}),
19+
);
20+
}
21+
22+
private handleConfigurationChange(e) {
23+
if (e.affectsConfiguration('python.terminal.autoActivationType')) {
24+
const autoActType = getAutoActivationType();
25+
if (autoActType === 'shellStartup') {
26+
s;
27+
} else {
28+
}
29+
}
30+
}
31+
32+
private async addActivationVariables(): Promise<void> {}
33+
34+
private async removeActivationVariables(): Promise<void> {}
1135

1236
dispose() {
1337
this.disposables.forEach((disposable) => disposable.dispose());
@@ -27,6 +51,7 @@ export async function removeAllStartupScripts(): Promise<void> {
2751
export function registerActivateUsingShellStartup(
2852
disposables: Disposable[],
2953
environmentVariableCollection: GlobalEnvironmentVariableCollection,
54+
em: EnvironmentManagers,
3055
) {
3156
let activateUsingShellStartup: ActivateUsingShellStartup | undefined;
3257

src/features/terminal/startup/powershellStartup.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import * as fs from 'fs-extra';
22
import { getUserHomeDir } from '../../../common/utils/pathUtils';
33
import { isWindows } from '../../../common/utils/platformUtils';
4+
import { ShellStartupProvider } from './startupProvider';
5+
import { EnvironmentVariableScope, GlobalEnvironmentVariableCollection } from 'vscode';
6+
import { EnvironmentManagers } from '../../../internal.api';
47

58
const pwshActivationEnvVarKey = 'VSCODE_PWSH_ACTIVATE';
69

@@ -74,7 +77,7 @@ async function getPowerShellProfile(): Promise<PowerShellProfile | undefined> {
7477
return existingProfiles.length > 0 ? existingProfiles.sort((a, b) => a.type - b.type)[0] : undefined;
7578
}
7679

77-
export async function isPowerShellStartupSetup(): Promise<boolean> {
80+
async function isPowerShellStartupSetup(): Promise<boolean> {
7881
const profile = await getPowerShellProfile();
7982
if (profile) {
8083
const content = await fs.readFile(profile.path, 'utf8');
@@ -83,7 +86,7 @@ export async function isPowerShellStartupSetup(): Promise<boolean> {
8386
return false;
8487
}
8588

86-
export async function setupPowerShellStartup(): Promise<void> {
89+
async function setupPowerShellStartup(): Promise<void> {
8790
const lineSep = isWindows() ? '\r\n' : '\n';
8891
const activationContent = `${lineSep}${lineSep}# VSCODE-PYTHON-ACTIVATION:START${lineSep}if ($env:${pwshActivationEnvVarKey} -ne $null) {${lineSep} Invoke-Expression $env:${pwshActivationEnvVarKey}${lineSep}}${lineSep}# VSCODE-PYTHON-ACTIVATION:END${lineSep}`;
8992
const profile = await getPowerShellProfile();
@@ -95,7 +98,7 @@ export async function setupPowerShellStartup(): Promise<void> {
9598
}
9699
}
97100

98-
export async function removePowerShellStartup(): Promise<void> {
101+
async function removePowerShellStartup(): Promise<void> {
99102
const profile = await getPowerShellProfile();
100103
if (profile) {
101104
const content = await fs.readFile(profile.path, 'utf8');
@@ -108,3 +111,30 @@ export async function removePowerShellStartup(): Promise<void> {
108111
}
109112
}
110113
}
114+
115+
export class PowershellStartupProvider implements ShellStartupProvider {
116+
constructor(private readonly em: EnvironmentManagers) {}
117+
async isSetup(): Promise<boolean> {
118+
return await isPowerShellStartupSetup();
119+
}
120+
121+
async setupScripts(): Promise<void> {
122+
await setupPowerShellStartup();
123+
}
124+
125+
async teardownScripts(): Promise<void> {
126+
await removePowerShellStartup();
127+
}
128+
129+
async updateEnvVariables(
130+
global: GlobalEnvironmentVariableCollection,
131+
scope: EnvironmentVariableScope,
132+
): Promise<void> {
133+
const envVars = global.getScoped(scope);
134+
}
135+
136+
async removeEnvVariables(envCollection: GlobalEnvironmentVariableCollection): Promise<void> {
137+
// Implementation for removing environment variables if needed
138+
// Currently, this method is not implemented
139+
}
140+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { GlobalEnvironmentVariableCollection, Uri } from 'vscode';
2+
3+
export interface ShellStartupProvider {
4+
isSetup(): Promise<boolean>;
5+
setupScripts(): Promise<void>;
6+
removeScripts(): Promise<void>;
7+
updateEnvVariables(global: GlobalEnvironmentVariableCollection, scope?: Uri): Promise<void>;
8+
removeEnvVariables(global: GlobalEnvironmentVariableCollection, scope?: Uri): Promise<void>;
9+
}

src/features/terminal/utils.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,26 @@ export function getAutoActivationType(): 'off' | 'command' | 'shellStartup' {
9797
const config = getConfiguration('python-envs');
9898
return config.get<'off' | 'command' | 'shellStartup'>('terminal.autoActivationType', 'command');
9999
}
100+
101+
export async function getAllDistinctProjectEnvironments(
102+
api: PythonProjectGetterApi & PythonProjectEnvironmentApi,
103+
): Promise<PythonEnvironment[] | undefined> {
104+
const envs: PythonEnvironment[] | undefined = [];
105+
106+
const projects = api.getPythonProjects();
107+
if (projects.length === 0) {
108+
const env = await api.getEnvironment(undefined);
109+
if (env) {
110+
envs.push(env);
111+
}
112+
} else if (projects.length === 1) {
113+
const env = await api.getEnvironment(projects[0].uri);
114+
if (env) {
115+
envs.push(env);
116+
}
117+
} else {
118+
envs.push(...(await getDistinctProjectEnvs(api, projects)));
119+
}
120+
121+
return envs.length > 0 ? envs : undefined;
122+
}

0 commit comments

Comments
 (0)