Skip to content

Commit

Permalink
auto-venv for python: update to be compatible with nu 0.80 (#513)
Browse files Browse the repository at this point in the history
* auto-venv python passes syntax checks

* update readme.md

* everything works!
  • Loading branch information
hyiltiz committed May 26, 2023
1 parent 4b82839 commit 272b7e2
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 26 deletions.
13 changes: 7 additions & 6 deletions modules/virtual_environments/auto-venv/README.md
Expand Up @@ -4,21 +4,22 @@ The scripts in this directory activate virtual environments whenever you cd into

## Usage

1. set `$env.AUTO_VENV_TRIGGER` to the name of a script file
1. set `$env.AUTO_VENV_TRIGGER` as the preferred name of a trigger file
1. import `auto-venv` into your environment
1. cd into a folder that contains your trigger file
1. Create a symlink via `ln -s` of the script file `./auto-venv/venvs/python-venv.nu` to a trigger file in project at `/path/to/project/$AUTO_VENV_TRIGGER`
1. `cd` into `/path/to/project/`

ex.
For example:
```nu
# config.nu
export-env {
let-env AUTO_VENV_TRIGGER = '__auto-venv.nu'
source-env ~/path/to/nu_scripts/virtual_environments/auto-venv/auto-venv.nu
source-env ~/path/to/nu_scripts/modules/virtual_environments/auto-venv/auto-venv.nu
}
use ~/path/to/nu_scripts/virtual_environments/auto-venv/auto-venv.nu *
use ~/path/to/nu_scripts/modules/virtual_environments/auto-venv/auto-venv.nu *
```

When `auto-venv` detects that a file of that name exists in the folder you cd'ed into (or one of it's parents), it will automatically enable an overlay (as defined by the trigger file).
Expand All @@ -30,4 +31,4 @@ NOTE: the trigger file *must* export a custom command called `auto-venv-on-enter

## Limitations

- Due to limitations with overlay naming, you cannot nest auto-venv triggers, even for separate languages / toolchains
- Due to limitations with overlay naming, you cannot nest auto-venv triggers, even for separate languages / toolchains
6 changes: 3 additions & 3 deletions modules/virtual_environments/auto-venv/auto-venv.nu
Expand Up @@ -10,7 +10,7 @@ export-env {
# this needs to be set somewhere
# let-env AUTO_VENV_TRIGGER = '__auto-venv.nu'

let hooks = default-hooks
let hooks = (default-hooks)

let hooks = ($hooks | append (build-hooks))

Expand All @@ -20,7 +20,7 @@ export-env {

def default-hooks [] {
(if ($env.config.hooks.env_change.PWD != $nothing) {
$env.config.hooks.env_change.PWD
[$env.config.hooks.env_change.PWD]
}
else {
[]
Expand All @@ -35,7 +35,7 @@ def build-hooks [] {
let _env = $env
let pwd = $_env.PWD
let trigger = path_extensions path find-sub . __trigger__ --type "file"
let trigger = (path_extensions path find-sub . __trigger__ --type ["symlink", "file"])
cd ($trigger | path dirname)
overlay use __trigger__ as __auto_venv
Expand Down
14 changes: 7 additions & 7 deletions modules/virtual_environments/auto-venv/path_extensions.nu
Expand Up @@ -14,7 +14,7 @@ export def "path walk" [
] {
let list = ($path | path expand | path split);

$list | each -n { |$part| (
$list | enumerate | each { |$part| (
$list | first ($part.index + 1) | path join;
)}

Expand All @@ -24,12 +24,12 @@ export def "path walk" [
export def "path check-sub" [
folder: any,
subfolder: string,
--type: string
--type: list
] {

(ls -a $folder
| where (
($type == $nothing or $it.type == $type)
($type == $nothing or $it.type in $type)
and ($it.name | path basename) == $subfolder
)
| length
Expand All @@ -43,9 +43,9 @@ export def "path check-sub" [
export def "path find-sub" [
folder: any,
subfolder: string,
--type: string
--type: list
] {
let paths = path walk $folder;
let paths = (path walk $folder);

let paths = ( $paths
| where (
Expand All @@ -55,5 +55,5 @@ export def "path find-sub" [

if ($paths != $nothing) and ($paths | length) > 0 {
[ ($paths | first), $subfolder ] | path join
}
}
} else {[]}
}
10 changes: 7 additions & 3 deletions modules/virtual_environments/auto-venv/venv_helpers.nu
Expand Up @@ -28,7 +28,7 @@ export def has-entered-venv [
after: path,
] {

let target = path find-sub $after $env.AUTO_VENV_TRIGGER
let target = (path find-sub $after $env.AUTO_VENV_TRIGGER)

(if ($target | is-empty) {
false
Expand All @@ -47,13 +47,17 @@ export def has-swapped-venv [
false
}
else {
let target = path find-sub $after $env.AUTO_VENV_TRIGGER
let target = (path find-sub $after $env.AUTO_VENV_TRIGGER)

(if ($target | is-empty) {
false
}
else {
$env.VIRTUAL_ENV != $target
(if ('VIRTUAL_ENV' in $env) {
$env.VIRTUAL_ENV != $target
} else {
false # should it default to `false`?
})
})

})
Expand Down
15 changes: 8 additions & 7 deletions modules/virtual_environments/auto-venv/venvs/python-venv.nu
Expand Up @@ -57,11 +57,12 @@ export def-env auto-venv-on-enter [
)

let venv_path = ([$virtual_env $bin] | path join)
let new_path = ($old_path | prepend $venv_path | str join $path_sep)
# let new_path = ($old_path | prepend $venv_path | str join $path_sep)
let new_path = ($old_path | prepend $venv_path)

# Creating the new prompt for the session
let virtual_prompt = if ($virtual_prompt == '') {
$'(char lparen)($virtual_env | path basename)(char rparen) '
$'(char lparen)($virtual_env | path split | drop 1 | path join | path basename)(char rparen) '
} else {
'(' + $virtual_prompt + ') '
}
Expand All @@ -74,13 +75,13 @@ export def-env auto-venv-on-enter [

# If there is no default prompt, then only the env is printed in the prompt
let new_prompt = if (has-env 'PROMPT_COMMAND') {
if ($old_prompt_command | describe) == 'block' {
{ $'($virtual_prompt)(do $old_prompt_command)' }
if (($old_prompt_command | describe) in ['block', 'closure']) {
$'($virtual_prompt)(do $old_prompt_command)'
} else {
{ $'($virtual_prompt)($old_prompt_command)' }
$'($virtual_prompt)($old_prompt_command)'
}
} else {
{ $'($virtual_prompt)' }
$'($virtual_prompt)'
}

# Environment variables that will be batched loaded to the virtual env
Expand All @@ -96,4 +97,4 @@ export def-env auto-venv-on-enter [
}

export alias pydoc = python -m pydoc
export alias pip = python -m pip
export alias pip = python -m pip

0 comments on commit 272b7e2

Please sign in to comment.