Skip to content

Update README Stats

Update README Stats #8

Workflow file for this run

name: Update README Stats
on:
schedule:
- cron: '0 0 * * *' # 每天 UTC 0:00 对应北京时间 8:00
workflow_dispatch: # 允许手动触发
permissions:
contents: write
jobs:
update-readme:
runs-on: ubuntu-latest
steps:
- name: 检出代码库
uses: actions/checkout@v4
- name: 统计并格式化各项数据
run: |
# 统计原始数据
lines=$(find . -type f \( -name "*.py" -o -name "*.js" -o -name "*.md" -o -name "*.cpp" -o -name "*.java" \) | xargs wc -l | awk '{s+=$1} END {print s}')
chars=$(find . -type f \( -name "*.py" -o -name "*.js" -o -name "*.md" -o -name "*.cpp" -o -name "*.java" \) | xargs wc -m | awk '{s+=$1} END {print s}')
main_count=$(grep -RHo 'int main' . | wc -l)
var_count=$(grep -RHoE '\b(int|long long|double|auto|vector|queue|string|class|struct|void)\b' . | wc -l)
# 千分位格式化函数
fmt(){ echo "$1" | sed ':a;s/\B[0-9]\{3\}\>/,&/;ta'; }
# 格式化输出
f_lines=$(fmt "$lines")
f_chars=$(fmt "$chars")
f_main=$(fmt "$main_count")
f_var=$(fmt "$var_count")
# 替换模板
sed -e "s/{line}/$f_lines/" \
-e "s/{char}/$f_chars/" \
-e "s/{main}/$f_main/" \
-e "s/{var}/$f_var/" \
README.template.md > README.md
- name: 提交更新
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git add README.md
git commit -m ":hammer: 更新 README 统计信息"
git push https://github.com/${{ github.repository }} HEAD:master
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}