Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/features/terminal/shells/bash/bashConstants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export const BASH_ENV_KEY = 'VSCODE_PYTHON_BASH_ACTIVATE';
export const ZSH_ENV_KEY = 'VSCODE_PYTHON_ZSH_ACTIVATE';
export const BASH_OLD_ENV_KEY = 'VSCODE_BASH_ACTIVATE';
export const ZSH_OLD_ENV_KEY = 'VSCODE_ZSH_ACTIVATE';
export const BASH_SCRIPT_VERSION = '0.1.1';
47 changes: 1 addition & 46 deletions src/features/terminal/shells/bash/bashEnvs.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { EnvironmentVariableCollection } from 'vscode';
import { PythonEnvironment } from '../../../../api';
import { traceError } from '../../../../common/logging';
import { ShellConstants } from '../../../common/shellConstants';
import { getShellActivationCommand, getShellCommandAsString } from '../common/shellUtils';
import { ShellEnvsProvider } from '../startupProvider';
import { BASH_ENV_KEY, ZSH_ENV_KEY } from './bashConstants';
import { BASH_ENV_KEY } from './bashConstants';

export class BashEnvsProvider implements ShellEnvsProvider {
constructor(public readonly shellType: 'bash' | 'gitbash') {}
Expand Down Expand Up @@ -50,47 +49,3 @@ export class BashEnvsProvider implements ShellEnvsProvider {
}
}
}

export class ZshEnvsProvider implements ShellEnvsProvider {
public readonly shellType: string = ShellConstants.ZSH;
updateEnvVariables(collection: EnvironmentVariableCollection, env: PythonEnvironment): void {
try {
const zshActivation = getShellActivationCommand(this.shellType, env);
if (zshActivation) {
const command = getShellCommandAsString(this.shellType, zshActivation);
const v = collection.get(ZSH_ENV_KEY);
if (v?.value === command) {
return;
}
collection.replace(ZSH_ENV_KEY, command);
} else {
collection.delete(ZSH_ENV_KEY);
}
} catch (err) {
traceError('Failed to update env variables for zsh', err);
collection.delete(ZSH_ENV_KEY);
}
}

removeEnvVariables(collection: EnvironmentVariableCollection): void {
collection.delete(ZSH_ENV_KEY);
}

getEnvVariables(env?: PythonEnvironment): Map<string, string | undefined> | undefined {
if (!env) {
return new Map([[ZSH_ENV_KEY, undefined]]);
}

try {
const zshActivation = getShellActivationCommand(this.shellType, env);
if (zshActivation) {
const command = getShellCommandAsString(this.shellType, zshActivation);
return new Map([[ZSH_ENV_KEY, command]]);
}
return undefined;
} catch (err) {
traceError('Failed to get env variables for zsh', err);
return undefined;
}
}
}
77 changes: 1 addition & 76 deletions src/features/terminal/shells/bash/bashStartup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,13 @@ import { traceError, traceInfo, traceVerbose } from '../../../../common/logging'
import { ShellConstants } from '../../../common/shellConstants';
import { hasStartupCode, insertStartupCode, removeStartupCode } from '../common/editUtils';
import { ShellScriptEditState, ShellSetupState, ShellStartupScriptProvider } from '../startupProvider';
import { BASH_ENV_KEY, BASH_OLD_ENV_KEY, BASH_SCRIPT_VERSION, ZSH_ENV_KEY, ZSH_OLD_ENV_KEY } from './bashConstants';
import { BASH_ENV_KEY, BASH_OLD_ENV_KEY, BASH_SCRIPT_VERSION } from './bashConstants';

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

async function isZshInstalled(): Promise<boolean> {
const result = await which('zsh', { nothrow: true });
return result !== null;
}

