|
| 1 | +import * as fs from 'fs-extra'; |
| 2 | +import { getUserHomeDir } from '../../../common/utils/pathUtils'; |
| 3 | +import { isWindows } from '../../../common/utils/platformUtils'; |
| 4 | + |
| 5 | +const pwshActivationEnvVarKey = 'VSCODE_PWSH_ACTIVATE'; |
| 6 | + |
| 7 | +enum PowerShellProfileType { |
| 8 | + AllUsersAllHosts = 4, |
| 9 | + AllUsersCurrentHost = 3, |
| 10 | + CurrentUserAllHosts = 2, |
| 11 | + CurrentUserCurrentHost = 1, |
| 12 | +} |
| 13 | + |
| 14 | +interface PowerShellProfile { |
| 15 | + type: PowerShellProfileType; |
| 16 | + path: string; |
| 17 | +} |
| 18 | + |
| 19 | +function getSearchPaths(): PowerShellProfile[] { |
| 20 | + const profilePaths: PowerShellProfile[] = []; |
| 21 | + const home = getUserHomeDir(); |
| 22 | + if (home) { |
| 23 | + if (isWindows()) { |
| 24 | + profilePaths.push( |
| 25 | + { |
| 26 | + type: PowerShellProfileType.CurrentUserAllHosts, |
| 27 | + path: `${home}\\Documents\\WindowsPowerShell\\profile.ps1`, |
| 28 | + }, |
| 29 | + { |
| 30 | + type: PowerShellProfileType.CurrentUserCurrentHost, |
| 31 | + path: `${home}\\Documents\\WindowsPowerShell\\Microsoft.PowerShell_profile.ps1`, |
| 32 | + }, |
| 33 | + ); |
| 34 | + } else { |
| 35 | + profilePaths.push( |
| 36 | + { |
| 37 | + type: PowerShellProfileType.CurrentUserAllHosts, |
| 38 | + path: `${home}/.config/powershell/profile.ps1`, |
| 39 | + }, |
| 40 | + { |
| 41 | + type: PowerShellProfileType.CurrentUserCurrentHost, |
| 42 | + path: `${home}.config/powershell/Microsoft.PowerShell_profile.ps1`, |
| 43 | + }, |
| 44 | + ); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return profilePaths.sort((a, b) => b.type - a.type); |
| 49 | +} |
| 50 | + |
| 51 | +async function getPowerShellProfile(): Promise<PowerShellProfile | undefined> { |
| 52 | + const profiles = getSearchPaths(); |
| 53 | + const existingProfiles: PowerShellProfile[] = []; |
| 54 | + await Promise.all( |
| 55 | + profiles.map(async (profile) => { |
| 56 | + if (await fs.pathExists(profile.path)) { |
| 57 | + existingProfiles.push(profile); |
| 58 | + } |
| 59 | + }), |
| 60 | + ); |
| 61 | + const containsActivation: PowerShellProfile[] = []; |
| 62 | + await Promise.all( |
| 63 | + existingProfiles.map(async (profile) => { |
| 64 | + const content = await fs.readFile(profile.path, 'utf8'); |
| 65 | + if (content.includes(pwshActivationEnvVarKey)) { |
| 66 | + containsActivation.push(profile); |
| 67 | + } |
| 68 | + }), |
| 69 | + ); |
| 70 | + |
| 71 | + if (containsActivation.length > 0) { |
| 72 | + return containsActivation.sort((a, b) => a.type - b.type)[0]; |
| 73 | + } |
| 74 | + return existingProfiles.length > 0 ? existingProfiles.sort((a, b) => a.type - b.type)[0] : undefined; |
| 75 | +} |
| 76 | + |
| 77 | +export async function isPowerShellStartupSetup(): Promise<boolean> { |
| 78 | + const profile = await getPowerShellProfile(); |
| 79 | + if (profile) { |
| 80 | + const content = await fs.readFile(profile.path, 'utf8'); |
| 81 | + return content.includes(pwshActivationEnvVarKey); |
| 82 | + } |
| 83 | + return false; |
| 84 | +} |
| 85 | + |
| 86 | +export async function setupPowerShellStartup(): Promise<void> { |
| 87 | + const lineSep = isWindows() ? '\r\n' : '\n'; |
| 88 | + 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}`; |
| 89 | + const profile = await getPowerShellProfile(); |
| 90 | + if (profile) { |
| 91 | + const content = await fs.readFile(profile.path, 'utf8'); |
| 92 | + if (!content.includes(pwshActivationEnvVarKey)) { |
| 93 | + await fs.writeFile(profile.path, `${content}${activationContent}`); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +export async function removePowerShellStartup(): Promise<void> { |
| 99 | + const profile = await getPowerShellProfile(); |
| 100 | + if (profile) { |
| 101 | + const content = await fs.readFile(profile.path, 'utf8'); |
| 102 | + if (content.includes(pwshActivationEnvVarKey)) { |
| 103 | + const newContent = content.replace( |
| 104 | + new RegExp(`# VSCODE-PYTHON-ACTIVATION:\\s*START.*# VSCODE-PYTHON-ACTIVATION:\\s*END`, 's'), |
| 105 | + '', |
| 106 | + ); |
| 107 | + await fs.writeFile(profile.path, newContent); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments