Skip to content

Commit fb142e1

Browse files
Tyriareleanorjboyd
andauthored
Move zsh code out of bash folder (#1150)
Fixes #1148 Co-authored-by: Eleanor Boyd <26030610+eleanorjboyd@users.noreply.github.com>
1 parent ac8abbd commit fb142e1

File tree

7 files changed

+216
-126
lines changed

7 files changed

+216
-126
lines changed
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
export const BASH_ENV_KEY = 'VSCODE_PYTHON_BASH_ACTIVATE';
2-
export const ZSH_ENV_KEY = 'VSCODE_PYTHON_ZSH_ACTIVATE';
32
export const BASH_OLD_ENV_KEY = 'VSCODE_BASH_ACTIVATE';
4-
export const ZSH_OLD_ENV_KEY = 'VSCODE_ZSH_ACTIVATE';
53
export const BASH_SCRIPT_VERSION = '0.1.1';
Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { EnvironmentVariableCollection } from 'vscode';
22
import { PythonEnvironment } from '../../../../api';
33
import { traceError } from '../../../../common/logging';
4-
import { ShellConstants } from '../../../common/shellConstants';
54
import { getShellActivationCommand, getShellCommandAsString } from '../common/shellUtils';
65
import { ShellEnvsProvider } from '../startupProvider';
7-
import { BASH_ENV_KEY, ZSH_ENV_KEY } from './bashConstants';
6+
import { BASH_ENV_KEY } from './bashConstants';
87

98
export class BashEnvsProvider implements ShellEnvsProvider {
109
constructor(public readonly shellType: 'bash' | 'gitbash') {}
@@ -50,47 +49,3 @@ export class BashEnvsProvider implements ShellEnvsProvider {
5049
}
5150
}
5251
}
53-
54-
export class ZshEnvsProvider implements ShellEnvsProvider {
55-
public readonly shellType: string = ShellConstants.ZSH;
56-
updateEnvVariables(collection: EnvironmentVariableCollection, env: PythonEnvironment): void {
57-
try {
58-
const zshActivation = getShellActivationCommand(this.shellType, env);
59-
if (zshActivation) {
60-
const command = getShellCommandAsString(this.shellType, zshActivation);
61-
const v = collection.get(ZSH_ENV_KEY);
62-
if (v?.value === command) {
63-
return;
64-
}
65-
collection.replace(ZSH_ENV_KEY, command);
66-
} else {
67-
collection.delete(ZSH_ENV_KEY);
68-
}
69-
} catch (err) {
70-
traceError('Failed to update env variables for zsh', err);
71-
collection.delete(ZSH_ENV_KEY);
72-
}
73-
}
74-
75-
removeEnvVariables(collection: EnvironmentVariableCollection): void {
76-
collection.delete(ZSH_ENV_KEY);
77-
}
78-
79-
getEnvVariables(env?: PythonEnvironment): Map<string, string | undefined> | undefined {
80-
if (!env) {
81-
return new Map([[ZSH_ENV_KEY, undefined]]);
82-
}
83-
84-
try {
85-
const zshActivation = getShellActivationCommand(this.shellType, env);
86-
if (zshActivation) {
87-
const command = getShellCommandAsString(this.shellType, zshActivation);
88-
return new Map([[ZSH_ENV_KEY, command]]);
89-
}
90-
return undefined;
91-
} catch (err) {
92-
traceError('Failed to get env variables for zsh', err);
93-
return undefined;
94-
}
95-
}
96-
}

src/features/terminal/shells/bash/bashStartup.ts

Lines changed: 1 addition & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,13 @@ import { traceError, traceInfo, traceVerbose } from '../../../../common/logging'
66
import { ShellConstants } from '../../../common/shellConstants';
77
import { hasStartupCode, insertStartupCode, removeStartupCode } from '../common/editUtils';
88
import { ShellScriptEditState, ShellSetupState, ShellStartupScriptProvider } from '../startupProvider';
9-
import { BASH_ENV_KEY, BASH_OLD_ENV_KEY, BASH_SCRIPT_VERSION, ZSH_ENV_KEY, ZSH_OLD_ENV_KEY } from './bashConstants';
9+
import { BASH_ENV_KEY, BASH_OLD_ENV_KEY, BASH_SCRIPT_VERSION } from './bashConstants';
1010

