Files
opsxapi/scripts/gui-dev-app.sh
T

108 lines
4.5 KiB
Bash

# sourced by run.sh — temp OpsxAPI.app for Dock icon (icons/icon.icns)
# shellcheck shell=bash
# .noindex:避免 Spotlight 把临时 Dock 壳当成可打开的历史 App
GUI_APP_DIR="$SCRIPT_DIR/src-tauri/target/gui-app.noindex"
GUI_APP="$GUI_APP_DIR/OpsxAPI.app"
# 源码在 apps/desktop/src-tauri;编译产物可能在根 src-tauri/target(历史路径)
ICONS_DIR="$SCRIPT_DIR/apps/desktop/src-tauri/icons"
[ -f "$ICONS_DIR/icon.icns" ] || ICONS_DIR="$SCRIPT_DIR/src-tauri/icons"
REL_APP="$SCRIPT_DIR/apps/desktop/src-tauri/target/release/bundle/macos/OpsxAPI.app"
[ -d "$REL_APP" ] || REL_APP="$SCRIPT_DIR/src-tauri/target/release/bundle/macos/OpsxAPI.app"
DBG_APP="$SCRIPT_DIR/apps/desktop/src-tauri/target/debug/bundle/macos/OpsxAPI.app"
[ -d "$DBG_APP" ] || DBG_APP="$SCRIPT_DIR/src-tauri/target/debug/bundle/macos/OpsxAPI.app"
REL_BIN="$SCRIPT_DIR/apps/desktop/src-tauri/target/release/OpsxAPI"
[ -x "$REL_BIN" ] || REL_BIN="$SCRIPT_DIR/src-tauri/target/release/OpsxAPI"
DBG_BIN_A="$SCRIPT_DIR/apps/desktop/src-tauri/target/debug/OpsxAPI"
[ -x "$DBG_BIN_A" ] || DBG_BIN_A="$SCRIPT_DIR/src-tauri/target/debug/OpsxAPI"
DBG_BIN_B="$SCRIPT_DIR/apps/desktop/src-tauri/target/debug/opsxapi"
[ -x "$DBG_BIN_B" ] || DBG_BIN_B="$SCRIPT_DIR/src-tauri/target/debug/opsxapi"
GUI_DEV_NEEDS_VITE=0
_gui_write_plist() {
cat >"$GUI_APP/Contents/Info.plist" <<'PLIST'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleDisplayName</key><string>OpsxAPI</string>
<key>CFBundleExecutable</key><string>OpsxAPI</string>
<key>CFBundleIconFile</key><string>icon.icns</string>
<key>CFBundleIdentifier</key><string>com.opsx.api.dev</string>
<key>CFBundleName</key><string>OpsxAPI</string>
<key>CFBundlePackageType</key><string>APPL</string>
<key>CFBundleShortVersionString</key><string>0.1.0-dev</string>
<key>CFBundleVersion</key><string>0</string>
<key>LSHighResolutionCapable</key><true/>
</dict>
</plist>
PLIST
}
_gui_wrap_bin() {
local src="$1"
mkdir -p "$GUI_APP/Contents/MacOS" "$GUI_APP/Contents/Resources"
cp "$src" "$GUI_APP/Contents/MacOS/OpsxAPI"
chmod +x "$GUI_APP/Contents/MacOS/OpsxAPI"
_gui_write_plist
}
ensure_gui_dev_app() {
local icon="$ICONS_DIR/icon.icns"
GUI_DEV_NEEDS_VITE=0
if [ ! -f "$icon" ]; then
error "缺少 ${icon}"
return 1
fi
mkdir -p "$GUI_APP_DIR"
if [ -d "$DBG_APP" ]; then
rm -rf "$GUI_APP"
cp -R "$DBG_APP" "$GUI_APP"
GUI_DEV_NEEDS_VITE=1
elif [ -x "$DBG_BIN_A" ]; then
rm -rf "$GUI_APP"
_gui_wrap_bin "$DBG_BIN_A"
GUI_DEV_NEEDS_VITE=1
elif [ -x "$DBG_BIN_B" ]; then
rm -rf "$GUI_APP"
_gui_wrap_bin "$DBG_BIN_B"
GUI_DEV_NEEDS_VITE=1
elif [ -d "$REL_APP" ]; then
rm -rf "$GUI_APP"
cp -R "$REL_APP" "$GUI_APP"
elif [ -x "$REL_BIN" ]; then
rm -rf "$GUI_APP"
_gui_wrap_bin "$REL_BIN"
else
error "没有可包装的桌面二进制。请先 ./run.sh gui 走一次 tauri 编译,或 npm run tauri -- build --no-bundle。"
return 1
fi
mkdir -p "$GUI_APP/Contents/Resources"
cp "$icon" "$GUI_APP/Contents/Resources/icon.icns"
/usr/libexec/PlistBuddy -c "Set :CFBundleDisplayName OpsxAPI" "$GUI_APP/Contents/Info.plist" 2>/dev/null || true
/usr/libexec/PlistBuddy -c "Set :CFBundleName OpsxAPI" "$GUI_APP/Contents/Info.plist" 2>/dev/null || true
/usr/libexec/PlistBuddy -c "Set :CFBundleExecutable OpsxAPI" "$GUI_APP/Contents/Info.plist" 2>/dev/null || true
/usr/libexec/PlistBuddy -c "Set :CFBundleIconFile icon.icns" "$GUI_APP/Contents/Info.plist" 2>/dev/null || true
/usr/libexec/PlistBuddy -c "Set :CFBundleIdentifier com.opsx.api.dev" "$GUI_APP/Contents/Info.plist" 2>/dev/null || true
local stamp
stamp="$(date +%Y%m%d%H%M%S)"
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion ${stamp}" "$GUI_APP/Contents/Info.plist" 2>/dev/null || true
touch "$GUI_APP"
success "临时 .app 已刷 icons/icon.icns → ${GUI_APP}"
}
kill_opsxapi_gui() {
stop_detached "$DESKTOP_LABEL"
pkill -f "$SCRIPT_DIR/src-tauri/target/debug/OpsxAPI" 2>/dev/null || true
pkill -f "$SCRIPT_DIR/apps/desktop/src-tauri/target/debug/OpsxAPI" 2>/dev/null || true
pkill -f "$SCRIPT_DIR/node_modules/.bin/tauri" 2>/dev/null || true
pkill -f "$SCRIPT_DIR/src-tauri/target/gui-app/OpsxAPI.app" 2>/dev/null || true
pkill -f "$GUI_APP" 2>/dev/null || true
pkill -x OpsxAPI 2>/dev/null || true
}
gui_is_running() {
pgrep -f "$GUI_APP/Contents/MacOS/" >/dev/null 2>&1 \
|| pgrep -x OpsxAPI >/dev/null 2>&1
}