Magpie/scripts/release.py
Xu 04ff6ea0a3
支持使用 clang-cl 编译 (#1195)
* chore: native 项目支持 clang-cl

* chore: 修复 clang 编译错误

* chore: 修复部分 clang 编译警告

* chore: 修复警告

* chore: 修复所有警告,优化 llvm 查找

* chore: 启用 LTO

* chore: 支持 ARM64

* chore: _ConanDeps 支持 clang-cl

* chore: 编译选项改变 _ConanDeps 自动重新编译

* chore: profile 更改时重新编译 _ConanDeps

* chore: 将 hu 和 ka 加入项目文件,但不参与生成
否则不参与批量替换

* chore: Magpie 项目支持并行编译

* chore: 添加几个提高性能的编译选项

* chore: 优化 _ConanDeps

* chore: publish.py 支持参数

* chore: CI 支持 clang 编译

* chore: 修复 CI 的 conan 缓存

* chore: 优化编译选项,修复 CI

* chore: 修复 CI

* chore: 改变参数顺序

* chore: 添加编译选项支持针对当前硬件生成优化代码

* chore: 优化 CI 脚本

* chore: publish.py 添加 --use-native-march 选项

* chore: 脚本集中在 scripts 文件夹,msbuild 启用并行编译

* chore: clang, x64 配置启用 CX16 指令

* chore: 更新依赖

* chore: 更新格式设置

* chore: 增加额外的斜杠以提高兼容性

* chore: 修复效果文件复制
2025-07-15 17:40:04 +08:00

164 lines
5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import sys
import os
import subprocess
import shutil
import requests
import hashlib
import json
import argparse
try:
# https://docs.github.com/en/actions/learn-github-actions/variables
if os.environ["GITHUB_ACTIONS"].lower() == "true":
# 不知为何在 Github Actions 中运行时默认编码为 ANSI并且 print 需刷新流才能正常显示
for stream in [sys.stdout, sys.stderr]:
stream.reconfigure(encoding="utf-8")
except:
pass
argParser = argparse.ArgumentParser()
argParser.add_argument("version_major", type=int)
argParser.add_argument("version_minor", type=int)
argParser.add_argument("version_patch", type=int)
argParser.add_argument("tag")
argParser.add_argument("prerelease")
argParser.add_argument("access_token")
args = argParser.parse_args()
isPrerelease = args.prerelease.lower() == "true"
repo = os.environ["GITHUB_REPOSITORY"]
actor = os.environ["GITHUB_ACTOR"]
subprocess.run("git config user.name " + actor)
subprocess.run(f"git config user.email {actor}@users.noreply.github.com")
subprocess.run(
f"git remote set-url origin https://{args.access_token}@github.com/{repo}.git"
)
# 打标签
if subprocess.run(f"git tag -a {args.tag} -m {args.tag}").returncode != 0:
raise Exception("打标签失败")
if subprocess.run("git push origin " + args.tag).returncode != 0:
raise Exception("推送标签失败")
print("已创建标签 " + args.tag, flush=True)
headers = {
"Accept": "application/vnd.github+json",
"Authorization": "Bearer " + args.access_token,
"X-GitHub-Api-Version": "2022-11-28",
}
# 获取前一个发布版本来生成默认发行说明
prevReleaseTag = None
try:
if isPrerelease:
# 发布预发行版与最新的版本(无论是正式版还是预发行版)对比
response = requests.get(
f"https://api.github.com/repos/{repo}/releases",
json={"per_page": 1},
headers=headers,
)
if response.ok:
prevReleaseTag = response.json()[0]["tag_name"]
else:
# 发布正式版则与最新的正式版对比
# 由于可以自己选择最新版本,此接口可能不会返回时间上最新发布的版本,不是大问题
response = requests.get(
f"https://api.github.com/repos/{repo}/releases/latest", headers=headers
)
if response.ok:
prevReleaseTag = response.json()["tag_name"]
except:
# 忽略错误
pass
# 发布 release
if prevReleaseTag == None:
body = ""
else:
# 默认发行说明为比较两个 tag
body = f"https://github.com/{repo}/compare/{prevReleaseTag}...{args.tag}"
response = requests.post(
f"https://api.github.com/repos/{repo}/releases",
json={
"tag_name": args.tag,
"name": args.tag,
"prerelease": isPrerelease,
"body": body,
"discussion_category_name": "Announcements",
},
headers=headers,
)
if not response.ok:
raise Exception("发布失败")
uploadUrl = response.json()["upload_url"]
uploadUrl = uploadUrl[: uploadUrl.find("{")] + "?name="
os.chdir(os.path.dirname(__file__) + "\\..\\publish")
pkgInfos = {}
for platform in ["x64", "ARM64"]:
# 打包成 zip
pkgName = "Magpie-" + args.tag + "-" + platform
shutil.make_archive(pkgName, "zip", pkgName)
pkgName += ".zip"
# 上传资产
with open(pkgName, "rb") as f:
# 流式上传
# https://requests.readthedocs.io/en/latest/user/advanced/#streaming-uploads
response = requests.post(
uploadUrl + pkgName,
data=f,
headers={**headers, "Content-Type": "application/zip"},
)
if not response.ok:
raise Exception("上传失败")
# 计算哈希
f.seek(0, os.SEEK_SET)
md5 = hashlib.file_digest(f, hashlib.md5).hexdigest()
pkgInfos[platform] = (pkgName, md5)
print("已发布 " + args.tag, flush=True)
# 更新 version.json
# 此步应在发布版本之后,因为程序使用 version.json 检查更新
os.chdir("..")
with open("version.json", "w", encoding="utf-8") as f:
json.dump(
{
"version": f"{args.version_major}.{args.version_minor}.{args.version_patch}",
"tag": args.tag,
"binary": {
"x64": {
"url": f"https://github.com/{repo}/releases/download/{args.tag}/{pkgInfos['x64'][0]}",
"hash": pkgInfos["x64"][1],
},
"ARM64": {
"url": f"https://github.com/{repo}/releases/download/{args.tag}/{pkgInfos['ARM64'][0]}",
"hash": pkgInfos["ARM64"][1],
},
},
},
f,
indent=4,
)
# 提交对 version.json 的更改
if subprocess.run("git add version.json").returncode != 0:
raise Exception("git add 失败")
if subprocess.run('git commit -m "Update version.json"').returncode != 0:
raise Exception("git commit 失败")
if subprocess.run("git push").returncode != 0:
raise Exception("git push 失败")