166 lines
4.1 KiB
Bash
Executable File
166 lines
4.1 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
|
|
# Compare the current tmux version against one or more version constraints.
|
|
#
|
|
# Each constraint pairs an operator and version in a single argument,
|
|
# e.g. ">= 3.2". Multiple constraints can be joined with the logical operators
|
|
# `and` / `or`, where `and` binds tighter than `or` (standard precedence). This
|
|
# allows a single invocation to express a range without spawning the script
|
|
# multiple times.
|
|
#
|
|
# Exit: 0 if the whole expression is true, 1 if false
|
|
#
|
|
# Examples:
|
|
# check-version.sh ">= 3.2"
|
|
# check-version.sh ">= 3.2" and "< 3.3"
|
|
# check-version.sh "< 2.9" or ">= 3.4"
|
|
|
|
set -euo pipefail
|
|
|
|
usage() {
|
|
cat >&2 <<'EOF'
|
|
usage: check-version.sh <constraint> [and|or <constraint> ...]
|
|
constraint: <operator> <version>, e.g. ">= 3.2"
|
|
operators: < <= == >= >
|
|
logical: and or (and binds tighter than or)
|
|
EOF
|
|
}
|
|
|
|
if [[ ${1:-} == "-h" || ${1:-} == "--help" ]]; then
|
|
usage
|
|
exit 0
|
|
fi
|
|
|
|
if [[ $# -eq 0 ]]; then
|
|
usage
|
|
exit 1
|
|
fi
|
|
|
|
# Extract tmux version (e.g., "tmux 3.3a" -> "3.3a")
|
|
current_version=$(tmux -V | sed 's/^tmux //')
|
|
|
|
# Compare two version strings
|
|
# Returns: -1 if v1 < v2, 0 if v1 == v2, 1 if v1 > v2
|
|
compare_versions() {
|
|
local v1="$1"
|
|
local v2="$2"
|
|
|
|
# Strip trailing letters (e.g., "3.3a" -> "3.3") and save them
|
|
local v1_suffix="${v1##*[0-9]}"
|
|
local v2_suffix="${v2##*[0-9]}"
|
|
v1="${v1%[a-zA-Z]*}"
|
|
v2="${v2%[a-zA-Z]*}"
|
|
|
|
# Split into components
|
|
IFS='.' read -ra v1_parts <<< "$v1"
|
|
IFS='.' read -ra v2_parts <<< "$v2"
|
|
|
|
# Compare numeric parts
|
|
local max_len=$(( ${#v1_parts[@]} > ${#v2_parts[@]} ? ${#v1_parts[@]} : ${#v2_parts[@]} ))
|
|
for ((i = 0; i < max_len; i++)); do
|
|
local p1="${v1_parts[i]:-0}"
|
|
local p2="${v2_parts[i]:-0}"
|
|
if ((p1 < p2)); then
|
|
echo -1
|
|
return
|
|
elif ((p1 > p2)); then
|
|
echo 1
|
|
return
|
|
fi
|
|
done
|
|
|
|
# Numeric parts equal, compare suffixes (no suffix < a < b < ...)
|
|
# A letter suffix indicates a patch release, so 3.6a > 3.6
|
|
if [[ -z "$v1_suffix" && -n "$v2_suffix" ]]; then
|
|
echo -1
|
|
return
|
|
elif [[ -n "$v1_suffix" && -z "$v2_suffix" ]]; then
|
|
echo 1
|
|
return
|
|
elif [[ "$v1_suffix" < "$v2_suffix" ]]; then
|
|
echo -1
|
|
return
|
|
elif [[ "$v1_suffix" > "$v2_suffix" ]]; then
|
|
echo 1
|
|
return
|
|
fi
|
|
|
|
echo 0
|
|
}
|
|
|
|
# Evaluate a single constraint (e.g. ">= 3.2") against the current version.
|
|
# Returns 0 (true) or 1 (false); exits on a malformed constraint.
|
|
eval_constraint() {
|
|
local constraint="$1"
|
|
local operator version
|
|
read -r operator version <<< "$constraint"
|
|
|
|
if [[ -z "$operator" || -z "$version" ]]; then
|
|
echo "error: invalid constraint: '$constraint' (expected '<operator> <version>', e.g. '>= 3.2')" >&2
|
|
usage
|
|
exit 1
|
|
fi
|
|
|
|
local result
|
|
result=$(compare_versions "$current_version" "$version")
|
|
|
|
case "$operator" in
|
|
'<') [[ "$result" -eq -1 ]] ;;
|
|
'<=') [[ "$result" -le 0 ]] ;;
|
|
'==') [[ "$result" -eq 0 ]] ;;
|
|
'>=') [[ "$result" -ge 0 ]] ;;
|
|
'>') [[ "$result" -eq 1 ]] ;;
|
|
*)
|
|
echo "error: invalid operator: '$operator'" >&2
|
|
usage
|
|
exit 1 ;;
|
|
esac
|
|
}
|
|
|
|
# Walk the alternating constraint/logical sequence, accumulating each `and`
|
|
# group (1 = true) and folding completed groups into the `or` result. Truth
|
|
# values are tracked as ints and converted to an exit code at the end.
|
|
or_result=0
|
|
and_result=1
|
|
expect=constraint
|
|
|
|
for token in "$@"; do
|
|
if [[ "$expect" == "constraint" ]]; then
|
|
case "$token" in
|
|
and | or)
|
|
echo "error: expected a constraint, got logical operator: $token" >&2
|
|
usage
|
|
exit 1 ;;
|
|
esac
|
|
if eval_constraint "$token"; then
|
|
and_result=$(( and_result & 1 ))
|
|
else
|
|
and_result=$(( and_result & 0 ))
|
|
fi
|
|
expect=logical
|
|
else
|
|
case "$token" in
|
|
and)
|
|
;;
|
|
or)
|
|
or_result=$(( or_result | and_result ))
|
|
and_result=1 ;;
|
|
*)
|
|
echo "error: expected 'and' or 'or', got: $token" >&2
|
|
usage
|
|
exit 1 ;;
|
|
esac
|
|
expect=constraint
|
|
fi
|
|
done
|
|
|
|
if [[ "$expect" == "constraint" ]]; then
|
|
echo "error: expression ends with a logical operator" >&2
|
|
usage
|
|
exit 1
|
|
fi
|
|
|
|
or_result=$(( or_result | and_result ))
|
|
|
|
[[ "$or_result" -eq 1 ]] && exit 0 || exit 1
|