Skip to content

Commit 74fac2f

Browse files
committed
feat(llc): improve script for generating Feeds client
This commit introduces significant improvements to the script used for generating the Dart client for Stream Feeds. Key changes: - **Configuration:** Moved the `CHAT_DIR` configuration to the top of the script for easier modification by developers. - **Path Handling:** Implemented more robust path handling using `SCRIPT_DIR` and `REPO_ROOT` to make the script less dependent on absolute paths. - **Validation:** Added checks for the existence of `CHAT_DIR`, `renamed-models.json`, and the presence of `go` and `dart` in the system PATH. - **Clarity:** Added `echo` statements to provide better feedback to the user during the generation process. - **Sed Portability:** Replaced `sed -i ''` with a more portable `sed_inplace` function that works on both macOS and Linux. - **Scoped Build & Format:** Limited `build_runner` and `dart format` to only run on the `stream_feeds` package, improving efficiency. - **Output Directory:** Ensured the output directory for generated code is created if it doesn't exist. - **Error Handling:** Utilized `set -euo pipefail` for better script error handling. - **Melos Integration:** Added a new melos script `gen:feeds` to trigger this generation script. The script now performs the following steps: 1. Generates the OpenAPI spec and Dart client from the chat repository. 2. Applies post-generation fixes (e.g., removing duplicate fields in generated models). 3. Runs `build_runner` specifically for the `stream_feeds` package. 4. Formats the generated code within the `stream_feeds` package.
1 parent bc98033 commit 74fac2f

File tree

2 files changed

+107
-26
lines changed

2 files changed

+107
-26
lines changed

melos.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,3 +175,7 @@ scripts:
175175
npm install -g https://github.com/GetStream/stream-chat-docusaurus-cli &&
176176
npx stream-chat-docusaurus -i -s
177177
description: Runs the docusaurus documentation locally.
178+
179+
gen:feeds:
180+
run: $MELOS_ROOT_PATH/scripts/generate.sh
181+
description: Generate Feeds Dart client from Chat OpenAPI

scripts/generate.sh

Lines changed: 103 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,118 @@
11
#!/usr/bin/env bash
2+
set -euo pipefail
23

3-
CHAT_DIR=/Users/xsahil03x/WebstormProjects/chat
4-
OUTPUT_DIR_FEEDS=/Users/xsahil03x/StudioProjects/stream-feeds-flutter/packages/stream_feeds/lib/src/generated/api/
5-
6-
if [ ! -d $CHAT_DIR ]
7-
then
8-
echo "cannot find chat path on the parent folder (${CHAT_DIR}), do you have a copy of the API source?";
9-
exit 1;
4+
# =========================
5+
# CONFIG — each dev sets this ONCE
6+
# =========================
7+
CHAT_DIR="/ABSOLUTE/PATH/TO/YOUR/chat/repo"
8+
if [[ "$CHAT_DIR" == "/ABSOLUTE/PATH/TO/YOUR/chat/repo" ]]; then
9+
echo "❌ Please edit scripts/gen-feeds.sh and set CHAT_DIR to your local chat repo path"
10+
exit 1
1011
fi
1112

12-
set -ex
13+
# =========================
14+
# Paths
15+
# =========================
16+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
17+
REPO_ROOT="${MELOS_ROOT_PATH:-$(cd "${SCRIPT_DIR}/.." && pwd)}"
18+
19+
PKG_DIR="${REPO_ROOT}/packages/stream_feeds"
20+
OUTPUT_DIR_FEEDS="${PKG_DIR}/lib/src/generated/api"
21+
RENAMED_MODELS="${REPO_ROOT}/scripts/renamed-models.json"
22+
23+
PRODUCTS="feeds,common,moderation"
24+
API_VERSION="v2"
25+
26+
# =========================
27+
# Helpers
28+
# =========================
29+
sed_inplace() { if sed --version >/dev/null 2>&1; then sed -i "$@"; else sed -i '' "$@"; fi; }
1330

14-
# remove old generated code
15-
rm -rf /Users/xsahil03x/StudioProjects/stream-feeds-flutter/packages/stream_feeds/lib/src/generated/api/
31+
# =========================
32+
# Validation
33+
# =========================
34+
[[ -d "$CHAT_DIR" ]] || { echo "❌ CHAT_DIR not found: $CHAT_DIR"; exit 1; }
35+
[[ -f "$RENAMED_MODELS" ]] || { echo "❌ renamed-models.json not found: $RENAMED_MODELS"; exit 1; }
36+
command -v go >/dev/null || { echo "❌ 'go' is required in PATH"; exit 1; }
37+
command -v dart >/dev/null || { echo "❌ 'dart' is required in PATH"; exit 1; }
38+
39+
echo ""
40+
echo "📂 Repo: $REPO_ROOT"
41+
echo "📦 Package: $PKG_DIR"
42+
echo "💬 CHAT_DIR: $CHAT_DIR"
43+
echo ""
44+
45+
# =========================
46+
# Generate spec & client
47+
# =========================
48+
echo "➡️ [1/4] Generating OpenAPI spec and Dart client..."
49+
rm -rf "$OUTPUT_DIR_FEEDS"
50+
mkdir -p "$OUTPUT_DIR_FEEDS"
1651

