11import * as fsapi from 'fs-extra' ;
22import * as os from 'os' ;
33import * as path from 'path' ;
4- import { l10n , LogOutputChannel , ProgressLocation , QuickPickItem , QuickPickItemKind , ThemeIcon , Uri } from 'vscode' ;
5- import {
6- EnvironmentManager ,
7- PythonCommandRunConfiguration ,
8- PythonEnvironment ,
9- PythonEnvironmentApi ,
10- PythonEnvironmentInfo ,
11- } from '../../api' ;
4+ import { l10n , LogOutputChannel , ProgressLocation , QuickPickItem , QuickPickItemKind , Uri } from 'vscode' ;
5+ import { EnvironmentManager , PythonEnvironment , PythonEnvironmentApi } from '../../api' ;
126import { ENVS_EXTENSION_ID } from '../../common/constants' ;
137import { Common , VenvManagerStrings } from '../../common/localize' ;
148import { traceInfo } from '../../common/logging' ;
159import { getWorkspacePersistentState } from '../../common/persistentState' ;
1610import { pickEnvironmentFrom } from '../../common/pickers/environments' ;
1711import { EventNames } from '../../common/telemetry/constants' ;
1812import { sendTelemetryEvent } from '../../common/telemetry/sender' ;
19- import { isWindows } from '../../common/utils/platformUtils' ;
2013import {
2114 showErrorMessage ,
2215 showInputBox ,
@@ -26,14 +19,13 @@ import {
2619 withProgress ,
2720} from '../../common/window.apis' ;
2821import { getConfiguration } from '../../common/workspace.apis' ;
29- import { ShellConstants } from '../../features/common/shellConstants' ;
3022import {
3123 isNativeEnvInfo ,
3224 NativeEnvInfo ,
3325 NativePythonEnvironmentKind ,
3426 NativePythonFinder ,
3527} from '../common/nativePythonFinder' ;
36- import { pathForGitBash , shortVersion , sortEnvironments } from '../common/utils' ;
28+ import { getPythonInfo , sortEnvironments } from '../common/utils' ;
3729import { isUvInstalled , runPython , runUV } from './helpers' ;
3830import { getProjectInstallable , getWorkspacePackagesToInstall , PipPackages } from './pipUtils' ;
3931import { resolveSystemPythonEnvironmentPath } from './utils' ;
@@ -106,122 +98,7 @@ export async function setVenvForGlobal(envPath: string | undefined): Promise<voi
10698 await state . set ( VENV_GLOBAL_KEY , envPath ) ;
10799}
108100
109- function getName ( binPath : string ) : string {
110- const dir1 = path . dirname ( binPath ) ;
111- if ( dir1 . endsWith ( 'bin' ) || dir1 . endsWith ( 'Scripts' ) || dir1 . endsWith ( 'scripts' ) ) {
112- return path . basename ( path . dirname ( dir1 ) ) ;
113- }
114- return path . basename ( dir1 ) ;
115- }
116-
117- async function getPythonInfo ( env : NativeEnvInfo ) : Promise < PythonEnvironmentInfo > {
118- if ( env . executable && env . version && env . prefix ) {
119- const venvName = env . name ?? getName ( env . executable ) ;
120- const sv = shortVersion ( env . version ) ;
121- const name = `${ venvName } (${ sv } )` ;
122-
123- const binDir = path . dirname ( env . executable ) ;
124-
125- const shellActivation : Map < string , PythonCommandRunConfiguration [ ] > = new Map ( ) ;
126- const shellDeactivation : Map < string , PythonCommandRunConfiguration [ ] > = new Map ( ) ;
127-
128- if ( isWindows ( ) ) {
129- shellActivation . set ( 'unknown' , [ { executable : path . join ( binDir , `activate` ) } ] ) ;
130- shellDeactivation . set ( 'unknown' , [ { executable : path . join ( binDir , `deactivate` ) } ] ) ;
131- } else {
132- shellActivation . set ( 'unknown' , [ { executable : 'source' , args : [ path . join ( binDir , `activate` ) ] } ] ) ;
133- shellDeactivation . set ( 'unknown' , [ { executable : 'deactivate' } ] ) ;
134- }
135-
136- shellActivation . set ( ShellConstants . SH , [ { executable : 'source' , args : [ path . join ( binDir , `activate` ) ] } ] ) ;
137- shellDeactivation . set ( ShellConstants . SH , [ { executable : 'deactivate' } ] ) ;
138-
139- shellActivation . set ( ShellConstants . BASH , [ { executable : 'source' , args : [ path . join ( binDir , `activate` ) ] } ] ) ;
140- shellDeactivation . set ( ShellConstants . BASH , [ { executable : 'deactivate' } ] ) ;
141-
142- shellActivation . set ( ShellConstants . GITBASH , [
143- { executable : 'source' , args : [ pathForGitBash ( path . join ( binDir , `activate` ) ) ] } ,
144- ] ) ;
145- shellDeactivation . set ( ShellConstants . GITBASH , [ { executable : 'deactivate' } ] ) ;
146-
147- shellActivation . set ( ShellConstants . ZSH , [ { executable : 'source' , args : [ path . join ( binDir , `activate` ) ] } ] ) ;
148- shellDeactivation . set ( ShellConstants . ZSH , [ { executable : 'deactivate' } ] ) ;
149-
150- shellActivation . set ( ShellConstants . KSH , [ { executable : '.' , args : [ path . join ( binDir , `activate` ) ] } ] ) ;
151- shellDeactivation . set ( ShellConstants . KSH , [ { executable : 'deactivate' } ] ) ;
152-
153- if ( await fsapi . pathExists ( path . join ( binDir , 'Activate.ps1' ) ) ) {
154- shellActivation . set ( ShellConstants . PWSH , [ { executable : '&' , args : [ path . join ( binDir , `Activate.ps1` ) ] } ] ) ;
155- shellDeactivation . set ( ShellConstants . PWSH , [ { executable : 'deactivate' } ] ) ;
156- } else if ( await fsapi . pathExists ( path . join ( binDir , 'activate.ps1' ) ) ) {
157- shellActivation . set ( ShellConstants . PWSH , [ { executable : '&' , args : [ path . join ( binDir , `activate.ps1` ) ] } ] ) ;
158- shellDeactivation . set ( ShellConstants . PWSH , [ { executable : 'deactivate' } ] ) ;
159- }
160101
161- if ( await fsapi . pathExists ( path . join ( binDir , 'activate.bat' ) ) ) {
162- shellActivation . set ( ShellConstants . CMD , [ { executable : path . join ( binDir , `activate.bat` ) } ] ) ;
163- shellDeactivation . set ( ShellConstants . CMD , [ { executable : path . join ( binDir , `deactivate.bat` ) } ] ) ;
164- }
165-
166- if ( await fsapi . pathExists ( path . join ( binDir , 'activate.csh' ) ) ) {
167- shellActivation . set ( ShellConstants . CSH , [
168- { executable : 'source' , args : [ path . join ( binDir , `activate.csh` ) ] } ,
169- ] ) ;
170- shellDeactivation . set ( ShellConstants . CSH , [ { executable : 'deactivate' } ] ) ;
171-
172- shellActivation . set ( ShellConstants . FISH , [
173- { executable : 'source' , args : [ path . join ( binDir , `activate.csh` ) ] } ,
174- ] ) ;
175- shellDeactivation . set ( ShellConstants . FISH , [ { executable : 'deactivate' } ] ) ;
176- }
177-
178- if ( await fsapi . pathExists ( path . join ( binDir , 'activate.fish' ) ) ) {
179- shellActivation . set ( ShellConstants . FISH , [
180- { executable : 'source' , args : [ path . join ( binDir , `activate.fish` ) ] } ,
181- ] ) ;
182- shellDeactivation . set ( ShellConstants . FISH , [ { executable : 'deactivate' } ] ) ;
183- }
184-
185- if ( await fsapi . pathExists ( path . join ( binDir , 'activate.xsh' ) ) ) {
186- shellActivation . set ( ShellConstants . XONSH , [
187- { executable : 'source' , args : [ path . join ( binDir , `activate.xsh` ) ] } ,
188- ] ) ;
189- shellDeactivation . set ( ShellConstants . XONSH , [ { executable : 'deactivate' } ] ) ;
190- }
191-
192- if ( await fsapi . pathExists ( path . join ( binDir , 'activate.nu' ) ) ) {
193- shellActivation . set ( ShellConstants . NU , [
194- { executable : 'overlay' , args : [ 'use' , path . join ( binDir , 'activate.nu' ) ] } ,
195- ] ) ;
196- shellDeactivation . set ( ShellConstants . NU , [ { executable : 'overlay' , args : [ 'hide' , 'activate' ] } ] ) ;
197- }
198-
199- return {
200- name : name ,
201- displayName : name ,
202- shortDisplayName : `${ sv } (${ venvName } )` ,
203- displayPath : env . executable ,
204- version : env . version ,
205- description : undefined ,
206- tooltip : env . executable ,
207- environmentPath : Uri . file ( env . executable ) ,
208- iconPath : new ThemeIcon ( 'python' ) ,
209- sysPrefix : env . prefix ,
210- execInfo : {
211- run : {
212- executable : env . executable ,
213- } ,
214- activatedRun : {
215- executable : env . executable ,
216- } ,
217- shellActivation,
218- shellDeactivation,
219- } ,
220- } ;
221- } else {
222- throw new Error ( `Invalid python info: ${ JSON . stringify ( env ) } ` ) ;
223- }
224- }
225102
226103export async function findVirtualEnvironments (
227104 hardRefresh : boolean ,
0 commit comments