Skip to content

Merge pull request #293 from fanhengkui/main #107

Merge pull request #293 from fanhengkui/main

Merge pull request #293 from fanhengkui/main #107

name: Update README Table
on:
push:
branches:
- main
paths:
- 'daily/**' # 监控 daily 目录
- '!daily/notify.py' # 排除 notify.py
- '!daily/**/*.md' # 排除 md 文件
- 'wxapp/**' # 监控 wxapp 目录
- '!wxapp/**/*.md' # 排除 wxapp 下的 md 文件
jobs:
update-readme:
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: 检出代码
uses: actions/checkout@v4
with:
fetch-depth: 2
- name: 配置 Python 环境
uses: actions/setup-python@v5
with:
python-version: '3.x'
- name: 运行更新脚本
env:
TZ: Asia/Shanghai
run: |
cat << 'EOF' > update_readme.py
import os
import re
import datetime
import subprocess
# 获取本次 Push 更改的文件列表
try:
diff_cmd = ['git', 'diff', '--name-only', 'HEAD~1', 'HEAD']
result = subprocess.run(diff_cmd, capture_output=True, text=True, check=True)
changed_files = result.stdout.splitlines()
except Exception:
try:
diff_cmd = ['git', 'diff', '--name-only', '--cached']
result = subprocess.run(diff_cmd, capture_output=True, text=True, check=True)
changed_files = result.stdout.splitlines()
except Exception:
changed_files = []
now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
# ========== 1. 收集更新信息 ==========
daily_updates = {} # 脚本名 -> 更新时间
wxapp_updates = {} # 脚本名 -> 更新时间
for f in changed_files:
# 处理 daily 目录
if f.startswith('daily/') and os.path.exists(f):
filename = os.path.basename(f)
if filename != 'notify.py' and not f.endswith('.md'):
with open(f, 'r', encoding='utf-8') as file:
content = file.read()
match = re.search(r'Env\s*\(\s*["\']([^"\']+)["\']\s*\)', content)
if match:
daily_updates[match.group(1)] = now
# 处理 wxapp 目录(同样使用 Env 提取)
elif f.startswith('wxapp/') and os.path.exists(f):
if f.endswith('.js') or f.endswith('.py'):
with open(f, 'r', encoding='utf-8') as file:
content = file.read()
match = re.search(r'Env\s*\(\s*["\']([^"\']+)["\']\s*\)', content)
if match:
wxapp_updates[match.group(1)] = now
# ========== 2. 扫描目录获取所有现有脚本 ==========
# daily 所有 Env 名称
all_daily_scripts = set()
if os.path.exists('daily'):
for root, _, files in os.walk('daily'):
for file in files:
if file != 'notify.py' and not file.endswith('.md'):
fpath = os.path.join(root, file).replace('\\', '/')
with open(fpath, 'r', encoding='utf-8') as f:
content = f.read()
match = re.search(r'Env\s*\(\s*["\']([^"\']+)["\']\s*\)', content)
if match:
all_daily_scripts.add(match.group(1))
# wxapp 所有 Env 名称
all_wxapp_scripts = set()
if os.path.exists('wxapp'):
for root, _, files in os.walk('wxapp'):
for file in files:
if file.endswith('.js') or file.endswith('.py'):
fpath = os.path.join(root, file).replace('\\', '/')
with open(fpath, 'r', encoding='utf-8') as f:
content = f.read()
match = re.search(r'Env\s*\(\s*["\']([^"\']+)["\']\s*\)', content)
if match:
all_wxapp_scripts.add(match.group(1))
# ========== 3. 更新 README 中的两个表格 ==========
readme_path = 'README.md'
if not os.path.exists(readme_path):
print("README.md 不存在!")
exit(1)
with open(readme_path, 'r', encoding='utf-8') as f:
full_text = f.read()
# ---------- 3.1 更新 daily 表格 ----------
start_daily = "<!-- README_TABLE_START -->"
end_daily = "<!-- README_TABLE_END -->"
if start_daily not in full_text or end_daily not in full_text:
print("错误:未找到 daily 表格的锚点标记!")
exit(1)
pre_part, temp = full_text.split(start_daily)
daily_table_section, post_part = temp.split(end_daily)
lines = daily_table_section.strip().split('\n')
header, separator, body_lines = [], [], []
mode = 'header'
for line in lines:
l_s = line.strip()
if not l_s.startswith('|'):
continue
if '脚本名称' in l_s:
header.append(line)
mode = 'separator'
elif '---' in l_s and mode == 'separator':
separator.append(line)
mode = 'body'
elif mode == 'body':
body_lines.append(line)
existing_daily = set()
new_daily_body = []
for line in body_lines:
parts = [p.strip() for p in line.split('|')]
if len(parts) >= 4:
name = parts[1]
existing_daily.add(name)
if name in daily_updates:
line = f"| {name} | {daily_updates[name]} | ✅ |"
new_daily_body.append(line)
for name, tm in daily_updates.items():
if name not in existing_daily:
new_daily_body.append(f"| {name} | {tm} | ✅ |")
existing_daily.add(name)
for name in all_daily_scripts:
if name not in existing_daily:
new_daily_body.append(f"| {name} | {now} | ✅ |")
final_daily_table = "\n".join(header + separator + new_daily_body)
# ---------- 3.2 更新 wxapp 表格 ----------
start_wxapp = "<!-- WXAPP_TABLE_START -->"
end_wxapp = "<!-- WXAPP_TABLE_END -->"
if start_wxapp not in full_text or end_wxapp not in full_text:
print("错误:未找到 wxapp 表格的锚点标记!")
exit(1)
# 从 post_part 中提取 wxapp 表格(因为 daily 表格已被切出)
wxapp_pre, wxapp_temp = post_part.split(start_wxapp)
wxapp_table_section, final_post = wxapp_temp.split(end_wxapp)
lines_wx = wxapp_table_section.strip().split('\n')
header_wx, separator_wx, body_lines_wx = [], [], []
mode_wx = 'header'
for line in lines_wx:
l_s = line.strip()
if not l_s.startswith('|'):
continue
if '脚本名称' in l_s:
header_wx.append(line)
mode_wx = 'separator'
elif '---' in l_s and mode_wx == 'separator':
separator_wx.append(line)
mode_wx = 'body'
elif mode_wx == 'body':
body_lines_wx.append(line)
existing_wxapp = set()
new_wxapp_body = []
for line in body_lines_wx:
parts = [p.strip() for p in line.split('|')]
if len(parts) >= 4:
name = parts[1]
existing_wxapp.add(name)
if name in wxapp_updates:
line = f"| {name} | {wxapp_updates[name]} | ✅ |"
new_wxapp_body.append(line)
for name, tm in wxapp_updates.items():
if name not in existing_wxapp:
new_wxapp_body.append(f"| {name} | {tm} | ✅ |")
existing_wxapp.add(name)
for name in all_wxapp_scripts:
if name not in existing_wxapp:
new_wxapp_body.append(f"| {name} | {now} | ✅ |")
final_wxapp_table = "\n".join(header_wx + separator_wx + new_wxapp_body)
# 重新组合整个 README
final_content = (
pre_part + start_daily + "\n" + final_daily_table + "\n" + end_daily +
wxapp_pre + start_wxapp + "\n" + final_wxapp_table + "\n" + end_wxapp +
final_post
)
with open(readme_path, 'w', encoding='utf-8') as f:
f.write(final_content)
print("README.md 处理完成。")
EOF
python update_readme.py
- name: 提交并推送变更
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add README.md
if ! git diff --staged --quiet; then
git commit -m "docs: 自动更新 daily 和 wxapp 脚本状态 [skip ci]"
git push
else
echo "无变更需推送。"
fi