Repository navigation
Expand file tree
/
Copy pathgenerate.sh
More file actions
executable file
·179 lines (152 loc) · 7.15 KB
/
Copy pathgenerate.sh
File metadata and controls
executable file
·179 lines (152 loc) · 7.15 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
#!/usr/bin/env bash
set -euo pipefail
REPO_ROOT=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)
cd "$REPO_ROOT"
SOURCE_PATH=${SOURCE_PATH:-../chat}
MODELS_DIR=src/main/java/io/getstream/models
FIXTURES_DIR=src/test/resources/fixtures/webhooks
# Everything generate-client and generate-webhook-fixtures write. Snapshotted
# before the run so a failure anywhere can put the tree back exactly as it was.
# services/ and models/ also hold hand-written sources, which is fine: the
# snapshot restores whatever was there rather than whatever git has.
GENERATED_PATHS=(
"$MODELS_DIR"
src/main/java/io/getstream/services
src/main/java/io/getstream/Webhook.java
src/test/java/io/getstream/WebhookTest.java
"$FIXTURES_DIR"
)
# Building chat-manager pulls in a huge Go dependency graph. Without headroom the
# compiler dies with "no space left on device" partway through and leaves a
# half-generated tree behind, so check up front instead.
MIN_FREE_GB=${MIN_FREE_GB:-30}
# df needs a path that exists; the Go caches may not have been created yet.
existing_ancestor() {
local path=$1
while [ ! -e "$path" ] && [ "$path" != "/" ]; do
path=$(dirname "$path")
done
printf '%s' "$path"
}
mount_point() {
df -Pk "$1" | awk 'NR == 2 { for (i = 6; i <= NF; i++) printf "%s%s", (i > 6 ? " " : ""), $i }'
}
free_gb() {
df -Pk "$1" | awk 'NR == 2 { print int($4 / 1048576) }'
}
# The build spends its space in the Go build and module caches, which usually
# live under $HOME rather than on the repo or TMPDIR volume. Probe all of them so
# the check still means something when they sit on separate disks.
check_free_space() {
local volumes=("${TMPDIR:-/tmp}" "$REPO_ROOT" "$HOME") checked_mounts="" volume mount available
if command -v go >/dev/null 2>&1; then
volumes+=("$(go env GOCACHE)" "$(go env GOMODCACHE)")
fi
for volume in "${volumes[@]}"; do
[ -n "$volume" ] || continue
volume=$(existing_ancestor "$volume")
mount=$(mount_point "$volume")
# One message per filesystem, however many probed paths share it.
if printf '%s' "$checked_mounts" | grep -Fxq "$mount"; then
continue
fi
checked_mounts="${checked_mounts}${mount}"$'\n'
available=$(free_gb "$volume")
if [ "$available" -lt "$MIN_FREE_GB" ]; then
echo "only ${available}GB free on ${mount} (${volume}), need ~${MIN_FREE_GB}GB to build the generator"
echo "reclaim space with: go clean -cache && ./gradlew --stop && rm -rf ~/.gradle/caches/build-cache-*"
exit 1
fi
done
}
SNAPSHOT=$(mktemp -d)
WORK=$(mktemp -d)
ROLLBACK_ARMED=0
take_snapshot() {
local path
for path in "${GENERATED_PATHS[@]}"; do
[ -e "$path" ] || continue
mkdir -p "$SNAPSHOT/$(dirname "$path")"
cp -R "$path" "$SNAPSHOT/$(dirname "$path")/"
done
}
# Restores the pre-run contents, including uncommitted edits and untracked files
# that a git-based rollback would throw away.
restore_snapshot() {
local path
for path in "${GENERATED_PATHS[@]}"; do
rm -rf "$path"
if [ -e "$SNAPSHOT/$path" ]; then
mkdir -p "$(dirname "$path")"
cp -R "$SNAPSHOT/$path" "$(dirname "$path")/"
fi
done
}
cleanup() {
status=$?
if [ "$status" -ne 0 ] && [ "$ROLLBACK_ARMED" -eq 1 ]; then
echo "generation failed (exit ${status}), restoring generated sources"
restore_snapshot
fi
git -C "$SOURCE_PATH" worktree remove --force "$WORK/chat" 2>/dev/null || true
rm -rf "$SNAPSHOT" "$WORK"
}
trap cleanup EXIT
# The spec comes from a protocol release rather than from chat's working tree, so the
# generated code always corresponds to a tag someone else can regenerate from. protocol
# carries other tag namespaces, hence the openapi- prefix filter. SPEC_TAG=openapi-vX
# picks that release instead of the latest.
SPEC_TAG=${SPEC_TAG:-$(curl -fsSL -H "Accept: application/vnd.github+json" https://api.github.com/repos/GetStream/protocol/releases \
| jq -r 'map(select(.tag_name | startswith("openapi-"))) | .[0].tag_name')}
[ -n "$SPEC_TAG" ] && [ "$SPEC_TAG" != "null" ] || { echo "could not resolve the latest protocol openapi release"; exit 1; }
SPEC_FILE="$WORK/serverside-api.yaml"
curl -fsSL -o "$SPEC_FILE" "https://raw.githubusercontent.com/GetStream/protocol/${SPEC_TAG}/openapi/v2/serverside-api.yaml"
set -x
# GENERATOR_BIN=<path> runs an openapi binary attached to a chat release, so no chat checkout is needed; .spec-version records the version it reports.
# Otherwise the generator is built from chat at the spec's own tag, so the templates match the breaking-change list the regen PR check reads from that spec.
# GENERATOR_REF=HEAD builds from the ../chat checkout as it is, for an unreleased generator fix; the regen PR then has to be a major.
# The generator is built first so a compile failure never touches the SDK tree.
if [ -n "${GENERATOR_BIN:-}" ]; then
GENERATOR=$(cd "$(dirname "$GENERATOR_BIN")" && pwd)/$(basename "$GENERATOR_BIN")
GENERATOR_VERSION=$("$GENERATOR" --version)
GENERATOR_VERSION=${GENERATOR_VERSION##* }
else
[ -d "$SOURCE_PATH" ] || { echo "cannot find chat path on the parent folder (${SOURCE_PATH}), do you have a copy of the API source?"; exit 1; }
check_free_space
GENERATOR_REF=${GENERATOR_REF:-${SPEC_TAG#openapi-}}
GENERATOR_PATH=$SOURCE_PATH
if [ "$GENERATOR_REF" != HEAD ]; then
git -C "$SOURCE_PATH" fetch -q origin tag "$GENERATOR_REF"
GENERATOR_PATH=$WORK/chat
git -C "$SOURCE_PATH" worktree add -q --detach "$GENERATOR_PATH" "$GENERATOR_REF"
fi
make -C "$GENERATOR_PATH/tools/openapi" build
GENERATOR=$(cd "$GENERATOR_PATH" && pwd)/build/openapi
GENERATOR_VERSION=$(git -C "$GENERATOR_PATH" describe --tags --always --dirty)
fi
take_snapshot
ROLLBACK_ARMED=1
# Every file the generator writes here is regenerated from scratch, so wipe them
# first: schemas and webhook events dropped from the spec would otherwise survive
# as stale sources. models/framework is hand-written, hence the maxdepth.
find "$MODELS_DIR" -maxdepth 1 -name '*.java' -delete
rm -rf "$FIXTURES_DIR"
"$GENERATOR" generate-client --language java --spec "$SPEC_FILE" --output "$REPO_ROOT"
# Generate webhook conformance fixtures (CHA-2961). The test template reads them from
# src/test/resources/fixtures/webhooks/ and gracefully skips if the dir is missing.
"$GENERATOR" generate-webhook-fixtures --output "$REPO_ROOT/$FIXTURES_DIR"
# CallParticipant carries both "role" and "Role"; drop the duplicate that Jackson
# would reject. The model comes and goes across spec revisions, so patch it only
# when it is actually generated.
if [ -f src/main/java/io/getstream/models/CallParticipant.java ]; then
perl -i -0pe 's/ \@JsonProperty\("Role"\)\n private String role;\n//g' src/main/java/io/getstream/models/CallParticipant.java
fi
# Clean up test files that may exist in main source from older generator versions
# (generator now outputs tests directly to src/test/)
rm -f src/main/java/io/getstream/WebhookTest.java
printf 'spec: %s\ngenerator: chat %s\n' "$SPEC_TAG" "$GENERATOR_VERSION" > .spec-version
# Generated output is complete and coherent from here on: a compile failure is
# something to inspect, not something to roll back.
ROLLBACK_ARMED=0
# format generated code, clean stale Gradle/Spotless caches, then build
./gradlew clean spotlessApply build -x test