dsh-ipa-build 1.3.1 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/NOTICE CHANGED
@@ -10,7 +10,8 @@ vendor/ 下的第三方组件各自按自己的许可证分发,和上面的 MI
10
10
  你的分发行为若触发 AGPL 义务(例如对外提供修改后的 ldid 或其衍生作品),
11
11
  需自行履行 AGPL 的源码提供义务。
12
12
  - vendor/libplist-2.6.0.tar.bz2 —— libplist,LGPL-2.1(https://github.com/libimobiledevice/libplist)。ldid 编译时静态/动态链接它,动态链接时保留替换库的权利即可。
13
- - vendor/zsign/zsign.exe —— zsign(https://github.com/zhlynn/zsign),按其仓库许可证分发。
13
+ - vendor/zsign/zsign.exe —— zsign v1.1.3 Windows 版,来自 https://github.com/zhlynn/zsign 。
14
+ 该仓库声明的许可证是 GPL-3.0(见上游 LICENSE),二进制未做改动;分发前请按该仓库最新声明核对。
14
15
  - vendor/iPhoneOS16.5.sdk.tar.xz —— Apple 的 iPhoneOS SDK,版权归 Apple,仅随本插件用于本地交叉编译,不单独再分发 SDK。
15
16
  - iPhoneOS SDK 中的系统库头文件与 .tbd stub 版权归 Apple Inc.
16
17
 
package/README.md CHANGED
@@ -48,9 +48,11 @@ Windows 把 `scripts/ipa` 换成 `scripts\ipa.ps1`,子命令和参数一样。
48
48
  三个命令都认 `--p12 --prov --password`:
49
49
 
50
50
  ```bash
51
- scripts/ipa resign old.ipa -o new.ipa --p12 dev.p12 --prov app.mobileprovision --password 证书密码
51
+ IPA_CERT_PASSWORD=证书密码 scripts/ipa resign old.ipa -o new.ipa --p12 dev.p12 --prov app.mobileprovision
52
52
  ```
53
53
 
54
+ 密码走 `IPA_CERT_PASSWORD` 环境变量,`--password` 只是兼容写法,会留在 shell 历史里。
55
+
54
56
  不给 p12 就是 ad-hoc 伪签:越狱机和 TrollStore 能装,普通设备装不上。Windows 上的 zsign 支持真签名;Linux/macOS 上要自己编一个 zsign 放进 `~/.ipa-toolchain/bin/`(上游只发 Windows 版)。
55
57
 
56
58
  真签名时 bundle id 必须跟描述文件对上,对不上签名会失败。zsign 签完会查 OCSP 吊销状态,断网时会慢一点或者报个警告。
@@ -71,8 +73,8 @@ scripts/ipa info dist/HelloApp.ipa
71
73
  ## 包里的东西
72
74
 
73
75
  - `vendor/iPhoneOS16.5.sdk.tar.xz` — 16.5 的 iPhoneOS SDK,`.tbd` 已展开成真文件。sha256 `1d55ed9e66a37c0e6813c5be29df6f64f1ce448f58911a6cb87db07d5f6b8c58`
74
- - `vendor/zsign/zsign.exe` — 来自 https://github.com/zhlynn/zsign v1.1.3 的 Windows 版。MD5 `c42f137b76eb4cb2cad2b1a227a0f8f0`
75
- - `vendor/libplist-2.6.0.tar.bz2`、`vendor/ldid-master/` — Linux/macOS 编 ldid 用
76
+ - `vendor/zsign/zsign.exe` — 来自 https://github.com/zhlynn/zsign v1.1.3 的 Windows 版。sha256 `abc1087da5a410c42fdbb8e90049802c2cc6c03fcd0ebb6d5c315b17f6d26a87`
77
+ - `vendor/libplist-2.6.0.tar.bz2`、`vendor/ldid-master/` — Linux/macOS 编 ldid 用。ldid 是上游 master 的源码快照(没有 pin commit,所以不是一个可复现的构建,介意就自己换固定版本重编)
76
78
 
77
79
  第三方组件许可证各不相同,见 `NOTICE`:仓库自己的代码是 MIT,ldid 是 AGPL-3.0,libplist 是 LGPL-2.1,SDK 归 Apple,zsign 见上游仓库。
78
80
 
package/SKILL.md CHANGED
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  name: dsh-ipa-build
3
- version: 1.3.1
3
+ version: 1.3.2
4
4
  description: 把 iOS 程序做成 .ipa。源码能直接交叉编译成 arm64 包,现成的 .app 能打包,已有 ipa 能重签、改 bundle id 做多开,dylib 也跟着签。SDK、ldid、zsign 随插件带,环境由 AI 自己铺好,用户不用装东西。
5
5
  ---
6
6
 
@@ -72,7 +72,7 @@ scripts/ipa resign 旧包.ipa -o 新包.ipa --p12 dev.p12 --prov app.mobileprovi
72
72
 
73
73
  `pack`、`build` 同样认这三个参数。bundle id 必须和描述文件一致,否则签名失败。
74
74
 
75
- 密码走命令行会进 shell 历史和进程列表,能让用户自己输入就别替他填。
75
+ 密码优先用 `IPA_CERT_PASSWORD` 环境变量,别写进命令行(会留在 shell 历史和进程列表里)。
76
76
 
77
77
  Windows 的 zsign 拿 p12 走完整签名链,签完查一次 OCSP,断网时会慢或报警告,不算失败。Linux / macOS 的证书签名也要 zsign,上游只发 Windows 版,得自己编一个放进 `~/.ipa-toolchain/bin/`。
78
78
 
package/index.js CHANGED
@@ -35,7 +35,7 @@ const provider = {
35
35
  },
36
36
  };
37
37
 
38
- const name = "skill-ipa-build";
38
+ const name = "dsh-ipa-build";
39
39
  const inject = ["skills"];
40
40
  function apply(ctx) {
41
41
  ctx.skills.registerProvider(() => provider);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-ipa-build",
3
- "version": "1.3.1",
3
+ "version": "1.3.2",
4
4
  "description": "把 iOS 源码打成 .ipa 的 DSH 插件。包内自带 iPhoneOS 16.5 SDK 和 zsign,Windows / Linux / macOS 都能编能签,用户装完不用再配环境。",
5
5
  "license": "MIT",
6
6
  "repository": {
package/plugin.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "产出 IPA",
3
3
  "id": "dsh-ipa-build",
4
- "version": "1.3.1",
4
+ "version": "1.3.2",
5
5
  "type": "skill",
6
6
  "skill": "SKILL.md",
7
7
  "setup": {
@@ -2,10 +2,10 @@
2
2
  # SDK 和 zsign 都在 vendor 里,直接从插件里拿;编译器没有的话脚本自己去搞。
3
3
 
4
4
  param(
5
+ # 只从 GitHub 官方 release 下。第三方加速代理能改文件内容,不在这里留位置;
6
+ # 慢就自己挂代理,别换源
5
7
  [string[]]$LlvmUrl = @(
6
- 'https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.8/clang+llvm-18.1.8-x86_64-pc-windows-msvc.tar.xz',
7
- 'https://ghfast.top/https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.8/clang+llvm-18.1.8-x86_64-pc-windows-msvc.tar.xz',
8
- 'https://gh-proxy.com/https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.8/clang+llvm-18.1.8-x86_64-pc-windows-msvc.tar.xz'
8
+ 'https://github.com/llvm/llvm-project/releases/download/llvmorg-18.1.8/clang+llvm-18.1.8-x86_64-pc-windows-msvc.tar.xz'
9
9
  )
10
10
  )
11
11
 
@@ -38,30 +38,61 @@ function Find-Llvm {
38
38
  return $null
39
39
  }
40
40
 
41
+ # 只改当前会话和工具链自己的 env.ps1,不动用户的持久 PATH:
42
+ # 永久把某个 LLVM 前置到 PATH 会盖掉用户原来装的 clang,卸载插件也还原不回来
41
43
  function Use-Path($bin) {
42
44
  $env:PATH = "$bin;$env:PATH"
43
- $old = [Environment]::GetEnvironmentVariable('Path', 'User')
44
- if ($old -and -not $old.Contains($bin)) {
45
- [Environment]::SetEnvironmentVariable('Path', "$bin;$old", 'User')
46
- }
45
+ $envFile = Join-Path $tc 'env.ps1'
46
+ $line = '$env:PATH = "' + $bin + ';" + $env:PATH'
47
+ [IO.File]::WriteAllText($envFile, $line + [Environment]::NewLine)
48
+ Write-Host "要常驻的话自己加:$line"
49
+ }
50
+
51
+ function Get-FileSha256($path) { (Get-FileHash -Path $path -Algorithm SHA256).Hash.ToLower() }
52
+
53
+ # 校验一个已有的归档:tar 能完整列出内容才算真的下全了。
54
+ # 只看 "> 300MB" 会把半截文件也当成好的,续传拼错的文件更是过不了这一步
55
+ function Test-Archive($path) {
56
+ if (-not (Test-Path $path)) { return $false }
57
+ & tar.exe -tJf $path *> $null
58
+ return ($LASTEXITCODE -eq 0)
47
59
  }
48
60
 
49
61
  function Get-LlvmArchive($dest) {
62
+ $hashFile = Join-Path $tc 'llvm.sha256'
63
+ $expect = if (Test-Path $hashFile) { (Get-Content $hashFile -Raw).Trim() } else { $null }
50
64
  foreach ($u in $LlvmUrl) {
51
65
  Write-Host "下载编译器:$u"
52
- # --continue-at - 让断掉的下载接着走,重跑脚本不用从头再来
53
- & curl.exe -L --retry 5 --retry-delay 5 --retry-all-errors --continue-at - --connect-timeout 15 -o $dest $u
54
- if ($LASTEXITCODE -eq 0 -and (Test-Path $dest) -and ((Get-Item $dest).Length -gt 300MB)) { return $true }
55
- Write-Host "这个源没成,换下一个"
66
+ # 不带 --continue-at:不支持 Range 的源会把文件拼坏,重下整份更稳
67
+ & curl.exe -L --retry 5 --retry-delay 5 --retry-all-errors --connect-timeout 15 -o ($dest + '.part') $u
68
+ if ($LASTEXITCODE -ne 0) { Write-Host "下载没成功,换下一个源"; continue }
69
+ Move-Item ($dest + '.part') $dest -Force
70
+ if (-not (Test-Archive $dest)) { Write-Host "下下来的包解不开,当没下成(多半是没下全或者被换过)"; Remove-Item $dest -Force; continue }
71
+ $sha = Get-FileSha256 $dest
72
+ Write-Host "sha256 $sha"
73
+ # 上游只发 GPG 签名,没有官方 sha256;所以第一次下完把哈希记下来,
74
+ # 以后每次下载都跟它比对,换文件立刻能发现
75
+ if ($expect -and $sha -ne $expect) { Write-Host "sha256 跟上次记的不一样(上次 $expect),这个文件不对,停下"; Remove-Item $dest -Force; continue }
76
+ if (-not $expect) { Set-Content -Path $hashFile -Value $sha -Encoding utf8 }
77
+ return $true
56
78
  }
57
79
  return $false
58
80
  }
59
81
 
60
82
  # SDK:vendor 里的包已经把 .tbd 软链展开成真文件了,Windows 解包不会再出空文件
61
83
  $sdkDir = Join-Path $tc 'sdk/iPhoneOS16.5.sdk'
84
+ $SdkSha = '1d55ed9e66a37c0e6813c5be29df6f64f1ce448f58911a6cb87db07d5f6b8c58'
85
+ $ZsignSha = 'abc1087da5a410c42fdbb8e90049802c2cc6c03fcd0ebb6d5c315b17f6d26a87'
86
+ function Assert-Sha($path, $want, $what) {
87
+ if (-not (Test-Path $path)) { return }
88
+ $got = (Get-FileHash -Path $path -Algorithm SHA256).Hash.ToLower()
89
+ if ($got -ne $want) { Write-Host "$what 的 sha256 对不上($got,应为 $want),文件被换过或没下全"; exit 1 }
90
+ }
91
+
62
92
  if (-not (Test-Path $sdkDir)) {
63
93
  $archive = Join-Path $root 'vendor/iPhoneOS16.5.sdk.tar.xz'
64
94
  if (-not (Test-Path $archive)) { Write-Host "vendor 里没有 SDK 包,插件不完整"; exit 1 }
95
+ Assert-Sha $archive $SdkSha 'vendor/iPhoneOS16.5.sdk.tar.xz'
65
96
  Write-Host "解包 SDK"
66
97
  & tar.exe -xJf $archive -C (Join-Path $tc 'sdk')
67
98
  if (-not (Test-Path $sdkDir)) { Write-Host "SDK 解包失败,看看 tar.exe 能不能用"; exit 1 }
@@ -89,7 +120,8 @@ if ($fixed -gt 0) { Write-Host "补了 $fixed 个没展开的 .tbd" }
89
120
  $zs = Join-Path $tc 'bin/zsign.exe'
90
121
  if (-not (Test-Path $zs)) {
91
122
  $srcZs = Join-Path $root 'vendor/zsign/zsign.exe'
92
- if (Test-Path $srcZs) { Copy-Item $srcZs $zs -Force } else { Write-Host "vendor 里没有 zsign.exe,签不了名" }
123
+ if (Test-Path $srcZs) { Assert-Sha $srcZs $ZsignSha 'vendor/zsign/zsign.exe'; Copy-Item $srcZs $zs -Force }
124
+ else { Write-Host "vendor 里没有 zsign.exe,签不了名" }
93
125
  }
94
126
 
95
127
  # 编译器:找得到就用,找不到就装上
@@ -100,7 +132,7 @@ if ($bin) {
100
132
  } else {
101
133
  $dl = Join-Path $tc 'llvm.tar.xz'
102
134
  $ok = $false
103
- if ((Test-Path $dl) -and ((Get-Item $dl).Length -gt 300MB)) { $ok = $true }
135
+ if (Test-Archive $dl) { $ok = $true }
104
136
  if (-not $ok) { $ok = Get-LlvmArchive $dl }
105
137
 
106
138
  if (-not $ok) {
@@ -110,6 +142,11 @@ if ($bin) {
110
142
  Write-Host "下载不通,用 winget 装"
111
143
  & winget install --id LLVM.LLVM -e --silent --accept-package-agreements --accept-source-agreements
112
144
  $bin = Find-Llvm
145
+ if ($bin -and (Test-Path (Join-Path $bin 'clang.exe'))) {
146
+ $ver = & (Join-Path $bin 'clang.exe') --version 2>$null | Select-Object -First 1
147
+ Write-Host "winget 装的编译器:$ver"
148
+ if ($ver -notmatch 'version 1[0-9]|version 2[0-9]') { Write-Host "版本跟预期差得远,编 iOS 可能出怪问题" }
149
+ }
113
150
  }
114
151
  } elseif ($ok) {
115
152
  Write-Host "解压编译器"
@@ -17,11 +17,12 @@ if [ "$os" = "Darwin" ]; then
17
17
  if ! command -v clang >/dev/null 2>&1; then
18
18
  echo "缺 Xcode 命令行工具,开始装(系统会弹一个确认框,点一下就行)"
19
19
  xcode-select --install >/dev/null 2>&1 || true
20
- for _ in 1 2 3 4 5 6 7 8; do
20
+ for i in 1 2 3 4 5 6 7 8; do
21
21
  command -v clang >/dev/null 2>&1 && break
22
+ echo "等 Xcode 命令行工具装完($((i*15))s / 120s,网络慢就会超时)"
22
23
  sleep 15
23
24
  done
24
- command -v clang >/dev/null 2>&1 || { echo "命令行工具还在装,装完再跑一次这个脚本"; exit 1; }
25
+ command -v clang >/dev/null 2>&1 || { echo "命令行工具还没装完(等了两分钟没等到)。等它装完再跑一次这个脚本就行"; exit 1; }
25
26
  fi
26
27
 
27
28
  # Apple 自带的 clang 不带 ld64.lld,得用 brew 的 llvm
@@ -38,16 +39,28 @@ if [ "$os" = "Darwin" ]; then
38
39
  fi
39
40
  else
40
41
  if ! command -v clang >/dev/null 2>&1; then
41
- echo "缺 clang,从 apt 装"
42
- # 不动 /etc/apt/sources.list:换源会影响这台机器上所有 apt 操作,
43
- # 要换请自己换,或者 IPA_APT_MIRROR=tuna 让脚本换一次
44
- if [ "${IPA_APT_MIRROR:-}" = "tuna" ] && [ -f /etc/apt/sources.list ] && ! grep -q tuna /etc/apt/sources.list; then
45
- sudo sed -i 's|http://archive.ubuntu.com/ubuntu/|https://mirrors.tuna.tsinghua.edu.cn/ubuntu/|g' /etc/apt/sources.list
46
- sudo sed -i 's|http://security.ubuntu.com/ubuntu/|https://mirrors.tuna.tsinghua.edu.cn/ubuntu/|g' /etc/apt/sources.list
42
+ echo "缺 clang,装一个"
43
+ if command -v apt-get >/dev/null 2>&1; then
44
+ # 不动系统软件源,换源影响这台机器上所有 apt 操作。
45
+ # 要换:IPA_APT_MIRROR=tuna bash scripts/bootstrap.sh,两种源格式都认
46
+ if [ "${IPA_APT_MIRROR:-}" = "tuna" ]; then
47
+ for f in /etc/apt/sources.list /etc/apt/sources.list.d/ubuntu.sources; do
48
+ [ -f "$f" ] || continue
49
+ sudo cp -n "$f" "$f.bak.ipa" 2>/dev/null || true
50
+ sudo sed -i 's|http://archive.ubuntu.com/ubuntu/|https://mirrors.tuna.tsinghua.edu.cn/ubuntu/|g; s|http://security.ubuntu.com/ubuntu/|https://mirrors.tuna.tsinghua.edu.cn/ubuntu/|g; s|http://deb.debian.org/debian/|https://mirrors.tuna.tsinghua.edu.cn/debian/|g; s|http://deb.debian.org/debian-security/|https://mirrors.tuna.tsinghua.edu.cn/debian-security/|g' "$f"
51
+ done
52
+ fi
53
+ sudo mkdir -p /var/lib/apt/lists/partial
54
+ sudo apt-get update -qq || true
55
+ sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq clang lld python3 zip unzip
56
+ elif command -v dnf >/dev/null 2>&1; then
57
+ sudo dnf install -y clang lld python3 zip unzip
58
+ elif command -v pacman >/dev/null 2>&1; then
59
+ sudo pacman -Sy --needed --noconfirm clang lld python zip unzip
60
+ else
61
+ echo "认不出包管理器(只有 apt / dnf / pacman 会装),请自己装 clang、lld、python3、zip、unzip" >&2
62
+ exit 1
47
63
  fi
48
- sudo mkdir -p /var/lib/apt/lists/partial
49
- sudo apt-get update -qq || true
50
- sudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq clang lld
51
64
  fi
52
65
 
53
66
  # clang 调 ld64.lld 时按这个名字找,系统装的是带版本号的文件,做个软链接
@@ -74,8 +87,23 @@ if [ ! -x "$tc/bin/ldid" ]; then
74
87
  && cp ldid "$tc/bin/ldid" )
75
88
  fi
76
89
 
90
+ SDK_SHA="1d55ed9e66a37c0e6813c5be29df6f64f1ce448f58911a6cb87db07d5f6b8c58"
91
+ ZSIGN_SHA="abc1087da5a410c42fdbb8e90049802c2cc6c03fcd0ebb6d5c315b17f6d26a87"
92
+
93
+ check_sha() {
94
+ local f="$1" want="$2"
95
+ [ -f "$f" ] || return 0
96
+ local got
97
+ if command -v sha256sum >/dev/null 2>&1; then got="$(sha256sum "$f" | cut -d' ' -f1)"
98
+ else got="$(shasum -a 256 "$f" | cut -d' ' -f1)"; fi
99
+ [ "$got" = "$want" ] || { echo "$f 的 sha256 对不上:$got(应为 $want),包被换过或者没下全,停下" >&2; exit 1; }
100
+ }
101
+
102
+ check_sha "$root/vendor/zsign/zsign.exe" "$ZSIGN_SHA"
103
+
77
104
  if [ ! -d "$tc/sdk/iPhoneOS16.5.sdk" ]; then
78
105
  echo "解包 SDK"
106
+ check_sha "$root/vendor/iPhoneOS16.5.sdk.tar.xz" "$SDK_SHA"
79
107
  tar -xJf "$root/vendor/iPhoneOS16.5.sdk.tar.xz" -C "$tc/sdk"
80
108
  fi
81
109
 
package/scripts/ipa CHANGED
@@ -1,6 +1,5 @@
1
1
  #!/usr/bin/env bash
2
2
  # ipa-build 入口。Linux / macOS 用这个,Windows 用同目录的 ipa.ps1。
3
-
4
3
  set -e
5
4
 
6
5
  here="$(cd "$(dirname "$0")" && pwd)"
@@ -11,6 +10,7 @@ ldid="${IPA_LDID:-$tc/bin/ldid}"
11
10
 
12
11
  die() { echo "$*" >&2; exit 1; }
13
12
  need_ldid() { [ -x "$ldid" ] || die "没找到 ldid,先跑 scripts/bootstrap.sh"; }
13
+ need_py() { command -v python3 >/dev/null || die "缺 python3,改 plist 要用它(bootstrap 不装系统包,请自行 apt/dnf/pacman 装)"; }
14
14
 
15
15
  zsign_bin() {
16
16
  if [ -n "${IPA_ZSIGN:-}" ]; then echo "$IPA_ZSIGN"; return; fi
@@ -18,7 +18,17 @@ zsign_bin() {
18
18
  command -v zsign 2>/dev/null || true
19
19
  }
20
20
 
21
- # 有 p12 就走 zsign 真证书签名,没 p12 的地方还是走 ldid 伪签
21
+ # 密码优先走环境变量,命令行参数只做兼容
22
+ cert_pass() {
23
+ local cli="$1"
24
+ if [ -n "$cli" ]; then
25
+ echo "提示:--password 会进 shell 历史和进程列表,建议改用 IPA_CERT_PASSWORD" >&2
26
+ printf '%s' "$cli"; return
27
+ fi
28
+ printf '%s' "${IPA_CERT_PASSWORD:-}"
29
+ }
30
+
31
+ # 有 p12 就走 zsign 真签名,没 p12 的地方还是走 ldid 伪签
22
32
  sign_with_cert() {
23
33
  local in="$1" out="$2"
24
34
  local z; z="$(zsign_bin)"
@@ -33,19 +43,18 @@ sign_with_cert() {
33
43
 
34
44
  plist_get() {
35
45
  local dir="$1" key="$2"
36
- python3 - "$dir/Info.plist" "$key" 2>/dev/null <<'PY' || true
46
+ python3 - "$dir/Info.plist" "$key" <<'PY' 2>/dev/null || true
37
47
  import plistlib, sys
38
48
  try:
39
49
  with open(sys.argv[1], 'rb') as f:
40
50
  d = plistlib.load(f)
41
- v = d.get(sys.argv[2])
42
- if v is not None:
43
- print(v)
51
+ print(d.get(sys.argv[2], '') or '')
44
52
  except Exception:
45
53
  pass
46
54
  PY
47
55
  }
48
56
 
57
+ # 只改/加字符串键;数字也按字符串写,CFBundleVersion 在规范里是字符串
49
58
  plist_set() {
50
59
  local dir="$1"; shift
51
60
  python3 - "$dir/Info.plist" "$@" <<'PY'
@@ -55,21 +64,86 @@ with open(path, 'rb') as f:
55
64
  d = plistlib.load(f)
56
65
  for p in pairs:
57
66
  k, _, v = p.partition('=')
58
- if k == 'CFBundleVersion' and v.isdigit():
59
- v = int(v)
60
67
  d[k] = v
61
68
  with open(path, 'wb') as f:
62
69
  plistlib.dump(d, f)
63
70
  PY
64
71
  }
65
72
 
66
- zip_payload() {
67
- local out="$1" root="$2"
68
- case "$out" in /*) ;; *) out="$PWD/$out";; esac
69
- mkdir -p "$(dirname "$out")"
70
- rm -f "$out"
71
- ( cd "$root" && zip -qr "$out" Payload )
72
- echo "$out" > "$root/.outpath"
73
+ # 架构 + 签名状态,一次读完。不再用 file / 字符串搜索判断
74
+ macho_report() {
75
+ python3 - "$1" <<'PY'
76
+ import struct, sys
77
+
78
+ CPU = {16777228: 'arm64', 16777223: 'x86_64', 12: 'armv7', 7: 'i386', 16777222: 'arm64_32'}
79
+
80
+ path = sys.argv[1]
81
+ try:
82
+ data = open(path, 'rb').read()
83
+ except Exception as e:
84
+ print('读不了|%s' % e)
85
+ sys.exit()
86
+
87
+ def u32(b, o):
88
+ return struct.unpack('<I', b[o:o + 4])[0]
89
+
90
+ def sig_state(off):
91
+ magic = u32(data, off)
92
+ if magic == 0xfeedfacf:
93
+ hsz = 32
94
+ elif magic == 0xfeedface:
95
+ hsz = 28
96
+ else:
97
+ return None
98
+ ncmds = u32(data, off + 16)
99
+ sizeofcmds = u32(data, off + 20)
100
+ pos, end = off + hsz, off + hsz + sizeofcmds
101
+ sigoff = None
102
+ while pos + 8 <= end and pos + 8 <= len(data):
103
+ cmd, size = u32(data, pos), u32(data, pos + 4)
104
+ if size < 8:
105
+ break
106
+ if cmd == 0x1d and pos + 16 <= len(data):
107
+ sigoff = u32(data, pos + 8)
108
+ pos += size
109
+ if sigoff is None or sigoff + 12 > len(data):
110
+ return None
111
+ if u32(data, sigoff) != 0xfade0cc0:
112
+ return None
113
+ count = u32(data, sigoff + 4)
114
+ types = []
115
+ for i in range(min(count, 20)):
116
+ types.append(u32(data, sigoff + 8 + i * 8))
117
+ if 0x10000 in types:
118
+ return 'cms'
119
+ if 0 in types:
120
+ return 'adhoc'
121
+ return None
122
+
123
+ magic = u32(data, 0)
124
+ archs = []
125
+ sig = None
126
+ if magic in (0xfeedface, 0xfeedfacf):
127
+ archs = [CPU.get(u32(data, 4), '?')]
128
+ sig = sig_state(0)
129
+ elif magic in (0xcafebabe, 0xcafebabf):
130
+ nfat = u32(data, 4)
131
+ if nfat <= 20:
132
+ for i in range(nfat):
133
+ e = 8 + i * (32 if magic == 0xcafebabf else 20)
134
+ if e + 8 > len(data):
135
+ break
136
+ archs.append(CPU.get(u32(data, e), '?'))
137
+ if sig is None:
138
+ off = struct.unpack('<Q' if magic == 0xcafebabf else '<I', data[e + 8:e + 16 if magic == 0xcafebabf else e + 12])[0]
139
+ if off < len(data):
140
+ sig = sig_state(off)
141
+ else:
142
+ archs = ['不是 Mach-O']
143
+
144
+ label = {'cms': '真证书签名(有 CMS)', 'adhoc': 'ad-hoc 伪签名(只有 CodeDirectory)', None: '没签'}[sig]
145
+ print('|'.join(['+'.join(archs) or '?', label]))
146
+ PY
73
147
  }
74
148
 
75
149
  # Info.plist 里的 CFBundleExecutable 和实际文件名对不上时(手改过、重打包过),
@@ -78,27 +152,52 @@ find_exe() {
78
152
  local app="$1" e
79
153
  e="$(plist_get "$app" CFBundleExecutable)"
80
154
  if [ -n "$e" ] && [ -f "$app/$e" ]; then echo "$e"; return; fi
81
- local f mag
82
- for f in "$app"/*; do
83
- [ -f "$f" ] || continue
84
- mag="$(head -c 4 "$f" | od -An -tx1 | tr -d ' \n')"
85
- case "$mag" in
86
- cffaedfe|cefaedfe) basename "$f"; return;;
87
- esac
88
- done
89
- echo ""
155
+ python3 - "$app" <<'PY' 2>/dev/null || true
156
+ import os, struct, sys
157
+ app = sys.argv[1]
158
+ MAG = (0xfeedface, 0xfeedfacf, 0xcafebabe, 0xcafebabf) # 32/64 位 Mach-O 和 fat
159
+ for name in sorted(os.listdir(app)):
160
+ p = os.path.join(app, name)
161
+ if not os.path.isfile(p):
162
+ continue
163
+ try:
164
+ head = open(p, 'rb').read(4)
165
+ m = struct.unpack('<I', head)[0]
166
+ if m == 0xcafebabe and len(head) == 4:
167
+ # 0xcafebabe 也可能是 Java 类,看 nfat 是否像一个正常值
168
+ nfat = struct.unpack('<I', open(p, 'rb').read(8)[4:8])[0]
169
+ if nfat > 20:
170
+ continue
171
+ except Exception:
172
+ continue
173
+ if m in MAG:
174
+ print(name)
175
+ break
176
+ PY
90
177
  }
91
178
 
92
179
  find_app() {
93
- find "$1" -maxdepth 3 -name '*.app' -type d | head -1
180
+ find "$1" -maxdepth 3 -name '*.app' -type d 2>/dev/null | awk '{ print length($0), $0 }' | sort -n | head -1 | cut -d' ' -f2-
94
181
  }
95
182
 
96
183
  cmd_doctor() {
97
- printf '%-9s %s\n' clang "$(command -v clang >/dev/null && clang --version | head -1 | sed 's/.*version //' || echo 缺)"
98
- printf '%-9s %s\n' ld64.lld "$(command -v ld64.lld >/dev/null && echo ok || echo 缺)"
184
+ local v
185
+ v="$(command -v clang >/dev/null 2>&1 && clang --version 2>/dev/null | head -1 | sed 's/.*version //' || echo 缺)"
186
+ printf '%-9s %s\n' clang "$v"
187
+ printf '%-9s %s\n' ld64.lld "$(command -v ld64.lld >/dev/null 2>&1 && echo ok || echo 缺)"
99
188
  printf '%-9s %s\n' sdk "$([ -d "$sdk" ] && echo "$sdk" || echo 缺)"
100
189
  printf '%-9s %s\n' ldid "$([ -x "$ldid" ] && echo "$ldid" || echo 缺)"
190
+ printf '%-9s %s\n' python3 "$(command -v python3 >/dev/null 2>&1 && echo ok || echo 缺)"
191
+ printf '%-9s %s\n' zip "$(command -v zip >/dev/null 2>&1 && echo ok || echo 缺)"
192
+ printf '%-9s %s\n' unzip "$(command -v unzip >/dev/null 2>&1 && echo ok || echo 缺)"
101
193
  printf '%-9s %s\n' 系统 "$(uname -s)"
194
+ case "$(uname -s)" in
195
+ Linux)
196
+ if command -v apt-get >/dev/null 2>&1; then echo "包管理 apt(clang lld python3 zip unzip)"
197
+ elif command -v dnf >/dev/null 2>&1; then echo "包管理 dnf(clang lld python3 zip unzip)"
198
+ elif command -v pacman >/dev/null 2>&1; then echo "包管理 pacman(clang lld python zip unzip)"
199
+ else echo "包管理 认不出,bootstrap 只支持 apt/dnf/pacman"; fi;;
200
+ esac
102
201
  }
103
202
 
104
203
  cmd_info() {
@@ -112,19 +211,25 @@ cmd_info() {
112
211
  local exe; exe="$(find_exe "$t")"
113
212
  [ -n "$exe" ] || exe="$(basename "$t" .app)"
114
213
 
214
+ local bid ver build minos
215
+ bid="$(plist_get "$t" CFBundleIdentifier)"
216
+ ver="$(plist_get "$t" CFBundleShortVersionString)"
217
+ build="$(plist_get "$t" CFBundleVersion)"
218
+ minos="$(plist_get "$t" MinimumOSVersion)"
219
+
115
220
  echo "位置 $orig"
116
221
  echo "可执行 $exe"
117
- echo "标识符 $(plist_get "$t" CFBundleIdentifier)"
118
- echo "版本 $(plist_get "$t" CFBundleShortVersionString) build $(plist_get "$t" CFBundleVersion)"
119
- echo "最低系统 $(plist_get "$t" MinimumOSVersion)"
222
+ echo "标识符 $bid"
223
+ echo "版本 $ver build $build"
224
+ echo "最低系统 $minos"
225
+ if [ -z "$bid$ver$minos" ]; then
226
+ echo "plist 读不出字段,多半是二进制格式(bplist)——Xcode 产出的包都是这种,换 pack 或 resign 处理前先 plutil -convert xml1"
227
+ fi
120
228
  if [ -f "$t/$exe" ]; then
121
- echo "架构 $(file "$t/$exe" | sed 's/^[^:]*: //')"
229
+ local rep; rep="$(macho_report "$t/$exe")"
230
+ echo "架构 ${rep%%|*}"
231
+ echo "签名 ${rep#*|}"
122
232
  echo "大小 $(du -h "$t/$exe" | cut -f1)"
123
- if "$ldid" -h "$t/$exe" 2>/dev/null | grep -q CodeDirectory; then
124
- echo "签名 有 CodeDirectory,是伪签名"
125
- else
126
- echo "签名 没签"
127
- fi
128
233
  fi
129
234
  [ -n "$tmp" ] && rm -rf "$tmp"
130
235
  true
@@ -139,7 +244,7 @@ cmd_build() {
139
244
  [ -d "$sdk" ] || die "没有 SDK,先跑 scripts/bootstrap.sh"
140
245
 
141
246
  local name="" bid="com.example.app" minios="14.0" ent="" icon="" out="" fw=()
142
- local p12="" prov="" pwd_=""
247
+ local p12="" prov="" pwd_cli=""
143
248
  while [ $# -gt 0 ]; do
144
249
  case "$1" in
145
250
  --name) name="$2"; shift 2;;
@@ -150,11 +255,12 @@ cmd_build() {
150
255
  --framework) fw+=(-framework "$2"); shift 2;;
151
256
  --p12) p12="$2"; shift 2;;
152
257
  --prov) prov="$2"; shift 2;;
153
- --password|--pass) pwd_="$2"; shift 2;;
258
+ --password|--pass) pwd_cli="$2"; shift 2;;
154
259
  -o) out="$2"; shift 2;;
155
260
  *) die "不认识的参数: $1";;
156
261
  esac
157
262
  done
263
+ pwd_="$(cert_pass "$pwd_cli")"
158
264
  [ -n "$name" ] || name="$(basename "$src")"
159
265
  [ -n "$out" ] || out="$PWD/dist/$name.ipa"
160
266
 
@@ -162,11 +268,11 @@ cmd_build() {
162
268
  local app="$work/Payload/$name.app"
163
269
  rm -rf "$work"; mkdir -p "$app"
164
270
 
271
+ # 源码不限层级,跟 Windows 版保持一致
165
272
  local files=() f
166
- while IFS= read -r f; do files+=("$f"); done < <(find "$src" -maxdepth 2 -type f \( -name '*.m' -o -name '*.mm' -o -name '*.c' -o -name '*.cpp' \))
273
+ while IFS= read -r f; do files+=("$f"); done < <(find "$src" -type f \( -name '*.m' -o -name '*.mm' -o -name '*.c' -o -name '*.cpp' \) ! -path '*/.build/*')
167
274
  [ ${#files[@]} -gt 0 ] || die "$src 里没有 .m/.mm/.c/.cpp"
168
275
 
169
- # 碰了 UIKit 才链 UIKit,不然白拉一堆依赖
170
276
  if [ ${#fw[@]} -eq 0 ]; then
171
277
  if grep -rq "UIKit/" "$src" 2>/dev/null; then
172
278
  fw=(-framework UIKit -framework Foundation)
@@ -181,44 +287,52 @@ cmd_build() {
181
287
  done
182
288
 
183
289
  echo "编译 ${#files[@]} 个文件,arm64 ios$minios"
184
- clang -target "arm64-apple-ios$minios" -isysroot "$sdk" \
185
- -F "$sdk/System/Library/Frameworks" \
186
- -fuse-ld=lld -O2 -fPIC "${arc[@]}" "${fw[@]}" \
187
- -o "$app/$name" "${files[@]}"
188
-
189
- cp "$here/templates/Info.plist" "$app/Info.plist"
190
- sed -i.bak "s|@NAME@|$name|g; s|@BUNDLEID@|$bid|g; s|@MINIOS@|$minios|g" "$app/Info.plist"
191
- rm -f "$app/Info.plist.bak"
290
+ clang -target "arm64-apple-ios$minios" -isysroot "$sdk" -F "$sdk/System/Library/Frameworks" -fuse-ld=lld -O2 -fPIC "${arc[@]}" "${fw[@]}" -o "$app/$name" "${files[@]}"
291
+
292
+ # 模板填充走 python,名字里带 & < | 也不会把 plist 弄坏
293
+ need_py
294
+ python3 - "$here/templates/Info.plist" "$app/Info.plist" "$name" "$bid" "$minios" <<'PY'
295
+ import sys
296
+ from xml.sax.saxutils import escape
297
+ tpl, out, name, bid, minios = sys.argv[1:6]
298
+ s = open(tpl, encoding='utf-8').read()
299
+ for k, v in (('@NAME@', name), ('@BUNDLEID@', bid), ('@MINIOS@', minios)):
300
+ s = s.replace(k, escape(v))
301
+ open(out, 'w', encoding='utf-8').write(s)
302
+ PY
192
303
  [ -n "$icon" ] && [ -f "$icon" ] && cp "$icon" "$app/AppIcon60x60@2x.png"
193
-
194
304
  chmod +x "$app/$name"
305
+
195
306
  if [ -z "$p12" ]; then
196
307
  need_ldid
197
- if [ -n "$ent" ] && [ -f "$ent" ]; then
198
- "$ldid" -S"$ent" "$app/$name" || die "ldid 签名失败"
199
- else
200
- "$ldid" -S "$app/$name" || die "ldid 签名失败"
201
- fi
308
+ if [ -n "$ent" ] && [ -f "$ent" ]; then "$ldid" -S"$ent" "$app/$name" || die "ldid 签名失败"
309
+ else "$ldid" -S "$app/$name" || die "ldid 签名失败"; fi
202
310
  fi
203
311
 
204
312
  local tmpipa="$out.tmp.ipa"
205
313
  zip_payload "$tmpipa" "$work"
206
- if [ -n "$p12" ]; then
207
- sign_with_cert "$tmpipa" "$out"
208
- rm -f "$tmpipa"
209
- else
210
- mv -f "$tmpipa" "$out"
211
- fi
314
+ if [ -n "$p12" ]; then sign_with_cert "$tmpipa" "$out"; rm -f "$tmpipa"
315
+ else mv -f "$tmpipa" "$out"; fi
316
+ rm -rf "$PWD/.build"
212
317
  echo "产物 $out"
213
318
  }
214
319
 
320
+ zip_payload() {
321
+ local out="$1" root="$2"
322
+ case "$out" in /*) ;; *) out="$PWD/$out";; esac
323
+ mkdir -p "$(dirname "$out")"
324
+ rm -f "$out"
325
+ # -y 保留符号链接,别把 .framework 里的链接展开成重复文件
326
+ ( cd "$root" && zip -qry "$out" Payload )
327
+ }
328
+
215
329
  cmd_pack() {
216
330
  shift
217
331
  local src="${1:-}"; [ -n "$src" ] || die "用法: ipa pack 某.app 或 含 Payload 的目录 [-o 输出.ipa]"
218
332
  shift
219
333
  [ -e "$src" ] || die "没有这个路径: $src"
220
334
 
221
- local out="" bid="" name="" version="" icon="" p12="" prov="" pwd_=""
335
+ local out="" bid="" name="" version="" icon="" p12="" prov="" pwd_cli=""
222
336
  while [ $# -gt 0 ]; do
223
337
  case "$1" in
224
338
  -o) out="$2"; shift 2;;
@@ -228,10 +342,11 @@ cmd_pack() {
228
342
  --icon) icon="$2"; shift 2;;
229
343
  --p12) p12="$2"; shift 2;;
230
344
  --prov) prov="$2"; shift 2;;
231
- --password|--pass) pwd_="$2"; shift 2;;
345
+ --password|--pass) pwd_cli="$2"; shift 2;;
232
346
  *) die "不认识的参数: $1";;
233
347
  esac
234
348
  done
349
+ pwd_="$(cert_pass "$pwd_cli")"
235
350
 
236
351
  local work; work="$(mktemp -d)"; mkdir -p "$work/Payload"
237
352
  if [ -d "$src" ] && [ "$(basename "$src")" = "Payload" ]; then
@@ -241,13 +356,21 @@ cmd_pack() {
241
356
  fi
242
357
 
243
358
  local app; app="$(find "$work/Payload" -maxdepth 1 -name '*.app' -type d | head -1)"
244
- [ -n "$app" ] || die "Payload 里没有 .app"
359
+ if [ -z "$app" ]; then
360
+ app="$(find "$work" -name '*.app' -type d | awk '{ print length($0), $0 }' | sort -n | head -1 | cut -d' ' -f2-)"
361
+ [ -n "$app" ] || die "这棵树里没找到 .app。可以直接把 .app 本身当参数传进来"
362
+ echo "给的目录里没有顶层 .app,改用找到的这个:$app"
363
+ fi
245
364
 
365
+ need_py
246
366
  local pairs=()
247
367
  [ -n "$bid" ] && pairs+=("CFBundleIdentifier=$bid")
248
368
  [ -n "$name" ] && pairs+=("CFBundleDisplayName=$name" "CFBundleExecutable=$name")
249
369
  [ -n "$version" ] && pairs+=("CFBundleShortVersionString=$version")
250
- [ ${#pairs[@]} -gt 0 ] && plist_set "$app" "${pairs[@]}"
370
+ if [ ${#pairs[@]} -gt 0 ]; then
371
+ [ -f "$app/Info.plist" ] || die "$app 里没有 Info.plist"
372
+ plist_set "$app" "${pairs[@]}"
373
+ fi
251
374
  [ -n "$icon" ] && [ -f "$icon" ] && cp "$icon" "$app/AppIcon60x60@2x.png"
252
375
 
253
376
  local exe; exe="$(find_exe "$app")"
@@ -256,7 +379,7 @@ cmd_pack() {
256
379
  mv "$app/$exe" "$app/$name"
257
380
  exe="$name"
258
381
  fi
259
- [ -z "$out" ] && out="$PWD/dist/$(basename "$app" .app).ipa"
382
+ [ -z "$out" ] && out="$PWD/dist/${name:-$(basename "$app" .app)}.ipa"
260
383
 
261
384
  if [ -z "$p12" ] && [ -f "$app/$exe" ] && [ -x "$ldid" ]; then
262
385
  rm -rf "$app/_CodeSignature"
@@ -265,23 +388,52 @@ cmd_pack() {
265
388
 
266
389
  local tmpipa="$out.tmp.ipa"
267
390
  zip_payload "$tmpipa" "$work"
268
- if [ -n "$p12" ]; then
269
- sign_with_cert "$tmpipa" "$out"
270
- rm -f "$tmpipa"
271
- else
272
- mv -f "$tmpipa" "$out"
273
- fi
391
+ if [ -n "$p12" ]; then sign_with_cert "$tmpipa" "$out"; rm -f "$tmpipa"
392
+ else mv -f "$tmpipa" "$out"; fi
274
393
  rm -rf "$work"
275
394
  echo "产物 $out"
276
395
  }
277
396
 
397
+ # 内嵌的 framework / appex / dylib 都得签,只签主程序启动就崩
398
+ sign_nested() {
399
+ local app="$1" ent="$2" d
400
+ if [ -d "$app/PlugIns" ]; then
401
+ while IFS= read -r x; do
402
+ local xe; xe="$(plist_get "$x" CFBundleExecutable)"
403
+ [ -n "$xe" ] || xe="$(basename "$x" .appex)"
404
+ if [ -n "$ent" ] && [ -f "$ent" ]; then "$ldid" -S"$ent" "$x/$xe" || die "签 $x 失败"
405
+ else "$ldid" -S "$x/$xe" || die "签 $x 失败"; fi
406
+ find "$x" -name '*.dylib' -type f | while read -r d; do "$ldid" -S "$d" || true; done
407
+ done < <(find "$app/PlugIns" -maxdepth 2 -name '*.appex' -type d 2>/dev/null)
408
+ fi
409
+ if [ -d "$app/Frameworks" ]; then
410
+ while IFS= read -r fwdir; do
411
+ local bin="$fwdir/$(basename "$fwdir" .framework)"
412
+ [ -f "$bin" ] && { "$ldid" -S "$bin" || die "签 $bin 失败"; }
413
+ find "$fwdir" -name '*.dylib' -type f | while read -r d; do "$ldid" -S "$d" || true; done
414
+ done < <(find "$app/Frameworks" -maxdepth 1 -name '*.framework' -type d 2>/dev/null)
415
+ find "$app/Frameworks" -maxdepth 1 -name '*.dylib' -type f | while read -r d; do "$ldid" -S "$d" || true; done
416
+ fi
417
+ }
418
+
419
+ # 换主 id 时扩展的 id 要跟着换,真签要求 appex id 以主 app id 开头
420
+ retag_appex() {
421
+ local app="$1" bid="$2" x eid base
422
+ while IFS= read -r x; do
423
+ eid="$(plist_get "$x" CFBundleIdentifier)"
424
+ [ -n "$eid" ] || continue
425
+ base="${eid##*.}"
426
+ plist_set "$x" "CFBundleIdentifier=$bid.$base"
427
+ done < <(find "$app/PlugIns" -maxdepth 2 -name '*.appex' -type d 2>/dev/null)
428
+ }
429
+
278
430
  cmd_resign() {
279
431
  shift
280
432
  local src="${1:-}"; [ -n "$src" ] || die "用法: ipa resign 某.ipa 或 某.app [-o 输出.ipa]"
281
433
  shift
282
434
  [ -e "$src" ] || die "没有这个路径: $src"
283
435
 
284
- local out="" ent="" bid="" p12="" prov="" pwd_=""
436
+ local out="" ent="" bid="" p12="" prov="" pwd_cli=""
285
437
  while [ $# -gt 0 ]; do
286
438
  case "$1" in
287
439
  -o) out="$2"; shift 2;;
@@ -289,10 +441,11 @@ cmd_resign() {
289
441
  --bundle-id) bid="$2"; shift 2;;
290
442
  --p12) p12="$2"; shift 2;;
291
443
  --prov) prov="$2"; shift 2;;
292
- --password|--pass) pwd_="$2"; shift 2;;
444
+ --password|--pass) pwd_cli="$2"; shift 2;;
293
445
  *) die "不认识的参数: $1";;
294
446
  esac
295
447
  done
448
+ pwd_="$(cert_pass "$pwd_cli")"
296
449
 
297
450
  local work; work="$(mktemp -d)"
298
451
  case "$src" in
@@ -304,31 +457,28 @@ cmd_resign() {
304
457
  local app; app="$(find "$work/Payload" -maxdepth 1 -name '*.app' -type d | head -1)"
305
458
  [ -n "$app" ] || die "包里没有 .app"
306
459
 
307
- [ -n "$bid" ] && plist_set "$app" "CFBundleIdentifier=$bid"
460
+ if [ -n "$bid" ]; then
461
+ need_py
462
+ plist_set "$app" "CFBundleIdentifier=$bid"
463
+ retag_appex "$app" "$bid"
464
+ fi
308
465
  rm -rf "$app/_CodeSignature"
309
466
 
310
467
  local exe; exe="$(find_exe "$app")"
311
468
  [ -z "$exe" ] && die "这个 app 里没找到可执行文件"
312
469
  if [ -z "$p12" ]; then
313
470
  need_ldid
314
- if [ -n "$ent" ] && [ -f "$ent" ]; then
315
- "$ldid" -S"$ent" "$app/$exe"
316
- else
317
- "$ldid" -S "$app/$exe"
318
- fi
319
- # 包里带的 dylib 也得签,不然启动就崩
320
- find "$app" -name '*.dylib' -type f 2>/dev/null | while read -r d; do "$ldid" -S "$d" 2>/dev/null || true; done
471
+ sign_nested "$app" "$ent"
472
+ if [ -n "$ent" ] && [ -f "$ent" ]; then "$ldid" -S"$ent" "$app/$exe" || die "ldid 签名失败"
473
+ else "$ldid" -S "$app/$exe" || die "ldid 签名失败"; fi
321
474
  fi
322
475
 
323
- [ -z "$out" ] && out="${src%.*}_resigned.ipa"
476
+ local leaf; leaf="$(basename "$src")"; leaf="${leaf%.*}"
477
+ [ -z "$out" ] && out="$PWD/dist/${leaf}_resigned.ipa"
324
478
  local tmpipa="$out.tmp.ipa"
325
479
  zip_payload "$tmpipa" "$work"
326
- if [ -n "$p12" ]; then
327
- sign_with_cert "$tmpipa" "$out"
328
- rm -f "$tmpipa"
329
- else
330
- mv -f "$tmpipa" "$out"
331
- fi
480
+ if [ -n "$p12" ]; then sign_with_cert "$tmpipa" "$out"; rm -f "$tmpipa"
481
+ else mv -f "$tmpipa" "$out"; fi
332
482
  rm -rf "$work"
333
483
  echo "产物 $out"
334
484
  }
@@ -343,5 +493,5 @@ case "${1:-}" in
343
493
  echo " info 看 ipa/app 的标识、版本、架构、签没签"
344
494
  echo " build .m/.c 编成 ipa,需要 clang 和 SDK"
345
495
  echo " pack .app 打成 ipa,可改标识/显示名/版本/图标"
346
- echo " resign 已有的 ipa 或 app 重做伪签名,可换标识";;
496
+ echo " resign 已有的 ipa 或 app 重签,可换标识";;
347
497
  esac
package/scripts/ipa.ps1 CHANGED
@@ -17,9 +17,111 @@ if (-not (Test-Path $zsign)) {
17
17
  if (Test-Path $vendored) { $zsign = $vendored }
18
18
  }
19
19
  if ($env:IPA_ZSIGN) { $zsign = $env:IPA_ZSIGN }
20
+ # 工具链自己的环境变量文件,bootstrap 写在这里(不动用户持久 PATH)
21
+ $envFile = Join-Path $tc 'env.ps1'
22
+ if ((Test-Path $envFile) -and -not $env:IPA_NO_ENV) { . $envFile }
20
23
 
21
24
  function Die($m) { Write-Host $m; exit 1 }
22
25
 
26
+ function Test-Bplist($path) {
27
+ if (-not (Test-Path $path)) { return $false }
28
+ $fs = [IO.File]::OpenRead($path)
29
+ try {
30
+ $b = New-Object byte[] 6
31
+ $fs.Read($b, 0, 6) | Out-Null
32
+ return ([Text.Encoding]::ASCII.GetString($b) -eq 'bplist')
33
+ } finally { $fs.Dispose() }
34
+ }
35
+
36
+ # 按偏移读一小段,不把整个可执行文件塞进内存
37
+ function Read-At($fs, $off, $len) {
38
+ $b = New-Object byte[] $len
39
+ $fs.Position = $off
40
+ $n = $fs.Read($b, 0, $len)
41
+ if ($n -lt $len) { return $null }
42
+ return ,$b
43
+ }
44
+
45
+ function Cpu-Name($v) {
46
+ switch ($v) {
47
+ 16777228 { 'arm64' }
48
+ 16777223 { 'x86_64' }
49
+ 12 { 'armv7' }
50
+ 7 { 'i386' }
51
+ 16777222 { 'arm64_32' }
52
+ default { '?' }
53
+ }
54
+ }
55
+
56
+ # 解析 LC_CODE_SIGNATURE 和签名 SuperBlob:有 CMS 才是真证书签名
57
+ function Get-SigState($fs, $off) {
58
+ $h = Read-At $fs $off 32
59
+ if ($h -eq $null) { return '没签' }
60
+ $mg = [BitConverter]::ToUInt32($h, 0)
61
+ if ($mg -eq 4277009103) { $hsz = 32 } elseif ($mg -eq 4277009102) { $hsz = 28 } else { return '没签' }
62
+ $ncmds = [BitConverter]::ToUInt32($h, 16)
63
+ $sizeofcmds = [BitConverter]::ToUInt32($h, 20)
64
+ if ($ncmds -gt 4096 -or $sizeofcmds -gt 1048576) { return '没签' }
65
+ $cmds = Read-At $fs ($off + $hsz) $sizeofcmds
66
+ if ($cmds -eq $null) { return '没签' }
67
+ $sigoff = -1
68
+ $pos = 0
69
+ for ($i = 0; $i -lt $ncmds; $i++) {
70
+ if ($pos + 8 -gt $sizeofcmds) { break }
71
+ $cmd = [BitConverter]::ToUInt32($cmds, $pos)
72
+ $size = [BitConverter]::ToUInt32($cmds, $pos + 4)
73
+ if ($size -lt 8) { break }
74
+ if ($cmd -eq 29 -and $pos + 16 -le $sizeofcmds) { $sigoff = [BitConverter]::ToUInt32($cmds, $pos + 8) }
75
+ $pos += $size
76
+ }
77
+ if ($sigoff -lt 0) { return '没签' }
78
+ $sb = Read-At $fs $sigoff 12
79
+ if ($sb -eq $null -or [BitConverter]::ToUInt32($sb, 0) -ne 4208856256) { return '没签' }
80
+ $cnt = [BitConverter]::ToUInt32($sb, 4)
81
+ if ($cnt -gt 20) { $cnt = 20 }
82
+ $idx = Read-At $fs ($sigoff + 8) ($cnt * 8)
83
+ if ($idx -eq $null) { return '没签' }
84
+ for ($i = 0; $i -lt $cnt; $i++) {
85
+ if ([BitConverter]::ToUInt32($idx, $i * 8) -eq 0x10000) { return '真证书签名(有 CMS)' }
86
+ }
87
+ return 'ad-hoc 伪签名(只有 CodeDirectory)'
88
+ }
89
+
90
+ # fat binary 的 offset 4 是 nfat_arch,不是 cputype
91
+ function Get-MachReport($path) {
92
+ $fs = $null
93
+ try { $fs = [IO.File]::OpenRead($path) } catch { return @('读不了', '读不了', 0) }
94
+ try {
95
+ $h = Read-At $fs 0 32
96
+ if ($h -eq $null) { return @('?', '读不了', (Get-Item $path).Length) }
97
+ $magic = [BitConverter]::ToUInt32($h, 0)
98
+ $archs = @()
99
+ $sig = '没签'
100
+ if ($magic -eq 4277009102 -or $magic -eq 4277009103) {
101
+ $archs += (Cpu-Name ([BitConverter]::ToUInt32($h, 4)))
102
+ $sig = Get-SigState $fs 0
103
+ } elseif ($magic -eq 3405691582 -or $magic -eq 3405691583) {
104
+ $nfat = [BitConverter]::ToUInt32($h, 4)
105
+ if ($nfat -gt 20) { $nfat = 0 }
106
+ $entSize = if ($magic -eq 3405691583) { 32 } else { 20 }
107
+ for ($i = 0; $i -lt $nfat; $i++) {
108
+ $e = Read-At $fs (8 + $i * $entSize) $entSize
109
+ if ($e -eq $null) { continue }
110
+ $archs += (Cpu-Name ([BitConverter]::ToUInt32($e, 0)))
111
+ $off = if ($entSize -eq 32) { [BitConverter]::ToUInt64($e, 8) } else { [BitConverter]::ToUInt32($e, 8) }
112
+ $st = Get-SigState $fs $off
113
+ if ($st -ne '没签') { $sig = $st }
114
+ }
115
+ } else {
116
+ $archs += '不是 Mach-O'
117
+ }
118
+ $archLabel = ($archs -join '+')
119
+ if ($archLabel -eq '') { $archLabel = '?' }
120
+ if ($archs.Count -gt 1) { $archLabel = "$archLabel(fat)" }
121
+ return @($archLabel, $sig, (Get-Item $path).Length)
122
+ } finally { if ($fs) { $fs.Dispose() } }
123
+ }
124
+
23
125
  # $argv[1..0] 在 PowerShell 里会倒序取出 @($null, $argv[0]),后面 $null.StartsWith 直接崩
24
126
  function Rest($a) {
25
127
  if ($a.Count -gt 1) { return @($a[1..($a.Count - 1)]) }
@@ -55,13 +157,21 @@ function Get-Value($pl, $key) {
55
157
 
56
158
  function Set-Value($pl, $key, $value) {
57
159
  if ($pl -eq $null) { return }
58
- $nodes = $pl.plist.dict.ChildNodes
160
+ $dict = $pl.plist.dict
161
+ $nodes = $dict.ChildNodes
59
162
  for ($i = 0; $i -lt $nodes.Count - 1; $i++) {
60
163
  if ($nodes[$i].Name -eq 'key' -and $nodes[$i].InnerText -eq $key) {
61
164
  $v = $nodes[$i + 1]
62
- if ($v.Name -eq 'string') { $v.InnerText = $value }
165
+ if ($v.Name -eq 'string') { $v.InnerText = $value; return }
63
166
  }
64
167
  }
168
+ # plist 里没有这个键就补上,不然 --name / --version 会静默不生效
169
+ $k = $pl.CreateElement('key')
170
+ $k.InnerText = $key
171
+ $v = $pl.CreateElement('string')
172
+ $v.InnerText = $value
173
+ $dict.AppendChild($k) | Out-Null
174
+ $dict.AppendChild($v) | Out-Null
65
175
  }
66
176
 
67
177
  function Get-Exe($app) {
@@ -77,8 +187,8 @@ function Get-Exe($app) {
77
187
  $b = New-Object byte[] 8
78
188
  $fs.Read($b, 0, 8) | Out-Null
79
189
  $fs.Close()
80
- $m = [BitConverter]::ToUInt32($b, 0)
81
- if ($m -eq 4277009103 -or $m -eq 4277009102 -or $m -eq 3199925962) { return $f.Name }
190
+ $m = [BitConverter]::ToUInt32($b, 0) # 64/32 位 Mach-O、fat 的魔数:64/32 位 Mach-O 或 fat
191
+ if ($m -eq 4277009103 -or $m -eq 4277009102 -or $m -eq 3405691582 -or $m -eq 3405691583 -or $m -eq 3199925962) { return $f.Name }
82
192
  }
83
193
  return $null
84
194
  }
@@ -90,6 +200,13 @@ function Find-App($dir) {
90
200
  }
91
201
 
92
202
  function Zip-Dir($src, $out) {
203
+ # .NET 的 CreateFromDirectory 不认符号链接,会把链接展开成整份文件副本。
204
+ # .app 里带 framework 符号链接时包里会多出一堆重复文件,这里先说清楚
205
+ $links = Get-ChildItem $src -Recurse -Force -ErrorAction SilentlyContinue |
206
+ Where-Object { $_.Attributes -band [IO.FileAttributes]::ReparsePoint }
207
+ if ($links) {
208
+ Write-Host "包里有 $($links.Count) 个符号链接,Windows 打包会展开成副本(结构能用但体积偏大)。要严格保留链接请在 macOS 上用 scripts/ipa"
209
+ }
93
210
  $parent = Split-Path -Parent $out
94
211
  if ($parent -and -not (Test-Path $parent)) { New-Item -ItemType Directory -Path $parent -Force | Out-Null }
95
212
  if (Test-Path $out) { Remove-Item $out -Force }
@@ -99,7 +216,7 @@ function Zip-Dir($src, $out) {
99
216
 
100
217
  # 有 p12 就走真证书签名(zsign -k / -m / -p),没有就走 -a ad-hoc。
101
218
  # 真证书签名 zsign 会联网查 OCSP 吊销状态,离线时可能变慢或告警,这是它本来行为。
102
- function Sign-Ipa($in, $out, $p12, $prov, $pass) {
219
+ function Sign-Ipa($in, $out, $p12, $prov, $pass, $ent) {
103
220
  if (-not (Test-Path $zsign)) { Die "没有 zsign.exe,先跑 scripts\bootstrap.ps1" }
104
221
  $parent = Split-Path -Parent $out
105
222
  if ($parent -and -not (Test-Path $parent)) { New-Item -ItemType Directory -Path $parent -Force | Out-Null }
@@ -111,6 +228,10 @@ function Sign-Ipa($in, $out, $p12, $prov, $pass) {
111
228
  if (-not (Test-Path $prov)) { Die "找不到描述文件: $prov" }
112
229
  $a = @('-k', (Resolve-Path $p12).Path, '-m', (Resolve-Path $prov).Path)
113
230
  if ($pass) { $a += @('-p', $pass) }
231
+ if ($ent) {
232
+ if (-not (Test-Path $ent)) { Die "找不到 entitlements 文件: $ent" }
233
+ $a += @('-e', (Resolve-Path $ent).Path)
234
+ }
114
235
  $a += @('-o', $out, $in)
115
236
  } else {
116
237
  if ($prov) { Write-Host "只有描述文件没有 p12,还是按伪签处理" }
@@ -119,6 +240,7 @@ function Sign-Ipa($in, $out, $p12, $prov, $pass) {
119
240
 
120
241
  # 直接用调用运算符传数组,PowerShell 会给带空格的参数自己加引号;
121
242
  # Start-Process 是把数组拼成一行再交给系统,密码里有空格就被拆成两个参数了
243
+ if ($pass) { Write-Host "提示:--password 会留在命令历史和进程列表里,建议改用环境变量 IPA_CERT_PASSWORD" }
122
244
  & $zsign @a
123
245
  if ($LASTEXITCODE -ne 0 -or -not (Test-Path $out)) {
124
246
  if ($p12) { Die "zsign 签名失败,退出码 $LASTEXITCODE。常见原因是 p12 密码不对,或者 bundle id 跟描述文件对不上。" }
@@ -197,31 +319,34 @@ function Cmd-Info($path) {
197
319
  $app = $found.FullName
198
320
  }
199
321
  $pl = Read-Plist $app
322
+ $exe = Get-Exe $app
200
323
  Write-Host "位置 $path"
201
- Write-Host "可执行 $(Get-Exe $app)"
324
+ Write-Host "可执行 $exe"
202
325
  Write-Host "标识符 $(Get-Value $pl 'CFBundleIdentifier')"
203
326
  Write-Host "版本 $(Get-Value $pl 'CFBundleShortVersionString') build $(Get-Value $pl 'CFBundleVersion')"
204
327
  Write-Host "最低系统 $(Get-Value $pl 'MinimumOSVersion')"
205
- $exe = Get-Exe $app
328
+ $iplist = Join-Path $app 'Info.plist'
329
+ if ($pl -eq $null -and (Test-Bplist $iplist)) {
330
+ Write-Host "plist 是二进制格式(bplist),字段读不出来。要看字段先在 macOS 上 plutil -convert xml1 转成 XML"
331
+ }
206
332
  if ($exe -ne $null) {
207
333
  $bin = Join-Path $app $exe
208
- $bytes = [IO.File]::ReadAllBytes($bin)
209
- $cpu = [BitConverter]::ToInt32($bytes, 4)
210
- $arch = '?'
211
- if ($cpu -eq 16777228) { $arch = 'arm64' }
212
- if ($cpu -eq 12) { $arch = 'armv7' }
213
- if ($cpu -eq 16777223) { $arch = 'x86_64' }
214
- Write-Host "架构 Mach-O $arch"
215
- Write-Host ("大小 " + [math]::Round($bytes.Length / 1KB) + " KB")
216
- $hasSig = $false
217
- $text = [Text.Encoding]::ASCII.GetString($bytes)
218
- if ($text.Contains('CodeDirectory')) { $hasSig = $true }
219
- Write-Host ("签名 " + $(if ($hasSig) { '有 CodeDirectory' } else { '没签' }))
334
+ $rep = Get-MachReport $bin
335
+ Write-Host "架构 $($rep[0])"
336
+ Write-Host "签名 $($rep[1])"
337
+ Write-Host ("大小 " + [math]::Round($rep[2] / 1KB) + " KB")
220
338
  }
221
339
  if (Test-Path (Join-Path $app '_CodeSignature')) { Write-Host "签名目录 有 _CodeSignature" }
222
340
  if ($tmp -ne $null) { Remove-Item $tmp -Recurse -Force -ErrorAction SilentlyContinue }
223
341
  }
224
342
 
343
+ # 密码:命令行的照收,但没有就用环境变量,省得进 shell 历史
344
+ function Get-Pass($opt) {
345
+ $p = Opt $opt @('--password', '--pass') $null
346
+ if (-not $p) { $p = $env:IPA_CERT_PASSWORD }
347
+ return $p
348
+ }
349
+
225
350
  function Cmd-Pack($argv) {
226
351
  if ($argv.Count -eq 0) { Die "用法: ipa.ps1 pack 某.app [-o 输出.ipa] [--bundle-id ...] [--p12 证书 --prov 描述文件 --password 密码]" }
227
352
  $src = $argv[0]
@@ -273,7 +398,7 @@ function Cmd-Pack($argv) {
273
398
  $tmp = Join-Path ([IO.Path]::GetTempPath()) ('pack_' + [guid]::NewGuid().ToString('N') + '.ipa')
274
399
  Zip-Dir $work $tmp
275
400
  if (Test-Path $zsign) {
276
- Sign-Ipa $tmp $out (Opt $opt @('--p12') $null) (Opt $opt @('--prov') $null) (Opt $opt @('--password', '--pass') $null)
401
+ Sign-Ipa $tmp $out (Opt $opt @('--p12') $null) (Opt $opt @('--prov') $null) (Get-Pass $opt) (Opt $opt @('--entitlements') $null)
277
402
  } else {
278
403
  Copy-Item $tmp $out -Force
279
404
  Write-Host "没有 zsign,包出来了但没签名"
@@ -317,9 +442,9 @@ function Cmd-Resign($argv) {
317
442
  $out = Opt $opt @('-o', '--out') $null
318
443
  if (-not $out) {
319
444
  $leaf = [IO.Path]::GetFileNameWithoutExtension((Split-Path -Leaf $src))
320
- $out = Join-Path (Get-Location) "dist/${leaf}_signed.ipa"
445
+ $out = Join-Path (Get-Location) "dist/${leaf}_resigned.ipa"
321
446
  }
322
- Sign-Ipa $in $out (Opt $opt @('--p12') $null) (Opt $opt @('--prov') $null) (Opt $opt @('--password', '--pass') $null)
447
+ Sign-Ipa $in $out (Opt $opt @('--p12') $null) (Opt $opt @('--prov') $null) (Get-Pass $opt) (Opt $opt @('--entitlements') $null)
323
448
  Write-Host "产物 $out"
324
449
  }
325
450
 
@@ -352,12 +477,17 @@ function Cmd-Build($argv) {
352
477
  $wsl = Get-Command wsl.exe -ErrorAction SilentlyContinue
353
478
  if ($wsl -ne $null) {
354
479
  Write-Host "本机没 clang,交给 WSL 编"
480
+ & wsl.exe bash -lc 'test -d "$HOME/.ipa-toolchain"'
481
+ if ($LASTEXITCODE -ne 0) {
482
+ Die "WSL 里还没铺过环境(那是 WSL 自己的 home,跟 Windows 这份不共用)。先在 WSL 里跑一次 bash scripts/bootstrap.sh,或者直接跑 scripts\bootstrap.ps1 把本机编译器装上。"
483
+ }
355
484
  $parts = @('bash', (Q (Join-Path $here 'ipa')), 'build', (Q (To-WslPath (Resolve-Path $src).Path)))
356
485
  # 路径参数一律换到 /mnt/...,不然产物落在 WSL 的 home 里,用户在 Windows 目录找不到
357
486
  $pathOpts = @('-o', '--out', '--icon', '--entitlements', '--p12', '--prov')
358
487
  $expectPath = $false
359
488
  foreach ($a in @(Rest $argv)) {
360
- if ($expectPath -and -not $a.StartsWith('-')) {
489
+ $looksPath = (Test-Path $a) -or ($a -match '^[A-Za-z]:') -or ($a -match '[/\\]')
490
+ if (-not $a.StartsWith('-') -and ($expectPath -or $looksPath)) {
361
491
  $full = $a
362
492
  if (Test-Path $a) { $full = (Resolve-Path $a).Path }
363
493
  elseif (-not [IO.Path]::IsPathRooted($a)) { $full = Join-Path (Get-Location) $a }
@@ -409,7 +539,7 @@ function Cmd-Build($argv) {
409
539
  $out = Opt $opt @('-o', '--out') $null
410
540
  if (-not $out) { $out = Join-Path (Get-Location) "dist/$name.ipa" }
411
541
  if (Test-Path $zsign) {
412
- Sign-Ipa $pre $out (Opt $opt @('--p12') $null) (Opt $opt @('--prov') $null) (Opt $opt @('--password', '--pass') $null)
542
+ Sign-Ipa $pre $out (Opt $opt @('--p12') $null) (Opt $opt @('--prov') $null) (Get-Pass $opt) (Opt $opt @('--entitlements') $null)
413
543
  } else { Copy-Item $pre $out; Write-Host "没签名,只是打了个包" }
414
544
  Remove-Item $pre -Force -ErrorAction SilentlyContinue
415
545
  Write-Host "产物 $out"
@@ -428,9 +558,9 @@ if ($args.Count -eq 0) {
428
558
 
429
559
  switch ($args[0]) {
430
560
  'doctor' { Cmd-Doctor }
431
- 'info' { Cmd-Info $args[1] }
432
- 'build' { Cmd-Build @($args[1..($args.Count - 1)]) }
433
- 'pack' { Cmd-Pack @($args[1..($args.Count - 1)]) }
434
- 'resign' { Cmd-Resign @($args[1..($args.Count - 1)]) }
561
+ 'info' { if ($args.Count -gt 1) { Cmd-Info $args[1] } else { Cmd-Info $null } }
562
+ 'build' { Cmd-Build @(Rest $args) }
563
+ 'pack' { Cmd-Pack @(Rest $args) }
564
+ 'resign' { Cmd-Resign @(Rest $args) }
435
565
  default { Write-Host "不认识的命令: $($args[0])"; exit 1 }
436
566
  }
@@ -6,8 +6,12 @@
6
6
  <key>CFBundleName</key><string>@NAME@</string>
7
7
  <key>CFBundleDisplayName</key><string>@NAME@</string>
8
8
  <key>CFBundlePackageType</key><string>APPL</string>
9
+ <key>CFBundleInfoDictionaryVersion</key><string>6.0</string>
10
+ <key>CFBundleSupportedPlatforms</key><array><string>iPhoneOS</string></array>
11
+ <key>LSRequiresIPhoneOS</key><true/>
9
12
  <key>CFBundleShortVersionString</key><string>1.0</string>
10
13
  <key>CFBundleVersion</key><string>1</string>
11
14
  <key>MinimumOSVersion</key><string>@MINIOS@</string>
12
15
  <key>UIDeviceFamily</key><array><integer>1</integer><integer>2</integer></array>
16
+ <key>UILaunchScreen</key><dict/>
13
17
  </dict></plist>
@@ -2,8 +2,13 @@
2
2
  <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
3
  <plist version="1.0">
4
4
  <dict>
5
- <key>get-task-allow</key><true/>
5
+ <!-- 调试用,ad-hoc 签名时最常用;正式分发要删掉 -->
6
+ <key>get-task-allow</key>
7
+ <true/>
8
+ <!-- 下面两条是越狱环境的高权限私有 entitlement,默认不开。
9
+ 要调试系统级进程时才放开,放开前先想清楚包会被谁装:
6
10
  <key>platform-application</key><true/>
7
11
  <key>com.apple.private.security.no-container</key><true/>
12
+ -->
8
13
  </dict>
9
14
  </plist>