Skip to content

Instantly share code, notes, and snippets.

@johnbuhay
Last active July 21, 2016 15:32
Show Gist options
  • Save johnbuhay/1fa0a7242b1b94baf689409f00ee8e43 to your computer and use it in GitHub Desktop.
Save johnbuhay/1fa0a7242b1b94baf689409f00ee8e43 to your computer and use it in GitHub Desktop.
shell scripts for versioning
#!/usr/bin/env bash
#set -x # for debugging
function print_help(){
read -r -d '' HELP_MSG << EOM
#
# See semver.org for details
# Commands look like
#
# update-version.sh \$1 = (major | minor | patch | snapshot | final) \$2 = (path/to/version.txt | defaults to ./version.txt)
#
# major: updates X.0.0
# minor: update 0.X.0
# patch: updates 0.0.X
# snapshot: adds -SNAPSHOT: 0.0.0-SNAPSHOT
# final: removes -SNAPSHOT: 0.0.0
EOM
echo "$HELP_MSG" # remember the quotes around the variable otherwise you won't see the newline characters
exit 0
}
VERSION_FILE=${2:-version.txt}
function version_to_var(){
FILE=$(cat $VERSION_FILE)
echo $FILE
}
function major(){
echo "Changing Major"
MAJOR=$(/usr/bin/grep -o "^[[:digit:]]\+" $VERSION_FILE)
NEW_MAJOR=$((MAJOR + 1))
/usr/bin/sed -i '' s/^$MAJOR/$NEW_MAJOR/ $VERSION_FILE
reset minor
reset patch
}
function get_minor(){
/usr/bin/grep -o "\.[[:digit:]]\+\." $VERSION_FILE | /usr/bin/grep -o "[[:digit:]]\+"
}
function set_minor(){
/usr/bin/sed -i '' s/\.$1\./.$2./ $VERSION_FILE
}
function minor(){
echo "Changing Minor"
MINOR=$(get_minor)
NEW_MINOR=$((MINOR + 1))
set_minor $MINOR $NEW_MINOR
reset patch
}
function get_patch(){
/usr/bin/grep -o "[[:digit:]]\+\(\-SNAPSHOT\)\?\$" $VERSION_FILE
}
function set_patch(){
/usr/bin/sed -i '' s/\.$1$/.$2/ $VERSION_FILE
}
function patch(){
echo "Changing Patch"
PATCH=$(get_patch)
if [ $(/usr/bin/grep "\-SNAPSHOT\$" $VERSION_FILE) ]; then
PATCH_NUM=$(echo $PATCH | /usr/bin/grep -o "^[[:digit:]]\+" )
NEW_PATCH=$((PATCH_NUM + 1))-SNAPSHOT
else
NEW_PATCH=$((PATCH + 1))
fi
set_patch $PATCH $NEW_PATCH
}
function snapshot(){
if [ $(/usr/bin/grep "\-SNAPSHOT\$" $VERSION_FILE) ]; then
echo "Already a Snapshot"
else
echo "Changing into Snapshot"
echo -n "$(version_to_var)-SNAPSHOT" > $VERSION_FILE
fi
}
function final(){
echo "Changing into Final"
/usr/bin/sed -i '' s/\-SNAPSHOT$// $VERSION_FILE
}
function reset(){
case $1 in
minor)
# set minor to 0
set_minor $(get_minor) 0
echo "Reset Minor"
;;
patch)
# set patch to 0
if [ $(/usr/bin/grep "\-SNAPSHOT\$" $VERSION_FILE) ]; then
IF_SNAPSHOT=-SNAPSHOT
fi
set_patch $(get_patch) 0$IF_SNAPSHOT
echo "Reset Patch"
;;
esac
}
function main(){
if [ ! -f "$VERSION_FILE" ]; then
echo "Version file not found"
exit 1
fi
case $1 in
major)
major
# shift
;;
minor)
minor
;;
patch|"")
# defaulting change mechanism to patch
patch
;;
snapshot|SNAPSHOT)
snapshot
;;
final)
final
;;
*)
# unknown option
print_help
;;
esac
}
main $@
echo "Version set to $(cat $VERSION_FILE)"
#!/usr/bin/env bash
# set -x # for debugging
function print_help(){
read -r -d '' HELP_MSG << EOM
#
# See semver.org for details
# Commands look like
#
# update-version.sh \$1 = (major | minor | patch | snapshot | final) \$2 = (path/to/version.txt | defaults to ./version.txt)
#
# major: updates X.0.0
# minor: update 0.X.0
# patch: updates 0.0.X
# snapshot: adds -SNAPSHOT: 0.0.0-SNAPSHOT
# final: removes -SNAPSHOT: 0.0.0
EOM
echo "$HELP_MSG" # remember the quotes around the variable otherwise you won't see the newline characters
exit 0
}
VERSION_FILE=${2:-./version.txt}
function version_to_var(){
FILE=$(cat $VERSION_FILE)
echo $FILE
}
function major(){
echo "Changing Major"
MAJOR=$(version_to_var | grep -oP "^\d+")
NEW_MAJOR=$((MAJOR + 1))
sed -i -r s/^[[:digit:]]+/$NEW_MAJOR/ $VERSION_FILE
reset minor
reset patch
}
function get_minor(){
version_to_var | grep -oP "\.\d+\." | grep -oP "\d+"
}
function set_minor(){
sed -i -r s/\.$1\./.$2./ $VERSION_FILE
}
function minor(){
echo "Changing Minor"
MINOR=$(get_minor)
NEW_MINOR=$((MINOR + 1))
set_minor $MINOR $NEW_MINOR
reset patch
}
function get_patch(){
version_to_var | grep -oP "\d+(-SNAPSHOT)?$"
}
function set_patch(){
sed -i -r s/\.$1$/.$2/ $VERSION_FILE
}
function patch(){
echo "Changing Patch"
PATCH=$(get_patch)
if [ $(grep -P '\-SNAPSHOT$' $VERSION_FILE) ]; then
PATCH_NUM=$(echo $PATCH | grep -oP "^\d+" )
NEW_PATCH=$((PATCH_NUM + 1))-SNAPSHOT
else
NEW_PATCH=$((PATCH + 1))
fi
set_patch $PATCH $NEW_PATCH
}
function snapshot(){
echo "Changing into Snapshot"
echo -n "$(version_to_var)-SNAPSHOT" > $VERSION_FILE
}
function final(){
echo "Changing into Final"
sed -i 's/-SNAPSHOT$//' $VERSION_FILE
}
function reset(){
case $1 in
minor)
# set minor to 0
set_minor $(get_minor) 0
echo "Reset Minor"
;;
patch)
# set patch to 0
if [ $(grep -P '\-SNAPSHOT$' $VERSION_FILE) ]; then
IF_SNAPSHOT=-SNAPSHOT
fi
set_patch $(get_patch) 0$IF_SNAPSHOT
echo "Reset Patch"
;;
esac
}
function main(){
if [ ! -f "$VERSION_FILE" ]; then
echo "Version file not found"
exit 1
fi
case $1 in
major)
major
# shift
;;
minor)
minor
;;
patch|"")
# defaulting change mechanism to patch
patch
;;
snapshot|SNAPSHOT)
snapshot
;;
final)
final
;;
*)
# unknown option
print_help
;;
esac
}
main $@
echo "Version set to $(cat $VERSION_FILE)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment