Fix deactivate nushell (#3929)

Co-authored-by: Weng Wei <wuvist@gmail.com>
This commit is contained in:
Casper van Elteren 2025-05-16 12:15:35 +02:00 committed by GitHub
parent cc7d2d5ab3
commit 3c42ca9712
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 42 additions and 49 deletions

View File

@ -489,62 +489,55 @@ namespace mamba
// TODO the following shouldn't live here but in a shell hook
content << R"nu(def --env ")nu" << exe_name << R"nu( activate" [name: string] {)nu";
content << R"###(
#add condabin when base env
if $env.MAMBA_SHLVL? == null {
$env.MAMBA_SHLVL = 0
$env.PATH = ($env.PATH | prepend $"($env.MAMBA_ROOT_PREFIX)/condabin")
}
try {
let new_env = ^($env.MAMBA_EXE) shell activate --shell nu $name
# Process and load environment only if mamba command succeeded
$new_env
| lines
| str replace --regex '\s+' '' --all
| parse --regex '([^=]+)=(.+)'
| reduce -f {} { |it, acc|
$acc | merge {
$it.capture0: (
if ($it.capture0 == "PATH") or ($it.capture0 | str ends-with "_PATH") {
$it.capture1 | split row (if $nu.os-info.name == "Windows" { ";" } else { ":" }) | where { |path| $path != "" }
} else {
$it.capture1
}
)
}
} | load-env
# Set up prompt
$env.CONDA_PROMPT_MODIFIER = "(" + $name + ")"
if ($env.PROMPT_COMMAND_BK? == null) {
$env.PROMPT_COMMAND_BK = $env.PROMPT_COMMAND
}
$env.PROMPT_COMMAND = {|| $env.CONDA_PROMPT_MODIFIER + " " + (do $env.PROMPT_COMMAND_BK)}
} catch { | err |
echo $"Failed to activate ($name) environment: ($err.msg)"
}
})###" << "\n";
#add condabin when base env
if $env.MAMBA_SHLVL? == null {
$env.MAMBA_SHLVL = 0
$env.PATH = ($env.PATH | prepend $"($env.MAMBA_ROOT_PREFIX)/condabin")
}
#ask mamba how to setup the environment and set the environment
(^($env.MAMBA_EXE) shell activate --shell nu $name
| str replace --regex '\s+' '' --all
| split row ";"
| parse --regex '(.*)=(.+)'
| transpose --header-row
| into record
| load-env
)
$env.PATH = $env.PATH | split row (char esep)
# update prompt
if ($env.CONDA_PROMPT_MODIFIER? != null) {
$env.PROMPT_COMMAND = {|| $env.CONDA_PROMPT_MODIFIER + (do $env.PROMPT_COMMAND_BK)}
}
})###" << "\n";
content << R"nu(def --env ")nu" << exe_name << R"nu( deactivate" [] {)nu";
content << R"###(
#remove active environment except base env
for x in (^$env.MAMBA_EXE shell deactivate --shell nu
| split row (if $nu.os-info.name == "windows" { ";" } else { ":" })) {
if ("hide-env" in $x) {
hide-env ($x | parse "hide-env {var}").var.0
} else if $x != "" {
let keyValue = ($x
| str replace --regex '\s+' "" --all
| parse '{key}={value}'
)
load-env {$keyValue.0.key: $keyValue.0.value}
def --env "micromamba deactivate" [] {
for x in (^$env.MAMBA_EXE shell deactivate --shell nu | lines) {
if ("hide-env" in $x) {
hide-env (($x | parse "hide-env {var}").0.var)
} else if ($x =~ "=") {
let keyValue = ($x
| str replace --regex '\s+' "" --all
| parse '{key}={value}'
)
if ($keyValue | is-empty) == false {
let k = $keyValue.0.key
let v = $keyValue.0.value
# special-case PATH: convert to list
if $k == "PATH" {
let path_list = ($v | split row ":")
load-env { PATH: $path_list }
} else {
load-env { $k: $v }
}
}
}
}
}
# reset prompt
$env.PROMPT_COMMAND = $env.PROMPT_COMMAND_BK
}
}
)###" << "\n";
content << "# <<< mamba initialize <<<\n";
return content.str();