-
Notifications
You must be signed in to change notification settings - Fork 13
73 lines (62 loc) · 3.26 KB
/
GenerateDirectoryTree.yaml
File metadata and controls
73 lines (62 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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@v6
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