Skip to content

Commit

Permalink
Switch to POSIX sh and enhance script readability
Browse files Browse the repository at this point in the history
Changed the shebang in `inc.functions.sh` to use `/usr/bin/env sh` for
better portability. Refactored script functions for improved readability
and consistency, and updated variable and function names to adhere to
POSIX standards.
  • Loading branch information
saschpe committed Sep 12, 2024
1 parent 429b43b commit 56803e7
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 62 deletions.
42 changes: 22 additions & 20 deletions scripts/inc.functions.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env sh
#
# Collection of shared functions
#
Expand All @@ -8,31 +8,33 @@ RED='\033[0;31m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color

function approve {
echo -e "${GREEN}$@${NC}"
approve() {
printf "${GREEN}%s${NC}\n" "$*"
}

function warn {
echo -e "${YELLOW}$@${NC}"
warn() {
printf "${YELLOW}%s${NC}\n" "$*"
}

function die {
echo -e "${RED}$@${NC}"
exit 1
die() {
printf "${RED}%s${NC}\n" "$*"
exit 1
}

function safe {
"$@"
local status=$?
if [[ ${status} -ne 0 ]]; then
die "\nBUILD FAILED\nAfter invoking \"$@\"\n" >&2
fi
return ${status}
safe() {
"$@"
_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
sed2() {
cmd="${1}"
shift
sed -i'.bak' "${cmd}" "${@}"
for file in "${@}"; do
rm "${file}.bak"
done
}
83 changes: 41 additions & 42 deletions scripts/release
Original file line number Diff line number Diff line change
Expand Up @@ -11,67 +11,66 @@ SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
. "${SCRIPT_DIR}/inc.functions.sh"

# Constants
GRADLE_FILES=(
customtabs/build.gradle.kts
customtabs-example/build.gradle.kts
FILES=(
customtabs-example/build.gradle.kts
customtabs/build.gradle.kts
)


# Functions
function get_version_code {
echo $(grep "versionCode = " $1 | xargs | cut -d"=" -f2)
usage() {
echo -e "Usage: $0 [VERSION_NAME]\n\n Example: $0 0.0.1"
exit 1
}

function get_version_name {
echo $(grep "versionName = " $1 | xargs | cut -d"=" -f2)
version_gt() {
test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"
}

function usage {
echo -e "Usage: $0 [VERSION_NAME]\n\n Example: $0 0.0.1"
exit 1
get_version_code() {
grep "versionCode = " "$1" | cut -d"=" -f2 | xargs
}

function version_gt() {
test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1";
get_version_name() {
grep "versionName = " "$1" | cut -d"=" -f2 | xargs
}

# Checks
if [[ $# -ne 1 ]] ; then
usage
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 (not applicable for libraries)
version_code_old=$(get_version_code ${gradle_file})
if [ -n "${version_code_old}" ] ; then
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}
fi

git_commit_files="${git_commit_files} ${gradle_file}"
IFS='.' read -r -a version_name_new_array <<<"${version_name_new}"

version_name_old=$(get_version_name "${FILES[0]}")

for file in ${FILES[@]}; do
# 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|${version_name_old}|${version_name_new}|g" "${file}"

# Increment app version code for Google's Play Store (not applicable for libraries)
version_code_old=$(get_version_code ${file})
if [ -n "${version_code_old}" ] ; then
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}/" ${file}
fi

git_commit_files="${git_commit_files} ${file}"
done

# Update README.md
Expand All @@ -81,4 +80,4 @@ git_commit_files="${git_commit_files} README.md"
# 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}
safe git tag -f "release/${version_name_new}"

0 comments on commit 56803e7

Please sign in to comment.