Skip to content
This repository was archived by the owner on Jan 27, 2026. It is now read-only.

Commit d0d2777

Browse files
committed
build: 增加自动化构建脚本
1 parent cb2bd90 commit d0d2777

File tree

4 files changed

+181
-16
lines changed

4 files changed

+181
-16
lines changed

!src-dist/ci.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding: utf-8 -*-
2+
# pip3 install semver
3+
4+
"""
5+
Github Action 自动化脚本
6+
7+
此脚本用于 CI 自动化发布打包工作
8+
"""
9+
10+
11+
# 引入自定义的打包发布模块和环境设置模块
12+
from plib.publish import run
13+
14+
15+
def main() -> None:
16+
run("ci")
17+
18+
19+
if __name__ == "__main__":
20+
main()

!src-dist/plib/environment.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,11 @@ def get_current_packet_id() -> str:
9999
返回:
100100
str: 当前插件集目录的名称。
101101
"""
102-
packet_path: str = os.path.abspath(
102+
parent_dir = os.path.abspath(
103103
os.path.join(__file__, os.pardir, os.pardir, os.pardir)
104104
)
105-
return os.path.basename(packet_path)
105+
for name in os.listdir(parent_dir):
106+
full_path = os.path.join(parent_dir, name)
107+
if os.path.isdir(full_path) and name.endswith("_!Base"):
108+
return name[:-6]
109+
raise Exception("获取当前插件集目录的名称失败")

!src-dist/plib/publish.py

Lines changed: 118 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def __build_addon(packet: str, addon: str, time_tag: str) -> None:
111111
return
112112
# 新增模块:增加计数,生成临时文件
113113
file_count += 1
114-
source_file: str = os.path.join(addon, parts[1])
114+
source_file: str = os.path.join(addon, parts[1].replace("\\", "/"))
115115
dist_file: str = os.path.join(dist_dir, f"{file_count}.lua")
116116
try:
117117
source_code: str = codecs.open(
@@ -469,13 +469,23 @@ def pathToModule(path: str) -> str:
469469

470470
print("--------------------------------")
471471
print("正在压缩打包...")
472-
# 调用系统命令压缩包(通过 start /wait 保证依次完成)
473-
cmd: str = (
474-
'cd ./!src-dist/dist && start /wait /b ../bin/7z.exe a -t7z "'
475-
+ file_name
476-
+ '" -xr!manifest.dat -xr!manifest.key -xr!publisher.key -x@.7zipignore'
477-
+ cmd_suffix
478-
)
472+
# 调用系统命令压缩包
473+
if os.name == "nt":
474+
# Windows: 使用7z.exe命令(通过 start /wait 保证依次完成)
475+
cmd: str = (
476+
'cd ./!src-dist/dist && start /wait /b ../bin/7z.exe a -t7z "'
477+
+ file_name
478+
+ '" -xr!manifest.dat -xr!manifest.key -xr!publisher.key -x@.7zipignore'
479+
+ cmd_suffix
480+
)
481+
else:
482+
# Linux/macOS: 使用7z命令
483+
cmd: str = (
484+
'cd ./!src-dist/dist && 7z a -bb0 -bd -bso0 -t7z "'
485+
+ file_name
486+
+ '" -xr!manifest.dat -xr!manifest.key -xr!publisher.key -x@.7zipignore'
487+
+ cmd_suffix
488+
)
479489
os.system(cmd)
480490
print(f'压缩打包完成:"{file_name}"。')
481491
if base_hash:
@@ -484,10 +494,88 @@ def pathToModule(path: str) -> str:
484494
print("全量打包。")
485495

486496

497+
def __lint(packet: str, packet_path: str, diff_version: Optional[str] = None) -> None:
498+
"""
499+
代码检查
500+
"""
501+
# 获取当前版本信息
502+
version_info: Dict[str, str] = __get_version_info(packet, diff_version)
503+
utils.assert_exit(
504+
version_info.get("current") != "",
505+
"错误:无法获取当前版本信息,请检查 Base.lua 文件!",
506+
)
507+
print(f"当前版本:{version_info.get('current')}")
508+
print(f"当前提交 hash:{version_info.get('current_hash')}")
509+
print(f"历史最大版本:{version_info.get('max')}")
510+
print(f"上一版本:{version_info.get('previous')}")
511+
print(f"上一版本提交信息:{version_info.get('previous_message')}")
512+
print(f"上一版本提交 hash:{version_info.get('previous_hash')}")
513+
514+
# 执行代码检查
515+
# 从上一版本(如果不存在则从最早commit开始)检查 commit message 是否符合规范
516+
print("\n正在检查提交信息规范...")
517+
518+
# 获取起始commit hash
519+
start_hash = version_info.get("previous_hash") or ""
520+
if not start_hash:
521+
# 如果没有上一版本,则获取3个月内最老的commit的hash
522+
start_hash = (
523+
os.popen(
524+
'git log --since="3 months ago" --format="%H" --reverse | head -n 1'
525+
)
526+
.read()
527+
.strip()
528+
)
529+
if not start_hash:
530+
# 如果3个月内没有commit,则获取最新的commit hash
531+
start_hash = os.popen("git rev-parse HEAD").read().strip()
532+
533+
# 获取从起始commit到当前的所有commit信息
534+
commits = (
535+
os.popen(f'git log {start_hash}..HEAD --pretty=format:"%h|%s"')
536+
.read()
537+
.strip()
538+
.split("\n")
539+
)
540+
541+
# 定义conventional commit的正则表达式
542+
commit_pattern = re.compile(
543+
r"^(feat|fix|docs|style|refactor|perf|test|chore|build|release)(\([^)]+\))?: .+"
544+
)
545+
546+
has_invalid = False
547+
invalid_commits = []
548+
for commit in commits:
549+
if not commit:
550+
continue
551+
hash_msg = commit.split("|")
552+
if len(hash_msg) != 2:
553+
continue
554+
555+
commit_hash, message = hash_msg
556+
# 忽略 merge commits
557+
if message.startswith("Merge "):
558+
continue
559+
560+
if not commit_pattern.match(message):
561+
invalid_commits.append(f" - {commit_hash}: {message}")
562+
has_invalid = True
563+
564+
if has_invalid:
565+
print("\n不规范的提交信息:")
566+
for invalid_commit in invalid_commits:
567+
print(invalid_commit)
568+
utils.exit_with_message(
569+
"\n错误:存在不符合规范的提交信息!\n提交信息必须符合格式:<type>[optional scope]: <description>\n其中type必须是:feat|fix|docs|style|refactor|perf|test|chore|build|release"
570+
)
571+
else:
572+
print("\n提交信息规范检查通过!")
573+
574+
487575
def __prepublish(packet: str, packet_path: str, diff_ver: Optional[str] = None) -> None:
488576
"""
489577
发布前准备工作:
490-
- 如果当前在master分支,切换至stable分支、rebse主分支变动并重置提交信息
578+
- 如果当前在master分支,切换至stable分支、rebase主分支变动并重置提交信息
491579
- 如果当前在stable分支且存在未提交变更,则提交release信息;
492580
- 确保工作区干净且当前分支为stable。
493581
@@ -545,11 +633,22 @@ def __pack(packet: str, packet_path: str, version_info: Dict[str, str]) -> None:
545633
time_tag = git.get_head_time_tag()
546634

