mirror of
https://github.com/RGBCube/nu_scripts
synced 2025-08-01 22:57:46 +00:00
Speeding up activating conda environment (#442)
* Speeding up activating conda environment That is mostly done by caching the expensive `conda info --envs --json` call * Formatting
This commit is contained in:
parent
ebdab3dea3
commit
c6b3ecf21c
1 changed files with 18 additions and 14 deletions
|
@ -4,22 +4,21 @@ export def-env activate [
|
|||
] {
|
||||
let conda_info = (conda info --envs --json | from json)
|
||||
|
||||
mut $env_name = $env_name
|
||||
if $env_name == null {$env_name = "base"}
|
||||
mut env_dir = ""
|
||||
mut env_dirs = []
|
||||
for i in $conda_info.envs_dirs {
|
||||
$env_dirs = ($env_dirs | append ($i | path join $env_name))
|
||||
let env_name = if $env_name == null {
|
||||
"base"
|
||||
} else {
|
||||
$env_name
|
||||
}
|
||||
if $env_name != "base" {
|
||||
|
||||
let env_dir = if $env_name != "base" {
|
||||
if ($env_name | path exists) and (($env_name | path expand) in $conda_info.envs ) {
|
||||
$env_dir = ($env_name | path expand)
|
||||
($env_name | path expand)
|
||||
} else {
|
||||
$env_dir = ((check-if-env-exists $env_name $env_dirs) | into string)
|
||||
((check-if-env-exists $env_name $conda_info) | into string)
|
||||
}
|
||||
} else {
|
||||
$env_dir = $conda_info.root_prefix
|
||||
}
|
||||
$conda_info.root_prefix
|
||||
}
|
||||
|
||||
let old_path = (system-path | str join (char esep))
|
||||
|
||||
|
@ -93,11 +92,16 @@ export def-env deactivate [] {
|
|||
hide-env CONDA_OLD_PROMPT_COMMAND
|
||||
}
|
||||
|
||||
def check-if-env-exists [ env_name: string, env_dir: list ] {
|
||||
let en = ($env_dir | each {|en| ( conda info --envs --json | from json | get envs ) | where $it == $en } | where ($it | length) == 1 | flatten)
|
||||
def check-if-env-exists [ env_name: string, conda_info: record ] {
|
||||
let env_dirs = (
|
||||
$conda_info.envs_dirs |
|
||||
each { path join $env_name }
|
||||
)
|
||||
|
||||
let en = ($env_dirs | each {|en| $conda_info.envs | where $it == $en } | where ($it | length) == 1 | flatten)
|
||||
if ($en | length) > 1 {
|
||||
error make --unspanned {msg: $"You have enviroments in multiple locations: ($en)"}
|
||||
}
|
||||
}
|
||||
if ($en | length) == 0 {
|
||||
error make --unspanned {msg: $"Could not find given environment: ($env_name)"}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue