1+ name : Initialize Repository
2+
3+ on :
4+ workflow_dispatch : # 手动触发,可用于仓库首次初始化
5+ push :
6+ paths :
7+ - ' LICENSE' # 当 LICENSE 文件发生变更时触发
8+
9+ jobs :
10+ init :
11+ runs-on : ubuntu-latest
12+ steps :
13+ - name : Checkout repository
14+ uses : actions/checkout@v3
15+ with :
16+ fetch-depth : 0 # 获取完整历史以确保可以正确提交
17+ token : ${{ secrets.GITHUB_TOKEN }} # 使用具有写入权限的token
18+
19+ - name : Set repository variables
20+ run : |
21+ # 从环境变量 GITHUB_REPOSITORY 中提取仓库名称和所有者(格式为 owner/repo)
22+ REPO_FULL="${GITHUB_REPOSITORY}"
23+ REPO_OWNER="${REPO_FULL%/*}"
24+ REPO_NAME="${REPO_FULL#*/}"
25+ # 将横杠替换为下划线
26+ UNDERSCORE_REPO_NAME=$(echo "$REPO_NAME" | tr '-' '_')
27+ echo "REPO_OWNER=${REPO_OWNER}" >> $GITHUB_ENV
28+ echo "REPO_NAME=${REPO_NAME}" >> $GITHUB_ENV
29+ echo "UNDERSCORE_REPO_NAME=${UNDERSCORE_REPO_NAME}" >> $GITHUB_ENV
30+ echo "Repository owner: $REPO_OWNER"
31+ echo "Repository name: $REPO_NAME"
32+ echo "Repository underscore name: $UNDERSCORE_REPO_NAME"
33+
34+ - name : Update README.md
35+ run : |
36+ # 替换 README.md 中的内容
37+ rm -f README.md
38+ sed -i.bak "s/owner/${REPO_OWNER}/g" readme_template.md
39+ sed -i.bak "s/nonebot-plugin-template/${REPO_NAME}/g" readme_template.md
40+ sed -i.bak "s/nonebot_plugin_template/${UNDERSCORE_REPO_NAME}/g" readme_template.md
41+ mv readme_template.md README.md
42+ rm -f README.md.bak
43+ rm -f readme_template.md.bak
44+
45+ - name : Update pyproject.toml
46+ run : |
47+ # 替换 pyproject.toml 中的内容
48+ sed -i.bak "s/nonebot-plugin-template/${REPO_NAME}/g" pyproject.toml
49+ sed -i.bak "s/owner/${REPO_OWNER}/g" pyproject.toml
50+ sed -i.bak "s/nonebot_plugin_template/${UNDERSCORE_REPO_NAME}/g" pyproject.toml
51+ rm -f pyproject.toml.bak
52+
53+ - name : Rename plugin metadata
54+ run : |
55+ # 修改 nonebot_plugin_template/__init__.py 中的插件元数据 中的 homepage
56+ sed -i.bak "s/owner/${REPO_OWNER}/g" nonebot_plugin_template/__init__.py
57+ sed -i.bak "s/nonebot-plugin-template/${REPO_NAME}/g" nonebot_plugin_template/__init__.py
58+ rm -f nonebot_plugin_template/__init__.py.bak
59+
60+ - name : Rename plugin folder
61+ run : |
62+ if [ -d "nonebot_plugin_template" ]; then
63+ mv nonebot_plugin_template "${UNDERSCORE_REPO_NAME}"
64+ echo "Successfully renamed plugin folder to ${UNDERSCORE_REPO_NAME}"
65+ else
66+ echo "Directory nonebot_plugin_template not found."
67+ exit 1
68+ fi
69+
70+ - name : Rename tests/plugin_test plugin name
71+ run : |
72+ # 修改 tests/plugin_test.py 中的插件名称
73+ sed -i.bak "s/nonebot_plugin_template/${UNDERSCORE_REPO_NAME}/g" tests/plugin_test.py
74+ rm -f tests/plugin_test.py.bak
75+
76+ - name : Commit and push changes
77+ run : |
78+ git config user.name "github-actions[bot]"
79+ git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
80+ git add .
81+ git commit -m "🎉 Initialize repository with correct naming"
82+ git push
0 commit comments