1
Fork 0
mirror of https://github.com/RGBCube/nu_scripts synced 2025-08-01 22:57:46 +00:00
nu_scripts/modules/virtual_environments/nu_conda/nu_conda.nu
Elizabeth eedcd10dbb
Fixed sys usage (#916)
Just as a heads up, I haven't really tested this since a lot of it is
stuff I don't use or know how to set up without some reading up. I have
tested the nu_conda_2 change since I have a python project that I use
that for, and I could look into testing more of it if needed.

I've tried finding (naively using `/sys\W/`) all the usage of the old
plain `sys` calls and replacing them with alternates as appropriate,
which mostly has been to swap a `(sys).host.name` call into a
`$nu.os-info.name` one, since it'll be tad faster and more consistent
across platforms with naming (especially as the value comes from the
[rust stdlib](https://doc.rust-lang.org/std/env/consts/constant.OS.html)
and is very predictable).

Fixes #897
2024-07-24 09:09:31 -05:00

91 lines
2.1 KiB
Text

export-env {
if not ("CONDA_CURR" in $env) {
$env.CONDA_BASE_PATH = (if $nu.os-info.name == 'windows' {$env.Path} else {$env.PATH})
let info = (
if not (which mamba | is-empty) {
(mamba info --envs --json | from json)
} else if not (which conda | is-empty) {
(conda info --envs --json | from json)
} else {
('{"root_prefix": "", "envs": ""}' | from json)
})
$env.CONDA_ROOT = $info.root_prefix
$env.CONDA_ENVS = ($info.envs | reduce -f {} {|it, acc|
if $it == $info.root_prefix {
$acc | upsert "base" $it
} else {
$acc | upsert ($it | path basename) $it
}})
$env.CONDA_CURR = null
}
}
export def --env activate [name: string] {
if ($env.CONDA_ROOT | is-empty) {
print "Neither Conda nor Mamba is available."
return
}
if not ($name in $env.CONDA_ENVS) {
print $"Environment ($name) is invalid. Available:"
print $env.CONDA_ENVS
return
}
let new_path = (
if $nu.os-info.name == 'windows' {
update-path-windows ($env.CONDA_ENVS | get $name)
} else {
update-path-linux ($env.CONDA_ENVS | get $name)
})
load-env ({CONDA_CURR: $name} | merge $new_path)
}
export def --env deactivate [] {
if ($env.CONDA_ROOT | is-empty) {
print "Neither Conda nor Mamba is available."
return
}
$env.CONDA_CURR = null
load-env {Path: $env.CONDA_BASE_PATH, PATH: $env.CONDA_BASE_PATH}
}
export def --env list [] {
$env.CONDA_ENVS |
flatten |
transpose |
rename name path |
insert active { |it| $it.name == $env.CONDA_CURR } |
move path --after active
}
def update-path-linux [env_path: path] {
let env_path = [
$env_path,
([$env_path, "bin"] | path join)
]
return {
Path: ($env.PATH | prepend $env_path),
PATH: ($env.PATH | prepend $env_path)
}
}
def update-path-windows [env_path: path] {
let env_path = [
$env_path,
([$env_path, "Scripts"] | path join),
]
return {
Path: ($env.Path | prepend $env_path),
PATH: ($env.Path | prepend $env_path)
}
}