-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.direnvrc
144 lines (119 loc) · 3.96 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
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')
layout_uv "$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-//')
layout_uv "$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)
layout_uv "$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
}
# taken from https://github.com/direnv/direnv/pull/1329/files#diff-65f096acc5cb8701ae62be810fce97e6b2820be683839581f0042a2e3761cfc8
layout_uv() {
local python_version=${1:-}
# If they have specified a python version, check to see if there is already a
# .python-version file. If there is, and the specified version is different,
# then recreate the virtual environment with the new version.
# Otherwise, just use the existing virtual environment, if there is already a
# .venv directory.
VIRTUAL_ENV="${PWD}/.venv"
# Get the current python version from the .python-version file
local python_version_file=".python-version"
local current_python_version=""
if [[ -f "$python_version_file" ]]; then
current_python_version=$(<"$python_version_file")
fi
# Check to see if there is already an existing virtual environment,
# OR if the current python version is different from the one specified in .python-version
if [[ -z $VIRTUAL_ENV || ! -d $VIRTUAL_ENV || (-n $python_version && $current_python_version != "$python_version") ]]; then
log_status "No virtual environment exists. Executing \`uv venv\` to create one."
if [[ -n $python_version ]]; then
uv venv --python "$python_version"
# Write the python version to the .python-version file
echo "$python_version" > .python-version
else
uv venv
fi
fi
PATH_add "$VIRTUAL_ENV/bin"
export UV_ACTIVE=1
export VIRTUAL_ENV
export UV_PROJECT_ENVIRONMENT=$VIRTUAL_ENV
}
# vim: et ts=2 sts=2 sw=2