async function isGitBashInstalled(): Promise<boolean> {
const gitPath = await which('git', { nothrow: true });
if (gitPath) {
Expand All @@ -34,14 +29,6 @@ async function getBashProfiles(): Promise<string> {
return profile;
}

async function getZshProfiles(): Promise<string> {
const zdotdir = process.env.ZDOTDIR;
const baseDir = zdotdir || os.homedir();
const profile: string = path.join(baseDir, '.zshrc');

return profile;
}

const regionStart = '# >>> vscode python';
const regionEnd = '# <<< vscode python';

Expand Down Expand Up @@ -180,68 +167,6 @@ export class BashStartupProvider implements ShellStartupScriptProvider {
}
}

export class ZshStartupProvider implements ShellStartupScriptProvider {
public readonly name: string = 'zsh';
public readonly shellType: string = ShellConstants.ZSH;

private async checkShellInstalled(): Promise<boolean> {
const found = await isZshInstalled();
if (!found) {
traceInfo('`zsh` was not found on the system', 'If it is installed make sure it is available on `PATH`');
}
return found;
}

async isSetup(): Promise<ShellSetupState> {
const found = await this.checkShellInstalled();
if (!found) {
return ShellSetupState.NotInstalled;
}

try {
const zshProfiles = await getZshProfiles();
return await isStartupSetup(zshProfiles, ZSH_ENV_KEY);
} catch (err) {
traceError('Failed to check zsh startup scripts', err);
return ShellSetupState.NotSetup;
}
}

async setupScripts(): Promise<ShellScriptEditState> {
const found = await this.checkShellInstalled();
if (!found) {
return ShellScriptEditState.NotInstalled;
}
try {
const zshProfiles = await getZshProfiles();
const result = await setupStartup(zshProfiles, ZSH_ENV_KEY, this.name);
return result ? ShellScriptEditState.Edited : ShellScriptEditState.NotEdited;
} catch (err) {
traceError('Failed to setup zsh startup scripts', err);
return ShellScriptEditState.NotEdited;
}
}

async teardownScripts(): Promise<ShellScriptEditState> {
const found = await this.checkShellInstalled();
if (!found) {
return ShellScriptEditState.NotInstalled;
}
try {
const zshProfiles = await getZshProfiles();
await removeStartup(zshProfiles, ZSH_OLD_ENV_KEY);
const result = await removeStartup(zshProfiles, ZSH_ENV_KEY);
return result ? ShellScriptEditState.Edited : ShellScriptEditState.NotEdited;
} catch (err) {
traceError('Failed to teardown zsh startup scripts', err);
return ShellScriptEditState.NotEdited;
}
}
clearCache(): Promise<void> {
return Promise.resolve();
}
}

export class GitBashStartupProvider implements ShellStartupScriptProvider {
public readonly name: string = 'Git bash';
public readonly shellType: string = ShellConstants.GITBASH;
Expand Down
6 changes: 4 additions & 2 deletions src/features/terminal/shells/providers.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { isWindows } from '../../../common/utils/platformUtils';
import { ShellConstants } from '../../common/shellConstants';
import { BashEnvsProvider, ZshEnvsProvider } from './bash/bashEnvs';
import { BashStartupProvider, GitBashStartupProvider, ZshStartupProvider } from './bash/bashStartup';
import { BashEnvsProvider } from './bash/bashEnvs';
import { BashStartupProvider, GitBashStartupProvider } from './bash/bashStartup';
import { CmdEnvsProvider } from './cmd/cmdEnvs';
import { CmdStartupProvider } from './cmd/cmdStartup';
import { FishEnvsProvider } from './fish/fishEnvs';
import { FishStartupProvider } from './fish/fishStartup';
import { PowerShellEnvsProvider } from './pwsh/pwshEnvs';
import { PwshStartupProvider } from './pwsh/pwshStartup';
import { ZshEnvsProvider } from './zsh/zshEnvs';
import { ZshStartupProvider } from './zsh/zshStartup';
import { ShellEnvsProvider, ShellStartupScriptProvider } from './startupProvider';

export function createShellStartupProviders(): ShellStartupScriptProvider[] {
Expand Down
3 changes: 3 additions & 0 deletions src/features/terminal/shells/zsh/zshConstants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const ZSH_ENV_KEY = 'VSCODE_PYTHON_ZSH_ACTIVATE';
export const ZSH_OLD_ENV_KEY = 'VSCODE_ZSH_ACTIVATE';
export const ZSH_SCRIPT_VERSION = '0.1.1';
52 changes: 52 additions & 0 deletions src/features/terminal/shells/zsh/zshEnvs.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { EnvironmentVariableCollection } from 'vscode';
import { PythonEnvironment } from '../../../../api';
import { traceError } from '../../../../common/logging';
import { ShellConstants } from '../../../common/shellConstants';
import { getShellActivationCommand, getShellCommandAsString } from '../common/shellUtils';
import { ShellEnvsProvider } from '../startupProvider';
import { ZSH_ENV_KEY } from './zshConstants';

export class ZshEnvsProvider implements ShellEnvsProvider {
readonly shellType: string = ShellConstants.ZSH;

updateEnvVariables(collection: EnvironmentVariableCollection, env: PythonEnvironment): void {
try {
const zshActivation = getShellActivationCommand(this.shellType, env);
if (zshActivation) {
const command = getShellCommandAsString(this.shellType, zshActivation);
const v = collection.get(ZSH_ENV_KEY);
if (v?.value === command) {
return;
}
collection.replace(ZSH_ENV_KEY, command);
} else {
collection.delete(ZSH_ENV_KEY);
}
} catch (err) {
traceError('Failed to update env variables for zsh', err);
collection.delete(ZSH_ENV_KEY);
}
}

removeEnvVariables(collection: EnvironmentVariableCollection): void {
collection.delete(ZSH_ENV_KEY);
}

getEnvVariables(env?: PythonEnvironment): Map<string, string | undefined> | undefined {
if (!env) {
return new Map([[ZSH_ENV_KEY, undefined]]);
}

try {
const zshActivation = getShellActivationCommand(this.shellType, env);
if (zshActivation) {
const command = getShellCommandAsString(this.shellType, zshActivation);
return new Map([[ZSH_ENV_KEY, command]]);
}
return undefined;
} catch (err) {
traceError('Failed to get env variables for zsh', err);
return undefined;
}
}
}
Loading
Loading