Repository navigation
Expand file tree
/
Copy path_build.sh
More file actions
381 lines (333 loc) · 13.2 KB
/
Copy path_build.sh
File metadata and controls
381 lines (333 loc) · 13.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#!/usr/bin/env bash
set -euo pipefail
# ─── Usage ───────────────────────────────────────────────────────────
usage() {
cat <<'EOF'
Usage: ./_build.sh [check|build|release] [options]
Tasks:
check - Run typecheck, tests, and prettier
build - Rebuild OpenTUI (Zig+TS) then opencode; collect artifacts to dist/
release - Run checks, build, and create release manifest
Options:
--version <ver> Override version for release (default: from package.json)
--skip-tests Skip test execution
--skip-typecheck Skip typecheck
Prerequisites (run before build):
python prompts_kernel/_assemble_prompts_kernel.py — kernel assembly
./_opentui.sh — OpenTUI Zig+TS rebuild
./_opentui.sh true — full OpenTUI monorepo
Typical workflow:
python prompts_kernel/_assemble_prompts_kernel.py # assemble + validate kernel
./_opentui.sh # build OpenTUI native + TS
./_build.sh # compile opencode + collect dist/
OpenTUI build chain:
packages/opentui/packages/core → bun run build (build:native + build:lib)
packages/opentui/packages/solid → bun run build
packages/opentui/packages/three → bun run build
then packages/opencode script/build.ts --single (copies native lib into compile)
EOF
exit 1
}
# ─── Helpers ─────────────────────────────────────────────────────────
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
CYAN='\033[0;36m'
WHITE='\033[1;37m'
NC='\033[0m'
step() { echo -e "\n${CYAN}════════════════════════════════════════${NC}"; echo -e "${WHITE} $*${NC}"; echo -e "${CYAN}════════════════════════════════════════${NC}\n"; }
ok() { echo -e " ${GREEN}[OK]${NC} $*"; }
fail() { echo -e " ${RED}[FAIL]${NC} $*"; }
warn() { echo -e " ${YELLOW}[-]${NC} $*"; }
run_check() {
local name="$1" script="$2"
if eval "$script"; then
ok "$name passed"
return 0
else
fail "$name failed"
return 1
fi
}
get_version() {
node -e "console.log(require('$ROOT/packages/opencode/package.json').version)"
}
# ─── Platform detection ──────────────────────────────────────────────
detect_platform() {
PLATFORM_OS="$(uname -s | tr '[:upper:]' '[:lower:]')"
PLATFORM_ARCH="$(uname -m)"
case "$PLATFORM_OS" in
darwin) PLATFORM_OS="darwin" ;;
linux) PLATFORM_OS="linux" ;;
*) echo "Unsupported OS: $PLATFORM_OS" >&2; exit 1 ;;
esac
case "$PLATFORM_ARCH" in
x86_64|amd64) PLATFORM_ARCH="x64" ;;
arm64|aarch64) PLATFORM_ARCH="arm64" ;;
*) echo "Unsupported arch: $PLATFORM_ARCH" >&2; exit 1 ;;
esac
# Native naming conventions
case "$PLATFORM_OS" in
darwin)
NATIVE_EXT="dylib"
NATIVE_PREFIX="lib"
OPENCODE_PLATFORM="opencode-darwin-${PLATFORM_ARCH}"
OPENTUI_PLATFORM="core-darwin-${PLATFORM_ARCH}"
;;
linux)
NATIVE_EXT="so"
NATIVE_PREFIX="lib"
OPENCODE_PLATFORM="opencode-linux-${PLATFORM_ARCH}"
OPENTUI_PLATFORM="core-linux-${PLATFORM_ARCH}"
;;
esac
}
# ─── Root paths ──────────────────────────────────────────────────────
ROOT="$(cd "$(dirname "$0")" && pwd)"
DIST_DIR="$ROOT/dist"
OPENCODE_PKG="$ROOT/packages/opencode"
OPENTUI_ROOT="$ROOT/packages/opentui"
# ── Prerequisites (run separately before build) ──
# python prompts_kernel/_assemble_prompts_kernel.py — kernel assembly
# ./_opentui.sh — OpenTUI Zig+TS rebuild
# ./_opentui.sh true — full OpenTUI monorepo
# ─── CHECK TASK ──────────────────────────────────────────────────────
task_check() {
step "Running Checks"
local all_passed=true
# Clean .temp/test/
if [ -d "$ROOT/.temp/test" ]; then
echo -e " ${YELLOW}Cleaning .temp/test/...${NC}"
rm -rf "$ROOT/.temp/test"
ok ".temp/test/ cleaned"
fi
# Typecheck
if [ "$SKIP_TYPECHECK" = "false" ]; then
run_check "Typecheck" "(cd '$OPENCODE_PKG' && bun typecheck)" || all_passed=false
else
warn "Skipping typecheck (--skip-typecheck)"
fi
# Tests
if [ "$SKIP_TESTS" = "false" ]; then
run_check "Tests" "(cd '$OPENCODE_PKG' && bun test)" || all_passed=false
else
warn "Skipping tests (--skip-tests)"
fi
# Prettier
if bun run prettier --check "packages/opencode/src/**/*.ts" 2>&1; then
ok "Prettier passed"
else
fail "Prettier failed"
all_passed=false
fi
if [ "$all_passed" = "false" ]; then
echo -e "\n${RED}Some checks failed. Fix the issues above and try again.${NC}"
exit 1
fi
ok "All checks passed"
}
# ─── BUILD TASK ──────────────────────────────────────────────────────
task_build() {
step "Building"
# Clean .temp/test/
if [ -d "$ROOT/.temp/test" ]; then
echo -e " ${YELLOW}Cleaning .temp/test/...${NC}"
rm -rf "$ROOT/.temp/test"
ok ".temp/test/ cleaned"
fi
# Build Rust WASM modules
echo -e " ${YELLOW}Building Rust WASM modules...${NC}"
if [ -f "$ROOT/_build_rust.sh" ]; then
bash "$ROOT/_build_rust.sh"
elif [ -f "$ROOT/_build_rust.ps1" ]; then
warn "_build_rust.sh not found — skipping Rust WASM (PowerShell-only _build_rust.ps1 present)"
else
warn "No Rust build script found — skipping WASM modules"
fi
# Clean dist
rm -rf "$DIST_DIR"
mkdir -p "$DIST_DIR"
# Build opencode package
echo -e " ${YELLOW}Building packages...${NC}"
(cd "$OPENCODE_PKG" && bun run script/build.ts --single) || { fail "opencode script/build.ts failed"; exit 1; }
ok "opencode package built"
# ─── Collect artifacts ─────────────────────────────────────────────
echo -e " ${YELLOW}Collecting release artifacts...${NC}"
# CLI script
local cli_js="$OPENCODE_PKG/dist/cli.js"
if [ -f "$cli_js" ]; then
cp "$cli_js" "$DIST_DIR/cli.js"
ok "CLI script copied"
fi
# Native binary
local native_bin="$OPENCODE_PKG/dist/${OPENCODE_PLATFORM}/bin/opencode"
if [ -f "$native_bin" ]; then
mkdir -p "$DIST_DIR/bin"
cp "$native_bin" "$DIST_DIR/bin/opencode"
chmod +x "$DIST_DIR/bin/opencode"
ok "Native binary copied (${OPENCODE_PLATFORM})"
fi
# Native markdownify binary
local markdownify_src="$ROOT/packages/native/markdownify/target/release/opencode-markdownify"
local markdownify_dest="$OPENCODE_PKG/dist/${OPENCODE_PLATFORM}/bin/opencode-markdownify"
if [ -f "$markdownify_src" ]; then
mkdir -p "$(dirname "$markdownify_dest")"
cp "$markdownify_src" "$markdownify_dest"
ok "Markdownify binary staged to platform dist"
fi
local markdownify_bin="$OPENCODE_PKG/dist/${OPENCODE_PLATFORM}/bin/opencode-markdownify"
if [ -f "$markdownify_bin" ]; then
mkdir -p "$DIST_DIR/bin"
cp "$markdownify_bin" "$DIST_DIR/bin/opencode-markdownify"
chmod +x "$DIST_DIR/bin/opencode-markdownify"
ok "Markdownify binary copied"
fi
# Native opentui lib
local opentui_lib="$OPENTUI_ROOT/packages/${OPENTUI_PLATFORM}/${NATIVE_PREFIX}opentui.${NATIVE_EXT}"
if [ -f "$opentui_lib" ]; then
# Copy to platform dist
local opentui_platform_dest="$OPENCODE_PKG/dist/${OPENCODE_PLATFORM}/bin"
mkdir -p "$opentui_platform_dest"
cp "$opentui_lib" "$opentui_platform_dest/${NATIVE_PREFIX}opentui.${NATIVE_EXT}"
# Copy to final dist/bin
mkdir -p "$DIST_DIR/bin"
cp "$opentui_lib" "$DIST_DIR/bin/${NATIVE_PREFIX}opentui.${NATIVE_EXT}"
ok "opentui native lib copied (from rebuilt ${OPENTUI_PLATFORM})"
else
fail "opentui native lib not found at $opentui_lib — build OpenTUI first: ./_opentui.sh"
exit 1
fi
# WASM sidecars: mirror packages/wasm/core/pkg as-is (no hardcoded asset list).
local wasm_src="$ROOT/packages/wasm/core/pkg"
local wasm_dst="$DIST_DIR/wasm/core/pkg"
if [ -d "$wasm_src" ]; then
rm -rf "$wasm_dst"
mkdir -p "$(dirname "$wasm_dst")"
cp -a "$wasm_src" "$wasm_dst"
# Tree-sitter runtime lives in node_modules, not pkg/ — stage if present.
local ts_wasm
ts_wasm=$(find "$ROOT/node_modules" -name "web-tree-sitter.wasm" -not -path "*/debug/*" 2>/dev/null | head -1)
if [ -n "$ts_wasm" ]; then
cp "$ts_wasm" "$wasm_dst/web-tree-sitter.wasm"
fi
local wasm_count
wasm_count=$(find "$wasm_dst" -type f -name '*.wasm' | wc -l | tr -d ' ')
if [ "${wasm_count:-0}" -lt 1 ]; then
fail "WASM mirror produced zero .wasm files under $wasm_dst"
exit 1
fi
ok "WASM modules mirrored to dist ($wasm_count .wasm files under wasm/core/pkg)"
else
warn "packages/wasm/core/pkg missing — skipping WASM sidecar copy"
fi
# SDK
local sdk_dir="$ROOT/packages/sdk/js/dist"
if [ -d "$sdk_dir" ]; then
cp -r "$sdk_dir" "$DIST_DIR/sdk"
ok "SDK copied"
fi
# App build
local app_dist="$ROOT/packages/app/dist"
if [ -d "$app_dist" ]; then
cp -r "$app_dist" "$DIST_DIR/app"
ok "App build copied"
fi
# Local services (artifacts_dist/)
if [ -d "$ROOT/artifacts_dist" ]; then
mkdir -p "$DIST_DIR/bin"
for svc_dir in "$ROOT/artifacts_dist"/*/; do
[ -d "$svc_dir" ] || continue
local svc_name
svc_name=$(basename "$svc_dir")
cp -r "$svc_dir" "$DIST_DIR/bin/$svc_name"
ok "Service $svc_name copied from artifacts_dist/"
done
fi
# Package.json files
for pkg_dir in "$ROOT/packages"/*/; do
[ -d "$pkg_dir" ] || continue
local pkg_json="$pkg_dir/package.json"
if [ -f "$pkg_json" ]; then
local pkg_name
pkg_name=$(basename "$pkg_dir")
mkdir -p "$DIST_DIR/packages/$pkg_name"
cp "$pkg_json" "$DIST_DIR/packages/$pkg_name/"
fi
done
ok "Build complete - artifacts in dist/"
}
# ─── RELEASE TASK ────────────────────────────────────────────────────
task_release() {
local release_version="$VERSION"
step "Creating Release $release_version"
# Run checks first
if [ "$SKIP_TYPECHECK" = "false" ] && [ "$SKIP_TESTS" = "false" ]; then
task_check
fi
# Build
task_build
# Get version if not provided
if [ -z "$release_version" ]; then
release_version=$(get_version)
fi
# Create release manifest
local manifest="$DIST_DIR/release-manifest.json"
node -e "
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
function walk(dir, base) {
const entries = [];
for (const entry of fs.readdirSync(dir, { recursive: true, withFileTypes: true })) {
// Node 20 compat: fs.readdirSync recursive returns strings, not Dirent
const fullPath = path.join(dir, entry);
if (fs.statSync(fullPath).isFile()) {
entries.push({ path: path.relative(base, fullPath).replace(/\\\\/g, '/'), size: fs.statSync(fullPath).size });
}
}
return entries;
}
const manifest = {
version: '$release_version',
buildTime: new Date().toISOString(),
gitSha: execSync('git rev-parse HEAD').toString().trim(),
gitBranch: execSync('git rev-parse --abbrev-ref HEAD').toString().trim(),
nodeVersion: process.version,
artifacts: walk('$DIST_DIR', '$DIST_DIR')
};
fs.writeFileSync('$manifest', JSON.stringify(manifest, null, 2));
console.log('Release manifest created');
" || { fail "Failed to create release manifest"; exit 1; }
ok "Release manifest created"
echo -e "\n${GREEN}════════════════════════════════════════${NC}"
echo -e "${GREEN} Release $release_version ready in dist/${NC}"
echo -e "${GREEN}════════════════════════════════════════${NC}\n"
echo -e "${YELLOW}Next steps:${NC}"
echo -e " 1. Review dist/ contents"
echo -e " 2. git add dist/ && git commit -m 'release: v$release_version'"
echo -e " 3. git tag v$release_version"
echo -e " 4. git push origin && git push origin v$release_version"
echo ""
}
# ─── MAIN ────────────────────────────────────────────────────────────
TASK="${1:-build}"
shift 2>/dev/null || true
SKIP_TESTS="false"
SKIP_TYPECHECK="false"
VERSION=""
while [ $# -gt 0 ]; do
case "$1" in
--skip-tests) SKIP_TESTS="true"; shift ;;
--skip-typecheck) SKIP_TYPECHECK="true"; shift ;;
--version) VERSION="$2"; shift 2 ;;
-h|--help) usage ;;
*) echo "Unknown option: $1"; usage ;;
esac
done
detect_platform
case "$TASK" in
check) task_check ;;
build) task_build ;;
release) task_release ;;
*) usage ;;
esac