Activate.ps1 2.6 KB

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