cursor-free-vip/build.py
2025-01-14 14:47:41 +08:00

101 lines
3.0 KiB
Python

import warnings
import os
import platform
import subprocess
import time
import threading
import shutil
from logo import print_logo
from dotenv import load_dotenv
# 忽略特定警告
warnings.filterwarnings("ignore", category=SyntaxWarning)
class LoadingAnimation:
def __init__(self):
self.is_running = False
self.animation_thread = None
def start(self, message="Building"):
self.is_running = True
self.animation_thread = threading.Thread(target=self._animate, args=(message,))
self.animation_thread.start()
def stop(self):
self.is_running = False
if self.animation_thread:
self.animation_thread.join()
print("\r" + " " * 70 + "\r", end="", flush=True)
def _animate(self, message):
animation = "|/-\\"
idx = 0
while self.is_running:
print(f"\r{message} {animation[idx % len(animation)]}", end="", flush=True)
idx += 1
time.sleep(0.1)
def progress_bar(progress, total, prefix="", length=50):
filled = int(length * progress // total)
bar = "" * filled + "" * (length - filled)
percent = f"{100 * progress / total:.1f}"
print(f"\r{prefix} |{bar}| {percent}% Complete", end="", flush=True)
if progress == total:
print()
def simulate_progress(message, duration=1.0, steps=20):
print(f"\033[94m{message}\033[0m")
for i in range(steps + 1):
time.sleep(duration / steps)
progress_bar(i, steps, prefix="Progress:", length=40)
def build():
# 清理屏幕
os.system("cls" if platform.system().lower() == "windows" else "clear")
# 顯示 logo
print_logo()
# 清理 PyInstaller 緩存
print("\033[93m🧹 清理構建緩存...\033[0m")
if os.path.exists('build'):
shutil.rmtree('build')
# 重新加載環境變量以確保獲取最新版本
load_dotenv(override=True)
version = os.getenv('VERSION', '1.0.0')
print(f"\033[93m📦 正在構建版本: v{version}\033[0m")
try:
simulate_progress("Preparing build environment...", 0.5)
loading = LoadingAnimation()
loading.start("Building in progress")
# 構建命令
os_type = "windows" if os.name == "nt" else "mac"
output_name = f"CursorFreeVIP_{version}_{os_type}"
build_command = f'pyinstaller --clean --noconfirm build.spec'
os.system(build_command)
loading.stop()
exe_path = os.path.join('dist', f'{output_name}.exe')
if os.path.exists(exe_path):
print(f"\n\033[92m✅ 構建完成!")
print(f"📦 可執行文件位於: {exe_path}\033[0m")
else:
print("\n\033[91m❌ 構建失敗:未找到輸出文件\033[0m")
return False
except Exception as e:
if loading:
loading.stop()
print(f"\n\033[91m❌ 構建過程出錯: {str(e)}\033[0m")
return False
return True
if __name__ == "__main__":
build()