Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port before v0.60/virtual_environments #849

Merged
merged 5 commits into from
May 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Prev Previous commit
Next Next commit
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`
  • Loading branch information
39555 committed May 26, 2024
commit 735ea44ce732a91ccdda45ca8935a9032b13fb59
45 changes: 15 additions & 30 deletions modules/virtual_environments/venv/venv.nu
Original file line number Diff line number Diff line change
@@ -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)
}
}
7 changes: 3 additions & 4 deletions modules/virtual_environments/venv/venv_deactivate.nu
Original file line number Diff line number Diff line change
@@ -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