-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_bundle_core.sh
More file actions
executable file
·338 lines (306 loc) · 11.5 KB
/
Copy pathsync_bundle_core.sh
File metadata and controls
executable file
·338 lines (306 loc) · 11.5 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
#!/usr/bin/env bash
# Shared digest-pinned bundle sync core for App-References (#174).
#
# Sourced by thin per-platform wrappers (`sync_*_bundle.sh`). Do not invoke
# this file as a standalone CI gate — use the named wrappers so platform
# destinations stay explicit.
#
# Pin source (single contract):
# $TRAVERSE_REPO/runtime/runtime.wasm
# $TRAVERSE_REPO/runtime/runtime-release.json # sha256 must match wasm
#
# App manifests always come from this repo:
# $REPO_ROOT/manifests/<app_id>/
#
# Optional Traverse example trees (web / WinUI):
# $TRAVERSE_REPO/examples/<app_id>/
# $TRAVERSE_REPO/workflows/examples/<app_id>/
# $TRAVERSE_REPO/contracts/examples/<app_id>/
#
# Public surfaces only — no private Traverse internals.
#
# Usage (from a wrapper):
# # shellcheck source=scripts/ci/sync_bundle_core.sh
# source "$(cd "$(dirname "$0")" && pwd)/sync_bundle_core.sh"
# sync_bundle_init
# sync_bundle_destination \
# --dest "$DEST" \
# --app traverse-starter \
# --components validate,process,summarize \
# --manifest-layout root \
# --runtime required \
# --traverse-assets required \
# --label "web starter"
set -euo pipefail
sync_bundle_init() {
SYNC_BUNDLE_SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SYNC_BUNDLE_SCRIPT_DIR/../.." && pwd)"
TRAVERSE_REPO="${TRAVERSE_REPO:-$REPO_ROOT/../Traverse}"
}
sync_bundle_sha256_file() {
local file="$1"
if command -v sha256sum >/dev/null 2>&1; then
sha256sum "$file" | awk '{print $1}'
else
shasum -a 256 "$file" | awk '{print $1}'
fi
}
# Verify runtime.wasm bytes match sibling runtime-release.json sha256.
sync_bundle_verify_runtime_digest() {
local runtime_dir="$1"
local wasm="$runtime_dir/runtime.wasm"
local meta="$runtime_dir/runtime-release.json"
if [ ! -f "$wasm" ] || [ ! -f "$meta" ]; then
echo "FAIL: missing runtime.wasm or runtime-release.json under $runtime_dir" >&2
return 1
fi
local expected actual
expected="$(python3 -c 'import json,sys; print(json.load(open(sys.argv[1]))["sha256"])' "$meta")"
actual="$(sync_bundle_sha256_file "$wasm")"
if [ "$expected" != "$actual" ]; then
echo "FAIL: runtime.wasm digest mismatch under $runtime_dir" >&2
echo " expected=$expected" >&2
echo " actual=$actual" >&2
return 1
fi
echo "OK: runtime.wasm digest matches runtime-release.json ($expected)"
}
sync_bundle_copy_component_manifests() {
local manifest_src="$1"
local dest="$2"
local components_csv="$3"
local IFS=','
local name
# shellcheck disable=SC2086
for name in $components_csv; do
mkdir -p "$dest/components/$name"
cp "$manifest_src/components/$name/component.manifest.json" \
"$dest/components/$name/"
done
}
sync_bundle_copy_manifests_root() {
local manifest_src="$1"
local dest="$2"
local components_csv="$3"
cp "$manifest_src/app.manifest.json" "$dest/"
sync_bundle_copy_component_manifests "$manifest_src" "$dest" "$components_csv"
}
sync_bundle_copy_manifests_subdir() {
local manifest_src="$1"
local dest="$2"
mkdir -p "$dest/manifests"
# Copy manifests only — never follow/copy Phase 2 `_traverse` checkout symlinks.
if command -v rsync >/dev/null 2>&1; then
rsync -a --exclude '_traverse' "$manifest_src/" "$dest/manifests/"
else
# Fallback without rsync: copy tree then drop symlink if present.
cp -a "$manifest_src/." "$dest/manifests/"
rm -f "$dest/manifests/_traverse"
fi
}
sync_bundle_copy_runtime() {
local dest="$1"
local mode="$2" # required|none
if [ "$mode" = "none" ]; then
return 0
fi
if [ ! -f "$TRAVERSE_REPO/runtime/runtime.wasm" ]; then
echo "FAIL: TRAVERSE_REPO runtime missing: $TRAVERSE_REPO/runtime/runtime.wasm" >&2
return 1
fi
if [ ! -f "$TRAVERSE_REPO/runtime/runtime-release.json" ]; then
echo "FAIL: TRAVERSE_REPO runtime missing: $TRAVERSE_REPO/runtime/runtime-release.json" >&2
return 1
fi
mkdir -p "$dest/runtime"
cp "$TRAVERSE_REPO/runtime/runtime.wasm" "$dest/runtime/"
cp "$TRAVERSE_REPO/runtime/runtime-release.json" "$dest/runtime/"
sync_bundle_verify_runtime_digest "$dest/runtime"
}
sync_bundle_copy_traverse_assets() {
local dest="$1"
local app_id="$2"
local mode="$3" # required|optional|none
if [ "$mode" = "none" ]; then
return 0
fi
local examples="$TRAVERSE_REPO/examples/$app_id"
local workflows="$TRAVERSE_REPO/workflows/examples/$app_id"
local contracts="$TRAVERSE_REPO/contracts/examples/$app_id"
if [ "$mode" = "required" ]; then
if [ ! -d "$examples" ]; then
echo "FAIL: TRAVERSE_REPO examples missing: $examples" >&2
return 1
fi
if [ ! -d "$workflows" ]; then
echo "FAIL: TRAVERSE_REPO workflows missing: $workflows" >&2
return 1
fi
if [ ! -d "$contracts" ]; then
echo "FAIL: TRAVERSE_REPO contracts missing: $contracts" >&2
return 1
fi
fi
mkdir -p \
"$dest/_traverse/examples/$app_id" \
"$dest/_traverse/workflows/examples/$app_id" \
"$dest/_traverse/contracts/examples/$app_id"
if [ -d "$examples" ]; then
if command -v rsync >/dev/null 2>&1; then
rsync -a "$examples/" "$dest/_traverse/examples/$app_id/"
else
cp -a "$examples/." "$dest/_traverse/examples/$app_id/"
fi
elif [ "$mode" = "required" ]; then
echo "FAIL: examples directory vanished: $examples" >&2
return 1
fi
if [ -d "$workflows" ]; then
if command -v rsync >/dev/null 2>&1; then
rsync -a "$workflows/" "$dest/_traverse/workflows/examples/$app_id/"
else
cp -a "$workflows/." "$dest/_traverse/workflows/examples/$app_id/"
fi
elif [ "$mode" = "required" ]; then
echo "FAIL: workflows directory vanished: $workflows" >&2
return 1
fi
if [ -d "$contracts" ]; then
if command -v rsync >/dev/null 2>&1; then
rsync -a "$contracts/" "$dest/_traverse/contracts/examples/$app_id/"
else
cp -a "$contracts/." "$dest/_traverse/contracts/examples/$app_id/"
fi
elif [ "$mode" = "required" ]; then
echo "FAIL: contracts directory vanished: $contracts" >&2
return 1
fi
}
# Sync one destination directory. Flags:
# --dest PATH (required)
# --app APP_ID (required)
# --components csv (required when manifest-layout=root)
# --manifest-layout root|subdir (default: root)
# --runtime required|none (default: none)
# --traverse-assets required|optional|none (default: none)
# --label TEXT (default: bundle)
sync_bundle_destination() {
local dest="" app_id="" components="" label="bundle"
local manifest_layout="root" runtime_mode="none" traverse_assets="none"
while [ "$#" -gt 0 ]; do
case "$1" in
--dest) dest="$2"; shift 2 ;;
--app) app_id="$2"; shift 2 ;;
--components) components="$2"; shift 2 ;;
--manifest-layout) manifest_layout="$2"; shift 2 ;;
--runtime) runtime_mode="$2"; shift 2 ;;
--traverse-assets) traverse_assets="$2"; shift 2 ;;
--label) label="$2"; shift 2 ;;
*)
echo "FAIL: unknown sync_bundle_destination flag: $1" >&2
return 1
;;
esac
done
if [ -z "$dest" ] || [ -z "$app_id" ]; then
echo "FAIL: sync_bundle_destination requires --dest and --app" >&2
return 1
fi
local manifest_src="$REPO_ROOT/manifests/$app_id"
if [ ! -d "$manifest_src" ]; then
echo "FAIL: app manifests missing: $manifest_src" >&2
return 1
fi
rm -rf "$dest"
mkdir -p "$dest"
case "$manifest_layout" in
root)
if [ -z "$components" ]; then
echo "FAIL: --components required for --manifest-layout root" >&2
return 1
fi
sync_bundle_copy_manifests_root "$manifest_src" "$dest" "$components"
;;
subdir)
sync_bundle_copy_manifests_subdir "$manifest_src" "$dest"
;;
*)
echo "FAIL: unknown --manifest-layout=$manifest_layout (use root|subdir)" >&2
return 1
;;
esac
sync_bundle_copy_runtime "$dest" "$runtime_mode"
sync_bundle_copy_traverse_assets "$dest" "$app_id" "$traverse_assets"
sync_bundle_materialize_registry_refs "$dest" "$app_id"
echo "OK: synced $label → $dest"
}
# Convert registry_ref-only component manifests in $dest into local wasm_*
# for FetchBundleLoader / embedded hosts that still require wasm_binary_path.
# Canonical source under manifests/ stays registry_ref; destinations materialize.
# Also copies the referenced example WASM + contract into dest/_traverse when missing
# (Android/Swift sync with --traverse-assets none).
sync_bundle_materialize_registry_refs() {
local dest="$1"
local app_id="$2"
REPO_ROOT="$REPO_ROOT" TRAVERSE_REPO="$TRAVERSE_REPO" DEST="$dest" APP_ID="$app_id" python3 - <<'PY'
import hashlib, json, os, pathlib, shutil, sys
dest = pathlib.Path(os.environ["DEST"])
traverse = pathlib.Path(os.environ["TRAVERSE_REPO"])
app_id = os.environ["APP_ID"]
KNOWN = {
("traverse-starter", "traverse-starter.process"): {
"cap": "process",
"stem": "process-agent",
},
}
components_root = dest / "components"
if not components_root.is_dir():
nested = dest / "manifests" / "components"
# Android/Swift subdir layout: dest/manifests/{app.manifest,components}
if not nested.is_dir():
nested = dest / "manifests" / app_id / "components"
components_root = nested if nested.is_dir() else components_root
if not components_root.is_dir():
sys.exit(0)
# App root that owns components/ and (materialized) _traverse/
app_root = components_root.parent
for comp_path in sorted(components_root.glob("*/component.manifest.json")):
data = json.loads(comp_path.read_text())
ref = data.get("registry_ref")
if not ref:
continue
if data.get("contract_path") or data.get("wasm_binary_path") or data.get("wasm_digest"):
print(f"FAIL: {comp_path} has registry_ref and local fields (xor violated)", file=sys.stderr)
sys.exit(1)
key = (ref.get("namespace"), ref.get("id"))
mapping = KNOWN.get(key)
if mapping is None:
print(f"FAIL: no materialize mapping for registry_ref {key} in {comp_path}", file=sys.stderr)
sys.exit(1)
stem = mapping["stem"]
cap = mapping["cap"]
abs_wasm = traverse / f"examples/{app_id}/{stem}/artifacts/{stem}.wasm"
abs_contract = traverse / f"contracts/examples/{app_id}/capabilities/{cap}/contract.json"
if not abs_wasm.is_file():
print(f"FAIL: cannot materialize {key}: missing {abs_wasm}", file=sys.stderr)
sys.exit(1)
if not abs_contract.is_file():
print(f"FAIL: cannot materialize {key}: missing {abs_contract}", file=sys.stderr)
sys.exit(1)
dest_wasm = app_root / f"_traverse/examples/{app_id}/{stem}/artifacts/{stem}.wasm"
dest_contract = app_root / f"_traverse/contracts/examples/{app_id}/capabilities/{cap}/contract.json"
dest_wasm.parent.mkdir(parents=True, exist_ok=True)
dest_contract.parent.mkdir(parents=True, exist_ok=True)
if not dest_wasm.is_file() or dest_wasm.read_bytes() != abs_wasm.read_bytes():
shutil.copyfile(abs_wasm, dest_wasm)
if not dest_contract.is_file() or dest_contract.read_bytes() != abs_contract.read_bytes():
shutil.copyfile(abs_contract, dest_contract)
digest = "sha256:" + hashlib.sha256(dest_wasm.read_bytes()).hexdigest()
data.pop("registry_ref", None)
data["contract_path"] = f"../../_traverse/contracts/examples/{app_id}/capabilities/{cap}/contract.json"
data["wasm_binary_path"] = f"../../_traverse/examples/{app_id}/{stem}/artifacts/{stem}.wasm"
data["wasm_digest"] = digest
comp_path.write_text(json.dumps(data, indent=2) + "\n")
print(f"OK: materialized registry_ref {key[0]}/{key[1]} → {comp_path} ({digest})")
PY
}