Skip to content

Commit 272b7e2

Browse files
authored
auto-venv for python: update to be compatible with nu 0.80 (nushell#513)
* auto-venv python passes syntax checks * update readme.md * everything works!
1 parent 4b82839 commit 272b7e2

File tree

5 files changed

+32
-26
lines changed

5 files changed

+32
-26
lines changed

modules/virtual_environments/auto-venv/README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,22 @@ The scripts in this directory activate virtual environments whenever you cd into
44

55
## Usage
66

7-
1. set `$env.AUTO_VENV_TRIGGER` to the name of a script file
7+
1. set `$env.AUTO_VENV_TRIGGER` as the preferred name of a trigger file
88
1. import `auto-venv` into your environment
9-
1. cd into a folder that contains your trigger file
9+
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`
10+
1. `cd` into `/path/to/project/`
1011

11-
ex.
12+
For example:
1213
```nu
1314
# config.nu
1415
1516
export-env {
1617
let-env AUTO_VENV_TRIGGER = '__auto-venv.nu'
1718
18-
source-env ~/path/to/nu_scripts/virtual_environments/auto-venv/auto-venv.nu
19+
source-env ~/path/to/nu_scripts/modules/virtual_environments/auto-venv/auto-venv.nu
1920
}
2021
21-
use ~/path/to/nu_scripts/virtual_environments/auto-venv/auto-venv.nu *
22+
use ~/path/to/nu_scripts/modules/virtual_environments/auto-venv/auto-venv.nu *
2223
```
2324

2425
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).
@@ -30,4 +31,4 @@ NOTE: the trigger file *must* export a custom command called `auto-venv-on-enter
3031

3132
## Limitations
3233

33-
- Due to limitations with overlay naming, you cannot nest auto-venv triggers, even for separate languages / toolchains
34+
- Due to limitations with overlay naming, you cannot nest auto-venv triggers, even for separate languages / toolchains

modules/virtual_environments/auto-venv/auto-venv.nu

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export-env {
1010
# this needs to be set somewhere
1111
# let-env AUTO_VENV_TRIGGER = '__auto-venv.nu'
1212

13-
let hooks = default-hooks
13+
let hooks = (default-hooks)
1414

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

@@ -20,7 +20,7 @@ export-env {
2020

2121
def default-hooks [] {
2222
(if ($env.config.hooks.env_change.PWD != $nothing) {
23-
$env.config.hooks.env_change.PWD
23+
[$env.config.hooks.env_change.PWD]
2424
}
2525
else {
2626
[]
@@ -35,7 +35,7 @@ def build-hooks [] {
3535
let _env = $env
3636
let pwd = $_env.PWD
3737
38-
let trigger = path_extensions path find-sub . __trigger__ --type "file"
38+
let trigger = (path_extensions path find-sub . __trigger__ --type ["symlink", "file"])
3939
4040
cd ($trigger | path dirname)
4141
overlay use __trigger__ as __auto_venv

modules/virtual_environments/auto-venv/path_extensions.nu

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export def "path walk" [
1414
] {
1515
let list = ($path | path expand | path split);
1616

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

@@ -24,12 +24,12 @@ export def "path walk" [
2424
export def "path check-sub" [
2525
folder: any,
2626
subfolder: string,
27-
--type: string
27+
--type: list
2828
] {
2929

3030
(ls -a $folder
3131
| where (
32-
($type == $nothing or $it.type == $type)
32+
($type == $nothing or $it.type in $type)
3333
and ($it.name | path basename) == $subfolder
3434
)
3535
| length
@@ -43,9 +43,9 @@ export def "path check-sub" [
4343
export def "path find-sub" [
4444
folder: any,
4545
subfolder: string,
46-
--type: string
46+
--type: list
4747
] {
48-
let paths = path walk $folder;
48+
let paths = (path walk $folder);
4949

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

5656
if ($paths != $nothing) and ($paths | length) > 0 {
5757
[ ($paths | first), $subfolder ] | path join
58-
}
59-
}
58+
} else {[]}
59+
}

modules/virtual_environments/auto-venv/venv_helpers.nu

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export def has-entered-venv [
2828
after: path,
2929
] {
3030

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

3333
(if ($target | is-empty) {
3434
false
@@ -47,13 +47,17 @@ export def has-swapped-venv [
4747
false
4848
}
4949
else {
50-
let target = path find-sub $after $env.AUTO_VENV_TRIGGER
50+
let target = (path find-sub $after $env.AUTO_VENV_TRIGGER)
5151

5252
(if ($target | is-empty) {
5353
false
5454
}
5555
else {
56-
$env.VIRTUAL_ENV != $target
56+
(if ('VIRTUAL_ENV' in $env) {
57+
$env.VIRTUAL_ENV != $target
58+
} else {
59+
false # should it default to `false`?
60+
})
5761
})
5862

5963
})

modules/virtual_environments/auto-venv/venvs/python-venv.nu

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@ export def-env auto-venv-on-enter [
5757
)
5858

5959
let venv_path = ([$virtual_env $bin] | path join)
60-
let new_path = ($old_path | prepend $venv_path | str join $path_sep)
60+
# let new_path = ($old_path | prepend $venv_path | str join $path_sep)
61+
let new_path = ($old_path | prepend $venv_path)
6162

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

7576
# If there is no default prompt, then only the env is printed in the prompt
7677
let new_prompt = if (has-env 'PROMPT_COMMAND') {
77-
if ($old_prompt_command | describe) == 'block' {
78-
{ $'($virtual_prompt)(do $old_prompt_command)' }
78+
if (($old_prompt_command | describe) in ['block', 'closure']) {
79+
$'($virtual_prompt)(do $old_prompt_command)'
7980
} else {
80-
{ $'($virtual_prompt)($old_prompt_command)' }
81+
$'($virtual_prompt)($old_prompt_command)'
8182
}
8283
} else {
83-
{ $'($virtual_prompt)' }
84+
$'($virtual_prompt)'
8485
}
8586

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

9899
export alias pydoc = python -m pydoc
99-
export alias pip = python -m pip
100+
export alias pip = python -m pip

0 commit comments

Comments
 (0)