Skip to content

Instantly share code, notes, and snippets.

@dbernheisel
Last active January 15, 2024 03:16
Show Gist options
  • Save dbernheisel/a234d7de03f5e39a041d7e6a20d775b0 to your computer and use it in GitHub Desktop.
Save dbernheisel/a234d7de03f5e39a041d7e6a20d775b0 to your computer and use it in GitHub Desktop.
Bash script to bump the version
#!/bin/bash
# Works with a file called VERSION in the current directory,
# the contents of which should be a semantic version number
# such as "1.2.3"
# this script will display the current version, automatically
# suggest a "minor" version update, and ask for input to use
# the suggestion, or use a newly entered value.
set -e
source bin/utils.sh
suggest_version() {
local CURRENT_VERSION
local CURRENT_MAJOR
local CURRENT_MINOR
CURRENT_VERSION=$(cat VERSION)
CURRENT_MAJOR=$(echo "$CURRENT_VERSION" | cut -d. -f1)
CURRENT_MINOR=$(echo "$CURRENT_VERSION" | cut -d. -f2)
SUGGESTED_MINOR=$((CURRENT_MINOR + 1))
SUGGESTED_PATCH=0
echo "$CURRENT_MAJOR.$SUGGESTED_MINOR.$SUGGESTED_PATCH"
}
update_version() {
NEW_VERSION=$1; shift
echo "$NEW_VERSION" > VERSION
git add VERSION
}
push_tags() {
NEW_VERSION=$1; shift
echo "Pushing new version to the origin"
git commit -m "Bump version to ${NEW_VERSION}."
git tag -a -m "Tag version ${NEW_VERSION}." "v$NEW_VERSION"
git push origin --tags
}
exit_if_dirty
if [ -f VERSION ]; then
SUGGESTED_VERSION=$(suggest_version)
echo_notice "Current version: ${WHITE}$(cat VERSION)"
NEW_VERSION=$(echo_prompt "Enter a version number" "[$SUGGESTED_VERSION]")
if [ "$NEW_VERSION" = "" ]; then NEW_VERSION=$SUGGESTED_VERSION; fi
echo "Will set new version to be $NEW_VERSION"
update_version "$NEW_VERSION"
push_tags "$NEW_VERSION"
else
echo_warning "Could not find a ${WHITE}VERSION${YELLOW} file."
RESPONSE=$(echo_prompt "Do you want to create a version file and start from scratch?" "(y/n)")
case "$RESPONSE" in
y|Y ) update_version "0.1.0" && push_tags "0.1.0";;
* ) exit 1;;
esac
fi
# Rails usage example:
## # config/application.rb
## module MyApp
## class Application < Rails::Application
## VERSION = File.read('VERSION').strip
## # ...
## end
## end
#
#
# Elixir usage example
## # mix.exs
## defmodule MyApp.MixProject do
## def project do
## [
## app: :my_app,
## version: File.read!("VERSION") |> String.trim(),
## # ...
## ]
## end
## end
#!/bin/bash
GREEN="\033[0;32m"
YELLOW="\033[1;33m"
CYAN="\033[1;36m"
WHITE="\033[1;37m"
RESET="\033[0m"
QUESTION_FLAG="${GREEN}?${RESET}"
WARNING_FLAG="${YELLOW}!${RESET}"
NOTICE_FLAG="${CYAN}❯${RESET}"
echo_notice() {
echo -e "$NOTICE_FLAG $CYAN $1$RESET"
}
echo_prompt() {
local PROMPT
local HINT
PROMPT=$1; shift
HINT=$1; shift
echo -ne "$QUESTION_FLAG $CYAN $PROMPT $WHITE$HINT$CYAN: $WHITE" > /dev/stderr
read -r CHOICE
echo "$CHOICE"
echo_reset > /dev/stderr
}
echo_warning() {
echo -e "$WARNING_FLAG $YELLOW $1$RESET"
}
echo_reset() {
echo -en "$RESET"
}
exit_if_dirty() {
if [[ $(git diff --stat) != '' ]]; then
echo_warning "You have uncommitted changes. Please resolve your changes first."
exit 1
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment