-
Notifications
You must be signed in to change notification settings - Fork 3
/
create_release.sh
executable file
·332 lines (278 loc) · 7.97 KB
/
create_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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/bin/bash
# rust create_release v0.5.5
STAR_LINE='****************************************'
CWD=$(pwd)
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
PURPLE='\033[0;35m'
RESET='\033[0m'
# $1 string - error message
error_close() {
echo -e "\n${RED}ERROR - EXITED: ${YELLOW}$1${RESET}\n"
exit 1
}
# Check that dialog is installed
if ! [ -x "$(command -v dialog)" ]; then
error_close "dialog is not installed"
fi
# $1 string - question to ask
ask_yn() {
printf "%b%s? [y/N]:%b " "${GREEN}" "$1" "${RESET}"
}
# return user input
user_input() {
read -r data
echo "$data"
}
# ask continue, or quit
ask_continue() {
ask_yn "continue"
if [[ ! "$(user_input)" =~ ^y$ ]]; then
exit
fi
}
# semver major update
update_major() {
local bumped_major
bumped_major=$((MAJOR + 1))
echo "${bumped_major}.0.0"
}
# semver minor update
update_minor() {
local bumped_minor
bumped_minor=$((MINOR + 1))
MINOR=bumped_minor
echo "${MAJOR}.${bumped_minor}.0"
}
# semver patch update
update_patch() {
local bumped_patch
bumped_patch=$((PATCH + 1))
PATCH=bumped_patch
echo "${MAJOR}.${MINOR}.${bumped_patch}"
}
# Get the url of the github repo, strip .git from the end of it
get_git_remote_url() {
GIT_REPO_URL="$(git config --get remote.origin.url | sed 's/\.git$//')"
}
# Check that git status is clean
check_git_clean() {
GIT_CLEAN=$(git status --porcelain)
if [[ -n $GIT_CLEAN ]]; then
error_close "git dirty"
fi
}
# Check currently on dev branch
check_git() {
CURRENT_GIT_BRANCH=$(git branch --show-current)
check_git_clean
if [[ ! "$CURRENT_GIT_BRANCH" =~ ^dev$ ]]; then
error_close "not on dev branch"
fi
}
# Ask user if current changelog is acceptable
ask_changelog_update() {
echo "${STAR_LINE}"
RELEASE_BODY_TEXT=$(sed '/# <a href=/Q' CHANGELOG.md)
printf "%s" "$RELEASE_BODY_TEXT"
printf "\n%s\n" "${STAR_LINE}"
ask_yn "accept release body"
if [[ "$(user_input)" =~ ^y$ ]]; then
update_release_body_and_changelog "$RELEASE_BODY_TEXT"
else
exit
fi
}
# Edit the release-body to include new lines from changelog
# add commit urls to changelog
# $1 RELEASE_BODY
update_release_body_and_changelog() {
echo -e
DATE_SUBHEADING="### $(date +'%Y-%m-%d')\n\n"
RELEASE_BODY_ADDITION="${DATE_SUBHEADING}$1"
# Put new changelog entries into release-body, add link to changelog
echo -e "${RELEASE_BODY_ADDITION}\n\nsee <a href='${GIT_REPO_URL}/blob/main/CHANGELOG.md'>CHANGELOG.md</a> for more details" >.github/release-body.md
# Add subheading with release version and date of release
echo -e "# <a href='${GIT_REPO_URL}/releases/tag/${NEW_TAG_WITH_V}'>${NEW_TAG_WITH_V}</a>\n${DATE_SUBHEADING}${CHANGELOG_ADDITION}$(cat CHANGELOG.md)" >CHANGELOG.md
# Update changelog to add links to commits [hex:8](url_with_full_commit)
# "[aaaaaaaaaabbbbbbbbbbccccccccccddddddddd]" -> "[aaaaaaaa](https:/www.../commit/aaaaaaaaaabbbbbbbbbbccccccccccddddddddd)"
sed -i -E "s=(\s)\[([0-9a-f]{8})([0-9a-f]{32})\]= [\2](${GIT_REPO_URL}/commit/\2\3)=g" ./CHANGELOG.md
# Update changelog to add links to closed issues - comma included!
# "closes #1" -> "closes [#1](https:/www.../issues/1),""
sed -i -r -E "s=closes \#([0-9]+)=closes [#\1](${GIT_REPO_URL}/issues/\1)=g" ./CHANGELOG.md
}
# Update package.json
update_json() {
local json_file="./package.json"
json_version_update=$(jq ".version = \"${NEW_TAG_WITH_V:1}\"" "${json_file}")
echo "$json_version_update" >"$json_file"
}
# update version in cargo.toml, to match selected current version
update_version_number_in_files() {
sed -i "s|^version = .*|version = \"${MAJOR}.${MINOR}.${PATCH}\"|" ./src-tauri/Cargo.toml
# update tauri.conf.json version
sed -i "s|\"version\": \"*.*.*\"|\"version\": \"${MAJOR}.${MINOR}.${PATCH}\"|" ./src-tauri/tauri.conf.json
}
# Work out the current version, based on git tags
# create new semver version based on user input
# Set MAJOR MINOR PATCH
check_tag() {
LATEST_TAG=$(git describe --tags "$(git rev-list --tags --max-count=1)")
echo -e "\nCurrent tag: ${PURPLE}${LATEST_TAG}${RESET}\n"
echo -e "${YELLOW}Choose new tag version:${RESET}\n"
if [[ $LATEST_TAG =~ ^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-((0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*))*))?(\+([0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*))?$ ]]; then
IFS="." read -r MAJOR MINOR PATCH <<<"${LATEST_TAG:1}"
else
MAJOR="0"
MINOR="0"
PATCH="0"
fi
OP_MAJOR="major___v$(update_major)"
OP_MINOR="minor___v$(update_minor)"
OP_PATCH="patch___v$(update_patch)"
OPTIONS=("$OP_MAJOR" "$OP_MINOR" "$OP_PATCH")
select choice in "${OPTIONS[@]}"; do
case $choice in
"$OP_MAJOR")
MAJOR=$((MAJOR + 1))
MINOR=0
PATCH=0
break
;;
"$OP_MINOR")
MINOR=$((MINOR + 1))
PATCH=0
break
;;
"$OP_PATCH")
PATCH=$((PATCH + 1))
break
;;
*)
error_close "invalid option $REPLY"
;;
esac
done
}
# $1 text to colourise
release_continue() {
echo -e "\n${PURPLE}$1${RESET}"
ask_continue
}
# run all tests
cargo_test() {
cargo test -- --test-threads=1
ask_continue
}
# build for production, as Github action would do
cargo_build() {
echo -e "\n${GREEN}cargo tauri build${RESET}"
cargo tauri build
ask_continue
}
check_typos() {
echo -e "\n${YELLOW}checking for typos${RESET}"
typos
ask_continue
}
# Make sure the unused lint isn't used
check_allow_unused() {
matches_any=$(find . -type d \( -name .git -o -name target \) -prune -o -type f -exec grep -lE '^#!\[allow\(unused\)\]$' {} +)
matches_cargo=$(grep "^unused = \"allow\"" ./Cargo.toml)
if [ -n "$matches_any" ]; then
echo "\"#[allow(unused)]\" in ${matches_any}"
ask_continue
elif [ -n "$matches_cargo" ]; then
echo "\"unused = \"allow\"\" in Cargo.toml"
ask_continue
fi
}
# Full flow to create a new release
release_flow() {
check_allow_unused
check_typos
check_git
get_git_remote_url
# cargo_test
cargo_build
echo -e "\n${YELLOW}cd ${CWD}${RESET}"
cd "${CWD}" || error_close "Can't find ${CWD}"
check_tag
NEW_TAG_WITH_V="v${MAJOR}.${MINOR}.${PATCH}"
printf "\nnew tag chosen: %s\n\n" "${NEW_TAG_WITH_V}"
RELEASE_BRANCH=release-$NEW_TAG_WITH_V
echo -e
ask_changelog_update
release_continue "checkout ${RELEASE_BRANCH}"
git checkout -b "$RELEASE_BRANCH"
release_continue "update_version_number_in_files"
update_version_number_in_files
update_json
echo -e "\n${YELLOW}cd src-tauri${RESET}"
cd src-tauri || error_close "Can't find src-tauri"
echo "cargo fmt"
cargo fmt
echo -e "\n${PURPLE}cargo check${RESET}"
cargo check
echo -e "\n${YELLOW}cd ${CWD}${RESET}"
cd "${CWD}" || error_close "Can't find ${CWD}"
release_continue "git add ."
git add .
release_continue "git commit -m \"chore: release \"${NEW_TAG_WITH_V}\""
git commit -m "chore: release ${NEW_TAG_WITH_V}"
release_continue "git checkout main"
git checkout main
echo -e "${PURPLE}git pull main${RESET}"
git pull origin main
echo -e "${PURPLE}git merge --no-ff \"${RELEASE_BRANCH}\" -m \"chore: merge ${RELEASE_BRANCH} into main\"${RESET}"
git merge --no-ff "$RELEASE_BRANCH" -m "chore: merge ${RELEASE_BRANCH} into main"
release_continue "git tag -am \"${RELEASE_BRANCH}\" \"$NEW_TAG_WITH_V\""
git tag -am "${RELEASE_BRANCH}" "$NEW_TAG_WITH_V"
release_continue "git push --atomic origin main \"$NEW_TAG_WITH_V\""
git push --atomic origin main "$NEW_TAG_WITH_V"
release_continue "git checkout dev"
git checkout dev
release_continue "git merge --no-ff main -m \"chore: merge main into dev\""
git merge --no-ff main -m "chore: merge main into dev"
release_continue "git push origin dev"
git push origin dev
release_continue "git branch -d \"$RELEASE_BRANCH\""
git branch -d "$RELEASE_BRANCH"
}
main() {
cmd=(dialog --backtitle "Choose option" --radiolist "choose" 14 80 16)
options=(
1 "test" off
2 "release" off
3 "build" off
)
choices=$("${cmd[@]}" "${options[@]}" 2>&1 >/dev/tty)
exitStatus=$?
clear
if [ $exitStatus -ne 0 ]; then
exit
fi
for choice in $choices; do
case $choice in
0)
exit
;;
1)
cargo_test
main
break
;;
2)
release_flow
break
;;
3)
cargo_build
main
break
;;
esac
done
}
main