github.com/whyrusleeping/gx@v0.14.3/bin/check_version (about)

     1  #!/bin/sh
     2  
     3  if test "x$UPGRADE_MSG" = "x"; then
     4      printf >&2 "fatal: Please set '"'$UPGRADE_MSG'"' before sourcing this script\n"
     5      exit 1
     6  fi
     7  
     8  die_upgrade() {
     9      printf >&2 "fatal: %s\n" "$@"
    10      printf >&2 "=> %s\n" "$UPGRADE_MSG"
    11      exit 1
    12  }
    13  
    14  major_number() {
    15      vers="$1"
    16  
    17      # Hack around 'expr' exiting with code 1 when it outputs 0
    18      case "$vers" in
    19          0) echo "0" ;;
    20          0.*) echo "0" ;;
    21          *) expr "$vers" : "\([^.]*\).*" || return 1
    22      esac
    23  }
    24  
    25  check_at_least_version() {
    26      MIN_VERS="$1"
    27      CUR_VERS="$2"
    28      PROG_NAME="$3"
    29  
    30      # Get major, minor and fix numbers for each version
    31      MIN_MAJ=$(major_number "$MIN_VERS") || die "No major version number in '$MIN_VERS' for '$PROG_NAME'"
    32      CUR_MAJ=$(major_number "$CUR_VERS") || die "No major version number in '$CUR_VERS' for '$PROG_NAME'"
    33  
    34      # We expect a version to be of form X.X.X
    35      # if the second dot doesn't match, we consider it a prerelease
    36  
    37      if MIN_MIN=$(expr "$MIN_VERS" : "[^.]*\.\([0-9]\+\)"); then
    38          # this captured digit is neccessary, since expr returns code 1 if the output is empty
    39          if expr "$MIN_VERS" : "[^.]*\.[0-9]*\([0-9]\.\|[0-9]\$\)" >/dev/null; then
    40              MIN_PRERELEASE="0"
    41          else
    42              MIN_PRERELEASE="1"
    43          fi
    44          MIN_FIX=$(expr "$MIN_VERS" : "[^.]*\.[0-9]\+[^0-9]\+\([0-9]\+\)") || MIN_FIX="0"
    45      else
    46          MIN_MIN="0"
    47          MIN_PRERELEASE="0"
    48          MIN_FIX="0"
    49      fi
    50      if CUR_MIN=$(expr "$CUR_VERS" : "[^.]*\.\([0-9]\+\)"); then
    51          # this captured digit is neccessary, since expr returns code 1 if the output is empty
    52          if expr "$CUR_VERS" : "[^.]*\.[0-9]*\([0-9]\.\|[0-9]\$\)" >/dev/null; then
    53              CUR_PRERELEASE="0"
    54          else
    55              CUR_PRERELEASE="1"
    56          fi
    57          CUR_FIX=$(expr "$CUR_VERS" : "[^.]*\.[0-9]\+[^0-9]\+\([0-9]\+\)") || CUR_FIX="0"
    58      else
    59          CUR_MIN="0"
    60          CUR_PRERELEASE="0"
    61          CUR_FIX="0"
    62      fi
    63  
    64      # Compare versions
    65      VERS_LEAST="$PROG_NAME version '$CUR_VERS' should be at least '$MIN_VERS'"
    66      test "$CUR_MAJ" -lt "$MIN_MAJ" && die_upgrade "$VERS_LEAST"
    67      test "$CUR_MAJ" -gt "$MIN_MAJ" || {
    68          test "$CUR_MIN" -lt "$MIN_MIN" && die_upgrade "$VERS_LEAST"
    69          test "$CUR_MIN" -gt "$MIN_MIN" || {
    70              test "$CUR_PRERELEASE" -gt "$MIN_PRERELEASE" && die_upgrade "$VERS_LEAST"
    71              test "$CUR_PRERELEASE" -lt "$MIN_PRERELEASE" || {
    72                  test "$CUR_FIX" -lt "$MIN_FIX" && die_upgrade "$VERS_LEAST"
    73                  true
    74              }
    75          }
    76      }
    77  }