-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrelease_utils.sh
111 lines (94 loc) · 3.56 KB
/
release_utils.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
#!/bin/bash
# Utils adapted from https://github.com/Homebrew/install/blob/master/install.sh
# string formatters
if [[ -t 1 ]]; then
tty_escape() { printf "\033[%sm" "$1"; }
else
tty_escape() { :; }
fi
tty_mkbold() { tty_escape "1;$1"; }
tty_underline="$(tty_escape "4;39")"
tty_blue="$(tty_mkbold 34)"
tty_red="$(tty_mkbold 31)"
tty_reset="$(tty_escape 0)"
# Takes multiple arguments and prints them joined with single spaces in-between
# while escaping any spaces in arguments themselves
shell_join() {
local arg
printf "%s" "$1"
shift
for arg in "$@"; do
printf " "
printf "%s" "${arg// /\ }"
done
}
# Takes multiple arguments, joins them and prints them in a colored format
ohai() {
printf "${tty_blue}==> %s${tty_reset}\n" "$(shell_join "$@")"
}
# Takes a single argument and prints it in a colored format
warn() {
printf "${tty_underline}${tty_red}Warning${tty_reset}: %s\n" "$1"
}
# Takes a single argument, prints it in a colored format and aborts the script
abort() {
printf "\n${tty_red}%s${tty_reset}\n" "$1"
exit 1
}
# Takes multiple arguments consisting a command and executes it. If the command
# is not successful aborts the script, printing the failed command and its
# arguments in a colored format.
#
# Returns the executed command's result if it's successful.
execute() {
if ! "$@"; then
abort "$(printf "Failed during: %s" "$(shell_join "$@")")"
fi
}
# Takes multiple arguments consisting a command and executes it.
# If the command is not successful, it will be retried for a maximum of 10 times.
# In case it keeps failing, the script will be aborted, printing the failed command
# and its arguments in a colored format.
#
# Returns the executed command's result if it's successful.
execute_until_succeeds() {
local max_retry=10
local retry_count=0
until "$@"
do
sleep 1
[[ retry_count -eq "$max_retry" ]] && abort "$(printf "Failed after retrying $retry_count times: %s" "$(shell_join "$@")")"
((retry_count++))
warn "Retrying '$(shell_join "$@")' command after $retry_count times..."
done
}
#####
# Confirm to Proceed Prompt
#####
# Accepts a single argument: a yes/no question (ending with a ? most likely) to ask the user
function confirm_to_proceed() {
read -r -p "$1 (y/n) " -n 1
echo ""
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
abort "Aborting release..."
fi
}
# Automation steps
#
# These could live in a dedicated file, because "utils" is a misleading name where to put them.
# However, they depend on functions such as execute and ohai, so for the moment it's easier to have them in here.
function update_ios_gutenberg_config() {
ios_config_file=$1
ref=$2
github_org=$3
ohai "Update GitHub organization and gutenberg-mobile ref"
test -f "$ios_config_file" || abort "Error: Could not find $ios_config_file"
yq eval ".github_org= \"$github_org\"" "$ios_config_file" -i || abort "Error: Failed updating GitHub organization in $ios_config_file"
# Make iOS point to the given commit, removing the configured tag, if any
yq eval ".ref.commit = \"$ref\"" "$ios_config_file" -i || abort "Error: Failed updating gutenberg ref in $ios_config_file (part 1 of 2, setting the commit)"
yq eval 'del(.ref.tag)' "$ios_config_file" -i || abort "Error: Failed updating gutenberg ref in $ios_config_file (part 2 of 2, commenting the tag)"
execute "bundle" "install"
execute_until_succeeds "rake" "dependencies"
execute "git" "add" "Podfile" "Podfile.lock" "$ios_config_file"
execute "git" "commit" "-m" "Release script: Update gutenberg-mobile ref"
}