fix(v4.sh): 修复github下载逻辑 (#15) #13
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Generate Directory Tree | |
| on: | |
| push: | |
| branches: | |
| - main # 主分支 | |
| permissions: | |
| contents: write | |
| jobs: | |
| generate-tree: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| with: | |
| fetch-depth: 0 # 获取完整的仓库历史 | |
| - name: Generate directory tree | |
| run: | | |
| # 安装tree命令 | |
| sudo apt-get install tree -y | |
| # 创建生成目录树的脚本 | |
| cat > generate_trees.sh << 'EOF' | |
| #!/bin/bash | |
| # 查找所有文件夹,排除特定目录 | |
| find . -type d \( ! -path "*/.git/*" -a ! -path "*/.github/*" -a ! -path "*/.vscode/*" \) | while read -r dir; do | |
| # 排除顶级隐藏目录 | |
| if [[ "$dir" == "./.git" || "$dir" == "./.github" || "$dir" == "./.vscode" ]]; then | |
| continue | |
| fi | |
| # 进入目录 | |
| cd "$dir" || continue | |
| # 生成目录树并保存到index.html,排除已有的index.html和隐藏目录 | |
| tree -H . -L 2 -o index.html --charset=UTF-8 --ignore-case -I "index.html|.git|.github|.vscode|generate_trees.sh" . | |
| # 将 `<a href="./">.</a>` 替换为 `<a href="../">👆 回到上一级目录</a>` | |
| sed -i 's|<a href="./">.</a>|<a href="../">👆 回到上一级目录</a>|g' index.html | |
| # 添加自定义样式 | |
| echo '<style>body { font-family: Arial, sans-serif; } .tree { margin: 20px; } .tree ul { list-style-type: none; margin-left: 20px; padding-left: 1em; position: relative; } .tree ul ul::before { content: ""; display: block; position: absolute; left: 0; top: 0; bottom: 0; width: 1px; background-color: #ccc; } .tree li { margin: 5px 0; padding-left: 1.5em; position: relative; } .tree li::before { content: ""; display: block; position: absolute; left: 0; top: 0.7em; width: 10px; height: 1px; background-color: #ccc; } .tree a { color: #0366d6; text-decoration: none; } .tree a:hover { text-decoration: underline; } .back-link { font-weight: bold; color: #0366d6; }</style>' >> index.html | |
| # 返回上一级目录 | |
| cd - > /dev/null | |
| done | |
| EOF | |
| # 执行脚本生成所有目录树 | |
| chmod +x generate_trees.sh | |
| ./generate_trees.sh | |
| # 将生成的index.html `<a href="../">👆 回到上一级目录</a>` 替换为 `<a href="./">.</a>` | |
| sed -i 's|<a href="../">👆 回到上一级目录</a>|<a href="./">.</a>|g' index.html | |
| # 删除脚本文件 | |
| rm generate_trees.sh | |
| - name: Commit and push | |
| run: | | |
| git config user.name "GitHub Action" | |
| git config user.email "action@github.com" | |
| # 拉取远程最新代码并rebase,确保本地与远程同步 | |
| # 检查是否有文件变更 | |
| if [[ -n $(git status --porcelain) ]]; then | |
| git add -A | |
| git commit -m "Update directory trees" | |
| git pull --rebase origin main | |
| # 推送时若有冲突,自动尝试rebase解决(需确保无手动冲突) | |
| git push --force-with-lease origin main | |
| else | |
| echo "No changes to commit" | |
| fi |