ituoke/scripts/sync-platform-tools.sh

54 lines
1.7 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
# [打包] 把 adb 同步到 third_party/platform-tools,供安装包内置
set -euo pipefail
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
DEST="$ROOT/third_party/platform-tools"
mkdir -p "$DEST"
copy_from() {
local src="$1"
echo "→ 从 $src 复制"
# 尽量拷整个 platform-tools 目录(含依赖),至少保证有 adb
local dir
dir="$(dirname "$src")"
if [[ -d "$dir" ]] && [[ "$(basename "$dir")" == "platform-tools" ]]; then
rsync -a --delete "$dir/" "$DEST/"
else
cp -f "$src" "$DEST/adb"
chmod +x "$DEST/adb"
fi
}
# 1) 本机已有 adb
if command -v adb >/dev/null 2>&1; then
ADB_BIN="$(command -v adb)"
# brew 的 adb 常是 symlink,解析到真实 platform-tools
if [[ -L "$ADB_BIN" ]]; then
ADB_BIN="$(python3 -c "import os,sys; print(os.path.realpath(sys.argv[1]))" "$ADB_BIN")"
fi
copy_from "$ADB_BIN"
echo "✓ 已同步到 $DEST"
"$DEST/adb" version | head -2
exit 0
fi
# 2) 下载 Google 官方 mac/linux/win 包(按当前系统)
OS="$(uname -s)"
case "$OS" in
Darwin) ZIP_URL="https://dl.google.com/android/repository/platform-tools-latest-darwin.zip" ;;
Linux) ZIP_URL="https://dl.google.com/android/repository/platform-tools-latest-linux.zip" ;;
MINGW*|MSYS*|CYGWIN*) ZIP_URL="https://dl.google.com/android/repository/platform-tools-latest-windows.zip" ;;
*) echo "请手动把对应平台的 platform-tools 放到 $DEST"; exit 1 ;;
esac
TMP="$(mktemp -d)"
echo "→ 下载 $ZIP_URL"
curl -L --fail -o "$TMP/pt.zip" "$ZIP_URL"
unzip -q "$TMP/pt.zip" -d "$TMP"
rsync -a --delete "$TMP/platform-tools/" "$DEST/"
rm -rf "$TMP"
chmod +x "$DEST/adb" 2>/dev/null || true
echo "✓ 已下载并同步到 $DEST"
"$DEST/adb" version | head -2