Repository navigation
Expand file tree
/
Copy pathgenerate.sh
More file actions
executable file
·66 lines (53 loc) · 3.24 KB
/
Copy pathgenerate.sh
File metadata and controls
executable file
·66 lines (53 loc) · 3.24 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
#!/usr/bin/env bash
set -euo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")"
DST_PATH=$(pwd)
SOURCE_PATH=${SOURCE_PATH:-../chat}
FIXTURES_DIR=tests/fixtures/webhooks
WORK=$(mktemp -d)
cleanup() {
git -C "$SOURCE_PATH" worktree remove --force "$WORK/chat" 2>/dev/null || true
rm -rf "$WORK"
}
trap cleanup EXIT
# The spec comes from a protocol release, so the generated code always matches a tag anyone can regenerate from. 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.
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; }
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
"$GENERATOR" generate-client --language dotnet --spec "$SPEC_FILE" --output "$DST_PATH"
# Webhook events dropped from the spec would otherwise leave stale fixtures behind.
rm -rf "$FIXTURES_DIR"
"$GENERATOR" generate-webhook-fixtures --output "$DST_PATH/$FIXTURES_DIR"
# The generator writes src/Models.cs, which only a case-insensitive file system folds into the tracked src/models.cs.
if [ -e src/Models.cs ] && ! [ src/Models.cs -ef src/models.cs ]; then mv src/Models.cs src/models.cs; fi
# A model carries both "role" and "Role"; drop the duplicate that System.Text.Json would reject.
perl -ni -e 'if (/\[JsonPropertyName\("Role"\)\]/) { <>; next } print' src/models.cs
# Rename Upload methods to match new naming convention
perl -pi -e 's/UploadFile/FileUpload/g; s/UploadImage/ImageUpload/g' src/Feed.cs tests/FeedIntegrationTests.cs src/CommonClient.cs
printf 'spec: %s\ngenerator: chat %s\n' "$SPEC_TAG" "$GENERATOR_VERSION" > .spec-version
echo "Formatting code..."
dotnet format
echo "Finished"