import subprocess
import sys
from pathlib import Path

from build import main as build_main
from build import bump_version


def publish_package():
    """发布包到 PyPI"""
    try:
        subprocess.run([
            sys.executable, '-m', 'twine', 'upload',
            'dist/*', '--verbose'
        ], check=True)
        print("发布成功！")
    except subprocess.CalledProcessError as e:
        print(f"发布失败: {e}")
        sys.exit(1)


def main():
    project_root = Path(__file__).parent.parent
    
    # 更新版本号
    bump_version(project_root)
    
    # 构建（不再更新版本号）
    build_main(do_bump_version=False)

    # 确认发布
    response = input("确认发布到 PyPI? (y/N): ")
    if response.lower() != 'y':
        print("取消发布")
        return

    # 发布
    publish_package()


if __name__ == '__main__':
    main()
