Skip to content

Instantly share code, notes, and snippets.

@twolfson
Created April 13, 2018 23:34
Show Gist options
  • Save twolfson/9febac60c0c8af907a1c6f7ed13ae1b0 to your computer and use it in GitHub Desktop.
Save twolfson/9febac60c0c8af907a1c6f7ed13ae1b0 to your computer and use it in GitHub Desktop.
Increment README version script
#!/usr/bin/env bash
# Exit on first error, unset variable, or pipe failure
set -euo pipefail
# Define our constants
README_FILEPATH="README.md"
# Resolve our version being deployed
# **1.0.0** -> 1.0.0
version="$(grep -E "\*\*\d+\.\d+\.\d+\*\*" "${README_FILEPATH}" | sed "s/\*//g")"
# Break up our versions
# https://stackoverflow.com/a/6253883
# DEV: `cut` can return the original string if the delimiter wasn't found, so we compare patch to the version
major="$(echo "$version" | cut -d . -f 1)"
minor="$(echo "$version" | cut -d . -f 2)"
patch="$(echo "$version" | cut -d . -f 3)"
if test "$version" == "" || test "$major" == "" || test "$minor" == "" || test "$patch" == "" || test "$patch" == "$version"; then
echo "Unable to parse version \"${version}\"" 1>&2
exit 1
fi
# Determine our increment target
increment_target="${1:-}"
if test "$increment_target" == ""; then
increment_target="minor"
fi
# Increment our target version as requested
if test "$increment_target" == "major"; then
# 1.6.3 -> 2.0.0
major="$(($major + 1))"
minor="0"
patch="0"
elif test "$increment_target" == "minor"; then
# 1.6.3 -> 1.7.0
minor="$(($minor + 1))"
patch="0"
elif test "$increment_target" == "patch"; then
# 1.6.3 -> 1.6.4
patch="$(($patch + 1))"
else
echo "Unrecognized increment target \"${increment_target}\". Please specify \"major\", \"minor\", or \"patch\"" 1>&2
echo "Usage: $0 [increment_target]" 1>&2
exit 1
fi
# Set our new version in place
new_version="${major}.${minor}.${patch}"
sed -i "" -E "s/\*\*[0-9]+\.[0-9]+\.[0-9]+\*\*/**${new_version}**/" "${README_FILEPATH}"
# Output our new version
echo "$new_version"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment