|
1 | | -# virtualenv activation module |
2 | | -# Activate with `overlay use activate.nu` |
3 | | -# Deactivate with `deactivate`, as usual |
| 1 | +# virtualenv activation module: |
| 2 | +# - Activate with `overlay use activate.nu` |
| 3 | +# - Deactivate with `deactivate`, as usual |
4 | 4 | # |
5 | | -# To customize the overlay name, you can call `overlay use activate.nu as foo`, |
6 | | -# but then simply `deactivate` won't work because it is just an alias to hide |
7 | | -# the "activate" overlay. You'd need to call `overlay hide foo` manually. |
| 5 | +# To customize the overlay name, you can call `overlay use activate.nu as foo`, but then simply `deactivate` won't work |
| 6 | +# because it is just an alias to hide the "activate" overlay. You'd need to call `overlay hide foo` manually. |
8 | 7 |
|
9 | | -# Warn users who accidentally 'source' it |
10 | 8 | module warning { |
11 | | - export-env { |
12 | | - const file = path self |
13 | | - error make -u { |
14 | | - msg: $"`($file | path basename)` is meant to be used with `overlay use`, not `source`" |
15 | | - } |
16 | | - } |
| 9 | + export-env { |
| 10 | + const file = path self |
| 11 | + error make -u { |
| 12 | + msg: $"`($file | path basename)` is meant to be used with `overlay use`, not `source`" |
| 13 | + } |
| 14 | + } |
| 15 | + |
17 | 16 | } |
18 | 17 |
|
19 | 18 | use warning |
20 | 19 |
|
21 | 20 | export-env { |
| 21 | + |
| 22 | + let nu_ver = (version | get version | split row '.' | take 2 | each { into int }) |
| 23 | + if $nu_ver.0 == 0 and $nu_ver.1 < 106 { |
| 24 | + error make { |
| 25 | + msg: 'virtualenv Nushell activation requires Nushell 0.106 or greater.' |
| 26 | + } |
| 27 | + } |
| 28 | + |
22 | 29 | def is-string [x] { |
23 | 30 | ($x | describe) == 'string' |
24 | 31 | } |
25 | 32 |
|
26 | 33 | def has-env [...names] { |
27 | | - $names | each {|n| |
28 | | - $n in $env |
29 | | - } | all {|i| $i == true} |
| 34 | + $names | each {|n| $n in $env } | all {|i| $i } |
30 | 35 | } |
31 | 36 |
|
32 | | - # Emulates a `test -z`, but better as it handles e.g 'false' |
33 | 37 | def is-env-true [name: string] { |
34 | | - if (has-env $name) { |
35 | | - # Try to parse 'true', '0', '1', and fail if not convertible |
36 | | - let parsed = (do -i { $env | get $name | into bool }) |
37 | | - if ($parsed | describe) == 'bool' { |
38 | | - $parsed |
| 38 | + if (has-env $name) { |
| 39 | + let val = ($env | get --optional $name) |
| 40 | + if ($val | describe) == 'bool' { |
| 41 | + $val |
| 42 | + } else { |
| 43 | + not ($val | is-empty) |
| 44 | + } |
39 | 45 | } else { |
40 | | - not ($env | get -i $name | is-empty) |
| 46 | + false |
41 | 47 | } |
42 | | - } else { |
43 | | - false |
44 | | - } |
45 | 48 | } |
46 | 49 |
|
47 | 50 | let virtual_env = __VIRTUAL_ENV__ |
48 | 51 | let bin = __BIN_NAME__ |
49 | | - |
50 | | - let is_windows = ($nu.os-info.family) == 'windows' |
51 | | - let path_name = (if (has-env 'Path') { |
52 | | - 'Path' |
53 | | - } else { |
54 | | - 'PATH' |
55 | | - } |
56 | | - ) |
57 | | - |
| 52 | + let path_name = if (has-env 'Path') { 'Path' } else { 'PATH' } |
58 | 53 | let venv_path = ([$virtual_env $bin] | path join) |
59 | 54 | let new_path = ($env | get $path_name | prepend $venv_path) |
60 | | - |
61 | | - # If there is no default prompt, then use the env name instead |
62 | | - let virtual_env_prompt = (if (__VIRTUAL_PROMPT__ | is-empty) { |
| 55 | + let virtual_env_prompt = if (__VIRTUAL_PROMPT__ | is-empty) { |
63 | 56 | ($virtual_env | path basename) |
64 | 57 | } else { |
65 | 58 | __VIRTUAL_PROMPT__ |
66 | | - }) |
67 | | - |
68 | | - let new_env = { |
69 | | - $path_name : $new_path |
70 | | - VIRTUAL_ENV : $virtual_env |
71 | | - VIRTUAL_ENV_PROMPT : $virtual_env_prompt |
72 | 59 | } |
73 | | - |
74 | | - let new_env = (if (is-env-true 'VIRTUAL_ENV_DISABLE_PROMPT') { |
75 | | - $new_env |
| 60 | + let new_env = { $path_name: $new_path VIRTUAL_ENV: $virtual_env VIRTUAL_ENV_PROMPT: $virtual_env_prompt } |
| 61 | + let old_prompt_command = if (has-env 'PROMPT_COMMAND') { $env.PROMPT_COMMAND } else { '' } |
| 62 | + let new_env = if (is-env-true 'VIRTUAL_ENV_DISABLE_PROMPT') { |
| 63 | + $new_env |
76 | 64 | } else { |
77 | | - # Creating the new prompt for the session |
78 | | - let virtual_prefix = $'(char lparen)($virtual_env_prompt)(char rparen) ' |
79 | | - |
80 | | - # Back up the old prompt builder |
81 | | - let old_prompt_command = (if (has-env 'PROMPT_COMMAND') { |
82 | | - $env.PROMPT_COMMAND |
83 | | - } else { |
84 | | - '' |
85 | | - }) |
86 | | - |
87 | | - let new_prompt = (if (has-env 'PROMPT_COMMAND') { |
88 | | - if 'closure' in ($old_prompt_command | describe) { |
89 | | - {|| $'($virtual_prefix)(do $old_prompt_command)' } |
90 | | - } else { |
91 | | - {|| $'($virtual_prefix)($old_prompt_command)' } |
92 | | - } |
93 | | - } else { |
94 | | - {|| $'($virtual_prefix)' } |
95 | | - }) |
96 | | - |
97 | | - $new_env | merge { |
98 | | - PROMPT_COMMAND : $new_prompt |
99 | | - VIRTUAL_PREFIX : $virtual_prefix |
100 | | - } |
101 | | - }) |
102 | | - |
103 | | - # Environment variables that will be loaded as the virtual env |
| 65 | + let virtual_prefix = $'(char lparen)($virtual_env_prompt)(char rparen) ' |
| 66 | + let new_prompt = if (has-env 'PROMPT_COMMAND') { |
| 67 | + if ('closure' in ($old_prompt_command | describe)) { |
| 68 | + {|| $'($virtual_prefix)(do $old_prompt_command)' } |
| 69 | + } else { |
| 70 | + {|| $'($virtual_prefix)($old_prompt_command)' } |
| 71 | + } |
| 72 | + } else { |
| 73 | + {|| $'($virtual_prefix)' } |
| 74 | + } |
| 75 | + $new_env | merge { PROMPT_COMMAND: $new_prompt VIRTUAL_PREFIX: $virtual_prefix } |
| 76 | + } |
104 | 77 | load-env $new_env |
105 | 78 | } |
106 | 79 |
|
|
0 commit comments