Skip to content

Instantly share code, notes, and snippets.

@saschpe
Created April 19, 2023 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saschpe/e0c11be1e15cfb37e1f9cee82afec790 to your computer and use it in GitHub Desktop.
Save saschpe/e0c11be1e15cfb37e1f9cee82afec790 to your computer and use it in GitHub Desktop.
Versioning script for Android projects
#!/bin/bash
#
# Script to create a release commit.
#
# Increases the version name and version code of an Android app,
# commits and tags the result.
#
# Based of semantic versioning: MAJOR.MINOR.PATCH
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
. "${SCRIPT_DIR}/inc.functions.sh"
# Constants
GRADLE_FILES=(
presentation/mobile/build.gradle.kts
presentation/wear/build.gradle.kts
)
# Functions
function get_version_code() {
grep "versionCode = " "$1" | cut -d"=" -f2 | xargs
}
function get_version_name() {
grep "versionName = " "$1" | cut -d"=" -f2 | xargs
}
function usage() {
echo -e "Usage: $0 [VERSION_NAME]\n\n Example: $0 0.0.1"
exit 1
}
function version_gt() {
test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"
}
# Checks
if [[ $# -ne 1 ]]; then
usage
fi
# Increase version name
version_name_new=$1
# Split new version name into major, minor, patch and pad each number to two
# digits to ensure we can safely switch from 1.9.0 to 1.10.0. Padded, the
# latter would yield '011000' as the new version code. The result has a length
# of six digits.
IFS='.' read -r -a version_name_new_array <<<"${version_name_new}"
for gradle_file in "${GRADLE_FILES[@]}" ; do
version_name_old=$(get_version_name "${gradle_file}")
# To be able to compare with the previous version, do the same for the old
# version name.
IFS='.' read -r -a version_name_old_array <<<"${version_name_old}"
if version_gt "${version_name_old}" "${version_name_new}"; then
die "Already at version ${version_name_old}"
fi
# Since we're otherwise fine, start changing the version name..
sed2 "s|versionName = \"${version_name_old}|versionName = \"${version_name_new}|" "${gradle_file}"
# Increment app version code for Google's Play Store
version_code_old=$(get_version_code "${gradle_file}")
# shellcheck disable=SC2086
padded_sem_ver=$(printf "%02d%02d%02d\n" "${version_name_new_array[0]}" "${version_name_new_array[1]}" ${version_name_new_array[2]})
# Replace the last six digits with the new padded semantic version
version_code_new=${version_code_old::${#version_code_old}-6}${padded_sem_ver}
sed2 "s/versionCode = ${version_code_old}/versionCode = ${version_code_new}/" "${gradle_file}"
git_commit_files="${git_commit_files} ${gradle_file}"
done
# Create a commit with appropriate tag
git_commit_message="Release version ${version_name_new}"
safe git commit --signoff ${git_commit_files} -m "${git_commit_message}"
safe git tag -f release/${version_name_new}
@saschpe
Copy link
Author

saschpe commented Apr 19, 2023

#!/bin/bash
#
# Collection of shared functions
#

GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color

function approve() {
  echo -e "${GREEN}$*${NC}"
}

function warn() {
  echo -e "${YELLOW}$*${NC}"
}

function die() {
  echo -e "${RED}$*${NC}"
  exit 1
}

function safe() {
  "$@"
  local status=$?
  if [[ ${status} -ne 0 ]]; then
    die "\nBUILD FAILED\nAfter invoking \"$*\"\n" >&2
  fi
  return ${status}
}

function sed2() {
  sed -i'.bak' "$1" "${@:2}"
  for file in "${@:2}"; do
    rm "${file}.bak"
  done
}

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