Skip to content

Instantly share code, notes, and snippets.

@tripflex
Created April 11, 2019 17:47
Show Gist options
  • Save tripflex/e373ad5b61958c79fc851795ea19c3dd to your computer and use it in GitHub Desktop.
Save tripflex/e373ad5b61958c79fc851795ea19c3dd to your computer and use it in GitHub Desktop.
Bash/Shell script to automatically increment Mongoose OS mos.yml version numbers
#!/usr/bin/env bash
# Usage: ./bump_version.sh <major|minor|patch> - Increments the relevant version part by one.
#
# Usage 2: ./bump_version.sh <version-from> <version-to>
# e.g: ./bump_version.sh 1.1.1 2.0
# Specifically coded for Mac OSX
# You must install grep from brew, like this:
# brew install coreutils
# brew install grep
# This installs unix grep as "ggrep"
set -e
# Define which files to update and the pattern to look for
# $1 Current version
# $2 New version
function bump_files() {
bump mos.yml "version: $1" "version: $2"
}
function confirm() {
read -r -p "$@ [Y/n]: " confirm
case "$confirm" in
[Nn][Oo]|[Nn])
echo "Aborting."
exit
;;
esac
}
if [ "$1" == "" ]; then
echo >&2 "No 'from' version set. Aborting."
exit 1
fi
if [ "$1" == "major" ] || [ "$1" == "minor" ] || [ "$1" == "patch" ]; then
current_version=$(ggrep -Po '(?<=version:\s)[^\s]*' mos.yml)
IFS='.' read -a version_parts <<< "$current_version"
major=${version_parts[0]}
minor=${version_parts[1]}
patch=${version_parts[2]}
# Hack to deal with incorrect regex matching more than just version above
current_version="$major.$minor.$patch"
case "$1" in
"major")
major=$((major + 1))
minor=0
patch=0
;;
"minor")
minor=$((minor + 1))
patch=0
;;
"patch")
patch=$((patch + 1))
;;
esac
new_version="$major.$minor.$patch"
else
if [ "$2" == "" ]; then
echo >&2 "No 'to' version set. Aborting."
exit 1
fi
current_version="$1"
new_version="$2"
fi
if ! [[ "$new_version" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo >&2 "'to' version doesn't look like a valid semver version tag (e.g: 1.2.3). Aborting."
exit 1
fi
#confirm "Bump version number from $current_version to $new_version?"
# bump_files "$current_version" "$new_version"
echo -n "Updating $1 in mos.yml ... from $current_version to $new_version"
sed -i '' "s/version: $current_version/version: $new_version/" mos.yml
@tripflex
Copy link
Author

tripflex commented Aug 6, 2019

As mentioned in comments, for example to update a version that is currently set to 1.0.0 to 1.0.1 you would use:

./bump_version.sh patch

I personally use this in an alias for when i build new versions of the firmware, something like this:

alias fwbuild='mos build --local --verbose'
alias fwbuildb='./bump_version.sh patch && mos build --local --verbose'
alias fwbuildc='mos build --local --verbose --clean'
alias fwbuildbc='./bump_version.sh patch && mos build --local --verbose --clean'

That way you can just call one of the alias in shell to do what you need ... automate all the things!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment