feat: 完成macOS应用打包功能

This commit is contained in:
ruisu
2025-02-19 16:29:59 +08:00
parent 6a00193333
commit 0b6b3f13aa
7 changed files with 253 additions and 8 deletions

171
build.py
View File

@@ -1,9 +1,15 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import warnings
import os
import platform
import subprocess
import time
import threading
import json
import sys
from pathlib import Path
# Ignore specific SyntaxWarning
warnings.filterwarnings("ignore", category=SyntaxWarning, module="DrissionPage")
@@ -79,6 +85,169 @@ def filter_output(output):
return "\n".join(important_lines)
def increment_version():
"""增加构建版本号"""
version_file = Path("version.json")
if version_file.exists():
with open(version_file, "r") as f:
version_data = json.load(f)
# 增加构建号
version_data["build"] += 1
# 更新版本号的最后一位
version_parts = version_data["version"].split(".")
version_parts[-1] = str(version_data["build"])
version_data["version"] = ".".join(version_parts)
# 保存更新后的版本信息
with open(version_file, "w") as f:
json.dump(version_data, f, indent=4)
return version_data
else:
print("错误:未找到 version.json 文件")
sys.exit(1)
def create_icns():
"""将 SVG 转换为 ICNS 格式"""
if not Path("icons/logo.svg").exists():
print("错误:未找到 logo.svg 文件")
sys.exit(1)
# 创建临时目录
os.makedirs("icons/tmp.iconset", exist_ok=True)
# 转换 SVG 到 PNG
sizes = [16, 32, 64, 128, 256, 512, 1024]
for size in sizes:
# 普通分辨率
os.system(f"rsvg-convert -w {size} -h {size} icons/logo.svg > icons/tmp.iconset/icon_{size}x{size}.png")
# 高分辨率(@2x
if size <= 512:
os.system(f"rsvg-convert -w {size*2} -h {size*2} icons/logo.svg > icons/tmp.iconset/icon_{size}x{size}@2x.png")
# 生成 icns 文件
os.system("iconutil -c icns icons/tmp.iconset -o icons/logo.icns")
# 清理临时文件
os.system("rm -rf icons/tmp.iconset")
def update_spec(version_data):
"""更新 spec 文件中的版本信息"""
spec_content = f'''# -*- mode: python ; coding: utf-8 -*-
block_cipher = None
a = Analysis(
['cursor_gui.py'],
pathex=[],
binaries=[],
datas=[],
hiddenimports=[],
hookspath=[],
hooksconfig={{}},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False,
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)
exe = EXE(
pyz,
a.scripts,
[],
exclude_binaries=True,
name='听泉Cursor助手',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=False,
disable_windowed_traceback=False,
argv_emulation=True,
target_arch=None,
codesign_identity=None,
entitlements_file=None,
icon='icons/logo.icns',
)
coll = COLLECT(
exe,
a.binaries,
a.zipfiles,
a.datas,
strip=False,
upx=True,
upx_exclude=[],
name='听泉Cursor助手',
)
app = BUNDLE(
coll,
name='听泉Cursor助手.app',
icon='icons/logo.icns',
bundle_identifier='com.tingquan.cursor',
info_plist={{
'CFBundleShortVersionString': '{version_data["version"]}',
'CFBundleVersion': '{version_data["build"]}',
'NSHighResolutionCapable': 'True',
'LSMinimumSystemVersion': '10.13.0',
'CFBundleName': '听泉Cursor助手',
'CFBundleDisplayName': '听泉Cursor助手',
}},
)'''
with open("cursor_app.spec", "w") as f:
f.write(spec_content)
def build_app():
"""构建应用程序"""
# 增加版本号
version_data = increment_version()
# 创建图标
create_icns()
# 更新 spec 文件
update_spec(version_data)
# 更新主程序中的版本号
update_main_version(version_data["version"])
# 构建应用
os.system("pyinstaller cursor_app.spec")
# 创建压缩包
os.system(f'cd dist && zip -r "听泉Cursor助手_v{version_data["version"]}.zip" "听泉Cursor助手.app"')
print(f"\n构建完成版本号v{version_data['version']}")
print(f"应用程序位置dist/听泉Cursor助手.app")
print(f"压缩包位置dist/听泉Cursor助手_v{version_data['version']}.zip")
def update_main_version(version):
"""更新主程序中的版本号"""
with open("cursor_gui.py", "r") as f:
content = f.read()
# 替换版本号
content = content.replace(
'self.setWindowTitle("Cursor账号管理器 v3.5.3")',
f'self.setWindowTitle("听泉Cursor助手 v{version}")'
)
with open("cursor_gui.py", "w") as f:
f.write(content)
def build():
# Clear screen
os.system("cls" if platform.system().lower() == "windows" else "clear")
@@ -176,4 +345,4 @@ def build():
if __name__ == "__main__":
build()
build_app()