-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython.ps1
42 lines (38 loc) · 872 Bytes
/
python.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
$global:currentVenv = ""
enum PythonVenvStatus {
inactive
active
activated
deactivated
}
function ActivatePython () {
if ($global:currentVenv) { & "$global:currentVenv\Scripts\Activate.ps1" }
}
function DeactivatePython () {
if ($global:currentVenv) {deactivate}
}
function Try-ActivatePython () {
$currentDir = (Get-Location).Path
$venvDir = ""
$venvDirs = ".venv", "venv"
foreach ($dir in $venvDirs) {
if (Test-Path "$currentDir\$dir" -PathType Container) {
$venvDir = $dir
break
}
}
if (!$venvDir -and $global:currentVenv) {
DeactivatePython
$global:currentVenv = ""
return [PythonVenvStatus]::deactivated
}
if (!$venvDir) {
return [PythonVenvStatus]::inactive
}
if ($global:currentVenv) {
return [PythonVenvStatus]::active
}
$global:currentVenv = "$currentDir\$venvDir"
ActivatePython
return [PythonVenvStatus]::activated
}