Repository navigation
Expand file tree
/
Copy pathutils.sh
More file actions
executable file
·61 lines (53 loc) · 2.06 KB
/
Copy pathutils.sh
File metadata and controls
executable file
·61 lines (53 loc) · 2.06 KB
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
#!/usr/bin/env bash
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
# Executes the Python command with the given arguments.
# Tries to run `python3`, with fallback to `python`.
# Stops the script when no Python command was found.
function exec_python() {
if command -v python3 &>/dev/null; then
python3 "$@"
elif command -v python &>/dev/null; then
python "$@"
else
echo "Error: No executable for Python 3 found." >&2
exit 1
fi
}
# Check active Python version.
# Stops the script when the version of the Python command is invalid.
function check_python_version() {
# We try to use the same Python version (LTS) for all TypeScript ShapeDiver projects.
local target_python_version="3.13"
local detected_version
if ! detected_version="$(exec_python -V 2>&1)"; then
echo "Error: No executable for Python 3 found." >&2
exit 1
fi
if [[ ! "${detected_version}" =~ ^Python\ ${target_python_version//./\\.}\. ]]; then
echo "Invalid Python version: Detected ${detected_version} but requires ${target_python_version}.x." >&2
echo "Run 'mise install' to install the Python version pinned in mise.toml." >&2
exit 1
fi
}
# Activates the Python virtual environment in the current shell.
# Tries to activate the venv first for Unix & Mac, with fallback to Windows.
# Stops the script when the virtual environment could not be activated.
function activate_python_venv() {
if pushd "${__dir}/../.venv/bin/" &>/dev/null || pushd "${__dir}/../.venv/Scripts/" &>/dev/null; then
source "./activate"
else
echo "Could not activate Python virtual environment: Run 'npm run init' to setup Python." >&2
exit 1
fi
}
# Tries to run a script that is located in `./custom/` and that has the same name as the calling
# script. If a custom script was found, the shell is stopped with the script's exit status.
# Otherwise, nothing happens.
function try_run_custom_script() {
custom_script="${__dir}/custom/${0##*/}"
if [[ -f "${custom_script}" ]]; then
# shellcheck source=/dev/null
source "${custom_script}" "$@"
exit "$?"
fi
}