| 1 |
#!/usr/bin/env bash |
| 2 |
set -Eeuo pipefail |
| 3 |
trap cleanup SIGINT SIGTERM ERR EXIT |
| 4 |
|
| 5 |
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P) |
| 6 |
REPO_DIR=$(dirname "$SCRIPT_DIR") |
| 7 |
|
| 8 |
usage() { |
| 9 |
cat <<EOF |
| 10 |
USAGE: $(basename "${BASH_SOURCE[0]}") |
| 11 |
|
| 12 |
Sets up the local environment for dev work. |
| 13 |
|
| 14 |
AVAILABLE OPTIONS: |
| 15 |
|
| 16 |
-h, --help Print this help and exit |
| 17 |
-v, --verbose Print script debug info |
| 18 |
EOF |
| 19 |
} |
| 20 |
|
| 21 |
cleanup() { |
| 22 |
trap - SIGINT SIGTERM ERR EXIT |
| 23 |
} |
| 24 |
|
| 25 |
msg() { |
| 26 |
echo >&2 -e "${1-}" |
| 27 |
} |
| 28 |
|
| 29 |
fail() { |
| 30 |
local msg=$1 |
| 31 |
local code=${2-1} |
| 32 |
msg "${RED}$msg${NOFORMAT}" |
| 33 |
printf "\n" |
| 34 |
usage |
| 35 |
exit "$code" |
| 36 |
} |
| 37 |
|
| 38 |
parse_params() { |
| 39 |
args=() |
| 40 |
while [[ $# -gt 0 ]]; do |
| 41 |
case "$1" in |
| 42 |
-h | --help) usage && exit 0 ;; |
| 43 |
-v | --verbose) set -x ;; |
| 44 |
-?*) fail "Unknown option: $1" ;; |
| 45 |
*) args+=("$1") ;; |
| 46 |
esac |
| 47 |
shift |
| 48 |
done |
| 49 |
return 0 |
| 50 |
} |
| 51 |
|
| 52 |
setup_colors() { |
| 53 |
if [[ -t 2 ]] && [[ -z "${NO_COLOR-}" ]] && [[ "${TERM-}" != "dumb" ]]; then |
| 54 |
NOFORMAT="\033[0m" RED="\033[0;31m" BLUE="\033[0;34m" GREEN='\033[0;32m' |
| 55 |
else |
| 56 |
NOFORMAT="" RED="" BLUE="" GREEN="" |
| 57 |
fi |
| 58 |
} |
| 59 |
|
| 60 |
validate_dependencies_exist() { |
| 61 |
local dependencies=( |
| 62 |
"pre-commit" |
| 63 |
"python" |
| 64 |
) |
| 65 |
|
| 66 |
for dependency in "${dependencies[@]}"; do |
| 67 |
if ! command -v "$dependency" &>/dev/null; then |
| 68 |
fail "Missing dependency: $dependency" |
| 69 |
fi |
| 70 |
done |
| 71 |
} |
| 72 |
|
| 73 |
main() { |
| 74 |
setup_colors |
| 75 |
parse_params "$@" |
| 76 |
|
| 77 |
if command -v "mise"; then |
| 78 |
msg "${BLUE}🔍 mise detected; configuring runtimes...${NOFORMAT}" |
| 79 |
mise install -y |
| 80 |
fi |
| 81 |
|
| 82 |
# Check if we're running in Python a virtual environment (creating one if not): |
| 83 |
if [[ -z "${VIRTUAL_ENV-}" ]]; then |
| 84 |
msg "${BLUE}🚜 Creating Python virtual environment...${NOFORMAT}" |
| 85 |
python -m venv "$REPO_DIR/.venv" |
| 86 |
# shellcheck disable=SC1091 |
| 87 |
source "$REPO_DIR/.venv/bin/activate" |
| 88 |
fi |
| 89 |
|
| 90 |
msg "${BLUE}🚜 Installing dependencies ...${NOFORMAT}" |
| 91 |
if ! command -v "uv" &>/dev/null; then |
| 92 |
if ! command -v "pip" &>/dev/null; then |
| 93 |
python -m ensurepip |
| 94 |
fi |
| 95 |
uv_version="$(grep "uv==" "$REPO_DIR/pyproject.toml" | awk -F'==' '{print $2}' | tr -d '",')" |
| 96 |
python -m pip install uv=="$uv_version" |
| 97 |
fi |
| 98 |
uv sync --all-extras |
| 99 |
|
| 100 |
msg "${BLUE}🚜 Installing pre-commit hooks...${NOFORMAT}" |
| 101 |
pre-commit install |
| 102 |
|
| 103 |
# At this stage, we should have all of our dependencies installed; confirm that: |
| 104 |
validate_dependencies_exist |
| 105 |
|
| 106 |
msg "${GREEN}✅ Setup complete!${NOFORMAT}" |
| 107 |
} |
| 108 |
|
| 109 |
main "$@" |