|
| 1 | +import * as fs from 'fs-extra'; |
| 2 | +import * as path from 'path'; |
| 3 | +import * as os from 'os'; |
| 4 | +import { ShellStartupProvider } from './startupProvider'; |
| 5 | +import { EnvironmentVariableCollection } from 'vscode'; |
| 6 | +import { PythonCommandRunConfiguration, PythonEnvironment, TerminalShellType } from '../../../api'; |
| 7 | +import { getActivationCommandForShell } from '../../common/activation'; |
| 8 | +import { quoteArgs } from '../../execution/execUtils'; |
| 9 | +import { traceInfo, traceVerbose } from '../../../common/logging'; |
| 10 | + |
| 11 | +const bashActivationEnvVarKey = 'VSCODE_BASH_ACTIVATE'; |
| 12 | + |
| 13 | +async function getBashProfiles(): Promise<string[]> { |
| 14 | + const homeDir = os.homedir(); |
| 15 | + const profiles: string[] = [path.join(homeDir, '.bashrc'), path.join(homeDir, '.bash_profile')]; |
| 16 | + |
| 17 | + // Filter to only existing profiles or the first one if none exist |
| 18 | + const existingProfiles = await Promise.all( |
| 19 | + profiles.map(async (profile) => ({ |
| 20 | + profilePath: profile, |
| 21 | + exists: await fs.pathExists(profile), |
| 22 | + })), |
| 23 | + ); |
| 24 | + |
| 25 | + const result = existingProfiles.filter((p) => p.exists); |
| 26 | + if (result.length === 0) { |
| 27 | + // If no profile exists, return the first one so we can create it |
| 28 | + return [profiles[0]]; |
| 29 | + } |
| 30 | + |
| 31 | + return result.map((p) => p.profilePath); |
| 32 | +} |
| 33 | + |
| 34 | +async function getZshProfiles(): Promise<string[]> { |
| 35 | + const homeDir = os.homedir(); |
| 36 | + const profiles: string[] = [path.join(homeDir, '.zshrc')]; |
| 37 | + |
| 38 | + // Filter to only existing profiles or the first one if none exist |
| 39 | + const existingProfiles = await Promise.all( |
| 40 | + profiles.map(async (profile) => ({ |
| 41 | + profilePath: profile, |
| 42 | + exists: await fs.pathExists(profile), |
| 43 | + })), |
| 44 | + ); |
| 45 | + |
| 46 | + const result = existingProfiles.filter((p) => p.exists); |
| 47 | + if (result.length === 0) { |
| 48 | + // If no profile exists, return the first one so we can create it |
| 49 | + return [profiles[0]]; |
| 50 | + } |
| 51 | + |
| 52 | + return result.map((p) => p.profilePath); |
| 53 | +} |
| 54 | + |
| 55 | +const regionStart = '# vscode python environment activation begin'; |
| 56 | +const regionEnd = '# vscode python environment activation end'; |
| 57 | + |
| 58 | +function getActivationContent(): string { |
| 59 | + const lineSep = '\n'; |
| 60 | + return `${lineSep}${lineSep}${regionStart}${lineSep}if [ -n "$${bashActivationEnvVarKey}" ]; then${lineSep} eval "$${bashActivationEnvVarKey}"${lineSep}fi${lineSep}${regionEnd}${lineSep}`; |
| 61 | +} |
| 62 | + |
| 63 | +async function isStartupSetup(profiles: string[]): Promise<boolean> { |
| 64 | + if (profiles.length === 0) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + // Check if any profile has our activation content |
| 69 | + const results = await Promise.all( |
| 70 | + profiles.map(async (profile) => { |
| 71 | + if (await fs.pathExists(profile)) { |
| 72 | + const content = await fs.readFile(profile, 'utf8'); |
| 73 | + if (content.includes(bashActivationEnvVarKey)) { |
| 74 | + return true; |
| 75 | + } |
| 76 | + } |
| 77 | + return false; |
| 78 | + }), |
| 79 | + ); |
| 80 | + |
| 81 | + return results.some((result) => result); |
| 82 | +} |
| 83 | + |
| 84 | +async function setupStartup(profiles: string[]): Promise<boolean> { |
| 85 | + if (profiles.length === 0) { |
| 86 | + traceVerbose('Cannot setup Bash startup - No profiles found'); |
| 87 | + return false; |
| 88 | + } |
| 89 | + |
| 90 | + const activationContent = getActivationContent(); |
| 91 | + let successfulUpdates = 0; |
| 92 | + |
| 93 | + for (const profile of profiles) { |
| 94 | + try { |
| 95 | + // Create profile directory if it doesn't exist |
| 96 | + await fs.mkdirp(path.dirname(profile)); |
| 97 | + |
| 98 | + // Create or update profile |
| 99 | + if (!(await fs.pathExists(profile))) { |
| 100 | + // Create new profile with our content |
| 101 | + await fs.writeFile(profile, activationContent); |
| 102 | + traceInfo(`Created new profile at: ${profile}\n${activationContent}`); |
| 103 | + successfulUpdates++; |
| 104 | + } else { |
| 105 | + // Update existing profile |
| 106 | + const content = await fs.readFile(profile, 'utf8'); |
| 107 | + if (!content.includes(bashActivationEnvVarKey)) { |
| 108 | + await fs.writeFile(profile, `${content}${activationContent}`); |
| 109 | + traceInfo(`Updated existing profile at: ${profile}\n${activationContent}`); |
| 110 | + successfulUpdates++; |
| 111 | + } else { |
| 112 | + // Already contains our activation code |
| 113 | + successfulUpdates++; |
| 114 | + } |
| 115 | + } |
| 116 | + } catch (err) { |
| 117 | + traceVerbose(`Failed to setup ${profile} startup`, err); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // Return true only if all profiles were successfully updated |
| 122 | + return profiles.length > 0 && successfulUpdates === profiles.length; |
| 123 | +} |
| 124 | + |
| 125 | +async function removeBashStartup(profiles: string[]): Promise<boolean> { |
| 126 | + let successfulRemovals = 0; |
| 127 | + |
| 128 | + for (const profile of profiles) { |
| 129 | + if (!(await fs.pathExists(profile))) { |
| 130 | + successfulRemovals++; // Count as success if file doesn't exist since there's nothing to remove |
| 131 | + continue; |
| 132 | + } |
| 133 | + |
| 134 | + try { |
| 135 | + const content = await fs.readFile(profile, 'utf8'); |
| 136 | + if (content.includes(bashActivationEnvVarKey)) { |
| 137 | + // Use regex to remove the entire region including newlines |
| 138 | + const pattern = new RegExp(`${regionStart}[\\s\\S]*?${regionEnd}\\n?`, 'g'); |
| 139 | + const newContent = content.replace(pattern, ''); |
| 140 | + await fs.writeFile(profile, newContent); |
| 141 | + traceInfo(`Removed activation from profile at: ${profile}`); |
| 142 | + successfulRemovals++; |
| 143 | + } else { |
| 144 | + successfulRemovals++; // Count as success if activation is not present |
| 145 | + } |
| 146 | + } catch (err) { |
| 147 | + traceVerbose(`Failed to remove ${profile} startup`, err); |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + // Return true only if all profiles were successfully processed |
| 152 | + return profiles.length > 0 && successfulRemovals === profiles.length; |
| 153 | +} |
| 154 | + |
| 155 | +function getCommandAsString(command: PythonCommandRunConfiguration[]): string { |
| 156 | + const parts = []; |
| 157 | + for (const cmd of command) { |
| 158 | + const args = cmd.args ?? []; |
| 159 | + // For bash, we need to ensure proper quoting |
| 160 | + parts.push(quoteArgs([cmd.executable, ...args]).join(' ')); |
| 161 | + } |
| 162 | + return parts.join(' && '); |
| 163 | +} |
| 164 | + |
| 165 | +export class BashStartupProvider implements ShellStartupProvider { |
| 166 | + public readonly name: string = 'sh|bash|zsh'; |
| 167 | + |
| 168 | + async isSetup(): Promise<boolean> { |
| 169 | + const bashProfiles = await getBashProfiles(); |
| 170 | + const zshProfiles = await getZshProfiles(); |
| 171 | + const result = await Promise.all([isStartupSetup(bashProfiles), isStartupSetup(zshProfiles)]); |
| 172 | + return result.every((res) => res); |
| 173 | + } |
| 174 | + |
| 175 | + async setupScripts(): Promise<boolean> { |
| 176 | + const bashProfiles = await getBashProfiles(); |
| 177 | + const zshProfiles = await getZshProfiles(); |
| 178 | + const result = await Promise.all([setupStartup(bashProfiles), setupStartup(zshProfiles)]); |
| 179 | + return result.every((res) => res); |
| 180 | + } |
| 181 | + |
| 182 | + async teardownScripts(): Promise<boolean> { |
| 183 | + const bashProfiles = await getBashProfiles(); |
| 184 | + const zshProfiles = await getZshProfiles(); |
| 185 | + const result = await Promise.all([removeBashStartup(bashProfiles), removeBashStartup(zshProfiles)]); |
| 186 | + return result.every((res) => res); |
| 187 | + } |
| 188 | + |
| 189 | + async updateEnvVariables(collection: EnvironmentVariableCollection, env: PythonEnvironment): Promise<void> { |
| 190 | + const bashActivation = getActivationCommandForShell(env, TerminalShellType.bash); |
| 191 | + if (bashActivation) { |
| 192 | + const command = getCommandAsString(bashActivation); |
| 193 | + collection.replace(bashActivationEnvVarKey, command); |
| 194 | + } else { |
| 195 | + collection.delete(bashActivationEnvVarKey); |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + async removeEnvVariables(envCollection: EnvironmentVariableCollection): Promise<void> { |
| 200 | + envCollection.delete(bashActivationEnvVarKey); |
| 201 | + } |
| 202 | + |
| 203 | + async getEnvVariables(env?: PythonEnvironment): Promise<Map<string, string | undefined> | undefined> { |
| 204 | + if (env) { |
| 205 | + const bashActivation = getActivationCommandForShell(env, TerminalShellType.bash); |
| 206 | + return bashActivation |
| 207 | + ? new Map([[bashActivationEnvVarKey, getCommandAsString(bashActivation)]]) |
| 208 | + : undefined; |
| 209 | + } else { |
| 210 | + return new Map([[bashActivationEnvVarKey, undefined]]); |
| 211 | + } |
| 212 | + } |
| 213 | +} |
0 commit comments