File: setup

package info (click to toggle)
aionotion 2025.02.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 604 kB
  • sloc: python: 1,597; sh: 122; makefile: 3
file content (109 lines) | stat: -rwxr-xr-x 2,404 bytes parent folder | download
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
#!/usr/bin/env bash
set -Eeuo pipefail
trap cleanup SIGINT SIGTERM ERR EXIT

SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
REPO_DIR=$(dirname "$SCRIPT_DIR")

usage() {
  cat <<EOF
USAGE: $(basename "${BASH_SOURCE[0]}")

Sets up the local environment for dev work.

AVAILABLE OPTIONS:

-h, --help      Print this help and exit
-v, --verbose   Print script debug info
EOF
}

cleanup() {
  trap - SIGINT SIGTERM ERR EXIT
}

msg() {
  echo >&2 -e "${1-}"
}

fail() {
  local msg=$1
  local code=${2-1}
  msg "${RED}$msg${NOFORMAT}"
  printf "\n"
  usage
  exit "$code"
}

parse_params() {
  args=()
  while [[ $# -gt 0 ]]; do
    case "$1" in
      -h | --help) usage && exit 0 ;;
      -v | --verbose) set -x ;;
      -?*) fail "Unknown option: $1" ;;
      *) args+=("$1") ;;
    esac
    shift
  done
  return 0
}

setup_colors() {
  if [[ -t 2 ]] && [[ -z "${NO_COLOR-}" ]] && [[ "${TERM-}" != "dumb" ]]; then
    NOFORMAT="\033[0m" RED="\033[0;31m" BLUE="\033[0;34m" GREEN='\033[0;32m'
  else
    NOFORMAT="" RED="" BLUE="" GREEN=""
  fi
}

validate_dependencies_exist() {
  local dependencies=(
    "pre-commit"
    "python"
  )

  for dependency in "${dependencies[@]}"; do
    if ! command -v "$dependency" &>/dev/null; then
      fail "Missing dependency: $dependency"
    fi
  done
}

main() {
  setup_colors
  parse_params "$@"

  if command -v "mise"; then
    msg "${BLUE}🔍 mise detected; configuring runtimes...${NOFORMAT}"
    mise install -y
  fi

  # Check if we're running in Python a virtual environment (creating one if not):
  if [[ -z "${VIRTUAL_ENV-}" ]]; then
    msg "${BLUE}🚜 Creating Python virtual environment...${NOFORMAT}"
    python -m venv "$REPO_DIR/.venv"
    # shellcheck disable=SC1091
    source "$REPO_DIR/.venv/bin/activate"
  fi

  msg "${BLUE}🚜 Installing dependencies ...${NOFORMAT}"
  if ! command -v "uv" &>/dev/null; then
    if ! command -v "pip" &>/dev/null; then
      python -m ensurepip
    fi
    uv_version="$(grep "uv==" "$REPO_DIR/pyproject.toml" | awk -F'==' '{print $2}' | tr -d '",')"
    python -m pip install uv=="$uv_version"
  fi
  uv sync --all-extras

  msg "${BLUE}🚜 Installing pre-commit hooks...${NOFORMAT}"
  pre-commit install

  # At this stage, we should have all of our dependencies installed; confirm that:
  validate_dependencies_exist

  msg "${GREEN}✅ Setup complete!${NOFORMAT}"
}

main "$@"