forked from nushell/nu_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvenv_helpers.nu
More file actions
90 lines (74 loc) · 1.82 KB
/
venv_helpers.nu
File metadata and controls
90 lines (74 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use path_extensions.nu *
def has-sub [
folder: string,
subfolder: string
] {
not (path find-sub $folder $subfolder | is-empty);
}
def get-env [
name: string
default: any
] {
(if ($name in $env) {
$env | get $name
}
else {
$default
})
}
export def venv-is-active [] {
'__auto_venv' in (overlay list | get name)
}
# Creates a virtual environment under the current directory
export def 'venv-create' [] {
let venv_path = $env.PWD
let venv_name = ($env.PWD | path basename)
let is_posix = ($nu.os-info.family == 'unix')
let python_name = if $is_posix {'python3'} else {'py.exe'}
run-external $python_name "-m" "venv" ".venv" "--clear" "--prompt" $venv_name
run-external $".venv/bin/($python_name)" "-m" "pip" "install" "-U" "pip" "wheel" "setuptools"
let trigger_file = ([$env.PWD, $env.AUTO_VENV_TRIGGER] | path join)
ln -sf $'($env.FILE_PWD)/venvs/python-venv.nu' $trigger_file
}
export def has-entered-venv [
after: path,
] {
let target = (path find-sub $after $env.AUTO_VENV_TRIGGER)
(if ($target | is-empty) {
false
}
else {
# if venv is already active, handle it in "venv swap" hook
not (venv-is-active)
})
}
export def has-swapped-venv [
after: path,
] {
(if not (venv-is-active) {
false
}
else {
let target = (path find-sub $after $env.AUTO_VENV_TRIGGER)
(if ($target | is-empty) {
false
}
else {
(if ('VIRTUAL_ENV' in $env) {
$env.VIRTUAL_ENV != $target
} else {
false # should it default to `false`?
})
})
})
}
export def has-exited-venv [
after: path,
] {
(if not (venv-is-active) {
false
}
else {
not (has-sub $after $env.AUTO_VENV_TRIGGER)
})
}