From 60c69ff6d21ce48f4d16119afd996e8076a34861 Mon Sep 17 00:00:00 2001 From: Igor Date: Mon, 13 May 2024 15:01:19 +0400 Subject: [PATCH 1/5] move `virtual_environments` `venv.nu` and `venv_deactivate.nu` to `modules/virtual_enviromnemts/venv.nu` `venv_deactivate.nu` --- {before_v0.60 => modules}/virtual_environments/venv.nu | 0 {before_v0.60 => modules}/virtual_environments/venv_deactivate.nu | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename {before_v0.60 => modules}/virtual_environments/venv.nu (100%) rename {before_v0.60 => modules}/virtual_environments/venv_deactivate.nu (100%) diff --git a/before_v0.60/virtual_environments/venv.nu b/modules/virtual_environments/venv.nu similarity index 100% rename from before_v0.60/virtual_environments/venv.nu rename to modules/virtual_environments/venv.nu diff --git a/before_v0.60/virtual_environments/venv_deactivate.nu b/modules/virtual_environments/venv_deactivate.nu similarity index 100% rename from before_v0.60/virtual_environments/venv_deactivate.nu rename to modules/virtual_environments/venv_deactivate.nu From fafb0133c4f8b98732f8de89ced282c29c5f041e Mon Sep 17 00:00:00 2001 From: Igor Date: Mon, 13 May 2024 15:01:31 +0400 Subject: [PATCH 2/5] remove `virtual_enviromnents` `/conda.nu` and `/README.md` - The `conda.nu` script has already been ported to `modules/virtual_environments/conda/*` - remove README.md because the information is already in `modules/virtual_environments/README.md` --- before_v0.60/virtual_environments/README.md | 53 ------------------- before_v0.60/virtual_environments/conda.nu | 45 ---------------- .../virtual_environments/conda_deactivate.nu | 7 --- 3 files changed, 105 deletions(-) delete mode 100644 before_v0.60/virtual_environments/README.md delete mode 100644 before_v0.60/virtual_environments/conda.nu delete mode 100644 before_v0.60/virtual_environments/conda_deactivate.nu diff --git a/before_v0.60/virtual_environments/README.md b/before_v0.60/virtual_environments/README.md deleted file mode 100644 index c578d4c4a..000000000 --- a/before_v0.60/virtual_environments/README.md +++ /dev/null @@ -1,53 +0,0 @@ -# Virtual environment scripts - -The scripts in this directory activate virtual environments like Python venvs -and Conda environments. They follow the pattern described in the [Nushell 0.32 -changelog](https://www.nushell.sh/blog/2021-06-01-nushell_0_32.html#environment-loading-lily-mara): - -``` -$ load-env (activate some-env) -``` - -A custom command (`activate`) creates a table with environment variables and -`load-env` is used to load it into the shell's scope. - -In most cases, deactivation is a matter of restoring the PATH variable to the -state before activating the env and removing any additional variables. There are -no environment-specific elements to this, which is why the same deactivation -script can be used for deactivating any environment of a specific type. For -example, `source conda_deactivate.nu` will deactivate any Conda env, there are -no input parameters. - -## Expected Usage - -1. Source an activation script in your `config.toml`. For example, `conda.nu`. - You'll then have the `conda-env` command available. -2. Create an alias for sourcing the appropriate deactivation script: - `alias conda-deactivate = source /path/to/conda_deactivate.nu` -3. Activate with `load-env (conda-env env-name)`. You might want to define some - shorter aliases for both commands, if typing that every time seems like a - hassle. -4. If you're using [Starship](https://starship.rs/), your prompt should reflect the activated env. -5. When done, deactivate with your alias: `conda-deactivate`. - -Look at the script files to find the exact command name for creating an environment table. - -## `conda.nu` - -Limitations: - -- The "root_prefix" might not actually correspond to the correct path to the Conda envs. You can fix - this for your setup by changing how the root prefix is found in the `conda-env` command. -- Nested envs are not well supported. If you activate a Conda env while another one is - activated, new elements will be appended to the PATH, but the other environment - variables will be overwritten. There's no way to then restore the PATH to the state - it was in before activating the *first* env (at least not with this script directly). -- The prompt is not updated by the script. Consider using [Starship](https://starship.rs/) - with the Python prompt element. - -## `venv.nu` - -Limitations: - -- The prompt is not updated by the script. Consider using [Starship](https://starship.rs/) - with the Python prompt element. \ No newline at end of file diff --git a/before_v0.60/virtual_environments/conda.nu b/before_v0.60/virtual_environments/conda.nu deleted file mode 100644 index 7e8740231..000000000 --- a/before_v0.60/virtual_environments/conda.nu +++ /dev/null @@ -1,45 +0,0 @@ -def conda-env [env-name] { - let conda-info = (conda info --envs --json | from json) - let suffix = (if $env-name == "base" {""} {(["envs" $env-name] | path join)}) - let env-dir = ([$conda-info.root_prefix $suffix] | path join) - let old-path = ($nu.path | str collect (path-sep)) - let new-path = (if (windows?) { (conda-create-path-windows $env-dir) } { (conda-create-path-unix $env-dir) }) - let new-env = [[name, value]; - [CONDA_DEFAULT_ENV $env-name] - [CONDA_PREFIX $env-dir] - [CONDA_PROMPT_MODIFIER $"[($env-name)]"] - [CONDA_SHLVL "1"] - [CONDA_OLD_PATH $old-path]] - - $new-env | append $new-path -} - -def conda-create-path-windows [env-dir] { - # 1. Conda on Windows needs a few additional Path elements - # 2. The path env var on Windows is called Path (not PATH) - let env-path = [ - $env-dir - ([$env-dir "Scripts"] | path join) - ([$env-dir "Library" "mingw-w64"] | path join) - ([$env-dir "Library" "bin"] | path join) - ([$env-dir "Library" "usr" "bin"] | path join) - ] - let new-path = ([$env-path $nu.path] | flatten | str collect (path-sep)) - [[name, value]; [Path $new-path]] -} - -def conda-create-path-unix [env-dir] { - let env-path = [ - ([$env-dir "bin"] | path join) - ] - let new-path = ([$env-path $nu.path] | flatten | str collect (path-sep)) - [[name, value]; [PATH $new-path]] -} - -def windows? [] { - (sys).host.name == "Windows" -} - -def path-sep [] { - if (windows?) { ";" } { ":" } -} diff --git a/before_v0.60/virtual_environments/conda_deactivate.nu b/before_v0.60/virtual_environments/conda_deactivate.nu deleted file mode 100644 index 69f3daeb4..000000000 --- a/before_v0.60/virtual_environments/conda_deactivate.nu +++ /dev/null @@ -1,7 +0,0 @@ -let path-name = (if ((sys).host.name == "Windows") { "Path" } { "PATH" }) -let-env $path-name = $nu.env.CONDA_OLD_PATH -unlet-env CONDA_PROMPT_MODIFIER -unlet-env CONDA_PREFIX -unlet-env CONDA_SHLVL -unlet-env CONDA_DEFAULT_ENV -unlet-env CONDA_OLD_PATH From 5c7191da60a96e99612b333df5accf8bc7c9be7e Mon Sep 17 00:00:00 2001 From: Igor Date: Mon, 13 May 2024 15:01:56 +0400 Subject: [PATCH 3/5] move `venv.nu` and `venv_deactivate.nu` to the `venv` subfolder similar to `conda` and `msvc` --- modules/virtual_environments/{ => venv}/venv.nu | 0 modules/virtual_environments/{ => venv}/venv_deactivate.nu | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename modules/virtual_environments/{ => venv}/venv.nu (100%) rename modules/virtual_environments/{ => venv}/venv_deactivate.nu (100%) diff --git a/modules/virtual_environments/venv.nu b/modules/virtual_environments/venv/venv.nu similarity index 100% rename from modules/virtual_environments/venv.nu rename to modules/virtual_environments/venv/venv.nu diff --git a/modules/virtual_environments/venv_deactivate.nu b/modules/virtual_environments/venv/venv_deactivate.nu similarity index 100% rename from modules/virtual_environments/venv_deactivate.nu rename to modules/virtual_environments/venv/venv_deactivate.nu From 735ea44ce732a91ccdda45ca8935a9032b13fb59 Mon Sep 17 00:00:00 2001 From: Igor Date: Mon, 13 May 2024 15:03:47 +0400 Subject: [PATCH 4/5] port `venv.nu` and `venv_deactivate.nu` - copy the already ported activation script from `conda.nu` - add `load-env` and `def --env` - remove duplication for PATH/Path because nu now supports case insensitive access - remove $nu.path use $env.PATH - remove PATH serialization to string/from string - use a record instead of a table for `load-env` --- modules/virtual_environments/venv/venv.nu | 45 +++++++------------ .../venv/venv_deactivate.nu | 7 ++- 2 files changed, 18 insertions(+), 34 deletions(-) diff --git a/modules/virtual_environments/venv/venv.nu b/modules/virtual_environments/venv/venv.nu index ba1ab81ff..0428f0d5b 100644 --- a/modules/virtual_environments/venv/venv.nu +++ b/modules/virtual_environments/venv/venv.nu @@ -1,33 +1,18 @@ -def venv [venv-dir] { - let venv-abs-dir = ($venv-dir | path expand) - let venv-name = ($venv-abs-dir | path basename) - let old-path = ($nu.path | str collect (path-sep)) - let new-path = (if (windows?) { (venv-path-windows $venv-abs-dir) } { (venv-path-unix $venv-abs-dir) }) - let new-env = [[name, value]; - [VENV_OLD_PATH $old-path] - [VIRTUAL_ENV $venv-name]] - - $new-env | append $new-path -} - -def venv-path-unix [venv-dir] { - let venv-path = ([$venv-dir "bin"] | path join) - let new-path = ($nu.path | prepend $venv-path | str collect (path-sep)) - [[name, value]; [PATH $new-path]] -} - -def venv-path-windows [venv-dir] { - # 1. Conda on Windows needs a few additional Path elements - # 2. The path env var on Windows is called Path (not PATH) - let venv-path = ([$venv-dir "Scripts"] | path join) - let new-path = ($nu.path | prepend $venv-path | str collect (path-sep)) - [[name, value]; [Path $new-path]] -} - -def windows? [] { - (sys).host.name == "Windows" +def --env venv [venv_dir] { + let venv_abs_dir = ($venv_dir | path expand) + let venv_name = ($venv_abs_dir | path basename) + let old_path = $env.PATH + let new_path = (venv-path $venv_abs_dir) + let new_env = ({VENV_OLD_PATH: $old_path, VIRTUAL_ENV: $venv_name} | merge $new_path) + load-env $new_env } -def path-sep [] { - if (windows?) { ";" } { ":" } +def "venv-path" [venv_dir] { + let env_path = [ + $venv_dir, + ([$venv_dir, "bin"] | path join) + ] + return { + PATH: ($env.PATH | prepend $env_path) + } } diff --git a/modules/virtual_environments/venv/venv_deactivate.nu b/modules/virtual_environments/venv/venv_deactivate.nu index 95cdd1157..6f7cfa0b4 100644 --- a/modules/virtual_environments/venv/venv_deactivate.nu +++ b/modules/virtual_environments/venv/venv_deactivate.nu @@ -1,4 +1,3 @@ -let path-name = (if ((sys).host.name == "Windows") { "Path" } { "PATH" }) -let-env $path-name = $nu.env.VENV_OLD_PATH -unlet-env VIRTUAL_ENV -unlet-env VENV_OLD_PATH +$env.PATH = $env.VENV_OLD_PATH +hide-env VIRTUAL_ENV +hide-env VENV_OLD_PATH From c987a3bc9fa510ac7426bcd075cf03ed8ebd1462 Mon Sep 17 00:00:00 2001 From: Igor Date: Sun, 26 May 2024 18:21:23 +0400 Subject: [PATCH 5/5] merge `venv_deactivate.nu` with `venv`, move `venv` from `venv/` make similar to `conda.nu` - add `export` keyword --- modules/virtual_environments/{venv => }/venv.nu | 9 ++++++++- modules/virtual_environments/venv/venv_deactivate.nu | 3 --- 2 files changed, 8 insertions(+), 4 deletions(-) rename modules/virtual_environments/{venv => }/venv.nu (74%) delete mode 100644 modules/virtual_environments/venv/venv_deactivate.nu diff --git a/modules/virtual_environments/venv/venv.nu b/modules/virtual_environments/venv.nu similarity index 74% rename from modules/virtual_environments/venv/venv.nu rename to modules/virtual_environments/venv.nu index 0428f0d5b..f70a49716 100644 --- a/modules/virtual_environments/venv/venv.nu +++ b/modules/virtual_environments/venv.nu @@ -1,4 +1,4 @@ -def --env venv [venv_dir] { +export def --env activate [venv_dir] { let venv_abs_dir = ($venv_dir | path expand) let venv_name = ($venv_abs_dir | path basename) let old_path = $env.PATH @@ -16,3 +16,10 @@ def "venv-path" [venv_dir] { PATH: ($env.PATH | prepend $env_path) } } + +export def --env deactivate [] { + $env.PATH = $env.VENV_OLD_PATH + hide-env VIRTUAL_ENV + hide-env VENV_OLD_PATH + +} diff --git a/modules/virtual_environments/venv/venv_deactivate.nu b/modules/virtual_environments/venv/venv_deactivate.nu deleted file mode 100644 index 6f7cfa0b4..000000000 --- a/modules/virtual_environments/venv/venv_deactivate.nu +++ /dev/null @@ -1,3 +0,0 @@ -$env.PATH = $env.VENV_OLD_PATH -hide-env VIRTUAL_ENV -hide-env VENV_OLD_PATH