forked from duckduckgo/GRDB.swift
-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepare_release.sh
executable file
·341 lines (276 loc) · 9.38 KB
/
prepare_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
333
334
335
336
337
338
339
340
341
#!/bin/bash
set -e
mute=">/dev/null 2>&1"
if [[ "$1" == "-v" ]]; then
mute=
fi
cwd="$(dirname "${BASH_SOURCE[0]}")"
workdir="$(mktemp -d)"
mkdir -p "${workdir}/Logs"
# trap 'rm -rf "$workdir"' EXIT
grdb_dir="${workdir}/GRDB-source"
sqlcipher_dir="${workdir}/sqlcipher-source"
print_usage_and_exit() {
cat <<- EOF
Usage:
$ $(basename "$0") [-v] [-h] [<grdb_tag>]
Options:
-h Show this message
-v Verbose output
EOF
exit 1
}
read_command_line_arguments() {
while getopts 'hv' OPTION; do
case "${OPTION}" in
h)
print_usage_and_exit
;;
v)
mute=
;;
*)
;;
esac
done
shift $((OPTIND-1))
grdb_tag="$1"
if [[ -n "$grdb_tag" ]]; then
force_release=1
fi
}
clone_grdb() {
if ! [[ -d "$grdb_dir" ]]; then
rm -rf "$grdb_dir"
printf '%s' "Cloning upstream GRDB.swift ... "
eval git clone https://github.com/groue/GRDB.swift.git "$grdb_dir" "$mute"
echo "✅"
fi
cd "${grdb_dir}"
grdb_tag="${1:-$(git describe --tags --abbrev=0)}"
eval git checkout "${grdb_tag}" "$mute"
cd -
echo "Checked out GRDB.swift latest tag: $grdb_tag"
}
clone_sqlcipher() {
printf '%s' "Cloning SQLCipher ... "
eval git clone https://github.com/sqlcipher/sqlcipher.git "$sqlcipher_dir" "$mute"
echo "✅"
export GIT_DIR="${sqlcipher_dir}/.git"
sqlcipher_tag="${SQLCIPHER_VERSION:-$(git describe --tags --abbrev=0)}"
eval git checkout "$(git describe --tags --abbrev=0)" "$mute"
unset GIT_DIR
echo "Checked out SQLCipher latest tag: $sqlcipher_tag"
}
update_license() {
rm -rf "${cwd}/LICENSE"
cp "$grdb_dir/LICENSE" "${cwd}/LICENSE"
}
update_readme() {
current_version="$(git describe --tags --abbrev=0 --exclude=v* main)"
current_upstream_version="$(grep '\* GRDB' README.md | cut -d '*' -f 3)"
current_sqlcipher_version="$(grep '\* SQLCipher' README.md | cut -d '*' -f 3)"
export new_version upstream_version="${grdb_tag#v}" sqlcipher_version="${sqlcipher_tag#v}"
if [[ "${current_upstream_version}" == "${upstream_version}" ]] && \
[[ "${current_sqlcipher_version}" == "${sqlcipher_version}" ]] && \
[[ -z "$force_release" ]]; then
echo "GRDB.swift (${upstream_version}) and SQLCipher (${sqlcipher_version}) versions did not change. Skipping release."
exit 1
fi
cat <<- EOF
Session GRDB.swift current version: ${current_version}
Upstream GRDB.swift version: ${current_upstream_version} -> ${upstream_version}
SQLCipher version: ${current_sqlcipher_version} -> ${sqlcipher_version}
EOF
while ! [[ "${new_version}" =~ [0-9]+\.[0-9]+\.[0-9]+ ]]; do
read -rp "Input Session GRDB.swift desired version number (x.y.z): " new_version < /dev/tty
done
envsubst < "${cwd}/assets/README.md.in" > README.md
echo "Updated README.md ✅"
echo ""
}
build_sqlcipher() {
local sqlcipher_destdir="${grdb_dir}/GRDB"
local header_path="${sqlcipher_destdir}/sqlite3.h"
local impl_path="${sqlcipher_destdir}/sqlite3.c"
eval pushd "$sqlcipher_dir" "$mute" || { echo "pushd failed"; exit 1; }
printf '%s' "Configuring SQLCipher ... "
eval ./configure --with-crypto-lib=none "$mute"
echo "✅"
printf '%s' "Building SQLCipher ... "
ncpu=$(sysctl -n hw.ncpu 2>/dev/null || echo "1")
eval make -j"${ncpu}" sqlite3.c "$mute"
echo "✅"
eval popd "$mute" || { echo "popd failed"; exit 1; }
printf '%s' "Moving SQLCipher artifacts into place ... "
rm -f "$header_path" "$impl_path"
mkdir -p "${sqlcipher_destdir}/include"
cp -f "${sqlcipher_dir}/sqlite3.h" "$header_path"
# Including param.h unconditionally removes compile time
# warnings about ambiguous MIN and MAX macros.
echo "#include <sys/param.h>" > "$impl_path"
cat "${sqlcipher_dir}/sqlite3.c" >> "${impl_path}"
echo "✅"
}
patch_grdb() {
local patch_file="${cwd}/assets/xcodeproj.patch"
local grdb_xcodeproj_file="${grdb_dir}/GRDB.xcodeproj"
printf '%s' "Patching GRDB ... "
: > "${grdb_dir}/GRDB/Export.swift"
# echo '#import "sqlite3.h"' > "${grdb_dir}/Support/GRDB-Bridging.h"
echo "#include \"${grdb_dir}/SQLCipher.xcconfig\"" >> "${grdb_dir}/Support/GRDBDeploymentTarget.xcconfig"
# sed -i -E 's/<sqlite3.h>/"sqlite3.h"/' "${grdb_dir}/Support/grdb_config.h"
if patch -s -p1 -f -d "$grdb_dir" < "$patch_file"; then
echo "✅"
else
echo "❌"
cat <<-EOF
Failed to automatically patch GRDB.swift Xcode project file. Please follow instructions for manual patching:
1. After you confirm reading instructions, two windows will open:
* Xcode, with GRDB.swift project
* Finder, with GRDB source code directory (look for sqlite3.h and sqlite3.c files)
2. Drag sqlite3.h and sqlite3.c to the Xcode project under GRDB directory. Add both files to GRDB target.
3. Select sqlite3.h in Xcode, open right hand side panel and adjust Target Membership by marking the header file as Public.
4. Close Xcode project, go back to terminal and press Ctrl+C to continue.
EOF
read -n 1 -srp "Press any key to continue"
open "${grdb_dir}/GRDB"
open "$grdb_xcodeproj_file"
echo ""
echo "Make edits to the project file, close it and press Ctrl+C when you're ready"
read -rp "Press enter to continue"
pushd "$grdb_dir" >/dev/null 2>&1
local diff
diff=$(git diff "GRDB.xcodeproj/project.pbxproj")
popd >/dev/null 2>&1
echo "$diff" > "${patch_file}"
echo "Updated Xcode project patch file ✅"
fi
}
setup_log_formatter() {
if command -v xcbeautify &> /dev/null; then
log_formatter='xcbeautify'
elif command -v xcpretty &> /dev/null; then
log_formatter='xcpretty'
else
echo
echo "xcbeautify and xcpretty not found - not prettifying Xcode logs. You can install xcbeautify using 'brew install xcbeautify'."
echo
log_formatter='tee'
fi
}
build_and_test_release() {
local derived_data_dir="${grdb_dir}/DerivedData"
local log_file="${workdir}/Logs/GRDB-${grdb_tag}-unittests.log"
setup_log_formatter
rm -rf "${derived_data_dir}"
cp -f "${cwd}/assets/SQLCipher.xcconfig" "${grdb_dir}"
printf '%s' "Building GRDB ... "
if xcodebuild build-for-testing \
-project "${grdb_dir}/GRDB.xcodeproj" \
-scheme "GRDB" \
-derivedDataPath "$derived_data_dir" >"$log_file" 2>&1; then
echo "✅"
else
echo "❌"
echo "Failed to build GRDB with SQLCipher support. See log file at ${log_file} for more info."
exit 1
fi
echo "Testing GRDB ... ⚙️"
# The skipped test references a test database added with a podfile.
# We're safe to disable it since we don't care about SQLCipher 3 compatibility anyway.
if xcodebuild test-without-building \
-project "${grdb_dir}/GRDB.xcodeproj" \
-scheme "GRDB" \
-derivedDataPath "$derived_data_dir" \
-skip-testing:GRDBTests/EncryptionTests/testSQLCipher3Compatibility \
| tee -a "$log_file" | $log_formatter 2>&1; then
echo "Unit tests succeeded ✅"
else
cat <<-EOF
Unit tests failed ❌
See log file at ${log_file} for more info.
Rerun with -f to skip testing.
EOF
exit 1
fi
}
build_archive() {
local platform=$1
local archives_path=$2
local log_file="${workdir}/Logs/GRDB-archive-${platform/ /-}.log"
printf '%s' " * Archiving for ${platform} ... "
if xcodebuild archive \
-project "${grdb_dir}/GRDB.xcodeproj" \
-scheme GRDB \
-destination "generic/platform=${platform}" \
-archivePath "${archives_path}/GRDB-${platform}" \
-derivedDataPath "${derived_data}" \
"${build_opts[@]}" >"$log_file" 2>&1; then
echo "✅"
else
echo "❌"
echo "Failed to create archive. See log file at ${log_file} for more info."
exit 1
fi
}
build_xcframework() {
local derived_data="${workdir}/DerivedData"
local xcframework="${workdir}/GRDB.xcframework"
xcframework_zip="${workdir}/GRDB.xcframework.zip"
local archives_dir="archives"
local archives_path="${workdir}/${archives_dir}"
build_opts=("BUILD_LIBRARY_FOR_DISTRIBUTION=YES" "SKIP_INSTALL=NO" "ONLY_ACTIVE_ARCH=NO")
echo ""
echo "Building XCFramework ⚙️"
rm -rf "${derived_data}" "${archives_path}" "${xcframework}"
build_archive "iOS" "$archives_path"
build_archive "iOS Simulator" "$archives_path"
build_archive "macOS" "$archives_path"
printf '%s' "Creating XCFramework ... "
pushd "$workdir" >/dev/null 2>&1
xcodebuild -create-xcframework \
-archive "${archives_dir}/GRDB-iOS.xcarchive" -framework GRDB.framework \
-archive "${archives_dir}/GRDB-iOS Simulator.xcarchive" -framework GRDB.framework \
-archive "${archives_dir}/GRDB-macOS.xcarchive" -framework GRDB.framework \
-output "${xcframework}" >/dev/null 2>&1
popd >/dev/null 2>&1
echo "✅"
printf '%s' "Compressing XCFramework ... "
rm -rf "$xcframework_zip"
ditto -c -k --keepParent "$xcframework" "$xcframework_zip"
echo "✅"
}
update_swift_package() {
printf '%s' "Updating Package.swift ... "
export checksum
checksum=$(swift package compute-checksum "$xcframework_zip")
envsubst < "${cwd}/assets/Package.swift.in" > "${cwd}/Package.swift"
echo "✅"
}
make_release() {
echo "Making ${new_version} release ... 🚢"
local commit_message="Session GRDB.swift ${new_version} (GRDB ${upstream_version}, SQLCipher ${sqlcipher_version})"
git add "${cwd}/README.md" "${cwd}/Package.swift" "${cwd}/assets/xcodeproj.patch"
git commit -m "$commit_message"
git tag -m "$commit_message" "$new_version"
mv "${xcframework_zip}" "./GRDB.xcframework.zip"
cat <<- EOF
🎉 Release is to upload, archive at "./GRDB.xcframework.zip"
EOF
}
main() {
printf '%s\n' "Using directory at ${workdir}"
read_command_line_arguments "$@"
clone_grdb "$grdb_tag"
clone_sqlcipher
update_license
update_readme
build_sqlcipher
patch_grdb
build_and_test_release
build_xcframework
update_swift_package
make_release
}
main "$@"