Skip to content

Commit ee018a3

Browse files
authored
Update Nushell activation scripts to 0.60 (#2321)
1 parent 63c94af commit ee018a3

3 files changed

Lines changed: 121 additions & 49 deletions

File tree

Lines changed: 90 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,92 @@
1-
# Setting all environment variables for the venv
2-
let path-name = (if ((sys).host.name == "Windows") { "Path" } { "PATH" })
3-
let virtual-env = "__VIRTUAL_ENV__"
4-
let bin = "__BIN_NAME__"
5-
let path-sep = "__PATH_SEP__"
6-
7-
let old-path = ($nu.path | str collect ($path-sep))
8-
9-
let venv-path = ([$virtual-env $bin] | path join)
10-
let new-path = ($nu.path | prepend $venv-path | str collect ($path-sep))
11-
12-
# environment variables that will be batched loaded to the virtual env
13-
let new-env = ([
14-
[name, value];
15-
[$path-name $new-path]
16-
[_OLD_VIRTUAL_PATH $old-path]
17-
[VIRTUAL_ENV $virtual-env]
18-
])
19-
20-
load-env $new-env
21-
22-
# Creating the new prompt for the session
23-
let virtual_prompt = (if ("__VIRTUAL_PROMPT__" != "") {
24-
"(__VIRTUAL_PROMPT__) "
25-
} {
26-
(build-string '(' ($virtual-env | path basename) ') ')
1+
# This command prepares the required environment variables
2+
def-env activate-virtualenv [] {
3+
def is-string [x] {
4+
($x | describe) == 'string'
5+
}
6+
7+
def has-env [name: string] {
8+
$name in (env).name
9+
}
10+
11+
let is-windows = ((sys).host.name | str downcase) == 'windows'
12+
let virtual-env = '__VIRTUAL_ENV__'
13+
let bin = '__BIN_NAME__'
14+
let path-sep = '__PATH_SEP__'
15+
let path-name = if $is-windows {
16+
if (has-env 'Path') {
17+
'Path'
18+
} else {
19+
'PATH'
20+
}
21+
} else {
22+
'PATH'
23+
}
24+
25+
let old-path = (
26+
if $is-windows {
27+
if (has-env 'Path') {
28+
$env.Path
29+
} else {
30+
$env.PATH
31+
}
32+
} else {
33+
$env.PATH
34+
} | if (is-string $in) {
35+
# if Path/PATH is a string, make it a list
36+
$in | split row $path-sep | path expand
37+
} else {
38+
$in
39+
}
40+
)
41+
42+
let venv-path = ([$virtual-env $bin] | path join)
43+
let new-path = ($old-path | prepend $venv-path | str collect $path-sep)
44+
45+
# Creating the new prompt for the session
46+
let virtual-prompt = if ('__VIRTUAL_PROMPT__' == '') {
47+
$'(char lparen)($virtual-env | path basename)(char rparen) '
48+
} else {
49+
'(__VIRTUAL_PROMPT__) '
50+
}
51+
52+
# Back up the old prompt builder
53+
let old-prompt-command = if (has-env 'VIRTUAL_ENV') && (has-env '_OLD_PROMPT_COMMAND') {
54+
$env._OLD_PROMPT_COMMAND
55+
} else {
56+
if (has-env 'PROMPT_COMMAND') {
57+
$env.PROMPT_COMMAND
58+
} else {
59+
''
60+
}
61+
}
62+
63+
# If there is no default prompt, then only the env is printed in the prompt
64+
let new-prompt = if (has-env 'PROMPT_COMMAND') {
65+
if ($old-prompt-command | describe) == 'block' {
66+
{ $'($virtual-prompt)(do $old-prompt-command)' }
67+
} else {
68+
{ $'($virtual-prompt)($old-prompt-command)' }
69+
}
70+
} else {
71+
{ $'($virtual-prompt)' }
72+
}
73+
74+
# Environment variables that will be batched loaded to the virtual env
75+
let new-env = {
76+
$path-name : $new-path
77+
VIRTUAL_ENV : $virtual-env
78+
_OLD_VIRTUAL_PATH : ($old-path | str collect $path-sep)
79+
_OLD_PROMPT_COMMAND : $old-prompt-command
80+
PROMPT_COMMAND : $new-prompt
81+
VIRTUAL_PROMPT : $virtual-prompt
82+
}
83+
84+
# Activate the environment variables
85+
load-env $new-env
2786
}
28-
)
29-
30-
# If there is no default prompt, then only the env is printed in the prompt
31-
let new_prompt = (if ( config | select prompt | empty? ) {
32-
($"build-string '($virtual_prompt)'")
33-
} {
34-
($"build-string '($virtual_prompt)' (config get prompt | str find-replace "build-string" "")")
35-
})
36-
let-env PROMPT_COMMAND = $new_prompt
37-
38-
# We are using alias as the function definitions because only aliases can be
39-
# removed from the scope
87+
88+
# Activate the virtualenv
89+
activate-virtualenv
90+
4091
alias pydoc = python -m pydoc
41-
alias deactivate = source "__DEACTIVATE_PATH__"
92+
alias deactivate = source '__DEACTIVATE_PATH__'
Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,32 @@
1-
# Setting the old path
2-
let path-name = (if ((sys).host.name == "Windows") { "Path" } { "PATH" })
3-
let-env $path-name = $nu.env._OLD_VIRTUAL_PATH
1+
def-env deactivate-virtualenv [] {
2+
def has-env [name: string] {
3+
$name in (env).name
4+
}
45

5-
# Unleting the environment variables that were created when activating the env
6-
unlet-env VIRTUAL_ENV
7-
unlet-env _OLD_VIRTUAL_PATH
8-
unlet-env PROMPT_COMMAND
6+
let is-windows = ((sys).host.name | str downcase) == 'windows'
97

10-
unalias pydoc
11-
unalias deactivate
8+
let path-name = if $is-windows {
9+
if (has-env 'Path') {
10+
'Path'
11+
} else {
12+
'PATH'
13+
}
14+
} else {
15+
'PATH'
16+
}
17+
18+
load-env { $path-name : $env._OLD_VIRTUAL_PATH }
19+
20+
let-env PROMPT_COMMAND = $env._OLD_PROMPT_COMMAND
21+
22+
# Hiding the environment variables that were created when activating the env
23+
hide _OLD_VIRTUAL_PATH
24+
hide _OLD_PROMPT_COMMAND
25+
hide VIRTUAL_ENV
26+
hide VIRTUAL_PROMPT
27+
}
28+
29+
deactivate-virtualenv
30+
31+
hide pydoc
32+
hide deactivate

tests/unit/activation/test_nushell.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ def __init__(self, session):
2424
self.unix_line_ending = not IS_WIN
2525

2626
def print_prompt(self):
27-
return r"echo $virtual_prompt; printf '\n'"
27+
return r"$env.VIRTUAL_PROMPT"
2828

2929
activation_tester(Nushell)

0 commit comments

Comments
 (0)