547635
# 根据运行环境确定压缩包输出目录
548-
dist_root: str = os.path.abspath(os.path.join(get_interface_path(), os.pardir))
549-
if os.path.isfile(os.path.abspath(os.path.join(dist_root, "gameupdater.exe"))):
550-
dist_root = os.path.join(packet_path, "!src-dist", "dist")
636+
interface_path = get_interface_path()
637+
if interface_path is None:
638+
dist_root = os.path.join(
639+
os.path.abspath(__file__),
640+
os.pardir,
641+
os.pardir,
642+
os.pardir,
643+
"!src-dist",
644+
"dist",
645+
)
551646
else:
552-
dist_root = os.path.abspath(os.path.join(dist_root, os.pardir, "dist"))
647+
dist_root: str = os.path.abspath(os.path.join(get_interface_path(), os.pardir))
648+
if os.path.isfile(os.path.abspath(os.path.join(dist_root, "gameupdater.exe"))):
649+
dist_root = os.path.join(packet_path, "!src-dist", "dist")
650+
else:
651+
dist_root = os.path.abspath(os.path.join(dist_root, os.pardir, "dist"))
553652

554653
# 如果存在上一个版本,则生成差异包
555654
if version_info.get("previous_hash"):
@@ -616,11 +715,16 @@ def run(mode: str, diff_ver: Optional[str] = None, is_source: bool = False) -> N
616715
if mode == "publish":
617716
__prepublish(packet, packet_path, diff_ver)
618717

718+
if mode == "publish" or mode == "ci":
719+
__lint(packet, packet_path, diff_ver)
720+
619721
# 执行整体构建打包流程
620722
__build(packet)
621723

622-
if mode == "publish":
724+
if mode == "publish" or mode == "ci":
623725
__pack(packet, packet_path, version_info)
726+
727+
if mode == "publish":
624728
os.system("git checkout master")
625729

626730
print("--------------------------------")

.github/workflows/build.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: Build
2+
3+
on: [push]
4+
5+
jobs:
6+
build-linux:
7+
runs-on: ubuntu-latest
8+
strategy:
9+
max-parallel: 5
10+
11+
steps:
12+
- uses: actions/checkout@v4
13+
with:
14+
fetch-depth: 0
15+
- name: Set up Python 3.10
16+
uses: actions/setup-python@v3
17+
with:
18+
python-version: '3.10'
19+
- name: Install pip dependencies
20+
run: |
21+
pip3 install semver luadata
22+
- name: Install apt dependencies
23+
run: |
24+
sudo apt install lua5.1 p7zip-full
25+
- name: Write secret to file
26+
run: |
27+
cat > secret.jx3dat << 'EOF'
28+
${{ secrets.SECRET_JX3DAT }}
29+
EOF
30+
- name: Run Build Command
31+
run: |
32+
python3 \!src-dist/ci.py
33+
- name: Upload Artifacts
34+
uses: actions/upload-artifact@v4
35+
with:
36+
name: dist-archives-${{ github.run_number }}-${{ github.sha }}
37+
path: '\!src-dist/dist/*.7z'

0 commit comments

Comments
 (0)