Files
reader/android/scripts/build-web-assets.sh
T
cnb.dQC5J6ohAGA baf63c5b28 fix(ci): 让 assets 流水线真正验证产物会被打进 APK
web-assets-ci 只跑了 build-web-assets.sh,证明脚本写完了 assets,却没证明
Gradle 会读那个目录 —— 而这两处此前就是漂移的:

- build.gradle.kts 的 assets.srcDirs 指向 android/app/web-assets,
  那是没有任何脚本写入的第三个路径;真正的产物落在
  android/app/src/main/assets/web-assets。于是 staged 的 bundle 不是 assets
  源,APK 照常构建成功,但里面没有客户端,启动即白屏。
- 注释依赖的 README.txt 占位文件已在 c18664f 被删除,所以干净 checkout 里
  这个目录根本不存在,assets.srcDirs 指向一个不存在的路径。
- build-web-assets.sh 的 rm -rf "$dest" 会把那个占位文件一起删掉,
  即使它还在,每次本地构建后工作树都会变脏。

改动:

- build.gradle.kts:assets.srcDirs 指向脚本实际写入的目录,注释写准。
- 恢复 web-assets.sh 目录下的 README.txt 占位,让目录进版本库。
- build-web-assets.sh:清理产物时保留 README.txt,不再制造脏工作树。
- 新增 verify-web-assets.sh:校验产物存在且是构建出的客户端、Gradle 声明的
  路径与产物路径一致、占位文件确实被 git 跟踪。
- .cnb.yml:web-assets-ci 增加该 stage,并把脚本自身 / build.gradle.kts /
  assets 目录纳入 ifModify —— 改这些本来不会触发任何验证。
- PR 侧补 android-contract-checks:桥接契约与渲染词表校验不需要 JDK/SDK,
  秒级,不该等到 main 的完整构建才发现。

验证:check-shell / build-web-assets / verify-web-assets 全部通过;
把 assets.srcDirs 改回错误路径可复现失败。.cnb.yml 通过语法/语义/Schema 校验。
2026-09-17 10:30:00 +08:00

45 lines
1.5 KiB
Bash
Executable File

#!/bin/sh
# Builds the shared web client and stages it as Android assets.
#
# This script is the mechanism that keeps the Android and H5 clients from becoming
# two implementations: one Vite bundle, one host-agnostic web layer, two hosts.
#
# Run from anywhere; paths are resolved relative to the script.
set -eu
script_dir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
android_dir=$(dirname -- "$script_dir")
repo_dir=$(dirname -- "$android_dir")
web_dir="$repo_dir/web"
dest="$android_dir/app/src/main/assets/web-assets"
if [ ! -d "$web_dir" ]; then
echo "web client not found at $web_dir" >&2
exit 1
fi
echo "==> building web client"
cd "$web_dir"
# `npm ci` when a lockfile exists so a release build is reproducible; `install`
# otherwise so a first-time contributor is not blocked on generating one.
if [ -f package-lock.json ]; then
npm ci
else
npm install
fi
npm run build
echo "==> staging assets into $dest"
# Clear the previous bundle but keep README.txt. That file is tracked, and a
# blanket `rm -rf "$dest"` deletes it — leaving a dirty working tree after every
# build and turning `git status` into noise that hides real changes. It is also
# the only reason the directory exists in a clean checkout.
mkdir -p "$dest"
find "$dest" -mindepth 1 ! -name 'README.txt' -exec rm -rf {} +
# Only the built output. Copying the sources would ship the whole TypeScript tree
# inside the APK for no benefit.
cp -R "$web_dir/dist/." "$dest/"
echo "==> done"
echo "The Android build now packs $(find "$dest" -type f | wc -l | tr -d ' ') files as assets."