Skip to content

Commit a855e58

Browse files
committed
chore: use env var for chat dir
1 parent 7d03e31 commit a855e58

File tree

1 file changed

+68
-50
lines changed

1 file changed

+68
-50
lines changed

scripts/generate.sh

Lines changed: 68 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,98 +1,112 @@
11
#!/usr/bin/env bash
22
set -euo pipefail
33

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/generate.sh and set CHAT_DIR to your local chat repo path"
10-
exit 1
11-
fi
12-
13-
# =========================
14-
# Paths
15-
# =========================
4+
# ============================================================
5+
# Usage:
6+
# CHAT_DIR=/absolute/path/to/chat melos run gen:feeds
7+
# (or) export CHAT_DIR=... then: melos run gen:feeds
8+
# Requires: go, dart (and optionally flutter)
9+
# Melos sets MELOS_ROOT_PATH when invoked via `melos run`
10+
# ============================================================
11+
12+
# ---------- config (env-required) ----------
13+
: "${CHAT_DIR:?❌ CHAT_DIR not set.
14+
Please run with:
15+
CHAT_DIR=/path/to/chat melos run gen:feeds
16+
or export it in your shell/profile.}"
17+
18+
# ---------- paths ----------
1619
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
1720
REPO_ROOT="${MELOS_ROOT_PATH:-$(cd "${SCRIPT_DIR}/.." && pwd)}"
1821

1922
PKG_DIR="${REPO_ROOT}/packages/stream_feeds"
2023
OUTPUT_DIR_FEEDS="${PKG_DIR}/lib/src/generated/api"
21-
RENAMED_MODELS="${REPO_ROOT}/scripts/renamed-models.json"
24+
RENAMED_MODELS="${REPO_ROOT}/scripts/renamed-models.json" # optional
2225

2326
PRODUCTS="feeds,common,moderation"
2427
API_VERSION="v2"
28+
SPEC_DIR_REL="releases/${API_VERSION}"
29+
SPEC_BASENAME="feeds-clientside-api"
30+
SPEC_PATH="${SPEC_DIR_REL}/${SPEC_BASENAME}.yaml"
2531

26-
# =========================
27-
# Helpers
28-
# =========================
32+
# ---------- helpers ----------
33+
section() { echo ""; echo "$*"; echo ""; }
34+
# cross-platform sed -i (GNU vs BSD)
2935
sed_inplace() { if sed --version >/dev/null 2>&1; then sed -i "$@"; else sed -i '' "$@"; fi; }
3036

