|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +set -o errexit -o nounset -o pipefail |
| 4 | + |
| 5 | +SEMVER_REGEX="^(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)\\.(0|[1-9][0-9]*)(\\-[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?(\\+[0-9A-Za-z-]+(\\.[0-9A-Za-z-]+)*)?$" |
| 6 | + |
| 7 | +PROG=semver |
| 8 | +PROG_VERSION=2.1.0 |
| 9 | + |
| 10 | +USAGE="\ |
| 11 | +Usage: |
| 12 | + $PROG bump (major|minor|patch|release|prerel <prerel>|build <build>) <version> |
| 13 | + $PROG compare <version> <other_version> |
| 14 | + $PROG get (major|minor|patch|release|prerel|build) <version> |
| 15 | + $PROG --help |
| 16 | + $PROG --version |
| 17 | +
|
| 18 | +Arguments: |
| 19 | + <version> A version must match the following regex pattern: |
| 20 | + \"${SEMVER_REGEX}\". |
| 21 | + In english, the version must match X.Y.Z(-PRERELEASE)(+BUILD) |
| 22 | + where X, Y and Z are positive integers, PRERELEASE is an optionnal |
| 23 | + string composed of alphanumeric characters and hyphens and |
| 24 | + BUILD is also an optional string composed of alphanumeric |
| 25 | + characters and hyphens. |
| 26 | +
|
| 27 | + <other_version> See <version> definition. |
| 28 | +
|
| 29 | + <prerel> String that must be composed of alphanumeric characters and hyphens. |
| 30 | +
|
| 31 | + <build> String that must be composed of alphanumeric characters and hyphens. |
| 32 | +
|
| 33 | +Options: |
| 34 | + -v, --version Print the version of this tool. |
| 35 | + -h, --help Print this help message. |
| 36 | +
|
| 37 | +Commands: |
| 38 | + bump Bump <version> by one of major, minor, patch, prerel, build |
| 39 | + or a forced potentialy conflicting version. The bumped version is |
| 40 | + shown to stdout. |
| 41 | +
|
| 42 | + compare Compare <version> with <other_version>, output to stdout the |
| 43 | + following values: -1 if <other_version> is newer, 0 if equal, 1 if |
| 44 | + older. |
| 45 | +
|
| 46 | + get Extract given part of <version>, where part is one of major, minor, |
| 47 | + patch, prerel, build." |
| 48 | + |
| 49 | + |
| 50 | +function error { |
| 51 | + echo -e "$1" >&2 |
| 52 | + exit 1 |
| 53 | +} |
| 54 | + |
| 55 | +function usage-help { |
| 56 | + error "$USAGE" |
| 57 | +} |
| 58 | + |
| 59 | +function usage-version { |
| 60 | + echo -e "${PROG}: $PROG_VERSION" |
| 61 | + exit 0 |
| 62 | +} |
| 63 | + |
| 64 | +function validate-version { |
| 65 | + local version=$1 |
| 66 | + if [[ "$version" =~ $SEMVER_REGEX ]]; then |
| 67 | + # if a second argument is passed, store the result in var named by $2 |
| 68 | + if [ "$#" -eq "2" ]; then |
| 69 | + local major=${BASH_REMATCH[1]} |
| 70 | + local minor=${BASH_REMATCH[2]} |
| 71 | + local patch=${BASH_REMATCH[3]} |
| 72 | + local prere=${BASH_REMATCH[4]} |
| 73 | + local build=${BASH_REMATCH[6]} |
| 74 | + eval "$2=(\"$major\" \"$minor\" \"$patch\" \"$prere\" \"$build\")" |
| 75 | + else |
| 76 | + echo "$version" |
| 77 | + fi |
| 78 | + else |
| 79 | + error "version $version does not match the semver scheme 'X.Y.Z(-PRERELEASE)(+BUILD)'. See help for more information." |
| 80 | + fi |
| 81 | +} |
| 82 | + |
| 83 | +function compare-version { |
| 84 | + validate-version "$1" V |
| 85 | + validate-version "$2" V_ |
| 86 | + |
| 87 | + # MAJOR, MINOR and PATCH should compare numericaly |
| 88 | + for i in 0 1 2; do |
| 89 | + local diff=$((${V[$i]} - ${V_[$i]})) |
| 90 | + if [[ $diff -lt 0 ]]; then |
| 91 | + echo -1; return 0 |
| 92 | + elif [[ $diff -gt 0 ]]; then |
| 93 | + echo 1; return 0 |
| 94 | + fi |
| 95 | + done |
| 96 | + |
| 97 | + # PREREL should compare with the ASCII order. |
| 98 | + if [[ -z "${V[3]}" ]] && [[ -n "${V_[3]}" ]]; then |
| 99 | + echo -1; return 0; |
| 100 | + elif [[ -n "${V[3]}" ]] && [[ -z "${V_[3]}" ]]; then |
| 101 | + echo 1; return 0; |
| 102 | + elif [[ -n "${V[3]}" ]] && [[ -n "${V_[3]}" ]]; then |
| 103 | + if [[ "${V[3]}" > "${V_[3]}" ]]; then |
| 104 | + echo 1; return 0; |
| 105 | + elif [[ "${V[3]}" < "${V_[3]}" ]]; then |
| 106 | + echo -1; return 0; |
| 107 | + fi |
| 108 | + fi |
| 109 | + |
| 110 | + echo 0 |
| 111 | +} |
| 112 | + |
| 113 | +function command-bump { |
| 114 | + local new; local version; local sub_version; local command; |
| 115 | + |
| 116 | + case $# in |
| 117 | + 2) case $1 in |
| 118 | + major|minor|patch|release) command=$1; version=$2;; |
| 119 | + *) usage-help;; |
| 120 | + esac ;; |
| 121 | + 3) case $1 in |
| 122 | + prerel|build) command=$1; sub_version=$2 version=$3 ;; |
| 123 | + *) usage-help;; |
| 124 | + esac ;; |
| 125 | + *) usage-help;; |
| 126 | + esac |
| 127 | + |
| 128 | + validate-version "$version" parts |
| 129 | + # shellcheck disable=SC2154 |
| 130 | + local major="${parts[0]}" |
| 131 | + local minor="${parts[1]}" |
| 132 | + local patch="${parts[2]}" |
| 133 | + local prere="${parts[3]}" |
| 134 | + local build="${parts[4]}" |
| 135 | + |
| 136 | + case "$command" in |
| 137 | + major) new="$((major + 1)).0.0";; |
| 138 | + minor) new="${major}.$((minor + 1)).0";; |
| 139 | + patch) new="${major}.${minor}.$((patch + 1))";; |
| 140 | + release) new="${major}.${minor}.${patch}";; |
| 141 | + prerel) new=$(validate-version "${major}.${minor}.${patch}-${sub_version}");; |
| 142 | + build) new=$(validate-version "${major}.${minor}.${patch}${prere}+${sub_version}");; |
| 143 | + *) usage-help ;; |
| 144 | + esac |
| 145 | + |
| 146 | + echo "$new" |
| 147 | + exit 0 |
| 148 | +} |
| 149 | + |
| 150 | +function command-compare { |
| 151 | + local v; local v_; |
| 152 | + |
| 153 | + case $# in |
| 154 | + 2) v=$(validate-version "$1"); v_=$(validate-version "$2") ;; |
| 155 | + *) usage-help ;; |
| 156 | + esac |
| 157 | + |
| 158 | + compare-version "$v" "$v_" |
| 159 | + exit 0 |
| 160 | +} |
| 161 | + |
| 162 | + |
| 163 | +# shellcheck disable=SC2034 |
| 164 | +function command-get { |
| 165 | + local part version |
| 166 | + |
| 167 | + if [[ "$#" -ne "2" ]] || [[ -z "$1" ]] || [[ -z "$2" ]]; then |
| 168 | + usage-help |
| 169 | + exit 0 |
| 170 | + fi |
| 171 | + |
| 172 | + part="$1" |
| 173 | + version="$2" |
| 174 | + |
| 175 | + validate-version "$version" parts |
| 176 | + local major="${parts[0]}" |
| 177 | + local minor="${parts[1]}" |
| 178 | + local patch="${parts[2]}" |
| 179 | + local prerel="${parts[3]:1}" |
| 180 | + local build="${parts[4]:1}" |
| 181 | + |
| 182 | + case "$part" in |
| 183 | + major|minor|patch|release|prerel|build) echo "${!part}" ;; |
| 184 | + *) usage-help ;; |
| 185 | + esac |
| 186 | + |
| 187 | + exit 0 |
| 188 | +} |
| 189 | + |
| 190 | +case $# in |
| 191 | + 0) echo "Unknown command: $*"; usage-help;; |
| 192 | +esac |
| 193 | + |
| 194 | +case $1 in |
| 195 | + --help|-h) echo -e "$USAGE"; exit 0;; |
| 196 | + --version|-v) usage-version ;; |
| 197 | + bump) shift; command-bump "$@";; |
| 198 | + get) shift; command-get "$@";; |
| 199 | + compare) shift; command-compare "$@";; |
| 200 | + *) echo "Unknown arguments: $*"; usage-help;; |
| 201 | +esac |
0 commit comments