-
Notifications
You must be signed in to change notification settings - Fork 0
/
.direnvrc
108 lines (87 loc) · 2.5 KB
/
.direnvrc
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
layout_poetry() {
if [[ ! -f pyproject.toml ]]; then
log_error 'No pyproject.toml found. Use `poetry new` or `poetry init` to create one first.'
exit 2
fi
# create venv if it doesn't exist
poetry run true
export VIRTUAL_ENV=$(poetry env info --path)
export POETRY_ACTIVE=1
PATH_add "$VIRTUAL_ENV/bin"
}
use_pyenv() {
unset PYENV_VERSION
# Because each python version is prepended to the PATH, add them in reverse order
for ((j = $#; j >= 1; j--)); do
local python_version=${!j}
local pyenv_python=$(pyenv root)/versions/${python_version}/bin/python
if [[ ! -x "$pyenv_python" ]]; then
log_error "Error: $pyenv_python can't be executed."
return 1
fi
unset PYTHONHOME
local ve=$($pyenv_python -c "import pkgutil; print('venv' if pkgutil.find_loader('venv') else ('virtualenv' if pkgutil.find_loader('virtualenv') else ''))")
case $ve in
"venv")
VIRTUAL_ENV=$(direnv_layout_dir)/python-$python_version
export VIRTUAL_ENV
if [[ ! -d $VIRTUAL_ENV ]]; then
$pyenv_python -m venv "$VIRTUAL_ENV"
fi
rm -f pyrightconfig.json
PATH_add "$VIRTUAL_ENV"/bin
;;
"virtualenv")
layout_python "$pyenv_python"
;;
*)
log_error "Error: neither venv nor virtualenv are available to ${pyenv_python}."
return 1
;;
esac
[[ -z "${PYENV_VERSION-}" ]] && PYENV_VERSION=$python_version || PYENV_VERSION="${python_version}:$PYENV_VERSION"
done
export PYENV_VERSION
}
use_nvm_from_nvm_rc() {
if [[ ! -f .nvmrc ]]; then
log_error 'No .nvmrc found'
exit 2
fi
local version=$(cat .nvmrc)
use_nvm "$version"
}
use_pyenv_from_poetry_lock() {
if [[ ! -f poetry.lock ]]; then
log_error 'No poetry.lock found'
exit 2
fi
local version=$(tomlq '.metadata|.["python-versions"]' poetry.lock | sed 's/\"//g')
use_pyenv "$version"
}
use_pyenv_from_runtime_txt() {
if [[ ! -f runtime.txt ]]; then
log_error 'No runtime.txt found'
exit 2
fi
local version=$(cat runtime.txt | sed 's/python-//')
use_pyenv "$version"
}
use_pyenv_from_python_version_file() {
if [[ ! -f .python-version ]]; then
log_error 'No .python-version found'
exit 2
fi
local version=$(cat .python-version)
use_pyenv "$version"
}
use_nvm() {
local node_version=$1
nvm_sh=$(brew --prefix)/opt/nvm/nvm.sh
which nvm || source $nvm_sh
# if [[ -e $nvm_sh ]]; then
# source $nvm_sh
nvm use $node_version
# fi
}
# vim: et ts=2 sts=2 sw=2