31-
# =========================
32-
# Validation
33-
# =========================
37+
# ---------- validation ----------
3438
[[ -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; }
39+
command -v go >/dev/null || { echo "❌ 'go' is required in PATH"; exit 1; }
3740
command -v dart >/dev/null || { echo "❌ 'dart' is required in PATH"; exit 1; }
3841

42+
# Optional renamed-models flag
43+
RENAMED_MODELS_FLAG=()
44+
if [[ -f "$RENAMED_MODELS" ]]; then
45+
RENAMED_MODELS_FLAG=(-renamed-models "$RENAMED_MODELS")
46+
echo "ℹ️ Using renamed-models.json: $RENAMED_MODELS"
47+
fi
48+
3949
echo ""
4050
echo "📂 Repo: $REPO_ROOT"
4151
echo "📦 Package: $PKG_DIR"
52+
echo "🗂 Output: $OUTPUT_DIR_FEEDS"
4253
echo "💬 CHAT_DIR: $CHAT_DIR"
4354
echo ""
4455

45-
# =========================
46-
# Generate spec & client
47-
# =========================
48-
echo "➡️ [1/4] Generating OpenAPI spec and Dart client..."
56+
# ---------- [1/4] Generate spec & client ----------
57+
section "➡️ [1/4] Generating OpenAPI spec and Dart client"
58+
59+
# Clean target & ensure parent exists
4960
rm -rf "$OUTPUT_DIR_FEEDS"
5061
mkdir -p "$OUTPUT_DIR_FEEDS"
5162

5263
(
5364
set -o pipefail
5465
cd "$CHAT_DIR"
5566

67+
# Generate OpenAPI spec (YAML)
5668
go run ./cmd/chat-manager openapi generate-spec \
5769
-products "$PRODUCTS" \
5870
-version "$API_VERSION" \
5971
--clientside \
6072
--encode-time-as-unix-timestamp \
61-
-output "releases/${API_VERSION}/feeds-clientside-api" \
62-
-renamed-models "$RENAMED_MODELS"
73+
-output "$SPEC_DIR_REL/$SPEC_BASENAME" \
74+
"${RENAMED_MODELS_FLAG[@]}"
6375

76+
# Generate Dart client into stream_feeds generated folder
6477
go run ./cmd/chat-manager openapi generate-client \
6578
--language dart \
66-
--spec "./releases/${API_VERSION}/feeds-clientside-api.yaml" \
79+
--spec "./$SPEC_PATH" \
6780
--output "$OUTPUT_DIR_FEEDS"
6881
)
69-
echo "✅ Finished generating client"
70-
echo ""
7182

72-
# =========================
73-
# Post-gen fixes
74-
# =========================
75-
echo "➡️ [2/4] Applying post-generation fixes..."
83+
section "✅ Finished generating client at: $OUTPUT_DIR_FEEDS"
84+
85+
# ---------- [2/4] Post-generation fixes ----------
86+
section "➡️ [2/4] Applying post-generation fixes…"
87+
7688
CALL_PARTICIPANT_FILE="$OUTPUT_DIR_FEEDS/model/call_participant.dart"
7789
if [[ -f "$CALL_PARTICIPANT_FILE" ]]; then
90+
# Remove duplicate constructor arg 'role'
7891
sed_inplace '/required this\.role,/{N;/required this\.role,.*\n.*required this\.role,/s/\n.*required this\.role,//;}' "$CALL_PARTICIPANT_FILE"
92+
# Remove duplicate field/override block for 'role'
7993
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"
8094
echo "• Fixed duplicate role in CallParticipant"
8195
fi
8296

8397
REACTION_GROUP_RESPONSE_FILE="$OUTPUT_DIR_FEEDS/model/reaction_group_response.dart"
8498
if [[ -f "$REACTION_GROUP_RESPONSE_FILE" ]]; then
99+
# Remove stray sumScores artifacts
85100
sed_inplace '/required this\.sumScores,/d' "$REACTION_GROUP_RESPONSE_FILE"
86101
sed_inplace '/@override/{N;/final int sumScores;/d;}' "$REACTION_GROUP_RESPONSE_FILE"
87102
echo "• Fixed extra sumScores in ReactionGroupResponse"
88103
fi
89-
echo "✅ Post-generation fixes applied"
90-
echo ""
91104

92-
# =========================
93-
# build_runner (package only)
94-
# =========================
95-
echo "➡️ [3/4] Running build_runner in stream_feeds..."
105+
section "✅ Post-generation fixes applied"
106+
107+
# ---------- [3/4] build_runner (package only) ----------
108+
section "➡️ [3/4] Running build_runner in stream_feeds…"
109+
96110
(
97111
cd "$PKG_DIR"
98112
if command -v flutter >/dev/null; then
@@ -101,18 +115,22 @@ echo "➡️ [3/4] Running build_runner in stream_feeds..."
101115
dart run build_runner build --delete-conflicting-outputs
102116
fi
103117
)
104-
echo "✅ build_runner completed"
105-
echo ""
106118

107-
# =========================
108-
# Format (package only)
109-
# =========================
110-
echo "➡️ [4/4] Formatting stream_feeds package..."
119+
section "✅ build_runner completed"
120+
121+
# ---------- [4/4] Format generated files only ----------
122+
section "➡️ [4/4] Formatting generated API files…"
123+
111124
(
112125
cd "$PKG_DIR"
113-
dart format "$OUTPUT_DIR_FEEDS" >/dev/null 2>&1 || true
126+
# Format only the generated directory; keep logs, ignore exit code
127+
dart format "$OUTPUT_DIR_FEEDS" || true
114128
)
115-
echo "✅ Formatting completed"
116-
echo ""
117129

118-
echo "🎉 All done! Generated + codegen + formatted: $PKG_DIR"
130+
section "✅ Formatting completed"
131+
132+
# ---------- summary ----------
133+
section "🎉 All done!"
134+
echo "• Spec: $CHAT_DIR/$SPEC_PATH"
135+
echo "• Client: $OUTPUT_DIR_FEEDS"
136+
echo ""

0 commit comments

Comments
 (0)