Skip to content

Instantly share code, notes, and snippets.

@colby
Created January 16, 2017 19:18
Show Gist options
  • Save colby/e6d0a5e84ddcd60810d1d69c5be53a1c to your computer and use it in GitHub Desktop.
Save colby/e6d0a5e84ddcd60810d1d69c5be53a1c to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
platform=$(uname)
if [ "$platform" == 'Darwin' ]
then
sed=$(which gsed || errcho "install gnu-sed")
else
sed=$(which sed)
fi
semver_regex="^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(\-[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?(\+[0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*)?$"
usage="\
Usage:
$0
$0 show
$0 major
Commands:
show fetch and show current versions.
major bump the major level of current version.
minor bump the minor level of current version.
patch bump the patch level of current version."
function errcho {
>&2 echo -e "error: $*"
exit 1
}
function update {
git fetch --tags
}
function show {
echo "Current git tag: $current_tag"
echo "Current meta version: $current_version"
}
function validate {
local version="$1"
if [[ "$version" =~ $semver_regex ]]
then
apply "$version"
else
errcho "version $version does not follow semver standard"
fi
}
function bump {
local level="$1"
IFS=$'.'
set -- $current_version
local major=$1
local minor=$2
local patch=$3
case $level in
major) ((major++));;
minor) ((minor++));;
patch) ((patch++));;
esac
validate "$major.$minor.$patch"
}
function apply {
local new_version="$1"
$sed -i "s/$current_version/$new_version/" "metadata.rb"
(git add metadata.rb && git commit -m"bump to $new_version") || true
git tag "$new_version"
git push origin
git push origin --tags
}
if [ ! -f metadata.rb ]
then
errcho "metadata.rb file missing"
fi
current_version=$(grep -E '^version' metadata.rb | cut -d"'" -f2 || errcho "a cookbook version was not found")
current_tag=$(update | git describe --abbrev=0 --tags || echo 'none found')
if [ -z "$1" ]
# there are no args
then
show
read -r -p "enter a new semver version: " answer
validate "$answer"
# there are args
else
# first arg was semver
if [[ "$1" =~ $semver_regex ]]
then
apply "$1"
else
# first arg is either a known command or throw usage
case "$1" in
show) show;;
major|minor|patch) bump "$1";;
*) echo "$usage"; exit 1;;
esac
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment