Skip to content

Commit 17a4eaa

Browse files
committed
Fix nushell deprecation warnings
Signed-off-by: Bernát Gábor <[email protected]>
1 parent 3e1786e commit 17a4eaa

File tree

3 files changed

+52
-75
lines changed

3 files changed

+52
-75
lines changed

.github/workflows/check.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ jobs:
7979
echo "deb https://apt.fury.io/nushell/ /" | sudo tee /etc/apt/sources.list.d/fury.list
8080
sudo apt-get update -y
8181
sudo apt-get install snapd fish csh nushell -y
82-
elif [ "${{ runner.os }}" = "macOS" ]; then
82+
elif [ "${{ runner.os }}" = "macOS" ]; then
83+
brew update
8384
if [[ "${{ matrix.py }}" == brew@* ]]; then
8485
PY=$(echo '${{ matrix.py }}' | cut -c 6-)
8586
brew install python@$PY || brew upgrade python@$PY

docs/changelog/2910.bugfix.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Resolve Nushell activation script deprecation warnings by dynamically selecting the ``--optional`` flag for Nushell
2+
``get`` command on version 0.106.0 and newer, while retaining the deprecated ``-i`` flag for older versions to maintain
3+
compatibility. Contributed by :user:`gaborbernat`.

src/virtualenv/activation/nushell/activate.nu

Lines changed: 47 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,106 +1,79 @@
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
44
#
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.
87

9-
# Warn users who accidentally 'source' it
108
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+
1716
}
1817

1918
use warning
2019

2120
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+
2229
def is-string [x] {
2330
($x | describe) == 'string'
2431
}
2532

2633
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 }
3035
}
3136

32-
# Emulates a `test -z`, but better as it handles e.g 'false'
3337
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+
}
3945
} else {
40-
not ($env | get -i $name | is-empty)
46+
false
4147
}
42-
} else {
43-
false
44-
}
4548
}
4649

4750
let virtual_env = __VIRTUAL_ENV__
4851
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' }
5853
let venv_path = ([$virtual_env $bin] | path join)
5954
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) {
6356
($virtual_env | path basename)
6457
} else {
6558
__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
7259
}
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
7664
} 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+
}
10477
load-env $new_env
10578
}
10679

0 commit comments

Comments
 (0)