Last active
January 15, 2016 17:12
-
-
Save markusfisch/1576205 to your computer and use it in GitHub Desktop.
Update version number of a project. Useful for sources with autotools and doxygen.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# check for required tools | |
for X in find grep sed mktemp | |
do | |
which $X &>/dev/null || { | |
echo "error: missing $X" >&2 | |
exit 1 | |
} | |
done | |
[ "$1" == -h ] && { | |
echo "usage: ${0##*/} [-h] [release|upgrade|update|N[.N[.N]]]" | |
exit | |
} | |
IGNORE=${IGNORE:-ChangeLog NEWS} | |
SRC=${SRC:-configure} | |
[ "$VERSION" ] || [ -r "$SRC" ] && { | |
VERSION=$(grep 'VERSION=' "$SRC") | |
VERSION=${VERSION##*=} | |
} | |
[ "$VERSION" ] || { | |
echo "error: no version number found or given" >&2 | |
exit 1 | |
} | |
# trim string | |
# shellcheck disable=SC2116 | |
# shellcheck disable=SC2086 | |
VERSION=$(echo $VERSION) | |
[[ $VERSION == [0-9]*\.[0-9]*\.[0-9]* ]] || { | |
echo "error: unsupported version format, expected x.x.x" >&2 | |
exit 1 | |
} | |
RELEASE=${VERSION%%.*} | |
UPGRADE=${VERSION%.*}; UPGRADE=${UPGRADE#*.} | |
UPDATE=${VERSION##*.} | |
ARG=${1:-update} | |
case "$ARG" in | |
r*) | |
(( ++RELEASE )) | |
NEW=$RELEASE.0.0 | |
;; | |
upg*) | |
(( ++UPGRADE )) | |
NEW=$RELEASE.$UPGRADE.0 | |
;; | |
upd*) | |
(( ++UPDATE )) | |
NEW=$RELEASE.$UPGRADE.$UPDATE | |
;; | |
[0-9]*\.[0-9]*\.[0-9]*) | |
NEW=$ARG | |
;; | |
[0-9]*\.[0-9]*) | |
NEW=$ARG.0 | |
;; | |
[0-9]*) | |
NEW=$ARG.0.0 | |
;; | |
*) | |
echo "error: unknown argument $ARG" >&2 | |
exit 1 | |
;; | |
esac | |
echo "updating $VERSION to $NEW" | |
# escape the dots | |
VERSION=${VERSION//\./\\.} | |
# use find because grep on OSX/BSD doesn't know --recursive | |
find . -type f -exec grep -l "$VERSION" {} \; | while read -r | |
do | |
# alternatively, use --exclude= with GNU grep if available | |
[[ $IGNORE == *${REPLY##*/}* ]] && continue | |
# ignore hidden files and directories | |
[[ $REPLY == */\.* ]] && continue | |
TMP=$(mktemp "$REPLY.XXXXXXXXXX") | |
if sed -e "s_${VERSION}_${NEW}_g" < "$REPLY" > "$TMP" && | |
mv "$TMP" "$REPLY" | |
then | |
echo "$REPLY" | |
else | |
echo "error: cannot process $REPLY" >&2 | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment