Skip to content

Instantly share code, notes, and snippets.

@invalidusrname
Created October 8, 2015 20:08
Show Gist options
  • Save invalidusrname/357136829f2f04b0d895 to your computer and use it in GitHub Desktop.
Save invalidusrname/357136829f2f04b0d895 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
usage(){
echo "Usage: ./script/release [current,major,minor,patch,pre] or ./script/release custom [version_number]"
exit 1
}
function version_release
{
mkdir -p config/initializers
echo -e " info: writing: '$1' to RELEASE and 001_version_helper.rb"
echo -e "ENV['CURRENT_VERSION'] = \"$1\"" > config/initializers/001_version_helper.rb
echo -e "ENV['CURRENT_SHA'] = \"\`git rev-parse HEAD\`\"" >> config/initializers/001_version_helper.rb
echo -e "# `date`" >> config/initializers/001_version_helper.rb
git add config/initializers/001_version_helper.rb
echo -e "$1" > RELEASE
git add RELEASE
git commit -am "Updating version_helper with latest tagged version $1"
echo " info: deployed version $VERSION is not yet tagged; tagging now as $1"
git tag -a $1 -m "Deployed $(LANG=en_US.UTF-8; date)."
}
function get_version_parts
{
MAJOR=$(echo $1 | cut -d '.' -f 1)
MINOR=$(echo $1 | cut -d '.' -f 2)
PATCH=$(echo $1 | cut -d '.' -f 3 | cut -d '-' -f 1)
if [[ $1 == *-* ]]; then
PRE=$(echo $1 | cut -d '-' -f 2 | cut -d '.' -f 1,2)
PRE_NAME=$(echo $PRE | cut -d '.' -f 1)
PRE_VERSION=$(echo $PRE | cut -d '.' -f 2)
else
PRE=''
PRE_NAME=''
PRE_VERSION=''
fi
if [[ $PRE_VERSION == $PRE ]]; then
PRE_VERSION=''
fi
}
function print_version_info
{
echo '-------------------------'
echo "VERSION: $VERSION"
echo "RELEASE: $RELEASE"
echo "MAJOR: $MAJOR"
echo "MINOR: $MINOR"
echo "PATCH: $PATCH"
echo "PRE: $PRE"
echo "PRE_NAME: $PRE_NAME"
echo "PRE_VERSION: $PRE_VERSION"
echo '-------------------------'
}
function print_current_version
{
echo -e "current release: $(cat RELEASE)"
}
[[ $# -eq 0 ]] && usage
if [[ ! -f RELEASE ]]; then
echo "no RELEASE file so creating one with version 1.0.0..."
echo "1.0.0" > RELEASE
fi
RELEASE=$(cat RELEASE | cut -d ' ' -f 1)
if [[ -z $RELEASE ]]; then
echo "NO RELEASE. add release to ./RELEASE"
fi
if [[ $# -eq 1 ]]; then
if [ "$1" = 'current' ]; then
print_current_version
exit
fi
print_current_version
VERSION=$(cat RELEASE)
get_version_parts $VERSION
print_version_info
# FIXME: handle cases when other pre tags are used (10.1.2-alpha)
if [[ $VERSION =~ beta ]] && [[ "$1" != 'pre' ]]; then
VERSION=$(echo $VERSION | sed "s/-beta.*//")
get_version_parts $VERSION
elif [ "$1" = 'major' ]; then
MAJOR=$((MAJOR+1))
VERSION="$MAJOR.0.0"
elif [ "$1" = 'minor' ]; then
MINOR=$((MINOR+1))
VERSION="$MAJOR.$MINOR.0"
elif [ "$1" = 'patch' ]; then
PATCH=$((PATCH+1))
VERSION="$MAJOR.$MINOR.$PATCH"
elif [ "$1" = 'pre' ]; then
if [[ "$PRE_NAME" = '' ]]; then
echo -n "enter prerelease version number: "
read VERSION
get_version_parts $VERSION
PRE_VERSION=''
PRE="-beta"
else
PRE_VERSION=$((PRE_VERSION+1))
PRE="-$PRE_NAME.$PRE_VERSION"
fi
VERSION="$MAJOR.$MINOR.$PATCH$PRE"
else
echo "error: only major,minor,patch,pre accepeted"
exit 1
fi
else
if [[ "$1" = 'custom' ]]; then
VERSION="$2"
fi
fi
get_version_parts $VERSION
print_version_info
if [ ! $(git describe 2> /dev/null) ]; then
echo "warning: no previous git tags, creating first version: $VERSION"
version_release $VERSION
elif [ ! $(git describe --exact-match $VERSION 2> /dev/null) ]; then
version_release $VERSION
else
echo "error: local git tag and remote REVISION match, can't version and create tag"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment