|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# This script is copied from the depot repo; edit it there, not in the destination repo. |
| 4 | + |
| 5 | +# Set the semver in various files. |
| 6 | + |
| 7 | +# Always performs sanity checking: |
| 8 | +# - There must be at least one version file. |
| 9 | +# - The version must be a valid semver. |
| 10 | +# - The version must not be 0.0.0. |
| 11 | +# - After running, altered files must already be under Git control, and they must be only the version files we know how to handle, |
| 12 | +# and modifications must be 0 or 1 line changes. |
| 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 | +if [ $# -ne 1 ] ; then |
| 20 | + echo "Usage: $0 version" 1>&2 |
| 21 | + exit 1 |
| 22 | +fi |
| 23 | +NEWVERS="$1" |
| 24 | + |
| 25 | +# Sanity check: Ignoring any pre-release info, version must not be 0.0.0. |
| 26 | +if [ "${NEWVERS/-*/}" = "0.0.0" ] ; then |
| 27 | + echo "Illegal zero version '${NEWVERS}'" 1>&2 |
| 28 | + exit 1 |
| 29 | +fi |
| 30 | +# Sanity check: Must be valid semver. |
| 31 | +if ! [[ ${NEWVERS} =~ ^(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 |
| 32 | + echo "Invalid version '${NEWVERS}'" 1>&2 |
| 33 | + exit 1 |
| 34 | +fi |
| 35 | + |
| 36 | +# Find the version files in this directory or its descendants, but don't recurse too deep. |
| 37 | +# This line must be kept in sync with "bump-version.get.sh". |
| 38 | +VERSFILES=$(find . -maxdepth 3 ! -path ./.git/\* | grep -v /node_modules/ | grep -E '.*/(version|Cargo.toml|package.json|pom.xml|version.sbt)$') |
| 39 | + |
| 40 | +# Edit the version files. |
| 41 | +for FILE in ${VERSFILES} ; do |
| 42 | + DIR=$(dirname "${FILE}") |
| 43 | + case $(basename "${FILE}") in |
| 44 | + version) |
| 45 | + echo "${NEWVERS}" > "${FILE}" |
| 46 | + ;; |
| 47 | + |
| 48 | + Cargo.toml) |
| 49 | + sed 's/^version = ".*"$/version = "'"${NEWVERS}"'"/' "${FILE}" > "${FILE}.tmp" |
| 50 | + mv "${FILE}.tmp" "${FILE}" |
| 51 | + |
| 52 | + # If there's a Cargo.lock, update it also. |
| 53 | + if [ -f "${DIR}/Cargo.lock" ] ; then |
| 54 | + CARGO_LOCKS="${CARGO_LOCKS} ${DIR}" |
| 55 | + fi |
| 56 | + ;; |
| 57 | + |
| 58 | + package.json) |
| 59 | + if [ "$(dirname "${FILE}")" = "." ] ; then |
| 60 | + # This is the root package.json, so we want .version. |
| 61 | + jq --indent 4 ".version=\"${NEWVERS}\"" "${FILE}" > "${FILE}.new" |
| 62 | + else |
| 63 | + # We already know the root package name from above, so reuse that here. |
| 64 | + jq --indent 4 ".dependencies[\"${ROOTJSNAME}\"]=\"^${NEWVERS}\"" "${FILE}" > "${FILE}.new" |
| 65 | + fi |
| 66 | + mv "${FILE}.new" "${FILE}" |
| 67 | + ;; |
| 68 | + |
| 69 | + pom.xml) |
| 70 | + # Replace -foo with -SNAPSHOT to be compatible with Java conventions. |
| 71 | + JAVAVERS="${NEWVERS/-*/-SNAPSHOT}" |
| 72 | + |
| 73 | + if [ "$(dirname "${FILE}")" = "." ] ; then |
| 74 | + # This is the root pom.xml, so we want /m:project/m:version. |
| 75 | + xmlstarlet ed -L -P -N m="http://maven.apache.org/POM/4.0.0" -u "/m:project/m:version" -v "${JAVAVERS}" "${FILE}" |
| 76 | + else |
| 77 | + # We've already computed our XPATH expression above, so reuse that here. |
| 78 | + xmlstarlet ed -L -P -N m="http://maven.apache.org/POM/4.0.0" -u "${XPATH}" -v "${JAVAVERS}" "${FILE}" |
| 79 | + fi |
| 80 | + ;; |
| 81 | + |
| 82 | + version.sbt) |
| 83 | + # Replace -foo with -SNAPSHOT to be compatible with Java conventions. |
| 84 | + # Disabling this logic to work with cmk-s3-proxy. Since we only use bump-version to publish our scala containers, not our |
| 85 | + # scala libs, the -SNAPSHOT suffix isn't an important convention. |
| 86 | + # JAVAVERS="${NEWVERS/-*/-SNAPSHOT}" |
| 87 | + JAVAVERS="${NEWVERS}" |
| 88 | + |
| 89 | + # The file might use the old, deprecated syntax or the newer syntax: |
| 90 | + # version in ThisBuild := "1.2.3-SNAPSHOT" |
| 91 | + # ThisBuild / version := "1.2.3-SNAPSHOT" |
| 92 | + sed 's,^ThisBuild / version := ".*"$,ThisBuild / version := "'"${JAVAVERS}"'",' "${FILE}" > "${FILE}.tmp" |
| 93 | + sed 's,^version in ThisBuild := ".*"$,ThisBuild / version := "'"${JAVAVERS}"'",' "${FILE}.tmp" > "${FILE}" |
| 94 | + rm "${FILE}.tmp" |
| 95 | + ;; |
| 96 | + |
| 97 | + *) |
| 98 | + echo "Can't edit '${FILE}' with new version" 1>&2 |
| 99 | + exit 1 |
| 100 | + esac |
| 101 | + |
| 102 | + # Add it to git. |
| 103 | + git add "${FILE}" |
| 104 | + # Verify that we've changed zero or one line. |
| 105 | + git diff --cached -w --numstat "${FILE}" > /tmp/diffcount |
| 106 | + if [ -s /tmp/diffcount ] ; then |
| 107 | + # shellcheck disable=SC2034 |
| 108 | + read -r ADDED REMOVED FILENAME < /tmp/diffcount |
| 109 | + if [ "${ADDED}" -ne 1 ] || [ "${REMOVED}" -ne 1 ] ; then |
| 110 | + echo "Changes to '${FILE}' must be zero or one line, but observed edits are:" 1>&2 |
| 111 | + git diff --cached "${FILE}" 1>&2 |
| 112 | + exit 1 |
| 113 | + fi |
| 114 | + fi |
| 115 | +done |
| 116 | + |
| 117 | +# If there are Cargo.lock files, we need to run "cargo fetch" after all the Cargo.toml files have been edited. |
| 118 | +for DIR in ${CARGO_LOCKS} ; do |
| 119 | + ( cd "${DIR}" && cargo fetch ) |
| 120 | + git add "${DIR}/Cargo.lock" |
| 121 | +done |
| 122 | + |
| 123 | +# Look for files that have been changed, but that we haven't told git about. |
| 124 | +echo "Checking for modified but untracked files:" |
| 125 | +if git status -s | grep -qEv '^M ' ; then |
| 126 | + echo "Modified but untracked files:" 1>&2 |
| 127 | + git status -s | grep -Ev '^M ' 1>&2 |
| 128 | + echo "This probably means '$0' modified a file but forgot to 'git add' it." 1>&2 |
| 129 | + exit 1 |
| 130 | +fi |
0 commit comments