From 97d49faeab6506eb4820417c274646a3bd9866a5 Mon Sep 17 00:00:00 2001 From: Osip Fatkullin Date: Tue, 30 Jul 2024 23:59:34 +0200 Subject: [PATCH] ci: Add release.sh script --- release.sh | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 release.sh diff --git a/release.sh b/release.sh new file mode 100644 index 0000000..0f29f13 --- /dev/null +++ b/release.sh @@ -0,0 +1,93 @@ +#!/usr/bin/env bash + +set -euo pipefail + +# The script could be run from any directory. +cd "$(dirname "$0")" + +# Configure the script +properties="gradle.properties" +changelog="CHANGELOG.md" +readme="README.md" +files_to_update_version=("$properties" "$readme") +github_repository_url="https://github.com/RedMadRobot/gears-android" + +#region Utils +function property { + grep "^${1}=" "$properties" | cut -d'=' -f2 +} + +function replace() { + # Escape linebreaks + local replacement=${2//$'\n'/\\\n} + # Portable in-place edit. + # See: https://stackoverflow.com/a/4247319 + sed -i".bak" -E "s~$1~$replacement~g" "$3" + rm "$3.bak" +} + +function diff_link() { + echo -n "$github_repository_url/compare/${1}...${2}" +} +#endregion + +# 0. Fetch remote changes +echo "️⏳ Updating local repository..." +git fetch --quiet -p origin +git switch --quiet main +git pull --quiet --rebase origin +echo "✅ Repository updated." +echo + +# 1. Calculate version values for later +last_version=$(property "version") +version=$(date "+%Y.%m.%d") +if [[ "$last_version" == "$version" ]]; then + echo "🤔 Version $version is already set." + exit 0 +fi +echo "🚀 Update $last_version -> $version" +echo + +# 2. Update version everywhere +for file in "${files_to_update_version[@]}" ; do + replace "$last_version" "$version" "$file" + echo "✅ Updated version in $file" +done + +# 3. Update header in CHANGELOG.md +header_replacement=\ +"## [Unreleased] + +### Changes + +- *No changes* + +## [$version]" +replace "^## \[Unreleased\].*" "$header_replacement" "$changelog" +echo "✅ Updated CHANGELOG.md header" + +# 4. Add link to version diff +unreleased_diff_link="[unreleased]: $(diff_link "$version" "main")" +version_diff_link="[$version]: $(diff_link "$last_version" "$version")" +replace "^\[unreleased\]:.*" "$unreleased_diff_link\n$version_diff_link" "$changelog" +echo "✅ Added a diff link to CHANGELOG.md" + +# 5. Ask if the changes should be pushed to remote branch +echo +echo "Do you want to commit the changes and create a release tag?" +echo "The release tag push will trigger a release workflow on CI." +read -p " Enter 'yes' to continue: " -r input +if [[ "$input" != "yes" ]]; then + echo "👌 DONE." + exit 0 +fi + +# 6. Push changes and trigger release on CI +echo +echo "⏳ Pushing the changes to the remote repository..." +git add "$readme" "$changelog" "$properties" +git commit --quiet --message "version: $version" +git tag "$version" +git push --quiet origin HEAD "$version" +echo "🎉 DONE."