Skip to content

Commit

Permalink
Merge pull request #10 from Night-stars-1/main
Browse files Browse the repository at this point in the history
feat: 添加资源包构建
  • Loading branch information
mmwuyou authored Sep 7, 2024
2 parents 2d449f5 + 07bce00 commit 49f7cb2
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/resource.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: res_build

on:
push:
branches: [ main ]
workflow_dispatch:

jobs:
update:
name: Update Map
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@master

- name: Setup python
uses: actions/setup-python@v5.2.0
with:
python-version: 3.9

- name: RES ZIP
run: |
python res_zip.py
- name: Commit changes
uses: EndBug/add-and-commit@v9.1.3
with:
author_name: github-actions[bot]
author_email: github-actions[bot]@users.noreply.github.com
message: ':wrench: 自动构建资源包'
add: |
'res.zip'
'version.txt'
Binary file added res.zip
Binary file not shown.
47 changes: 47 additions & 0 deletions res_zip.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import os
import subprocess
import zipfile

# 压缩包名称
zip_filename = 'res.zip'

def add_folder_to_zip(zipf: zipfile.ZipFile, folder_path: str):
"""
将指定文件夹添加到 ZIP 文件中,同时保持目录结构
:param zipf: zipfile.ZipFile 对象
:param folder_path: 要添加到 ZIP 文件中的文件夹路径
"""
for foldername, subfolders, filenames in os.walk(folder_path):
for filename in filenames:
file_path = os.path.join(foldername, filename)
# 在 ZIP 文件中创建相对路径
relative_path = file_path.replace(r"assets\resource\base", "")
zipf.write(file_path, relative_path)

def get_git_commit_count():
"""
获取仓库的提交总数
:param repo_path: 仓库路径,默认为当前路径
:return: 提交总数,如果获取失败则返回空字符串
"""
try:
# 执行 Git 命令获取提交总数
result = subprocess.run(
['git', 'rev-list', '--count', 'HEAD'],
text=True,
capture_output=True,
check=True
)
# 返回提交总数
return result.stdout.strip()
except subprocess.CalledProcessError as e:
print(f"获取提交数失败: {e}")
return ''

# 创建压缩包并添加文件
with zipfile.ZipFile(zip_filename, 'w', zipfile.ZIP_DEFLATED) as zipf:
add_folder_to_zip(zipf, r'assets\resource\base\pipeline')
add_folder_to_zip(zipf, r'assets\resource\base\image')

with open('version.txt', 'w', encoding='utf-8') as f:
f.write(get_git_commit_count())
1 change: 1 addition & 0 deletions version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1

0 comments on commit 49f7cb2

Please sign in to comment.