-
Notifications
You must be signed in to change notification settings - Fork 3
/
release.sh
executable file
·136 lines (113 loc) · 3.74 KB
/
release.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env bash
#
# Prepares the specified library for release. Creates a release branch from the 'main'.
#
# Usage: ./release.sh [release-name]
# Where `release-name` is: [library-name]-v[version]
#
# Example: ./release.sh gears-compose-v1.0.0
#
# Original release script: https://github.com/RedMadRobot/android-library-template/blob/main/release.sh
set -euo pipefail
# The script could be run from any directory.
cd "$(dirname "$0")"
# Handle release input parameter
release=${1:?Please specify the release to be released as a parameter}
module=$(./scripts/get-module-name.sh "$release")
module_path=${module#:} # :gears:gears-compose -> gears:gears-compose
module_path=${module_path//://} # gears:gears-compose -> gears/gears-compose
library=${release%-v*} # gears-compose-v1.0.0 -> gears-compose
# Configure the script
properties="$module_path/gradle.properties"
changelog="$module_path/CHANGELOG.md"
readme="$module_path/README.md"
build_script="$module_path/build.gradle.kts"
files_to_update_version=("$properties" "$build_script" "$readme")
github_repository_url="https://github.com/RedMadRobot/gears-android"
#region Utils
function error() {
echo "❌ $1"
return 1
}
function skip() {
echo "👌 SKIPPED."
exit 0
}
function get_current_version() {
local version=""
if [[ -f $properties ]]; then
version=$(grep "^version=" "$properties")
fi
if [[ -z $version && -f $build_script ]]; then
version=$(grep "^version = \"" "$build_script")
fi
if [[ -z $version ]]; then
error "Can't find current version. Checked locations: $properties, $build_script"
fi
echo "$version" | cut -d'=' -f2 | tr -d ' "'
}
function replace() {
local file=$3
# 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" "$file"
rm "$file.bak"
}
function diff_link() {
echo -n "$github_repository_url/compare/${1}...${2}"
}
function wait_confirmation() {
local input
read -p " Enter 'yes' to continue: " -r input
[[ $input == "yes" ]]
}
#endregion
# Check if the module exists
[[ -d $module_path ]] || error "Module '$module' not found on path '$module_path'."
# 0. Fetch remote changes
echo "️⏳ Creating release branch..."
release_branch="release/$release"
git checkout --quiet -B "$release_branch"
git pull --quiet --rebase origin main || wait_confirmation || skip
echo "✅ Branch '$release_branch' created"
echo
# 1. Calculate module and version values for later
last_version=$(get_current_version)
version=${release##*-v} # library-v1.0.0 -> 1.0.0
if [[ "$last_version" == "$version" ]]; then
echo "🤔 Version $version is already set."
wait_confirmation || skip
fi
echo "🚀 [$module] Update $last_version → $version"
echo
# 2. Update version everywhere
for file in "${files_to_update_version[@]}" ; do
if [[ -f $file ]]; then
replace "$last_version" "$version" "$file"
echo "✅ Updated version in $file"
fi
done
# 3. Update header in CHANGELOG.md
date=$(date -u +%Y-%m-%d)
header_replacement=\
"## Unreleased
- *No changes*
## $version ($date)"
replace "^## Unreleased.*" "$header_replacement" "$changelog"
echo "✅ Updated CHANGELOG.md header"
# 4. Ask if the changes should be pushed to remote branch
echo
echo "Do you want to commit the changes and push the release branch and tag?"
echo "The release tag push triggers a release workflow on CI."
wait_confirmation || skip
# 5. Push changes, trigger release on CI, and give a link to open PR
echo
echo "⏳ Pushing the changes to the remote repository..."
git add "$module_path"
git commit --quiet --message "$library: $version"
git tag "$release"
git push --quiet origin HEAD "$release"
echo "🎉 DONE."
echo "Create a Pull Request: $(diff_link main "$release_branch")"