diff --git a/.github/workflows/assets.yml b/.github/workflows/assets.yml index 70a413dc5..d55754cfe 100644 --- a/.github/workflows/assets.yml +++ b/.github/workflows/assets.yml @@ -3,8 +3,8 @@ on: push: branches: [ main ] paths: - - 'common/data/xml/assets/**' # 仅监听assets目录变更 - workflow_dispatch: # 保留手动触发 + - 'common/data/xml/assets/**' + workflow_dispatch: jobs: sync: @@ -15,21 +15,60 @@ jobs: with: fetch-depth: 0 - - name: Prepare files for B Repo (and remove XML comments) + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.x' # 使用最新稳定版Python + + - name: Prepare files for B Repo (remove comments via Python) run: | - # 创建临时目录用于处理文件 mkdir -p temp-b-repo/out/assets - # 检查源目录是否存在 if [ -d "common/data/xml/assets" ]; then - # 复制源文件到临时目录(包括隐藏文件) + # 复制源文件到临时目录 cp -rv common/data/xml/assets/* common/data/xml/assets/.* temp-b-repo/out/assets/ 2>/dev/null || true - echo "Files copied to temp directory (before comment removal):" + echo "Files copied to temp directory (before processing):" ls -la temp-b-repo/out/assets - # 关键步骤:删除临时目录中所有XML文件的注释() - # 匹配规则:删除从结束的所有内容(包括多行注释) - find temp-b-repo/out/assets -name "*.xml" -exec sed -i '//d' {} + + # 关键步骤:创建并运行Python脚本删除XML注释 + cat > remove_xml_comments.py << 'EOF' +import xml.etree.ElementTree as ET +import os + +def remove_comments_from_xml(file_path): + try: + # 解析XML文件,保留注释(需自定义解析器) + parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=True)) + tree = ET.parse(file_path, parser=parser) + root = tree.getroot() + + # 递归查找并删除所有注释节点 + def remove_comments(element): + for child in list(element): + # 注释节点的tag为 'comment'(ElementTree内部标记) + if child.tag is ET.Comment: + element.remove(child) + else: + remove_comments(child) + + remove_comments(root) + + # 保存修改后的文件(覆盖原文件) + tree.write(file_path, encoding='utf-8', xml_declaration=True) + print(f"Processed: {file_path}") + except Exception as e: + print(f"Error processing {file_path}: {e}") + +# 遍历临时目录下所有XML文件并处理 +for root_dir, _, files in os.walk("temp-b-repo/out/assets"): + for file in files: + if file.endswith(".xml"): + file_path = os.path.join(root_dir, file) + remove_comments_from_xml(file_path) +EOF + + # 运行Python脚本处理所有XML文件 + python remove_xml_comments.py echo "Files in temp directory after comment removal:" ls -la temp-b-repo/out/assets @@ -46,23 +85,19 @@ jobs: git config --global user.name "GitHub Actions" git config --global user.email "actions@github.com" - # 克隆仓库B rm -rf b-repo git clone https://$B_REPO_TOKEN@${B_REPO_URL#https://} b-repo || { echo "Failed to clone B repo"; exit 1; } - # 同步仓库B的目标分支 cd b-repo git checkout $B_BRANCH || { echo "Failed to checkout B branch"; exit 1; } git pull origin $B_BRANCH || { echo "Failed to pull B repo"; exit 1; } - # 创建目标目录并复制处理后的文件(已移除注释) mkdir -p out/assets cp -rv ../temp-b-repo/out/assets/* ../temp-b-repo/out/assets/.* out/assets/ 2>/dev/null || true echo "Files in B repo target directory after copy:" ls -la out/assets - # 提交并推送变更 git add out/assets - git commit -m "Sync assets (without comments): $(date +'%Y-%m-%d %H:%M:%S')" + git commit -m "Sync assets (no comments): $(date +'%Y-%m-%d %H:%M:%S')" git push origin $B_BRANCH || { echo "Failed to push to B repo"; exit 1; } - echo "Sync completed successfully (XML comments removed)." + echo "Sync completed successfully."