-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_env_lib.sh
More file actions
93 lines (86 loc) · 2.57 KB
/
project_env_lib.sh
File metadata and controls
93 lines (86 loc) · 2.57 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
90
91
92
93
#!/usr/bin/env bash
# Shared helpers for resolving a per-project external virtualenv/config.
project_env_load_files() {
local root="$1"
local env_file=""
local line=""
local key=""
local value=""
for env_file in "$root/.env" "$root/.env.local"; do
if [[ -f "$env_file" ]]; then
while IFS= read -r line || [[ -n "$line" ]]; do
line="${line#"${line%%[![:space:]]*}"}"
if [[ -z "$line" || "${line:0:1}" == "#" ]]; then
continue
fi
if [[ "$line" != *=* ]]; then
continue
fi
key="${line%%=*}"
value="${line#*=}"
key="${key%"${key##*[![:space:]]}"}"
if [[ ! "$key" =~ ^[A-Za-z_][A-Za-z0-9_]*$ ]]; then
continue
fi
if [[ "$value" == \"*\" && "$value" == *\" ]]; then
value="${value:1:${#value}-2}"
elif [[ "$value" == \'*\' && "$value" == *\' ]]; then
value="${value:1:${#value}-2}"
fi
export "$key=$value"
done < "$env_file"
fi
done
}
project_env_normalize_path() {
local root="$1"
local raw="$2"
if [[ -z "$raw" ]]; then
return 1
fi
raw="${raw/#\~/$HOME}"
case "$raw" in
/*) printf '%s\n' "$raw" ;;
*) printf '%s\n' "$root/$raw" ;;
esac
}
project_env_resolve_venv_root() {
local root="$1"
local candidate=""
for candidate in "${PROJECT_VENV_ROOT:-}" "${UV_PROJECT_ENVIRONMENT:-}" "${VIRTUAL_ENV:-}"; do
if [[ -n "$candidate" ]]; then
project_env_normalize_path "$root" "$candidate"
return 0
fi
done
return 1
}
project_env_assert_outside_repo() {
local root="$1"
local candidate="$2"
local label="$3"
case "$candidate" in
"$root"/*)
echo "Error: $label points inside the repo: $candidate" >&2
echo "Set $label to an external path via environment or $root/.env" >&2
return 2
;;
esac
}
project_env_activate_exports() {
local root="$1"
local venv_root="$2"
export VIRTUAL_ENV="$venv_root"
export UV_PROJECT_ENVIRONMENT="$venv_root"
export UV_CACHE_DIR="${UV_CACHE_DIR:-$venv_root/.cache/uv}"
export RUFF_CACHE_DIR="${RUFF_CACHE_DIR:-$venv_root/.cache/ruff}"
export NPM_CONFIG_CACHE="${NPM_CONFIG_CACHE:-$venv_root/.cache/npm}"
export PLAYWRIGHT_BROWSERS_PATH="${PLAYWRIGHT_BROWSERS_PATH:-$venv_root/.cache/playwright}"
export PYTHONDONTWRITEBYTECODE="${PYTHONDONTWRITEBYTECODE:-1}"
export PYTHONPYCACHEPREFIX="${PYTHONPYCACHEPREFIX:-$venv_root/.cache/pycache}"
export WINEPREFIX="${WINEPREFIX:-$venv_root/.cache/wine}"
case ":$PATH:" in
*":$venv_root/bin:"*) ;;
*) export PATH="$venv_root/bin:$PATH" ;;
esac
}