| 1 | #!/usr/bin/env bash |
| 2 | set -euo pipefail |
| 3 | |
| 4 | # Compare two public Desktop versions in one release channel. The result is: |
| 5 | # update - candidate is newer, or the channel pointer does not exist yet |
| 6 | # skip - candidate is equal to or older than the current pointer |
| 7 | # |
| 8 | # Decimal components are compared without shell arithmetic so arbitrarily large |
| 9 | # canonical components retain their exact ordering. |
| 10 | |
| 11 | export LC_ALL=C |
| 12 | |
| 13 | channel="${1:-}" |
| 14 | candidate="${2:-}" |
| 15 | current="${3:-}" |
| 16 | |
| 17 | case "$channel" in |
| 18 | stable) |
| 19 | version_pattern='^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)$' |
| 20 | ;; |
| 21 | preview) |
| 22 | version_pattern='^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)-preview\.(0|[1-9][0-9]*)$' |
| 23 | ;; |
| 24 | *) |
| 25 | echo "unsupported Desktop release channel: $channel" >&2 |
| 26 | exit 2 |
| 27 | ;; |
| 28 | esac |
| 29 | |
| 30 | parse_version() { |
| 31 | local value="$1" |
| 32 | if [[ "$value" =~ $version_pattern ]]; then |
| 33 | if [ "$channel" = "stable" ]; then |
| 34 | printf '%s %s %s\n' "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" "${BASH_REMATCH[3]}" |
| 35 | else |
| 36 | printf '%s %s %s %s\n' "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" "${BASH_REMATCH[3]}" "${BASH_REMATCH[4]}" |
| 37 | fi |
| 38 | return 0 |
| 39 | fi |
| 40 | echo "invalid $channel Desktop release version: $value" >&2 |
| 41 | return 1 |
| 42 | } |
| 43 | |
| 44 | candidate_parts="$(parse_version "$candidate")" |
| 45 | if [ -z "$current" ]; then |
| 46 | echo update |
| 47 | exit 0 |
| 48 | fi |
| 49 | current_parts="$(parse_version "$current")" |
| 50 | |
| 51 | candidate_is_newer=0 |
| 52 | read -r -a candidate_values <<<"$candidate_parts" |
| 53 | read -r -a current_values <<<"$current_parts" |
| 54 | for i in "${!candidate_values[@]}"; do |
| 55 | candidate_value="${candidate_values[$i]}" |
| 56 | current_value="${current_values[$i]}" |
| 57 | if [ "${#candidate_value}" -gt "${#current_value}" ] || |
| 58 | { [ "${#candidate_value}" -eq "${#current_value}" ] && [[ "$candidate_value" > "$current_value" ]]; }; then |
| 59 | candidate_is_newer=1 |
| 60 | break |
| 61 | fi |
| 62 | if [ "${#candidate_value}" -lt "${#current_value}" ] || |
| 63 | { [ "${#candidate_value}" -eq "${#current_value}" ] && [[ "$candidate_value" < "$current_value" ]]; }; then |
| 64 | break |
| 65 | fi |
| 66 | done |
| 67 | |
| 68 | if [ "$candidate_is_newer" -eq 1 ]; then |
| 69 | echo update |
| 70 | else |
| 71 | echo skip |
| 72 | fi |
| 73 |