-
Notifications
You must be signed in to change notification settings - Fork 0
/
.envrc
executable file
·87 lines (68 loc) · 2.79 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
#!/bin/sh
update_dotenv() {
var_name="$1"
var_value="$2"
if grep -qE "^${var_name}=.*$" .env; then
sed_script="$(printf 's#^%s=.*$#%s#g\n' "${var_name}" "${var_name}=${var_value}")"
sed -i.bak "${sed_script}" .env || (
sed "${sed_script}" .env >/tmp/env
cp /tmp/env ./.env
)
else
printf '%s=%s\n' "${var_name}" "${var_value}" >>.env
fi
}
if command -v poetry >/dev/null; then
test -f poetry.lock || poetry install
touch .poetry.lock.md5 # create an empty file if it doesn't exist to protect the following command
if command -v md5sum >/dev/null; then
md5sum=md5sum
else
md5sum="md5 -r"
fi
# hash `poetry.lock` and compare it to the last time dependencies were installed
MD5_LAST="$(cut -d' ' -f1 <.poetry.lock.md5)"
MD5_CURRENT="$($md5sum poetry.lock | cut -d' ' -f1)"
if [ "$MD5_LAST" != "$MD5_CURRENT" ]; then
printf '🚀 Detected changes in \033[0;36mpoetry.lock\033[0m; installing dependencies\n'
poetry install && echo "$MD5_CURRENT" >.poetry.lock.md5
# display environment info
poetry env info
fi
# shellcheck disable=SC1091
test -f "$(poetry env info -p)/bin/activate" && . "$(poetry env info -p)/bin/activate"
# shellcheck disable=SC2154
test -z "${PRE_COMMIT_DISABLED}" &&
command -v pre-commit >/dev/null &&
pre-commit install --install-hooks
fi
test -f .env || cp etc/base.env .env
ci_commit_short_sha="$(git rev-parse --short=8 HEAD)"
export CI_COMMIT_SHORT_SHA="${ci_commit_short_sha}"
test -n "$PYSPRY_TAG" || export PYSPRY_TAG="${CI_COMMIT_SHORT_SHA}"
update_dotenv "PYSPRY_TAG" "${PYSPRY_TAG}"
pyspry_branch_tag="$(git branch --show-current | tr -d '\n' | tr -c '[:alnum:]' '-')"
export PYSPRY_BRANCH_TAG="${pyspry_branch_tag}"
# ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯
# export the environment variables into the current shell
write_env_script() {
# Write the variables defined in the given 'dotenv' file to a source-able script for export.
dotenv_file="$1"
output_file="$2"
grep -vE "^#" "$dotenv_file" | # strip comment lines
grep -oE "^.{1,}=[^#]{1,}" | # filter to only lines with an '=', stripping EOL comments
sed "s/[\`'\"]//g" | # remove any quotes or backticks
while read -r line; do
var_name="${line%=*}"
var_value="${line#*=}"
# surround each value in quotes
printf '%s="%s"\n' "${var_name}" "${var_value}" >>"$output_file"
done
}
# shellcheck disable=SC1091
test -f .env &&
write_env_script .env tmp.env &&
set -o allexport &&
. ./tmp.env &&
set +o allexport &&
rm tmp.env