17-
# cd in API repo, generate new spec and then generate code from it
1852
(
19-
cd $CHAT_DIR &&
20-
go run ./cmd/chat-manager openapi generate-spec -products feeds,common,moderation -version v2 -clientside -encode-time-as-unix-timestamp -output releases/v2/feeds-clientside-api -renamed-models /Users/xsahil03x/StudioProjects/stream-feeds-flutter/scripts/renamed-models.json &&
21-
go run ./cmd/chat-manager openapi generate-client --language dart --spec ./releases/v2/feeds-clientside-api.yaml --output $OUTPUT_DIR_FEEDS
53+
set -o pipefail
54+
cd "$CHAT_DIR"
55+
56+
go run ./cmd/chat-manager openapi generate-spec \
57+
-products "$PRODUCTS" \
58+
-version "$API_VERSION" \
59+
--clientside \
60+
--encode-time-as-unix-timestamp \
61+
-output "releases/${API_VERSION}/feeds-clientside-api" \
62+
-renamed-models "$RENAMED_MODELS"
63+
64+
go run ./cmd/chat-manager openapi generate-client \
65+
--language dart \
66+
--spec "./releases/${API_VERSION}/feeds-clientside-api.yaml" \
67+
--output "$OUTPUT_DIR_FEEDS"
2268
)
69+
echo "✅ Finished generating client"
70+
echo ""
2371

24-
# Fix duplicate role field in CallParticipant model (OpenAPI template bug)
72+
# =========================
73+
# Post-gen fixes
74+
# =========================
75+
echo "➡️ [2/4] Applying post-generation fixes..."
2576
CALL_PARTICIPANT_FILE="$OUTPUT_DIR_FEEDS/model/call_participant.dart"
26-
if [ -f "$CALL_PARTICIPANT_FILE" ]; then
27-
sed -i '' '/required this\.role,/{N; /required this\.role,.*\n.*required this\.role,/s/\n.*required this\.role,//;}' "$CALL_PARTICIPANT_FILE"
28-
sed -i '' '/final String role;/{N;N;N; /final String role;.*\n.*\n.*@override.*\n.*final String role;/s/\n.*\n.*@override.*\n.*final String role;//;}' "$CALL_PARTICIPANT_FILE"
29-
echo "Fixed duplicate role field in CallParticipant model using sed"
77+
if [[ -f "$CALL_PARTICIPANT_FILE" ]]; then
78+
sed_inplace '/required this\.role,/{N;/required this\.role,.*\n.*required this\.role,/s/\n.*required this\.role,//;}' "$CALL_PARTICIPANT_FILE"
79+
sed_inplace '/final String role;/{N;N;N;/final String role;.*\n.*\n.*@override.*\n.*final String role;/s/\n.*\n.*@override.*\n.*final String role;//;}' "$CALL_PARTICIPANT_FILE"
80+
echo "Fixed duplicate role in CallParticipant"
3081
fi
3182

32-
# Fix extra sumScores field in ReactionGroupResponse model (OpenAPI template bug)
3383
REACTION_GROUP_RESPONSE_FILE="$OUTPUT_DIR_FEEDS/model/reaction_group_response.dart"
34-
if [ -f "$REACTION_GROUP_RESPONSE_FILE" ]; then
35-
sed -i '' '/required this\.sumScores,/d' "$REACTION_GROUP_RESPONSE_FILE"
36-
sed -i '' '/@override/{N; /final int sumScores;/d;}' "$REACTION_GROUP_RESPONSE_FILE"
37-
echo "Fixed extra sumScores field in ReactionGroupResponse model using sed"
84+
if [[ -f "$REACTION_GROUP_RESPONSE_FILE" ]]; then
85+
sed_inplace '/required this\.sumScores,/d' "$REACTION_GROUP_RESPONSE_FILE"
86+
sed_inplace '/@override/{N;/final int sumScores;/d;}' "$REACTION_GROUP_RESPONSE_FILE"
87+
echo "Fixed extra sumScores in ReactionGroupResponse"
3888
fi
89+
echo "✅ Post-generation fixes applied"
90+
echo ""
91+
92+
# =========================
93+
# build_runner (package only)
94+
# =========================
95+
echo "➡️ [3/4] Running build_runner in stream_feeds..."
96+
(
97+
cd "$PKG_DIR"
98+
if command -v flutter >/dev/null; then
99+
flutter pub run build_runner build --delete-conflicting-outputs
100+
else
101+
dart run build_runner build --delete-conflicting-outputs
102+
fi
103+
)
104+
echo "✅ build_runner completed"
105+
echo ""
106+
107+
# =========================
108+
# Format (package only)
109+
# =========================
110+
echo "➡️ [4/4] Formatting stream_feeds package..."
111+
(
112+
cd "$PKG_DIR"
113+
dart format "$OUTPUT_DIR_FEEDS" >/dev/null 2>&1 || true
114+
)
115+
echo "✅ Formatting completed"
116+
echo ""
39117

40-
melos generate:all
41-
melos format
118+
echo "🎉 All done! Generated + codegen + formatted: $PKG_DIR"

0 commit comments

Comments
 (0)