diff --git a/.github/workflows/assets.yml b/.github/workflows/assets.yml index d00575a33..df2400870 100644 --- a/.github/workflows/assets.yml +++ b/.github/workflows/assets.yml @@ -20,61 +20,162 @@ jobs: with: python-version: '3.x' - - name: Prepare files for B Repo (remove comments via Python) + - name: Prepare files and remove XML comments 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 processing):" - ls -la temp-b-repo/out/assets - - # 关键步骤:创建并运行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 # 修正:确保EOF单独成行,且没有多余空格或缩进 - - # 运行Python脚本处理所有XML文件 - python remove_xml_comments.py - - echo "Files in temp directory after comment removal:" - ls -la temp-b-repo/out/assets - else + # 检查源目录是否存在 + if [ ! -d "common/data/xml/assets" ]; then echo "Error: common/data/xml/assets directory not found!" && exit 1 fi + + # 复制源文件到临时目录 + 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 processing):" + ls -la temp-b-repo/out/assets + + # 生成Python脚本(用echo逐行写入,避免EOF语法问题) + echo "import xml.etree.ElementTree as ET" > remove_xml_comments.py + echo "import os" >> remove_xml_comments.py + echo "" >> remove_xml_comments.py + echo "def remove_comments_from_xml(file_path):" >> remove_xml_comments.py + echo " try:" >> remove_xml_comments.py + echo " # 解析XML并保留注释" >> remove_xml_comments.py + echo " parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=True))" >> remove_xml_comments.py + echo " tree = ET.parse(file_path, parser=parser)" >> remove_xml_comments.py + echo " root = tree.getroot()" >> remove_xml_comments.py + echo "" >> remove_xml_comments.py + echo " # 递归删除注释节点" >> remove_xml_comments.py + echo " def remove_comments(element):" >> remove_xml_comments.py + echo " for child in list(element):" >> remove_xml_comments.py + echo " if child.tag is ET.Comment:" >> remove_xml_comments.py + echo " element.remove(child)" >> remove_xml_comments.py + echo " else:" >> remove_xml_comments.py + echo " remove_comments(child)" >> remove_xml_comments.py + echo "" >> remove_xml_comments.py + echo " remove_comments(root)" >> remove_xml_comments.py + echo "" >> remove_xml_comments.py + echo " # 保存修改后的文件" >> remove_xml_comments.py + echo " tree.write(file_path, encoding='utf-8', xml_declaration=True)" >> remove_xml_comments.py + echo " print(f'Processed: {file_path}')" >> remove_xml_comments.py + echo " except Exception as e:" >> remove_xml_comments.py + echo " print(f'Error processing {file_path}: {e}')" >> remove_xml_comments.py + echo "" >> remove_xml_comments.py + echo "# 遍历所有XML文件并处理" >> remove_xml_comments.py + echo "for root_dir, _, files in os.walk('temp-b-repo/out/assets'):" >> remove_xml_comments.py + echo " for file in files:" >> remove_xml_comments.py + echo " if file.endswith('.xml'):" >> remove_xml_comments.py + echo " file_path = os.path.join(root_dir, file)" >> remove_xml_comments.py + echo " remove_comments_from_xml(file_path)" >> remove_xml_comments.py + + # 运行Python脚本 + python remove_xml_comments.py + + echo "Files in temp directory after comment removal:" + ls -la temp-b-repo/out/assets + + - name: Push to Private B Repo + env: + B_REPO_TOKEN: ${{ secrets.B_REPO_TOKEN }} + B_REPO_URL: https://kkgithub.com/TO-teams/flash.git + B_BRANCH: main + run: | + git config --global user.name "GitHub Actions" + git config --global user.email "actions@github.com" + + rm -rf b-repo + git clone https://$B_REPO_TOKEN@${B_REPO_URL#https://} b-repo || { echo "Failed to clone B repo"; exit 1; } + + 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 (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." +name: Sync Assets to Private B Repo +on: + push: + branches: [ main ] + paths: + - 'common/data/xml/assets/**' + workflow_dispatch: + +jobs: + sync: + runs-on: ubuntu-latest + steps: + - name: Checkout A Repo + uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Set up Python + uses: actions/setup-python@v5 + with: + python-version: '3.x' + + - name: Prepare files and remove XML comments + run: | + # 创建临时目录 + mkdir -p temp-b-repo/out/assets + + # 检查源目录是否存在 + if [ ! -d "common/data/xml/assets" ]; then + echo "Error: common/data/xml/assets directory not found!" && exit 1 + fi + + # 复制源文件到临时目录 + 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 processing):" + ls -la temp-b-repo/out/assets + + # 生成Python脚本(用echo逐行写入,避免EOF语法问题) + echo "import xml.etree.ElementTree as ET" > remove_xml_comments.py + echo "import os" >> remove_xml_comments.py + echo "" >> remove_xml_comments.py + echo "def remove_comments_from_xml(file_path):" >> remove_xml_comments.py + echo " try:" >> remove_xml_comments.py + echo " # 解析XML并保留注释" >> remove_xml_comments.py + echo " parser = ET.XMLParser(target=ET.TreeBuilder(insert_comments=True))" >> remove_xml_comments.py + echo " tree = ET.parse(file_path, parser=parser)" >> remove_xml_comments.py + echo " root = tree.getroot()" >> remove_xml_comments.py + echo "" >> remove_xml_comments.py + echo " # 递归删除注释节点" >> remove_xml_comments.py + echo " def remove_comments(element):" >> remove_xml_comments.py + echo " for child in list(element):" >> remove_xml_comments.py + echo " if child.tag is ET.Comment:" >> remove_xml_comments.py + echo " element.remove(child)" >> remove_xml_comments.py + echo " else:" >> remove_xml_comments.py + echo " remove_comments(child)" >> remove_xml_comments.py + echo "" >> remove_xml_comments.py + echo " remove_comments(root)" >> remove_xml_comments.py + echo "" >> remove_xml_comments.py + echo " # 保存修改后的文件" >> remove_xml_comments.py + echo " tree.write(file_path, encoding='utf-8', xml_declaration=True)" >> remove_xml_comments.py + echo " print(f'Processed: {file_path}')" >> remove_xml_comments.py + echo " except Exception as e:" >> remove_xml_comments.py + echo " print(f'Error processing {file_path}: {e}')" >> remove_xml_comments.py + echo "" >> remove_xml_comments.py + echo "# 遍历所有XML文件并处理" >> remove_xml_comments.py + echo "for root_dir, _, files in os.walk('temp-b-repo/out/assets'):" >> remove_xml_comments.py + echo " for file in files:" >> remove_xml_comments.py + echo " if file.endswith('.xml'):" >> remove_xml_comments.py + echo " file_path = os.path.join(root_dir, file)" >> remove_xml_comments.py + echo " remove_comments_from_xml(file_path)" >> remove_xml_comments.py + + # 运行Python脚本 + python remove_xml_comments.py + + echo "Files in temp directory after comment removal:" + ls -la temp-b-repo/out/assets - name: Push to Private B Repo env: