/
.envrc
125 lines (107 loc) · 3.99 KB
/
.envrc
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
#!/usr/bin/env bash
set -o errexit
set -o nounset
set -o pipefail
# Handle M1 vs Intel Macs
if [[ "$(uname -m)" == "arm64" ]]; then
eval "$(/opt/homebrew/bin/brew shellenv)"
# Fix pyenv installation for 3.9.7 https://github.com/pyenv/pyenv/issues/1768#issuecomment-931750061
export PYTHON_CONFIGURE_OPTS
PYTHON_CONFIGURE_OPTS="--build=aarch64-apple-darwin$(uname -r)"
else
eval "$(/usr/local/bin/brew shellenv)"
fi
function matches() {
local src_file
src_file="${1}"
local status_file
status_file="${2}"
[[ -f "${src_file}" ]] && [[ "$(shasum "${src_file}")" == "$(cat "${status_file}" 2>/dev/null)" ]]
}
function setup_brew() {
if ! hash brew 2>/dev/null; then
sudo true # Trigger sudo password prompt so brew doesn't have to and we can skip through the next install prompt
echo "" | bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install.sh)"
fi
if ! matches Brewfile "$(direnv_layout_dir)/status_Brewfile"; then
echo "Updating brew..."
brew update
brew bundle --no-lock
shasum Brewfile >"$(direnv_layout_dir)/status_Brewfile"
fi
watch_file Brewfile
}
function setup_python() {
if [[ ! -f pyproject.toml ]]; then
log_error 'No pyproject.toml found. Use "poetry new" or "poetry init" to create one first.'
exit 2
fi
export PYENV_VERSION
PYENV_VERSION="$(head -n 1 .python-version)" # Only use the first version
local pyenv_python="${HOME}/.pyenv/versions/${PYENV_VERSION}/bin/python"
# Install python versions
local status_file
status_file="$(direnv_layout_dir)/status_${PYENV_VERSION}_pyenv"
if ! matches .python-version "${status_file}"; then
echo "Installing python ${PYENV_VERSION}"
pyenv install "${PYENV_VERSION}" --skip-existing
shasum .python-version >"${status_file}"
fi
watch_file .python-version
# Poetry must be installed *before* activating the venvs (ie: outside them).
if [[ ! -f $HOME/.local/bin/poetry ]]; then
curl -sSL https://install.python-poetry.org | "${pyenv_python}" -
fi
PATH_add "$HOME/.local/bin"
export POETRY_ACTIVE=1
export VIRTUAL_ENV
VIRTUAL_ENV="$(direnv_layout_dir)/python-$PYENV_VERSION"
unset PYTHONHOME
if [[ ! -d $VIRTUAL_ENV ]]; then
echo "Creating python ${PYENV_VERSION} venv..."
$pyenv_python -m venv "$VIRTUAL_ENV"
fi
PATH_add "$VIRTUAL_ENV"/bin
local pyproject_status_file
pyproject_status_file="$(direnv_layout_dir)/status_${PYENV_VERSION}_pyproject.toml"
local pyproject_lock_file
pyproject_lock_file="$(direnv_layout_dir)/status_${PYENV_VERSION}_poetry.lock"
if ! matches poetry.lock "${pyproject_lock_file}"; then
# Poetry can be picky. If it detects a "broken" venv, it'll emit short warning and create a different one.
#
# Check for merge conflicts and avoid (failing to) installing deps.
if grep -q '<<<' poetry.lock; then
log_error "Detected conflicts in poetry.lock, skipping syncing deps! Please resolve the conflict and relock."
else
echo "Syncing python ${PYENV_VERSION} venv..."
poetry install --all-extras --no-interaction --sync
shasum pyproject.toml >"${pyproject_status_file}"
shasum poetry.lock >"${pyproject_lock_file}"
fi
fi
watch_file pyproject.toml poetry.lock
}
function setup_pre_commit() {
# Don't run for worktrees where .git is a file, not dir
if [[ -d .git ]]; then
for hook in "pre-commit" "pre-push" "prepare-commit-msg"; do
if [[ ! -f ".git/hooks/$hook" ]]; then
pre-commit install --install-hooks -t "$hook"
fi
done
fi
}
# If not running within direnv (such as bootstrapping), add the stdlib and auto-allow the env.
if [[ -z "${DIRENV_IN_ENVRC:-}" ]]; then
eval "$(direnv stdlib)"
direnv allow
fi
# Set env var default - users may override in .envrc.secrets
export PYLINTRC="${PWD}/pyproject.toml"
# export PYTHONBREAKPOINT=ipdb.set_trace # Temporarily disable until https://github.com/prompt-toolkit/python-prompt-toolkit/issues/1726 is fixed
mkdir -p "$(direnv_layout_dir)"
setup_brew
source_env_if_exists .envrc.secrets # Allow user to override env vars before setting up other tools
setup_python
setup_pre_commit
path_add PATH "$PWD/bin"