diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c8f50f7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +npm-debug.log diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..5d17b86 --- /dev/null +++ b/Makefile @@ -0,0 +1,16 @@ +SHELL := /usr/bin/env bash + +release-major: + npm version major -m "Release %s" + git push + npm publish + +release-minor: + npm version minor -m "Release %s" + git push + npm publish + +release-patch: + npm version patch -m "Release %s" + git push + npm publish diff --git a/package.json b/package.json new file mode 100644 index 0000000..1587a66 --- /dev/null +++ b/package.json @@ -0,0 +1,3 @@ +{ + "version": "1.0.0" +} diff --git a/src/bump.sh b/src/bump.sh new file mode 100755 index 0000000..a27e108 --- /dev/null +++ b/src/bump.sh @@ -0,0 +1,131 @@ +#!/usr/bin/env bash +# Copyright (c) 2014, Transloadit Ltd. +# +# This file: +# +# - Bumps a semantic version as specified in first argument +# - Or: Bumps a semantic version in a file as specified in first argument +# - Returns the version if no levelName is provided in second argument +# - Only supports Go files ending in 'var Version = ...' +# +# Run as: +# +# ./bump.sh 0.0.1 patch +# ./bump.sh ./VERSION patch +# ./bump.sh ./VERSION patch +# ./bump.sh ./VERSION major 1 +# ./bump.sh ./version.go patch 2 +# +# Returns: +# +# v0.0.1 +# +# Requires: +# +# - gsed on OSX (brew install gnu-sed) +# +# Authors: +# +# - Kevin van Zonneveld + +set -o pipefail +set -o errexit +set -o nounset +# set -o xtrace + +# Set magic variables for current FILE & DIR +__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +__root="$(cd "$(dirname "${__dir}")" && pwd)" +__file="${__dir}/$(basename "${BASH_SOURCE[0]}")" +__base="$(basename ${__file} .sh)" + +gsed="" +[ -n "$(which sed)" ] && gsed="$(which sed)" +[ -n "$(which gsed)" ] && gsed="$(which gsed)" + + +. ${__dir}/semver.sh + +function readFromFile() { + local filepath="${1}" + local extension="${filepath##*.}" + + if [ "${extension}" = "go" ]; then + curVersion="$(awk -F'"' '/^var Version = / {print $2}' "${filepath}" | tail -n1)" || true + else + curVersion="$(echo $(cat "${filepath}"))" || true + fi + + if [ -z "${curVersion}" ]; then + curVersion="v0.0.0" + fi + + echo "${curVersion}" +} + +function writeToFile() { + local filepath="${1}" + local newVersion="${2}" + local extension="${filepath##*.}" + + if [ "${extension}" = "go" ]; then + buf="$(cat "${filepath}" |egrep -v '^var Version = ')" || true + echo -e "${buf}\nvar Version = \"${newVersion}\"" > "${filepath}" + else + echo "${newVersion}" > "${filepath}" + fi +} + +function bump() { + local version="${1}" + local levelName="${2}" + local bump="${3}" + + local major=0 + local minor=0 + local patch=0 + local special="" + + local newVersion="" + + semverParseInto "${version}" major minor patch special + + if [ "${levelName}" = "major" ]; then + let "major = major + ${bump}" + minor=0 + patch=0 + special="" + fi + if [ "${levelName}" = "minor" ]; then + let "minor = minor + ${bump}" + patch=0 + special="" + fi + if [ "${levelName}" = "patch" ]; then + let "patch = patch + ${bump}" + special="" + fi + if [ "${levelName}" = "special" ]; then + special="${bump}" + fi + + newVersion="v${major}.${minor}.${patch}" + if [ -n "${special}" ]; then + newVersion=".${newVersion}" + fi + echo "${newVersion}" +} + +if [ -f "${1}" ]; then + filepath="${1}" + curVersion="$(readFromFile "${filepath}")" +else + curVersion="${1}" +fi + +newVersion=$(bump "${curVersion}" "${2:-}" "${3:-1}") +echo "${newVersion}" + +if [ -n "${filepath:-}" ]; then + writeToFile "${filepath}" "${newVersion}" +fi diff --git a/src/semver.sh b/src/semver.sh new file mode 100755 index 0000000..f18a784 --- /dev/null +++ b/src/semver.sh @@ -0,0 +1,132 @@ +#!/usr/bin/env sh +# From: https://github.com/cloudflare/semver_bash +# https://raw.githubusercontent.com/cloudflare/semver_bash/master/semver.sh + +function semverParseInto() { + local RE='[^0-9]*\([0-9]*\)[.]\([0-9]*\)[.]\([0-9]*\)\([0-9A-Za-z-]*\)' + #MAJOR + eval $2=`echo $1 | sed -e "s#$RE#\1#"` + #MINOR + eval $3=`echo $1 | sed -e "s#$RE#\2#"` + #MINOR + eval $4=`echo $1 | sed -e "s#$RE#\3#"` + #SPECIAL + eval $5=`echo $1 | sed -e "s#$RE#\4#"` +} + +function semverEQ() { + local MAJOR_A=0 + local MINOR_A=0 + local PATCH_A=0 + local SPECIAL_A=0 + + local MAJOR_B=0 + local MINOR_B=0 + local PATCH_B=0 + local SPECIAL_B=0 + + semverParseInto $1 MAJOR_A MINOR_A PATCH_A SPECIAL_A + semverParseInto $2 MAJOR_B MINOR_B PATCH_B SPECIAL_B + + if [ $MAJOR_A -ne $MAJOR_B ]; then + return 1 + fi + + if [ $MINOR_A -ne $MINOR_B ]; then + return 1 + fi + + if [ $PATCH_A -ne $PATCH_B ]; then + return 1 + fi + + if [[ "_$SPECIAL_A" != "_$SPECIAL_B" ]]; then + return 1 + fi + + + return 0 + +} + +function semverLT() { + local MAJOR_A=0 + local MINOR_A=0 + local PATCH_A=0 + local SPECIAL_A=0 + + local MAJOR_B=0 + local MINOR_B=0 + local PATCH_B=0 + local SPECIAL_B=0 + + semverParseInto $1 MAJOR_A MINOR_A PATCH_A SPECIAL_A + semverParseInto $2 MAJOR_B MINOR_B PATCH_B SPECIAL_B + + if [ $MAJOR_A -lt $MAJOR_B ]; then + return 0 + fi + + if [[ $MAJOR_A -le $MAJOR_B && $MINOR_A -lt $MINOR_B ]]; then + return 0 + fi + + if [[ $MAJOR_A -le $MAJOR_B && $MINOR_A -le $MINOR_B && $PATCH_A -lt $PATCH_B ]]; then + return 0 + fi + + if [[ "_$SPECIAL_A" == "_" ]] && [[ "_$SPECIAL_B" == "_" ]] ; then + return 1 + fi + if [[ "_$SPECIAL_A" == "_" ]] && [[ "_$SPECIAL_B" != "_" ]] ; then + return 1 + fi + if [[ "_$SPECIAL_A" != "_" ]] && [[ "_$SPECIAL_B" == "_" ]] ; then + return 0 + fi + + if [[ "_$SPECIAL_A" < "_$SPECIAL_B" ]]; then + return 0 + fi + + return 1 + +} + +function semverGT() { + semverEQ $1 $2 + local EQ=$? + + semverLT $1 $2 + local LT=$? + + if [ $EQ -ne 0 ] && [ $LT -ne 0 ]; then + return 0 + else + return 1 + fi +} + +if [ "___semver.sh" == "___`basename $0`" ]; then + +MAJOR=0 +MINOR=0 +PATCH=0 +SPECIAL="" + +semverParseInto $1 MAJOR MINOR PATCH SPECIAL +echo "$1 -> M: $MAJOR m:$MINOR p:$PATCH s:$SPECIAL" + +semverParseInto $2 MAJOR MINOR PATCH SPECIAL +echo "$2 -> M: $MAJOR m:$MINOR p:$PATCH s:$SPECIAL" + +semverEQ $1 $2 +echo "$1 == $2 -> $?." + +semverLT $1 $2 +echo "$1 < $2 -> $?." + +semverGT $1 $2 +echo "$1 > $2 -> $?." + +fi diff --git a/src/templater.sh b/src/templater.sh new file mode 100755 index 0000000..8b5b3ac --- /dev/null +++ b/src/templater.sh @@ -0,0 +1,42 @@ +#!/usr/bin/env bash +# Copyright (c) 2014, Transloadit Ltd. +# +# This file +# - takes a source (template) & destination (config) filepath argument +# - and then replaces placeholders with variables found in the environment +# +# Authors: +# - Kevin van Zonneveld + +set -o pipefail +set -o errexit +# set -o xtrace +# set -o nounset + +sed="" +[ -n "$(which sed)" ] && sed="$(which sed)" +[ -n "$(which gsed)" ] && sed="$(which gsed)" + +templateSrc="${1}" +templateDst="${2}" + +if [ ! -f "${templateSrc}" ]; then + echo "ERROR: Template source '${templateSrc}' needs to exist" + exit 1 +fi +if [ ! -n "${templateDst}" ]; then + echo "ERROR: Template destination '${templateDst}' needs to exist" + exit 1 +fi + +cp -f "${templateSrc}" "${templateDst}" +for var in $(env |awk -F= '{print $1}' |egrep '^[A-Z0-9_]+$'); do + ${sed} "s#\${${var}}#${!var}#g" -i "${templateDst}" +done + +# cat "${templateDst}" + +if grep '${' ${templateDst}; then + echo "ERROR: Unable to replace the above template vars" + exit 1 +fi