|
1 | 1 | import * as fs from 'fs-extra'; |
2 | 2 | import path from 'path'; |
3 | | -import { ThemeIcon, Uri } from 'vscode'; |
4 | | -import { PythonCommandRunConfiguration, PythonEnvironment, PythonEnvironmentInfo } from '../../api'; |
| 3 | +import { PythonCommandRunConfiguration, PythonEnvironment } from '../../api'; |
5 | 4 | import { isWindows } from '../../common/utils/platformUtils'; |
6 | 5 | import { ShellConstants } from '../../features/common/shellConstants'; |
7 | | -import { NativeEnvInfo } from './nativePythonFinder'; |
8 | 6 | import { Installable } from './types'; |
9 | 7 |
|
10 | 8 | export function noop() { |
@@ -118,111 +116,81 @@ export function compareVersions(version1: string, version2: string): number { |
118 | 116 | return 0; |
119 | 117 | } |
120 | 118 |
|
121 | | -export async function getPythonInfo(env: NativeEnvInfo): Promise<PythonEnvironmentInfo> { |
122 | | - if (env.executable && env.version && env.prefix) { |
123 | | - const venvName = env.name; |
124 | | - const sv = shortVersion(env.version); |
125 | | - const name = `${venvName} (${sv})`; |
| 119 | +export async function getShellActivationCommands(binDir: string): Promise<{ |
| 120 | + shellActivation: Map<string, PythonCommandRunConfiguration[]>; |
| 121 | + shellDeactivation: Map<string, PythonCommandRunConfiguration[]>; |
| 122 | +}> { |
| 123 | + const shellActivation: Map<string, PythonCommandRunConfiguration[]> = new Map(); |
| 124 | + const shellDeactivation: Map<string, PythonCommandRunConfiguration[]> = new Map(); |
126 | 125 |
|
127 | | - const binDir = path.dirname(env.executable); |
128 | | - |
129 | | - const shellActivation: Map<string, PythonCommandRunConfiguration[]> = new Map(); |
130 | | - const shellDeactivation: Map<string, PythonCommandRunConfiguration[]> = new Map(); |
131 | | - |
132 | | - if (isWindows()) { |
133 | | - shellActivation.set('unknown', [{ executable: path.join(binDir, `activate`) }]); |
134 | | - shellDeactivation.set('unknown', [{ executable: path.join(binDir, `deactivate`) }]); |
135 | | - } else { |
136 | | - shellActivation.set('unknown', [{ executable: 'source', args: [path.join(binDir, `activate`)] }]); |
137 | | - shellDeactivation.set('unknown', [{ executable: 'deactivate' }]); |
138 | | - } |
| 126 | + if (isWindows()) { |
| 127 | + shellActivation.set('unknown', [{ executable: path.join(binDir, `activate`) }]); |
| 128 | + shellDeactivation.set('unknown', [{ executable: path.join(binDir, `deactivate`) }]); |
| 129 | + } else { |
| 130 | + shellActivation.set('unknown', [{ executable: 'source', args: [path.join(binDir, `activate`)] }]); |
| 131 | + shellDeactivation.set('unknown', [{ executable: 'deactivate' }]); |
| 132 | + } |
139 | 133 |
|
140 | | - shellActivation.set(ShellConstants.SH, [{ executable: 'source', args: [path.join(binDir, `activate`)] }]); |
141 | | - shellDeactivation.set(ShellConstants.SH, [{ executable: 'deactivate' }]); |
| 134 | + shellActivation.set(ShellConstants.SH, [{ executable: 'source', args: [path.join(binDir, `activate`)] }]); |
| 135 | + shellDeactivation.set(ShellConstants.SH, [{ executable: 'deactivate' }]); |
142 | 136 |
|
143 | | - shellActivation.set(ShellConstants.BASH, [{ executable: 'source', args: [path.join(binDir, `activate`)] }]); |
144 | | - shellDeactivation.set(ShellConstants.BASH, [{ executable: 'deactivate' }]); |
| 137 | + shellActivation.set(ShellConstants.BASH, [{ executable: 'source', args: [path.join(binDir, `activate`)] }]); |
| 138 | + shellDeactivation.set(ShellConstants.BASH, [{ executable: 'deactivate' }]); |
145 | 139 |
|
146 | | - shellActivation.set(ShellConstants.GITBASH, [ |
147 | | - { executable: 'source', args: [pathForGitBash(path.join(binDir, `activate`))] }, |
148 | | - ]); |
149 | | - shellDeactivation.set(ShellConstants.GITBASH, [{ executable: 'deactivate' }]); |
| 140 | + shellActivation.set(ShellConstants.GITBASH, [ |
| 141 | + { executable: 'source', args: [pathForGitBash(path.join(binDir, `activate`))] }, |
| 142 | + ]); |
| 143 | + shellDeactivation.set(ShellConstants.GITBASH, [{ executable: 'deactivate' }]); |
150 | 144 |
|
151 | | - shellActivation.set(ShellConstants.ZSH, [{ executable: 'source', args: [path.join(binDir, `activate`)] }]); |
152 | | - shellDeactivation.set(ShellConstants.ZSH, [{ executable: 'deactivate' }]); |
| 145 | + shellActivation.set(ShellConstants.ZSH, [{ executable: 'source', args: [path.join(binDir, `activate`)] }]); |
| 146 | + shellDeactivation.set(ShellConstants.ZSH, [{ executable: 'deactivate' }]); |
153 | 147 |
|
154 | | - shellActivation.set(ShellConstants.KSH, [{ executable: '.', args: [path.join(binDir, `activate`)] }]); |
155 | | - shellDeactivation.set(ShellConstants.KSH, [{ executable: 'deactivate' }]); |
| 148 | + shellActivation.set(ShellConstants.KSH, [{ executable: '.', args: [path.join(binDir, `activate`)] }]); |
| 149 | + shellDeactivation.set(ShellConstants.KSH, [{ executable: 'deactivate' }]); |
156 | 150 |
|
157 | | - if (await fs.pathExists(path.join(binDir, 'Activate.ps1'))) { |
158 | | - shellActivation.set(ShellConstants.PWSH, [{ executable: '&', args: [path.join(binDir, `Activate.ps1`)] }]); |
159 | | - shellDeactivation.set(ShellConstants.PWSH, [{ executable: 'deactivate' }]); |
160 | | - } else if (await fs.pathExists(path.join(binDir, 'activate.ps1'))) { |
161 | | - shellActivation.set(ShellConstants.PWSH, [{ executable: '&', args: [path.join(binDir, `activate.ps1`)] }]); |
162 | | - shellDeactivation.set(ShellConstants.PWSH, [{ executable: 'deactivate' }]); |
163 | | - } |
164 | | - |
165 | | - if (await fs.pathExists(path.join(binDir, 'activate.bat'))) { |
166 | | - shellActivation.set(ShellConstants.CMD, [{ executable: path.join(binDir, `activate.bat`) }]); |
167 | | - shellDeactivation.set(ShellConstants.CMD, [{ executable: path.join(binDir, `deactivate.bat`) }]); |
168 | | - } |
| 151 | + if (await fs.pathExists(path.join(binDir, 'Activate.ps1'))) { |
| 152 | + shellActivation.set(ShellConstants.PWSH, [{ executable: '&', args: [path.join(binDir, `Activate.ps1`)] }]); |
| 153 | + shellDeactivation.set(ShellConstants.PWSH, [{ executable: 'deactivate' }]); |
| 154 | + } else if (await fs.pathExists(path.join(binDir, 'activate.ps1'))) { |
| 155 | + shellActivation.set(ShellConstants.PWSH, [{ executable: '&', args: [path.join(binDir, `activate.ps1`)] }]); |
| 156 | + shellDeactivation.set(ShellConstants.PWSH, [{ executable: 'deactivate' }]); |
| 157 | + } |
169 | 158 |
|
170 | | - if (await fs.pathExists(path.join(binDir, 'activate.csh'))) { |
171 | | - shellActivation.set(ShellConstants.CSH, [ |
172 | | - { executable: 'source', args: [path.join(binDir, `activate.csh`)] }, |
173 | | - ]); |
174 | | - shellDeactivation.set(ShellConstants.CSH, [{ executable: 'deactivate' }]); |
| 159 | + if (await fs.pathExists(path.join(binDir, 'activate.bat'))) { |
| 160 | + shellActivation.set(ShellConstants.CMD, [{ executable: path.join(binDir, `activate.bat`) }]); |
| 161 | + shellDeactivation.set(ShellConstants.CMD, [{ executable: path.join(binDir, `deactivate.bat`) }]); |
| 162 | + } |
175 | 163 |
|
176 | | - shellActivation.set(ShellConstants.FISH, [ |
177 | | - { executable: 'source', args: [path.join(binDir, `activate.csh`)] }, |
178 | | - ]); |
179 | | - shellDeactivation.set(ShellConstants.FISH, [{ executable: 'deactivate' }]); |
180 | | - } |
| 164 | + if (await fs.pathExists(path.join(binDir, 'activate.csh'))) { |
| 165 | + shellActivation.set(ShellConstants.CSH, [{ executable: 'source', args: [path.join(binDir, `activate.csh`)] }]); |
| 166 | + shellDeactivation.set(ShellConstants.CSH, [{ executable: 'deactivate' }]); |
181 | 167 |
|
182 | | - if (await fs.pathExists(path.join(binDir, 'activate.fish'))) { |
183 | | - shellActivation.set(ShellConstants.FISH, [ |
184 | | - { executable: 'source', args: [path.join(binDir, `activate.fish`)] }, |
185 | | - ]); |
186 | | - shellDeactivation.set(ShellConstants.FISH, [{ executable: 'deactivate' }]); |
187 | | - } |
| 168 | + shellActivation.set(ShellConstants.FISH, [{ executable: 'source', args: [path.join(binDir, `activate.csh`)] }]); |
| 169 | + shellDeactivation.set(ShellConstants.FISH, [{ executable: 'deactivate' }]); |
| 170 | + } |
188 | 171 |
|
189 | | - if (await fs.pathExists(path.join(binDir, 'activate.xsh'))) { |
190 | | - shellActivation.set(ShellConstants.XONSH, [ |
191 | | - { executable: 'source', args: [path.join(binDir, `activate.xsh`)] }, |
192 | | - ]); |
193 | | - shellDeactivation.set(ShellConstants.XONSH, [{ executable: 'deactivate' }]); |
194 | | - } |
| 172 | + if (await fs.pathExists(path.join(binDir, 'activate.fish'))) { |
| 173 | + shellActivation.set(ShellConstants.FISH, [ |
| 174 | + { executable: 'source', args: [path.join(binDir, `activate.fish`)] }, |
| 175 | + ]); |
| 176 | + shellDeactivation.set(ShellConstants.FISH, [{ executable: 'deactivate' }]); |
| 177 | + } |
195 | 178 |
|
196 | | - if (await fs.pathExists(path.join(binDir, 'activate.nu'))) { |
197 | | - shellActivation.set(ShellConstants.NU, [ |
198 | | - { executable: 'overlay', args: ['use', path.join(binDir, 'activate.nu')] }, |
199 | | - ]); |
200 | | - shellDeactivation.set(ShellConstants.NU, [{ executable: 'overlay', args: ['hide', 'activate'] }]); |
201 | | - } |
| 179 | + if (await fs.pathExists(path.join(binDir, 'activate.xsh'))) { |
| 180 | + shellActivation.set(ShellConstants.XONSH, [ |
| 181 | + { executable: 'source', args: [path.join(binDir, `activate.xsh`)] }, |
| 182 | + ]); |
| 183 | + shellDeactivation.set(ShellConstants.XONSH, [{ executable: 'deactivate' }]); |
| 184 | + } |
202 | 185 |
|
203 | | - return { |
204 | | - name: name, |
205 | | - displayName: name, |
206 | | - shortDisplayName: `${sv} (${venvName})`, |
207 | | - displayPath: env.executable, |
208 | | - version: env.version, |
209 | | - description: undefined, |
210 | | - tooltip: env.executable, |
211 | | - environmentPath: Uri.file(env.executable), |
212 | | - iconPath: new ThemeIcon('python'), |
213 | | - sysPrefix: env.prefix, |
214 | | - execInfo: { |
215 | | - run: { |
216 | | - executable: env.executable, |
217 | | - }, |
218 | | - activatedRun: { |
219 | | - executable: env.executable, |
220 | | - }, |
221 | | - shellActivation, |
222 | | - shellDeactivation, |
223 | | - }, |
224 | | - }; |
225 | | - } else { |
226 | | - throw new Error(`Invalid python info: ${JSON.stringify(env)}`); |
| 186 | + if (await fs.pathExists(path.join(binDir, 'activate.nu'))) { |
| 187 | + shellActivation.set(ShellConstants.NU, [ |
| 188 | + { executable: 'overlay', args: ['use', path.join(binDir, 'activate.nu')] }, |
| 189 | + ]); |
| 190 | + shellDeactivation.set(ShellConstants.NU, [{ executable: 'overlay', args: ['hide', 'activate'] }]); |
227 | 191 | } |
| 192 | + return { |
| 193 | + shellActivation, |
| 194 | + shellDeactivation, |
| 195 | + }; |
228 | 196 | } |
0 commit comments