Repository navigation
Expand file tree
/
Copy pathbuild.sh
More file actions
executable file
·374 lines (323 loc) · 12.2 KB
/
Copy pathbuild.sh
File metadata and controls
executable file
·374 lines (323 loc) · 12.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
#!/bin/bash
SCRIPT_DIR=$(dirname "$(readlink -f "$0")")
PROJECT_ROOT="$SCRIPT_DIR"
export TEXINPUTS=.:$PROJECT_ROOT/tex/:
echo "TEXINPUTS=$TEXINPUTS"
function show_result {
ret_code=$?
# if return code is zero, then echo "Done" else echo "Failed"
if [ $ret_code -ne 0 ]; then
# echo a red "Failed"
echo -e "\033[0;31mFailed\033[0m"
else
# echo a gree "Done"
echo -e "\033[0;32mDone\033[0m"
fi
return $ret_code
}
function show_lize_result {
# if return code is zero, then echo "Done" else echo "Failed"
if [ $? -ne 0 ]; then
# echo a red "Failed"
echo -e "\033[0;31mFailed\033[0m"
tail -n 50 "build/$1.log"
echo "open build/$1.log to see the log."
else
# echo a gree "Done"
echo -e "\033[0;32mDone\033[0m"
fi
echo "Open build/$1.log to see the log."
echo "Open build/$1.tex to see the LaTeX source."
echo "Open output/$1.pdf to see the result."
}
function discover_primary_root {
if [ -n "${FOREST_PRIMARY_ROOT:-}" ]; then
return 0
fi
local jj_repo_dir git_common_dir
if [ -f "$PROJECT_ROOT/.jj/repo" ]; then
IFS= read -r jj_repo_dir < "$PROJECT_ROOT/.jj/repo"
FOREST_PRIMARY_ROOT=${jj_repo_dir%/.jj/repo}
fi
if [ -z "${FOREST_PRIMARY_ROOT:-}" ] && \
git_common_dir=$(git -C "$PROJECT_ROOT" rev-parse --git-common-dir 2>/dev/null); then
case "$git_common_dir" in
/*) ;;
*) git_common_dir="$PROJECT_ROOT/$git_common_dir" ;;
esac
FOREST_PRIMARY_ROOT=$(dirname "$git_common_dir")
fi
}
discover_primary_root
export FOREST_PRIMARY_ROOT
function wasm_package_has_output {
local pkg_dir=$1
[ -f "$pkg_dir/package.json" ] &&
compgen -G "$pkg_dir/*.js" > /dev/null &&
compgen -G "$pkg_dir/*.wasm" > /dev/null
}
function wasm_package_matches {
local pkg_dir=$1
local hash=$2
wasm_package_has_output "$pkg_dir" &&
[ -f "$pkg_dir/.commit_hash" ] &&
[ "$(cat "$pkg_dir/.commit_hash")" = "$hash" ]
}
function prep_wasm {
mkdir -p lib
local lib_name=$1
local url=$2
local hash=$3
local lib_path=${4:-$lib_name}
local pkg_dir="lib/$lib_path/pkg"
local hash_file="$pkg_dir/.commit_hash"
local needs_build=false
local cached_pkg
if [ -z "$CI" ] && [ -z "$UTS_DEV" ]; then
if ! wasm_package_matches "$pkg_dir" "$hash"; then
cached_pkg="$FOREST_PRIMARY_ROOT/lib/$lib_path/pkg"
if [ -n "$FOREST_PRIMARY_ROOT" ] && \
[ "$FOREST_PRIMARY_ROOT" != "$PROJECT_ROOT" ] && \
wasm_package_matches "$cached_pkg" "$hash"; then
mkdir -p "$pkg_dir"
cp -R "$cached_pkg"/. "$pkg_dir"/ || return 1
echo "Using pinned WASM package from the primary checkout for $lib_path"
fi
fi
if ! wasm_package_matches "$pkg_dir" "$hash"; then
echo "No pinned WASM package is available for $lib_name." >&2
echo "Run UTS_DEV=1 just build in the primary Forest checkout first." >&2
return 1
fi
cp "$pkg_dir"/*.wasm output/forest/ || return 1
return 0
fi
# A CI cache restores only the generated package, not its source checkout.
# The pinned commit marker and usable WASM output are sufficient to reuse it.
local package_ready=false
if wasm_package_matches "$pkg_dir" "$hash"; then
package_ready=true
fi
if [ "$package_ready" = "true" ]; then
echo "Using cached WASM package for $lib_name"
elif [ ! -d "lib/$lib_name/.git" ]; then
# No git repo (stale pkg-only cache restore or first run) — clone fresh
rm -rf "lib/$lib_name"
git clone --depth 1 "$url" "lib/$lib_name"
if [ -n "$hash" ]; then
(cd "lib/$lib_name" && git fetch --depth 1 origin "$hash" && git checkout "$hash")
fi
needs_build=true
elif [ -n "$hash" ] && [ "$(cd "lib/$lib_name" && git rev-parse HEAD)" != "$hash" ]; then
# Repo exists but pinned to wrong commit
(cd "lib/$lib_name" && git fetch --depth 1 origin "$hash" && git checkout "$hash")
needs_build=true
fi
if [ "$package_ready" != "true" ] && { [ ! -d "$pkg_dir" ] || [ -z "$(ls -A "$pkg_dir")" ]; }; then
needs_build=true
elif [ "$package_ready" != "true" ] && { [ ! -f "$hash_file" ] || [ "$(cat "$hash_file")" != "$hash" ]; }; then
needs_build=true
fi
# only run wasm-pack build in CI or for `dev.sh`, so other people would not need Rust dependencies
if [ -n "$CI" ] || [ -n "$UTS_DEV" ]; then
if [ "$needs_build" = "true" ]; then
echo "Building WASM package for $lib_name..."
if ! (cd "lib/$lib_path" && bunx wasm-pack -v build --target web --release --no-opt . --out-dir pkg); then
echo -e "\033[0;31mwasm-pack build failed for $lib_name\033[0m" >&2
return 1
fi
[ -d "lib/$lib_path/pkg" ] && echo "$hash" > "$hash_file"
elif [ "$package_ready" != "true" ]; then
echo "Using cached WASM package for $lib_name"
fi
fi
if ! wasm_package_has_output "$pkg_dir"; then
echo "Missing WASM output for $lib_name" >&2
return 1
fi
cp "lib/$lib_path"/pkg/*.wasm output/forest/ || return 1
}
function bun_build {
# don't run `bun install` for `dev.sh`
if [ -z "$UTS_DEV" ]; then
bun install --frozen-lockfile || return 1
fi
mkdir -p output/forest
prep_wasm wgputoy https://github.com/utensil/wgpu-compute-toy.git dd2dfbe5a686090ece17672de4e430a7980bd2d9 || return 1
prep_wasm egglog https://github.com/egraphs-good/egglog.git 8d9b10ec712106b21d10b7bf45d10c0f9d1d09c7 egglog/web-demo || return 1
prep_wasm rhaiscript https://github.com/rhaiscript/playground 9fa80661bc9eb69363ac86879826dcd8ccb604af || return 1
# failed:
# prep_wasm nalgebra https://github.com/dimforge/nalgebra
# for each files in the directory `bun`, run bun build
for FILE in $(ls -1 bun); do
# if the file extension is .css
if [[ $FILE == *".css" ]]; then
echo "🚀 lightningcss"
just css "bun/$FILE" || return 1
# check result
# EXIT_CODE=$?
# if [ $EXIT_CODE -ne 0 ]; then
# echo "🚨 lightningcss failed with $EXIT_CODE"
# exit $EXIT_CODE
# fi
elif [[ $FILE == *".ts" || $FILE == *".tsx" || $FILE == *".jsx" ]]; then
just js "bun/$FILE" || return 1
# bun build bun/$FILE --outdir output
fi
done
}
function build_ssr {
echo "⭐ Rebuilding SSR assets"
echo >build/ssr.log
bunx roger trios assets/penrose/*.trio.json -o output 1>>build/ssr.log 2>>build/ssr.log
}
function backup_xml_files() {
echo "⭐ Backing up XML files"
mkdir -p output/.bak
cp output/*.xml output/.bak/ 2>/dev/null || true
}
function needs_update() {
local xml_file=$1
local html_file=$2
local backup_file="output/.bak/$(basename "$xml_file")"
# If HTML doesn't exist, needs update
if [ ! -f "$html_file" ]; then
return 0
fi
# If backup doesn't exist (first run), needs update
if [ ! -f "$backup_file" ]; then
return 0
fi
# Compare current XML with backup
if ! cmp -s "$xml_file" "$backup_file"; then
return 0
fi
# Check if XSL template is newer than HTML file
if [ "assets/html.xsl" -nt "$html_file" ]; then
return 0
fi
return 1
}
source convert_xml.sh
function backup_html_before_forester() {
echo "⭐ Backing up HTML files before forester build"
local backup_dir="build/.html-bak"
rm -rf "$backup_dir"
mkdir -p "$backup_dir"
for html_file in output/forest/*/index.html; do
[ -f "$html_file" ] || continue
local note_id=$(basename $(dirname "$html_file"))
mkdir -p "$backup_dir/$note_id"
cp -f "$html_file" "$backup_dir/$note_id/index.html" 2>/dev/null || true
done
}
function restore_html_after_forester() {
echo "⭐ Restoring HTML files over forester redirect stubs"
local backup_dir="build/.html-bak"
local restored=0
for bak_file in "$backup_dir"/*/index.html; do
[ -f "$bak_file" ] || continue
local note_id=$(basename $(dirname "$bak_file"))
local html_file="output/forest/$note_id/index.html"
# Only restore if the current HTML is a redirect stub
if [ -f "$html_file" ] && grep -q 'meta.*http-equiv.*refresh.*index\.xml' "$html_file" 2>/dev/null; then
cp -f "$bak_file" "$html_file"
((restored++))
fi
done
rm -rf "$backup_dir"
echo " Restored $restored HTML files over redirect stubs"
}
function remove_forester_debug_trees() {
# AGENT-NOTE: Forester 5.0 writes source-like debug files beside public pages.
local debug_tree_count
debug_tree_count=$(find output/forest -type f -name index.tree | wc -l | tr -d ' ')
if [ "$debug_tree_count" -gt 0 ]; then
echo "⭐ Removing $debug_tree_count Forester debug tree file(s) from public output"
find output/forest -type f -name index.tree -delete
fi
# Earlier builds wrote duplicate HTML backups into the publish tree.
if [ -d output/forest/.html-bak ]; then
echo "⭐ Removing stale HTML backup files from public output"
rm -rf output/forest/.html-bak
fi
# Final Forester 5.0 writes its native executable bundle into static output.
# Forest's XSL renderer does not load it; its Base Theme JS is copied by `just assets`.
if [ -f output/forest/min.js ]; then
echo "⭐ Removing unused Forester native bundle from public output"
rm -f output/forest/min.js
fi
}
function write_root_redirect() {
# AGENT-NOTE: Forest publishes its home tree through the configured absolute site path.
local site_url home_tree target_path
site_url=$(sed -nE 's/^url = "https?:\/\/[^/]+([^\"]*)"/\1/p' forest.toml | head -1)
home_tree=$(sed -nE 's/^home = "([^\"]+)"/\1/p' forest.toml | head -1)
target_path="${site_url%/}/${home_tree}/"
if [ -z "$site_url" ] || [ -z "$home_tree" ]; then
echo "Error: forest.toml must define forest.url and forest.home" >&2
exit 1
fi
printf '<!DOCTYPE html>\n<html>\n <head>\n <meta http-equiv="refresh" content="0;url=%s" />\n <meta charset="utf-8" />\n </head>\n</html>\n' "$target_path" > output/forest/index.html
}
function build {
mkdir -p build
echo "⭐ Rebuilding bun"
if ! bun_build; then
echo -e "\033[0;31mError: browser asset build failed.\033[0m" >&2
return 1
fi
backup_xml_files
backup_html_before_forester
echo "⭐ Rebuilding forest"
if ! just forest; then
echo -e "\033[0;31mError: Forest build failed.\033[0m"
return 1
fi
restore_html_after_forester
remove_forester_debug_trees
write_root_redirect
# Check if index.xml was generated
# if [ ! -f "output/index.xml" ]; then
# echo -e "\033[0;31mError: index.xml not found in output directory. Forest build likely failed.\033[0m"
# exit 1
# fi
just assets || return 1
# if the env var UTS_DEV is not set
# if [ -z "$UTS_DEV" ]; then
convert_xml_files true || return 1
# build_ssr
# show_result
# echo "Open build/forester.log to see the log."
}
function lize {
./lize.sh spin-0001 # >/dev/null # 2>&1
show_lize_result spin-0001
./lize.sh hopf-0001 # >/dev/null # 2>&1
show_lize_result hopf-0001
./lize.sh ca-0001 # >/dev/null # 2>&1
show_lize_result ca-0001
./lize.sh fgap-0001 # >/dev/null # 2>&1
show_lize_result fgap-0001
./lize.sh fcap-0001 # >/dev/null # 2>&1
show_lize_result fcap-0001
./lize.sh connes-0001 # >/dev/null # 2>&1
show_lize_result connes-0001
./lize.sh tt-0001 # >/dev/null # 2>&1
show_lize_result tt-0001
./lize.sh uts-000C # >/dev/null # 2>&1
show_lize_result uts-000C
# ./lize.sh uts-0001 > /dev/null 2>&1
# show_lize_result uts-0001
}
if ! build; then
exit 1
fi
echo
# Generate the legacy local PDF set only when explicitly requested. CI builds
# the complete fixture set once in `just verify-pdf-fixtures`.
if [ -n "${LIZE:-}" ]; then
echo "⭐ Rebuilding LaTeX"
time lize
echo
fi