1111
async function isBashLikeInstalled(): Promise<boolean> {
1212
const result = await Promise.all([which('bash', { nothrow: true }), which('sh', { nothrow: true })]);
1313
return result.some((r) => r !== null);
1414
}
1515

16-
async function isZshInstalled(): Promise<boolean> {
17-
const result = await which('zsh', { nothrow: true });
18-
return result !== null;
19-
}
20-
2116
async function isGitBashInstalled(): Promise<boolean> {
2217
const gitPath = await which('git', { nothrow: true });
2318
if (gitPath) {
@@ -34,14 +29,6 @@ async function getBashProfiles(): Promise<string> {
3429
return profile;
3530
}
3631

37-
async function getZshProfiles(): Promise<string> {
38-
const zdotdir = process.env.ZDOTDIR;
39-
const baseDir = zdotdir || os.homedir();
40-
const profile: string = path.join(baseDir, '.zshrc');
41-
42-
return profile;
43-
}
44-
4532
const regionStart = '# >>> vscode python';
4633
const regionEnd = '# <<< vscode python';
4734

@@ -180,68 +167,6 @@ export class BashStartupProvider implements ShellStartupScriptProvider {
180167
}
181168
}
182169

183-
export class ZshStartupProvider implements ShellStartupScriptProvider {
184-
public readonly name: string = 'zsh';
185-
public readonly shellType: string = ShellConstants.ZSH;
186-
187-
private async checkShellInstalled(): Promise<boolean> {
188-
const found = await isZshInstalled();
189-
if (!found) {
190-
traceInfo('`zsh` was not found on the system', 'If it is installed make sure it is available on `PATH`');
191-
}
192-
return found;
193-
}
194-
195-
async isSetup(): Promise<ShellSetupState> {
196-
const found = await this.checkShellInstalled();
197-
if (!found) {
198-
return ShellSetupState.NotInstalled;
199-
}
200-
201-
try {
202-
const zshProfiles = await getZshProfiles();
203-
return await isStartupSetup(zshProfiles, ZSH_ENV_KEY);
204-
} catch (err) {
205-
traceError('Failed to check zsh startup scripts', err);
206-
return ShellSetupState.NotSetup;
207-
}
208-
}
209-
210-
async setupScripts(): Promise<ShellScriptEditState> {
211-
const found = await this.checkShellInstalled();
212-
if (!found) {
213-
return ShellScriptEditState.NotInstalled;
214-
}
215-
try {
216-
const zshProfiles = await getZshProfiles();
217-
const result = await setupStartup(zshProfiles, ZSH_ENV_KEY, this.name);
218-
return result ? ShellScriptEditState.Edited : ShellScriptEditState.NotEdited;
219-
} catch (err) {
220-
traceError('Failed to setup zsh startup scripts', err);
221-
return ShellScriptEditState.NotEdited;
222-
}
223-
}
224-
225-
async teardownScripts(): Promise<ShellScriptEditState> {
226-
const found = await this.checkShellInstalled();
227-
if (!found) {
228-
return ShellScriptEditState.NotInstalled;
229-
}
230-
try {
231-
const zshProfiles = await getZshProfiles();
232-
await removeStartup(zshProfiles, ZSH_OLD_ENV_KEY);
233-
const result = await removeStartup(zshProfiles, ZSH_ENV_KEY);
234-
return result ? ShellScriptEditState.Edited : ShellScriptEditState.NotEdited;
235-
} catch (err) {
236-
traceError('Failed to teardown zsh startup scripts', err);
237-
return ShellScriptEditState.NotEdited;
238-
}
239-
}
240-
clearCache(): Promise<void> {
241-
return Promise.resolve();
242-
}
243-
}
244-
245170
export class GitBashStartupProvider implements ShellStartupScriptProvider {
246171
public readonly name: string = 'Git bash';
247172
public readonly shellType: string = ShellConstants.GITBASH;

src/features/terminal/shells/providers.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { isWindows } from '../../../common/utils/platformUtils';
22
import { ShellConstants } from '../../common/shellConstants';
3-
import { BashEnvsProvider, ZshEnvsProvider } from './bash/bashEnvs';
4-
import { BashStartupProvider, GitBashStartupProvider, ZshStartupProvider } from './bash/bashStartup';
3+
import { BashEnvsProvider } from './bash/bashEnvs';
4+
import { BashStartupProvider, GitBashStartupProvider } from './bash/bashStartup';
55
import { CmdEnvsProvider } from './cmd/cmdEnvs';
66
import { CmdStartupProvider } from './cmd/cmdStartup';
77
import { FishEnvsProvider } from './fish/fishEnvs';
88
import { FishStartupProvider } from './fish/fishStartup';
99
import { PowerShellEnvsProvider } from './pwsh/pwshEnvs';
1010
import { PwshStartupProvider } from './pwsh/pwshStartup';
11+
import { ZshEnvsProvider } from './zsh/zshEnvs';
12+
import { ZshStartupProvider } from './zsh/zshStartup';
1113
import { ShellEnvsProvider, ShellStartupScriptProvider } from './startupProvider';
1214

1315
export function createShellStartupProviders(): ShellStartupScriptProvider[] {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const ZSH_ENV_KEY = 'VSCODE_PYTHON_ZSH_ACTIVATE';
2+
export const ZSH_OLD_ENV_KEY = 'VSCODE_ZSH_ACTIVATE';
3+
export const ZSH_SCRIPT_VERSION = '0.1.1';
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { EnvironmentVariableCollection } from 'vscode';
2+
import { PythonEnvironment } from '../../../../api';
3+
import { traceError } from '../../../../common/logging';
4+
import { ShellConstants } from '../../../common/shellConstants';
5+
import { getShellActivationCommand, getShellCommandAsString } from '../common/shellUtils';
6+
import { ShellEnvsProvider } from '../startupProvider';
7+
import { ZSH_ENV_KEY } from './zshConstants';
8+
9+
export class ZshEnvsProvider implements ShellEnvsProvider {
10+
readonly shellType: string = ShellConstants.ZSH;
11+
12+
updateEnvVariables(collection: EnvironmentVariableCollection, env: PythonEnvironment): void {
13+
try {
14+
const zshActivation = getShellActivationCommand(this.shellType, env);
15+
if (zshActivation) {
16+
const command = getShellCommandAsString(this.shellType, zshActivation);
17+
const v = collection.get(ZSH_ENV_KEY);
18+
if (v?.value === command) {
19+
return;
20+
}
21+
collection.replace(ZSH_ENV_KEY, command);
22+
} else {
23+
collection.delete(ZSH_ENV_KEY);
24+
}
25+
} catch (err) {
26+
traceError('Failed to update env variables for zsh', err);
27+
collection.delete(ZSH_ENV_KEY);
28+
}
29+
}
30+
31+
removeEnvVariables(collection: EnvironmentVariableCollection): void {
32+
collection.delete(ZSH_ENV_KEY);
33+
}
34+
35+
getEnvVariables(env?: PythonEnvironment): Map<string, string | undefined> | undefined {
36+
if (!env) {
37+
return new Map([[ZSH_ENV_KEY, undefined]]);
38+
}
39+
40+
try {
41+
const zshActivation = getShellActivationCommand(this.shellType, env);
42+
if (zshActivation) {
43+
const command = getShellCommandAsString(this.shellType, zshActivation);
44+
return new Map([[ZSH_ENV_KEY, command]]);
45+
}
46+
return undefined;
47+
} catch (err) {
48+
traceError('Failed to get env variables for zsh', err);
49+
return undefined;
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)