|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# https://stackoverflow.com/questions/2683279/how-to-detect-if-a-script-is-being-sourced |
| 4 | +sourced=0 |
| 5 | +if [ -n "$ZSH_VERSION" ]; then |
| 6 | + case $ZSH_EVAL_CONTEXT in *:file) sourced=1;; esac |
| 7 | +elif [ -n "$KSH_VERSION" ]; then |
| 8 | + # shellcheck disable=SC2296 |
| 9 | + [ "$(cd -- "$(dirname -- "$0")" && pwd -P)/$(basename -- "$0")" != "$(cd -- "$(dirname -- "${.sh.file}")" && pwd -P)/$(basename -- "${.sh.file}")" ] && sourced=1 |
| 10 | +elif [ -n "$BASH_VERSION" ]; then |
| 11 | + (return 0 2>/dev/null) && sourced=1 |
| 12 | +else |
| 13 | + # All other shells: examine $0 for known shell binary filenames. |
| 14 | + # Detects `sh` and `dash`; add additional shell filenames as needed. |
| 15 | + case ${0##*/} in sh|-sh|dash|-dash) |
| 16 | + if [ -z "$PROJECT_ROOT" ]; then |
| 17 | + echo "POSIX environments need PROJECT_ROOT in the environment for 'source run.sh activate' to work" |
| 18 | + echo "This must be set to the root of this repository" |
| 19 | + return 1 |
| 20 | + fi |
| 21 | + ;; |
| 22 | + esac |
| 23 | +fi |
| 24 | + |
| 25 | +# Bash does not make it easy to find where this file is |
| 26 | +# Here I'm making it so it doesn't matter what directory you are in |
| 27 | +# when you execute this script. And it doesn't matter whether you're |
| 28 | +# executing a symlink to this script |
| 29 | +# Note the `-h` in the while loop asks if this path is a symlink |
| 30 | +pushd . >'/dev/null' |
| 31 | +DIRECTORY_BEFORE="$(pwd)" |
| 32 | +SCRIPT_PATH="${BASH_SOURCE[0]:-$0}" |
| 33 | + |
| 34 | +find_here() { |
| 35 | + while [ -h "$SCRIPT_PATH" ]; do |
| 36 | + cd "$(dirname -- "$SCRIPT_PATH")" || return 1 |
| 37 | + SCRIPT_PATH="$(readlink -f -- "$SCRIPT_PATH")" |
| 38 | + done |
| 39 | + cd "$(dirname -- "$SCRIPT_PATH")" >'/dev/null' || return 1 |
| 40 | +} |
| 41 | + |
| 42 | +if ! find_here; then |
| 43 | + if [ "$sourced" = "1" ]; then |
| 44 | + return 1 |
| 45 | + else |
| 46 | + exit 1 |
| 47 | + fi |
| 48 | +fi |
| 49 | + |
| 50 | +PROJECT_ROOT=$(pwd) |
| 51 | +export PROJECT_ROOT |
| 52 | + |
| 53 | +if ! ./tools/uv sync --locked -q; then |
| 54 | + if [ "$sourced" = "1" ]; then |
| 55 | + return 1 |
| 56 | + else |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | +fi |
| 60 | + |
| 61 | +if [ -f ./tools/requirements.local.txt ]; then |
| 62 | + if ! ./tools/uv pip install -q -r ./tools/requirements.local.txt; then |
| 63 | + if [ "$sourced" = "1" ]; then |
| 64 | + return 1 |
| 65 | + else |
| 66 | + exit 1 |
| 67 | + fi |
| 68 | + fi |
| 69 | +fi |
| 70 | + |
| 71 | +if [ "$sourced" = "1" ]; then |
| 72 | + # shellcheck source=/dev/null |
| 73 | + source .venv/bin/activate |
| 74 | + cd "$DIRECTORY_BEFORE" || return 1 |
| 75 | +else |
| 76 | + exec ./tools/uv run ./tools/run.py "$@" |
| 77 | +fi |
0 commit comments