Files
jiu/scripts/local_test.sh
T
wangjia 709e59b1b7 fix(scripts): local_test.sh --ios 检测不到已连 iPhone——设备状态匹配 connected
新版 Xcode devicectl 对 USB 直连并解锁的 iPhone 显示状态 'connected',脚本原来只筛
'available (paired)'(该状态在新版仅配对未直连的设备如 Apple Watch 才有),导致
'未检测到已配对 iPhone'。改为 grep iPhone 行 + 词匹配 connected|available(两版 Xcode
都收,-w 避免误命中 disconnected/unavailable)。

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-11 12:18:34 +08:00

98 lines
4.3 KiB
Bash
Executable File
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env bash
# local_test.sh — 本地编译一份「指向线上后端」的 app 用于发版前自测。
#
# 用法:
# sh scripts/local_test.sh # 编译 macOS + 启动
# sh scripts/local_test.sh --no-open # 只编译 macOS 不启动
# sh scripts/local_test.sh --ios # 编译 iOS 并装到 USB iPhone(开发签名)
#
# 后端固定指向生产 https://jiu.51yanmei.com(与 CI compile 脚本一致)。
# 版本号 = 最新 client-v* tag 的 patch+1 加 -dev 后缀(如 v1.1.4-dev),
# App 内显示走 APP_VERSION dart-defineupdate_provider.currentAppVersion),
# 与 CI 同一读取链路——本地构建不再显示 pubspec 旧版本(2026-07-04 修复)。
#
# 本脚本用到 bash 专属特性(数组、进程替换 `>(...)`)。若被 `sh` 调起(macOS 的
# /bin/sh 实为 bash --posix:会设 BASH_VERSION 但禁用进程替换,导致下方 xcodebuild
# 那行报 `syntax error near unexpected token '>'`),或被 dash 等非 bash 调起,则在
# 解析到那行之前先重执行到普通模式 bash。此判断须是第一条可执行语句。
_posix=no
case ":${SHELLOPTS:-}:" in *:posix:*) _posix=yes ;; esac
if [ -z "${BASH_VERSION:-}" ] || [ "$_posix" = yes ]; then
exec bash "$0" "$@"
fi
unset _posix
set -euo pipefail
export PATH="/opt/homebrew/bin:$PATH"
REPO_ROOT="$(cd "$(dirname "$0")/.." && pwd)"
cd "$REPO_ROOT"
LATEST_TAG="$(git describe --tags --match 'client-v*' --abbrev=0 2>/dev/null || true)"
if [ -n "$LATEST_TAG" ]; then
BASE_VER="${LATEST_TAG#client-v}"
MAJOR_MINOR="${BASE_VER%.*}"
PATCH="${BASE_VER##*.}"
VER="${MAJOR_MINOR}.$((PATCH + 1))-dev"
else
VER="0.0.0-dev"
fi
DEFINES=(
"--dart-define=BASE_URL=https://jiu.51yanmei.com"
"--dart-define=PUBLIC_URL=https://jiu.51yanmei.com"
"--dart-define=APP_VERSION=v${VER}"
)
if [ "${1:-}" = "--ios" ]; then
# ── iOS:开发签名构建 + devicectl 装到 USB iPhone ──
# Release 配置是 CI 的 Manual+App Store 签名,本机没有分发证书,
# 故 xcodebuild 用 Automatic + Apple Development 覆盖(不改工程文件)。
echo "==> 编译 iOS(线上后端,版本 v${VER}"
cd client
flutter build ios --release --config-only "${DEFINES[@]}"
cd ios
# 已配对可安装的 iPhone:新版 Xcode(devicectl) USB 直连并解锁显示 "connected"
# 旧版曾显示 "available (paired)";两者都收。-w 词匹配避免误命中 disconnected/unavailable。
CD_ID="$(xcrun devicectl list devices 2>/dev/null | grep 'iPhone' | grep -Ew 'connected|available' | grep -oE '[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}' | head -1 || true)"
if [ -z "$CD_ID" ]; then
echo "!! 未检测到已配对 iPhone(插线并解锁后重试)" >&2
exit 1
fi
# stderr 滤掉锁屏 iPhone 的握手噪音(DTDK "passcode protected" 每 3s 刷一条,
# 构建目标是 generic/platform=iOS 不需要真机,纯噪音;真错误仍透传)
xcodebuild -workspace Runner.xcworkspace -scheme Runner -configuration Release \
-destination "generic/platform=iOS" \
CODE_SIGN_STYLE=Automatic DEVELOPMENT_TEAM=BYL4KQHMTN \
CODE_SIGN_IDENTITY="Apple Development" PROVISIONING_PROFILE_SPECIFIER= \
-allowProvisioningUpdates -allowProvisioningDeviceRegistration build \
2> >(grep -vE "DTDKRemoteDeviceConnection|DVTDeviceOperation|passcode protected|dtdevicekit|DTDeviceKitBase|DVTFoundation|libdispatch|libsystem_pthread|^\)|^\s+[0-9]+ " >&2) \
| grep -E "BUILD (SUCCEEDED|FAILED)"
APP_PATH="$(find "$HOME/Library/Developer/Xcode/DerivedData" -path '*Release-iphoneos/Runner.app' -maxdepth 6 2>/dev/null | head -1)"
if [ -z "$APP_PATH" ]; then
echo "!! 找不到构建产物 Runner.app" >&2
exit 1
fi
echo "==> 安装到 iPhone${CD_ID}"
xcrun devicectl device install app --device "$CD_ID" "$APP_PATH" | grep -m1 databaseSequenceNumber || true
echo "==> 完成:解锁手机打开 岩美酒库(版本 v${VER}"
exit 0
fi
APP="client/build/macos/Build/Products/Release/jiu_client.app"
echo "==> 编译 macOS app(线上后端,版本 v${VER}"
cd client
flutter build macos --release "${DEFINES[@]}"
cd "$REPO_ROOT"
echo "==> 产物:$APP"
if [ "${1:-}" = "--no-open" ]; then
echo "==> 跳过启动(--no-open"
else
echo "==> 前台启动 app(终端附着日志,退出 app 才返回;Ctrl-C 可终止)"
exec "$APP/Contents/MacOS/jiu_client"
fi