|
| 1 | +$script:THIS_PATH = $myinvocation.mycommand.path |
| 2 | +$script:BASE_DIR = Split-Path (Resolve-Path "$THIS_PATH/..") -Parent |
| 3 | + |
| 4 | +function global:deactivate([switch] $NonDestructive) { |
| 5 | + if (Test-Path variable:_OLD_VIRTUAL_PATH) { |
| 6 | + $env:PATH = $variable:_OLD_VIRTUAL_PATH |
| 7 | + Remove-Variable "_OLD_VIRTUAL_PATH" -Scope global |
| 8 | + } |
| 9 | + |
| 10 | + if (Test-Path function:_old_virtual_prompt) { |
| 11 | + $function:prompt = $function:_old_virtual_prompt |
| 12 | + Remove-Item function:\_old_virtual_prompt |
| 13 | + } |
| 14 | + |
| 15 | + if ($env:VIRTUAL_ENV) { |
| 16 | + Remove-Item env:VIRTUAL_ENV -ErrorAction SilentlyContinue |
| 17 | + } |
| 18 | + |
| 19 | + if ($env:VIRTUAL_ENV_PROMPT) { |
| 20 | + Remove-Item env:VIRTUAL_ENV_PROMPT -ErrorAction SilentlyContinue |
| 21 | + } |
| 22 | + |
| 23 | + if (!$NonDestructive) { |
| 24 | + # Self destruct! |
| 25 | + Remove-Item function:deactivate |
| 26 | + Remove-Item function:pydoc |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +function global:pydoc { |
| 31 | + python -m pydoc $args |
| 32 | +} |
| 33 | + |
| 34 | +# unset irrelevant variables |
| 35 | +deactivate -nondestructive |
| 36 | + |
| 37 | +$VIRTUAL_ENV = $BASE_DIR |
| 38 | +$env:VIRTUAL_ENV = $VIRTUAL_ENV |
| 39 | + |
| 40 | +if (__VIRTUAL_PROMPT__ -ne "") { |
| 41 | + $env:VIRTUAL_ENV_PROMPT = __VIRTUAL_PROMPT__ |
| 42 | +} |
| 43 | +else { |
| 44 | + $env:VIRTUAL_ENV_PROMPT = $( Split-Path $env:VIRTUAL_ENV -Leaf ) |
| 45 | +} |
| 46 | + |
| 47 | +New-Variable -Scope global -Name _OLD_VIRTUAL_PATH -Value $env:PATH |
| 48 | + |
| 49 | +$env:PATH = "$env:VIRTUAL_ENV/" + __BIN_NAME__ + __PATH_SEP__ + $env:PATH |
| 50 | +if (!$env:VIRTUAL_ENV_DISABLE_PROMPT) { |
| 51 | + function global:_old_virtual_prompt { |
| 52 | + "" |
| 53 | + } |
| 54 | + $function:_old_virtual_prompt = $function:prompt |
| 55 | + |
| 56 | + function global:prompt { |
| 57 | + # Add the custom prefix to the existing prompt |
| 58 | + $previous_prompt_value = & $function:_old_virtual_prompt |
| 59 | + ("(" + $env:VIRTUAL_ENV_PROMPT + ") " + $previous_prompt_value) |
| 60 | + } |
| 61 | +} |
0 commit comments