-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·339 lines (300 loc) · 10.2 KB
/
Copy pathbuild.sh
File metadata and controls
executable file
·339 lines (300 loc) · 10.2 KB
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
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "$0")"
usage() {
cat <<'EOF'
Usage:
./build.sh package [test] [nxn] [gradle options...]
./build.sh install [test] [nxn] [gradle options...]
./build.sh deploy [test] [nxn] [gradle options...]
./build.sh release [deploy] [test] [nxn] [gradle options...]
Commands:
package Build packages. Equivalent to Maven package.
install Build packages and publish to Maven Local.
deploy Build packages, publish to Maven Local, then upload to Maven Central.
release Prepare a release commit and tag. With test, verify before deploy. With deploy, upload before next snapshot.
test Run unit tests only. The entire dbvisitor-test module is excluded.
nxn Run only the dbvisitor-test datasource matrix against real databases.
Examples:
./build.sh package
./build.sh package test
./build.sh nxn
./build.sh test nxn
Deploy properties:
Read from ~/.gradle/gradle.properties
Template:
maven.central.username=
maven.central.password=
maven.central.signing_key=
maven.central.signing_password=
maven.central.publishing_type=USER_MANAGED
EOF
}
gradle_user_properties="${HOME}/.gradle/gradle.properties"
read_gradle_property() {
local key="$1"
[[ -f "$gradle_user_properties" ]] || return 0
awk -F= -v key="$key" '
$0 !~ /^[[:space:]]*(#|!|$)/ {
prop=$1
sub(/^[[:space:]]+/, "", prop)
sub(/[[:space:]]+$/, "", prop)
if (prop == key) {
value=substr($0, index($0, "=") + 1)
sub(/^[[:space:]]+/, "", value)
sub(/[[:space:]]+$/, "", value)
print value
exit
}
}
' "$gradle_user_properties"
}
project_version() {
sed -n 's/^version=//p' gradle.properties
}
release_version_of() {
local version="$1"
printf '%s\n' "${version%-SNAPSHOT}"
}
next_snapshot_of() {
local release_version="$1"
if [[ "$release_version" =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
printf '%s.%s.%s-SNAPSHOT\n' "${BASH_REMATCH[1]}" "${BASH_REMATCH[2]}" "$((BASH_REMATCH[3] + 1))"
else
printf '%s-SNAPSHOT\n' "$release_version"
fi
}
prompt_with_default() {
local prompt="$1"
local default_value="$2"
local value
read -r -p "${prompt} [${default_value}]: " value
printf '%s\n' "${value:-$default_value}"
}
set_project_version() {
local new_version="$1"
sed -i "s/^version=.*/version=${new_version}/" gradle.properties
}
ensure_clean_worktree() {
if [[ -n "$(git status --porcelain)" ]]; then
echo "Release requires a clean git worktree." >&2
git status --short >&2
exit 1
fi
}
prepare_release() {
local release_deploy="$1"
local run_tests="$2"
local run_nxn="$3"
shift
shift
shift
local gradle_options=("$@")
local current_version release_default release_version next_default next_version confirm
local build_args
ensure_clean_worktree
current_version="$(project_version)"
release_default="$(release_version_of "$current_version")"
next_default="$(next_snapshot_of "$release_default")"
echo "Current version: ${current_version}"
release_version="$(prompt_with_default "Release version" "$release_default")"
next_default="$(next_snapshot_of "$release_version")"
next_version="$(prompt_with_default "Next development version" "$next_default")"
echo
echo "Release plan:"
echo " release version: ${release_version}"
echo " release tag: v${release_version}"
if [[ "$run_tests" == "true" ]]; then
echo " test: yes, before deploy"
fi
if [[ "$release_deploy" == "true" ]]; then
echo " deploy: yes, before next version"
fi
echo " next version: ${next_version}"
read -r -p "Continue? [y/N]: " confirm
if [[ "$confirm" != "y" && "$confirm" != "Y" ]]; then
echo "Release cancelled."
exit 1
fi
set_project_version "$release_version"
if [[ "$run_tests" == "true" || "$run_nxn" == "true" ]]; then
build_args=(package)
if [[ "$run_tests" == "true" ]]; then
build_args+=(test)
fi
if [[ "$run_nxn" == "true" ]]; then
build_args+=(nxn)
fi
if ! ./build.sh "${build_args[@]}" "${gradle_options[@]}"; then
set_project_version "$current_version"
echo "Release build failed. Version restored to ${current_version}." >&2
exit 1
fi
else
if ! ./build.sh package "${gradle_options[@]}"; then
set_project_version "$current_version"
echo "Release build failed. Version restored to ${current_version}." >&2
exit 1
fi
fi
git add gradle.properties
git commit -m "Release v${release_version}"
git tag -a "v${release_version}" -m "Release v${release_version}"
if [[ "$release_deploy" == "true" ]]; then
if ! ./build.sh deploy "${gradle_options[@]}"; then
echo "Deploy failed. Version remains at ${release_version}; next version was not committed." >&2
exit 1
fi
fi
set_project_version "$next_version"
git add gradle.properties
git commit -m "Next development version ${next_version}"
}
if [[ "$#" -eq 0 ]]; then
usage
exit 0
fi
mode="package"
mode_explicit="false"
run_tests="false"
run_nxn="false"
dry_run="false"
release_deploy="false"
gradle_args=()
for arg in "$@"; do
case "$arg" in
help|-h|--help)
usage
exit 0
;;
package)
mode="package"
mode_explicit="true"
;;
install)
mode="install"
mode_explicit="true"
;;
deploy)
if [[ "$mode" == "release" ]]; then
release_deploy="true"
else
mode="deploy"
fi
mode_explicit="true"
;;
release)
if [[ "$mode" == "deploy" ]]; then
release_deploy="true"
fi
mode="release"
mode_explicit="true"
;;
test)
run_tests="true"
;;
nxn)
run_nxn="true"
;;
--dry-run)
dry_run="true"
gradle_args+=("$arg")
;;
*)
gradle_args+=("$arg")
;;
esac
done
if [[ "$run_nxn" == "true" && "$run_tests" != "true" && "$mode_explicit" != "true" ]]; then
./runnxn.sh all "${gradle_args[@]}"
exit 0
fi
if [[ "$mode" == "release" ]]; then
if [[ "$dry_run" == "true" ]]; then
echo "Release does not support --dry-run because it creates commits and tags." >&2
exit 1
fi
prepare_release "$release_deploy" "$run_tests" "$run_nxn" "${gradle_args[@]}"
exit 0
fi
tasks=(build)
if [[ "$mode" == "install" || "$mode" == "deploy" ]]; then
tasks+=(publishToMavenLocal)
fi
if [[ "$mode" == "deploy" ]]; then
version="$(sed -n 's/^version=//p' gradle.properties)"
if [[ "$version" == *-SNAPSHOT ]]; then
echo "Maven Central deploy requires a release version, current version is ${version}." >&2
exit 1
fi
central_username="$(read_gradle_property 'maven.central.username')"
central_password="$(read_gradle_property 'maven.central.password')"
publishing_type="$(read_gradle_property 'maven.central.publishing_type')"
publishing_type="${publishing_type:-USER_MANAGED}"
if [[ "$publishing_type" != "AUTOMATIC" && "$publishing_type" != "USER_MANAGED" ]]; then
echo "maven.central.publishing_type must be AUTOMATIC or USER_MANAGED." >&2
exit 1
fi
for prop_name in maven.central.username maven.central.password maven.central.signing_key; do
prop_value="$(read_gradle_property "$prop_name")"
if [[ -z "$prop_value" ]]; then
echo "${prop_name} is required in ${gradle_user_properties} for deploy." >&2
exit 1
fi
done
central_dir="$PWD/build/central-bundle/repository"
central_zip="$PWD/build/central-bundle/central-bundle.zip"
tasks+=(publishAllPublicationsToCentralBundleRepository)
gradle_args+=("-PcentralRelease=true")
gradle_args+=("-PcentralBundleDir=${central_dir}")
fi
build_gradle_args=("${gradle_args[@]}")
if [[ "$run_tests" != "true" ]]; then
build_gradle_args+=("-x" "test")
fi
has_parallel_option="false"
has_max_workers_option="false"
for arg in "${gradle_args[@]}"; do
case "$arg" in
--parallel|--no-parallel)
has_parallel_option="true"
;;
--max-workers|--max-workers=*)
has_max_workers_option="true"
;;
esac
done
gradle_defaults=()
if [[ "$has_parallel_option" == "false" ]]; then
gradle_defaults+=(--parallel)
fi
if [[ "$has_max_workers_option" == "false" ]]; then
gradle_defaults+=(--max-workers 8)
fi
if [[ "$mode" == "deploy" && "$dry_run" != "true" ]]; then
rm -rf -- "$central_dir"
rm -f -- "$central_zip"
fi
./gradlew clean "${gradle_defaults[@]}" "${build_gradle_args[@]}"
./gradlew "${tasks[@]}" "${gradle_defaults[@]}" "${build_gradle_args[@]}"
if [[ "$run_nxn" == "true" ]]; then
./runnxn.sh all "${gradle_args[@]}"
fi
if [[ "$mode" == "deploy" && "$dry_run" != "true" ]]; then
find "$central_dir" -type f \
! -name '*.asc' ! -name '*.md5' ! -name '*.sha1' ! -name '*.sha256' ! -name '*.sha512' \
-print0 | while IFS= read -r -d '' file; do
md5sum "$file" | awk '{print $1}' > "${file}.md5"
sha1sum "$file" | awk '{print $1}' > "${file}.sha1"
sha256sum "$file" | awk '{print $1}' > "${file}.sha256"
sha512sum "$file" | awk '{print $1}' > "${file}.sha512"
done
rm -f "$central_zip"
jar --create --no-manifest --file "$central_zip" -C "$central_dir" .
token="$(printf '%s:%s' "$central_username" "$central_password" | base64 | tr -d '\n')"
deploy_id="$(curl --fail --silent --show-error --request POST \
--header "Authorization: Bearer ${token}" \
--form "bundle=@${central_zip};type=application/octet-stream" \
"https://central.sonatype.com/api/v1/publisher/upload?name=dbvisitor-${version}&publishingType=${publishing_type}")"
echo "Maven Central deployment id: ${deploy_id}"
fi