forked from laishulu/macism
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathbuild.sh
executable file
·172 lines (154 loc) · 4.98 KB
/
build.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#!/bin/bash
# 检查 Swift 编译器是否可用
if ! command -v swiftc &> /dev/null; then
echo "错误: 未找到 Swift 编译器。请确保已安装 Xcode 命令行工具。"
echo "可以通过运行以下命令安装:"
echo "xcode-select --install"
exit 1
fi
# 清理旧的构建
rm -rf dist
# 创建目录结构
mkdir -p dist/MacVimSwitch.app/Contents/{MacOS,Resources}
# 复制 Info.plist
cp Info.plist dist/MacVimSwitch.app/Contents/
# 构建 ARM64 版本
echo "构建 ARM64 版本..."
if ! swiftc -o dist/MacVimSwitch.app/Contents/MacOS/macvimswitch-arm64 \
inputsource.swift \
main.swift \
AppDelegate.swift \
StatusBarManager.swift \
InputMethodManager.swift \
UserPreferences.swift \
LaunchManager.swift \
-framework Cocoa \
-framework Carbon \
-target arm64-apple-macos11 \
-sdk $(xcrun --show-sdk-path) \
-O \
-whole-module-optimization \
-Xlinker -rpath \
-Xlinker @executable_path/../Frameworks; then
echo "ARM64 构建失败。"
exit 1
fi
# 构建 x86_64 版本
echo "构建 x86_64 版本..."
if ! swiftc -o dist/MacVimSwitch.app/Contents/MacOS/macvimswitch-x86_64 \
inputsource.swift \
main.swift \
AppDelegate.swift \
StatusBarManager.swift \
InputMethodManager.swift \
UserPreferences.swift \
LaunchManager.swift \
-framework Cocoa \
-framework Carbon \
-target x86_64-apple-macos11 \
-sdk $(xcrun --show-sdk-path) \
-O \
-whole-module-optimization \
-Xlinker -rpath \
-Xlinker @executable_path/../Frameworks; then
echo "x86_64 构建失败。"
exit 1
fi
# 合并为通用二进制
echo "合并为通用二进制..."
if ! lipo -create \
dist/MacVimSwitch.app/Contents/MacOS/macvimswitch-arm64 \
dist/MacVimSwitch.app/Contents/MacOS/macvimswitch-x86_64 \
-output dist/MacVimSwitch.app/Contents/MacOS/macvimswitch; then
echo "合并二进制失败。"
exit 1
fi
# 清理临时文件
rm dist/MacVimSwitch.app/Contents/MacOS/macvimswitch-arm64
rm dist/MacVimSwitch.app/Contents/MacOS/macvimswitch-x86_64
# 创建 Info.plist
cat > dist/MacVimSwitch.app/Contents/Info.plist << EOL
<?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>CFBundleExecutable</key>
<string>macvimswitch</string>
<key>CFBundleIconFile</key>
<string>AppIcon</string>
<key>CFBundleIdentifier</key>
<string>com.jackiexiao.macvimswitch</string>
<key>CFBundleName</key>
<string>MacVimSwitch</string>
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>1.0.0</string>
<key>LSMinimumSystemVersion</key>
<string>11.0</string>
<key>LSUIElement</key>
<true/>
<key>NSHighResolutionCapable</key>
<true/>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>NSPrincipalClass</key>
<string>NSApplication</string>
<key>NSAppleEventsUsageDescription</key>
<string>MacVimSwitch needs to control system events to manage input sources.</string>
<key>NSAppleScriptEnabled</key>
<true/>
<key>LSBackgroundOnly</key>
<false/>
<key>NSAccessibilityUsageDescription</key>
<string>MacVimSwitch needs accessibility access to monitor keyboard events.</string>
</dict>
</plist>
EOL
# 创建 entitlements.plist
cat > entitlements.plist << EOL
<?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>com.apple.security.automation.apple-events</key>
<true/>
<key>com.apple.security.temporary-exception.apple-events</key>
<array>
<string>com.apple.systemevents</string>
</array>
<key>com.apple.security.cs.allow-jit</key>
<true/>
<key>com.apple.security.cs.allow-unsigned-executable-memory</key>
<true/>
<key>com.apple.security.cs.disable-library-validation</key>
<true/>
</dict>
</plist>
EOL
# 设置执行权限
chmod +x dist/MacVimSwitch.app/Contents/MacOS/macvimswitch
# 使用自签名
if ! codesign --force --deep --sign - --entitlements entitlements.plist dist/MacVimSwitch.app; then
echo "签名失败。请确保你的开发环境正确配置。"
exit 1
fi
# 创建 DMG(可选)
if [ "$1" = "--create-dmg" ]; then
# 创建临时挂载点
mkdir -p /tmp/dmg
# 创建应用程序文件夹符号链接
ln -s /Applications /tmp/dmg/Applications
# 复制应用
cp -r dist/MacVimSwitch.app /tmp/dmg/
# 创建 DMG
if ! hdiutil create -volname "MacVimSwitch" -srcfolder /tmp/dmg -ov -format UDZO MacVimSwitch.dmg; then
echo "创建 DMG 失败。请确保你的开发环境正确配置。"
exit 1
fi
# 清理
rm -rf /tmp/dmg
echo "DMG created: MacVimSwitch.dmg"
fi
echo "构建成功完成!生成了通用二进制(Universal Binary)应用程序。"
echo "该应用程序可以在 Intel 和 Apple Silicon Mac 上原生运行,无需 Rosetta。"