|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Get or set the semver in various files. |
| 4 | + |
| 5 | +# If called with no args, return the current version. |
| 6 | +# If called with one arg, set the version to the new value. |
| 7 | + |
| 8 | +# Always performs sanity checking: |
| 9 | +# - There must be at least one version file. |
| 10 | +# - All version files must agree. (Ignoring the contents but not existence of pre-release version.) |
| 11 | +# - The version must be a valid semver. |
| 12 | +# - The version must not be 0.0.0. |
| 13 | + |
| 14 | +# If setting the version to $a.$b.$c-$pre, substitute "SNAPSHOT" for $pre in any Java-related files. |
| 15 | + |
| 16 | +set -e |
| 17 | + |
| 18 | +# Parse args |
| 19 | +case "$#" in |
| 20 | +"0") |
| 21 | + NEWVERS="" |
| 22 | + ;; |
| 23 | +"1") |
| 24 | + NEWVERS="$1" |
| 25 | + ;; |
| 26 | +"*") |
| 27 | + echo "Usage: $0 [version]" 1>&2 |
| 28 | + exit 1 |
| 29 | + ;; |
| 30 | +esac |
| 31 | + |
| 32 | +# Find the version files in this directory or its descendants, but don't recurse too deep. |
| 33 | +VERSFILES=$(find . -maxdepth 3 ! -path ./.git/\* | grep -E '.*/(version|Cargo.toml|package.json|pom.xml|version.sbt)$') |
| 34 | + |
| 35 | +# Do we have at least one? |
| 36 | +if [ -z "${VERSFILES}" ] ; then |
| 37 | + echo "No version files found; aborting" 1>&2 |
| 38 | + exit 1 |
| 39 | +fi |
| 40 | + |
| 41 | +# Read the versions. |
| 42 | +CURRENTVERS="" |
| 43 | +for FILE in ${VERSFILES} ; do |
| 44 | + # Parse each version file according to its type. |
| 45 | + case $(basename "${FILE}") in |
| 46 | + version) |
| 47 | + # It's a file to capture version info for generic things that don't have their own format. |
| 48 | + VERS=$(cat "${FILE}") |
| 49 | + ;; |
| 50 | + Cargo.toml) |
| 51 | + VERS=$(cargo metadata --manifest-path "${FILE}" --no-deps --offline --format-version 1 | jq -r '.packages[0].version') |
| 52 | + ;; |
| 53 | + package.json) |
| 54 | + if [ "$(dirname "${FILE}")" = "." ] ; then |
| 55 | + # This is the root package.json, so we want .version. |
| 56 | + VERS=$(jq -r '.version' < "${FILE}") |
| 57 | + else |
| 58 | + # This isn't the root package.json, so we assume it depends on the package declared in the root package.json. We need to |
| 59 | + # get the root package's name. |
| 60 | + ROOTJSNAME=$(jq -r '.name' < package.json) |
| 61 | + VERS=$(jq -r ".dependencies[\"${ROOTJSNAME}\"]" < "${FILE}") |
| 62 | + # Strip off any leading "^". |
| 63 | + VERS=${VERS/^/} |
| 64 | + fi |
| 65 | + ;; |
| 66 | + ./pom.xml) |
| 67 | + if [ "$(dirname "${FILE}")" = "." ] ; then |
| 68 | + # This is the root pom.xml, so we want /m:project/m:version. |
| 69 | + VERS=$(xmlstarlet sel -N m="http://maven.apache.org/POM/4.0.0" -t -v "/m:project/m:version" < "${FILE}") |
| 70 | + else |
| 71 | + # This isn't the root pom.xml, so we assume it depends on the package declared in the root pom.xml. We need to get the |
| 72 | + # root pom's artifactId. |
| 73 | + ROOTID=$(xmlstarlet sel -N m="http://maven.apache.org/POM/4.0.0" -t -v "/m:project/m:artifactId" < pom.xml) |
| 74 | + # Select /m:project/m:dependencies/m:dependency/m:version where it has a sibling m:artifactId with the correct value. |
| 75 | + XPATH="/m:project/m:dependencies/m:dependency[m:artifactId=\"${ROOTID}\"]/m:version" |
| 76 | + VERS=$(xmlstarlet sel -N m="http://maven.apache.org/POM/4.0.0" -t -v "${XPATH}" < "${FILE}") |
| 77 | + fi |
| 78 | + ;; |
| 79 | + version.sbt) |
| 80 | + VERS=$(sed -e 's/^[^"]*"//' -e 's/"$//' < "${FILE}") |
| 81 | + ;; |
| 82 | + *) |
| 83 | + echo "Can't parse '${FILE}' for version" 1>&2 |
| 84 | + exit 1 |
| 85 | + ;; |
| 86 | + esac |
| 87 | + |
| 88 | + if [ -z "${VERS}" ] ; then |
| 89 | + echo "Empty version from '${FILE}'" 1>&2 |
| 90 | + exit 1 |
| 91 | + fi |
| 92 | + |
| 93 | + # If this is the first parsed version file, then set current version. |
| 94 | + if [ -z "${CURRENTVERS}" ] ; then |
| 95 | + CURRENTVERS="${VERS}" |
| 96 | + fi |
| 97 | + |
| 98 | + # Compare this file's version to other files' version. Ignore anything after the "-" in a pre-release version, but keep the "-" |
| 99 | + # so a release version is unequal to a pre-release. |
| 100 | + if ! [ "${CURRENTVERS/-*/-}" = "${VERS/-*/-}" ] ; then |
| 101 | + echo "Version '${VERS}' in '${FILE}' doesn't match '${CURRENTVERS}' from others in '${VERSFILES}'" 1>&2 |
| 102 | + exit 1 |
| 103 | + fi |
| 104 | +done |
| 105 | + |
| 106 | +# Sanity check: Ignoring any pre-release info, version must not be 0.0.0. |
| 107 | +if [ "${CURRENTVERS/-*/}" = "0.0.0" ] ; then |
| 108 | + echo "Illegal zero version '${CURRENTVERS}'" 1>&2 |
| 109 | + exit 1 |
| 110 | +fi |
| 111 | +# Sanity check: Must be valid semver. |
| 112 | +if ! [[ ${CURRENTVERS} =~ ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-((0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*))*))?(\+([0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*))?$ ]] ; then |
| 113 | + echo "Invalid version '${CURRENTVERS}'" 1>&2 |
| 114 | + exit 1 |
| 115 | +fi |
| 116 | + |
| 117 | +# If we're just getting the current version, print it and exit successfully. |
| 118 | +if [ -z "${NEWVERS}" ] ; then |
| 119 | + echo "${CURRENTVERS}" |
| 120 | + exit 0 |
| 121 | +fi |
| 122 | + |
| 123 | +# If we reach this point, it means we're setting the version. |
| 124 | + |
| 125 | +# Edit the version files. |
| 126 | +for FILE in ${VERSFILES} ; do |
| 127 | + DIR=$(dirname "${FILE}") |
| 128 | + case $(basename "${FILE}") in |
| 129 | + version) |
| 130 | + echo "${NEWVERS}" > "${FILE}" |
| 131 | + ;; |
| 132 | + |
| 133 | + Cargo.toml) |
| 134 | + sed -i 's/^version = ".*"$/version = "'"${NEWVERS}"'"/' "${FILE}" |
| 135 | + |
| 136 | + # If there's a Cargo.lock, update it also. |
| 137 | + if [ -f "${DIR}/Cargo.lock" ] ; then |
| 138 | + ( cd "${DIR}" && cargo fetch ) |
| 139 | + git add "${DIR}/Cargo.lock" |
| 140 | + fi |
| 141 | + ;; |
| 142 | + |
| 143 | + package.json) |
| 144 | + if [ "$(dirname "${FILE}")" = "." ] ; then |
| 145 | + # This is the root package.json, so we want .version. |
| 146 | + jq --indent 4 ".version=\"${NEWVERS}\"" "${FILE}" > "${FILE}.new" |
| 147 | + else |
| 148 | + # We already know the root package name from above, so reuse that here. |
| 149 | + jq --indent 4 ".dependencies[\"${ROOTJSNAME}\"]=\"^${NEWVERS}\"" "${FILE}" > "${FILE}.new" |
| 150 | + fi |
| 151 | + mv "${FILE}.new" "${FILE}" |
| 152 | + ;; |
| 153 | + |
| 154 | + pom.xml) |
| 155 | + # Replace -foo with -SNAPSHOT to be compatible with Java conventions. |
| 156 | + JAVAVERS="${NEWVERS/-*/-SNAPSHOT}" |
| 157 | + |
| 158 | + if [ "$(dirname "${FILE}")" = "." ] ; then |
| 159 | + # This is the root pom.xml, so we want /m:project/m:version. |
| 160 | + xmlstarlet ed -L -P -N m="http://maven.apache.org/POM/4.0.0" -u "/m:project/m:version" -v "${JAVAVERS}" "${FILE}" |
| 161 | + else |
| 162 | + # We've already computed our XPATH expression above, so reuse that here. |
| 163 | + xmlstarlet ed -L -P -N m="http://maven.apache.org/POM/4.0.0" -u "${XPATH}" -v "${JAVAVERS}" "${FILE}" |
| 164 | + fi |
| 165 | + ;; |
| 166 | + |
| 167 | + version.sbt) |
| 168 | + # Replace -foo with -SNAPSHOT to be compatible with Java conventions. |
| 169 | + JAVAVERS="${NEWVERS/-*/-SNAPSHOT}" |
| 170 | + |
| 171 | + # The file might use the old, deprecated syntax or the newer syntax: |
| 172 | + # version in ThisBuild := "1.2.3-SNAPSHOT" |
| 173 | + # ThisBuild / version := "1.2.3-SNAPSHOT" |
| 174 | + sed -i 's,^ThisBuild / version := ".*"$,ThisBuild / version := "'"${JAVAVERS}"'",' "${FILE}" |
| 175 | + sed -i 's,^version in ThisBuild := ".*"$,ThisBuild / version := "'"${JAVAVERS}"'",' "${FILE}" |
| 176 | + ;; |
| 177 | + |
| 178 | + *) |
| 179 | + echo "Can't edit '${FILE}' with new version" 1>&2 |
| 180 | + exit 1 |
| 181 | + esac |
| 182 | + |
| 183 | + # Add it to git. |
| 184 | + git add "${FILE}" |
| 185 | + # Verify that we've changed zero or one line. |
| 186 | + git diff --cached -w --numstat "${FILE}" > /tmp/diffcount |
| 187 | + if [ -s /tmp/diffcount ] ; then |
| 188 | + # shellcheck disable=SC2034 |
| 189 | + read -r ADDED REMOVED FILENAME < /tmp/diffcount |
| 190 | + if [ "${ADDED}" -ne 1 ] || [ "${REMOVED}" -ne 1 ] ; then |
| 191 | + echo "Changes to '${FILE}' must be zero or one line, but observed edits are:" 1>&2 |
| 192 | + git diff --cached "${FILE}" 1>&2 |
| 193 | + exit 1 |
| 194 | + fi |
| 195 | + fi |
| 196 | +done |
| 197 | + |
| 198 | +# Look for files that have been changed, but that we haven't told git about. |
| 199 | +echo "Checking for modified but untracked files:" |
| 200 | +if git status -s | grep -qEv '^M ' ; then |
| 201 | + echo "Modified but untracked files:" 1>&2 |
| 202 | + git status -s | grep -Ev '^M ' 1>&2 |
| 203 | + echo "This probably means '$0' modified a file but forgot to 'git add' it." 1>&2 |
| 204 | + exit 1 |
| 205 | +fi |
0 commit comments