setup_new_project.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 新项目快速设置脚本
  5. 帮助用户快速配置新的SDK文档项目
  6. """
  7. import os
  8. import sys
  9. import shutil
  10. import yaml
  11. from pathlib import Path
  12. def get_user_input(prompt, default=""):
  13. """获取用户输入"""
  14. if default:
  15. user_input = input(f"{prompt} (默认: {default}): ").strip()
  16. return user_input if user_input else default
  17. else:
  18. return input(f"{prompt}: ").strip()
  19. def create_config_interactive():
  20. """交互式创建配置文件"""
  21. print("=" * 60)
  22. print("SDK 文档项目快速设置")
  23. print("=" * 60)
  24. # 项目基本信息
  25. print("\n📋 项目基本信息:")
  26. project_name = get_user_input("SDK文档名称", "My_SDK_Docs")
  27. project_title = get_user_input("SDK文档标题", "My SDK 文档")
  28. project_description = get_user_input("SDK描述", f"{project_title} 提供了丰富的示例项目,包括基础功能、驱动示例和组件示例。")
  29. project_version = get_user_input("版本号", "0.0.1")
  30. project_author = get_user_input("作者", "your_name")
  31. project_copyright = get_user_input("版权信息", "2025, your_company")
  32. # 仓库信息
  33. print("\n📦 仓库信息:")
  34. repo_name = get_user_input("GitHub仓库名称", "your-sdk-repo")
  35. # 项目命名模式
  36. print("\n🔧 项目命名模式:")
  37. print("请根据你的项目命名规则设置模式匹配")
  38. sdk_prefix = get_user_input("SDK项目前缀", "my_sdk")
  39. # 构建配置
  40. config = {
  41. 'project': {
  42. 'name': project_name,
  43. 'title': project_title,
  44. 'description': project_description,
  45. 'version': project_version,
  46. 'author': project_author,
  47. 'copyright': project_copyright,
  48. 'language': 'zh_CN'
  49. },
  50. 'repository': {
  51. 'name': repo_name,
  52. 'projects_dir': '../projects',
  53. 'docs_dir': '.'
  54. },
  55. 'categories': {
  56. 'basic': {
  57. 'name': '基础篇',
  58. 'description': '基础功能示例',
  59. 'patterns': [
  60. f'{sdk_prefix}_basic_*',
  61. f'{sdk_prefix}_blink_led',
  62. f'{sdk_prefix}_factory'
  63. ]
  64. },
  65. 'driver': {
  66. 'name': '驱动篇',
  67. 'description': '外设驱动示例',
  68. 'patterns': [
  69. f'{sdk_prefix}_driver_*',
  70. f'{sdk_prefix}_usb_*'
  71. ]
  72. },
  73. 'component': {
  74. 'name': '组件篇',
  75. 'description': '网络组件示例',
  76. 'patterns': [
  77. f'{sdk_prefix}_component_*'
  78. ]
  79. },
  80. 'protocol': {
  81. 'name': '工业协议篇',
  82. 'description': '工业协议示例',
  83. 'patterns': [
  84. f'{sdk_prefix}_ethercat_*',
  85. f'{sdk_prefix}_modbus_*',
  86. f'{sdk_prefix}_profinet_*',
  87. f'{sdk_prefix}_ethernetip_*',
  88. f'{sdk_prefix}_ethernet'
  89. ]
  90. }
  91. },
  92. 'generation': {
  93. 'copy_files': [
  94. 'README.md',
  95. 'README_zh.md'
  96. ],
  97. 'copy_dirs': [
  98. 'figures'
  99. ],
  100. 'output_structure': [
  101. 'basic',
  102. 'driver',
  103. 'component',
  104. 'protocol'
  105. ]
  106. },
  107. 'sphinx': {
  108. 'theme': 'sphinx_rtd_theme',
  109. 'extensions': [
  110. 'myst_parser'
  111. ],
  112. 'source_suffix': {
  113. '.rst': 'restructuredtext',
  114. '.md': 'markdown'
  115. },
  116. 'myst_extensions': [
  117. 'colon_fence',
  118. 'deflist',
  119. 'dollarmath',
  120. 'html_image',
  121. 'html_admonition',
  122. 'replacements',
  123. 'smartquotes',
  124. 'strikethrough',
  125. 'substitution',
  126. 'tasklist'
  127. ]
  128. }
  129. }
  130. return config
  131. def save_config(config, output_path="config.yaml"):
  132. """保存配置文件"""
  133. with open(output_path, 'w', encoding='utf-8') as f:
  134. yaml.dump(config, f, default_flow_style=False, allow_unicode=True, indent=2)
  135. print(f"✓ 配置文件已保存: {output_path}")
  136. def create_github_workflow(repo_name):
  137. """创建GitHub Actions工作流"""
  138. workflow_content = f"""name: Deploy Sphinx docs to GitHub Pages
  139. on:
  140. push:
  141. branches:
  142. - master
  143. - main
  144. jobs:
  145. build-deploy:
  146. runs-on: ubuntu-latest
  147. permissions:
  148. contents: write
  149. pages: write
  150. id-token: write
  151. steps:
  152. - name: Checkout code
  153. uses: actions/checkout@v4
  154. with:
  155. fetch-depth: 0
  156. - name: Set up Python
  157. uses: actions/setup-python@v5
  158. with:
  159. python-version: '3.11'
  160. - name: Set up environment
  161. run: |
  162. export LANG=C.UTF-8
  163. export LC_ALL=C.UTF-8
  164. - name: Configure Git
  165. run: |
  166. git config --global user.name "github-actions[bot]"
  167. git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
  168. - name: Cache pip dependencies
  169. uses: actions/cache@v3
  170. with:
  171. path: ~/.cache/pip
  172. key: ${{{{ runner.os }}}}-pip-${{{{ hashFiles('source/requirements.txt') }}}}
  173. restore-keys: |
  174. ${{{{ runner.os }}}}-pip-
  175. - name: Install dependencies
  176. run: |
  177. python -m pip install --upgrade pip
  178. pip install -r source/requirements.txt
  179. - name: Generate documentation
  180. run: |
  181. cd source
  182. python version_generator.py
  183. - name: Deploy to GitHub Pages
  184. uses: peaceiris/actions-gh-pages@v4
  185. with:
  186. github_token: ${{{{ secrets.GITHUB_TOKEN }}}}
  187. publish_dir: source/_build/html
  188. force_orphan: true
  189. user_name: 'github-actions[bot]'
  190. user_email: '41898282+github-actions[bot]@users.noreply.github.com'
  191. """
  192. workflow_dir = Path("../.github/workflows")
  193. workflow_dir.mkdir(parents=True, exist_ok=True)
  194. workflow_path = workflow_dir / "gh-pages.yml"
  195. with open(workflow_path, 'w', encoding='utf-8') as f:
  196. f.write(workflow_content)
  197. print(f"✓ GitHub Actions工作流已创建: {workflow_path}")
  198. def create_readthedocs_config():
  199. """创建Read the Docs配置文件"""
  200. rtd_config = """# .readthedocs.yaml
  201. # Read the Docs configuration file
  202. # See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
  203. # Required
  204. version: 2
  205. # Set the OS, Python version and other tools you might need
  206. build:
  207. os: ubuntu-22.04
  208. tools:
  209. python: "3.12"
  210. # Build documentation in the "docs/" directory with Sphinx
  211. sphinx:
  212. configuration: source/conf.py
  213. # Optional but recommended, declare the Python requirements required
  214. # to build your documentation
  215. python:
  216. install:
  217. - requirements: source/requirements.txt
  218. """
  219. rtd_path = Path("../.readthedocs.yaml")
  220. with open(rtd_path, 'w', encoding='utf-8') as f:
  221. f.write(rtd_config)
  222. print(f"✓ Read the Docs配置文件已创建: {rtd_path}")
  223. def create_versions_list():
  224. """创建版本列表文件"""
  225. versions_content = """# 版本列表
  226. # 每行一个版本,以#开头的行为注释
  227. # 支持以下格式:
  228. # - master (最新版本)
  229. # - v1.0 (具体版本)
  230. # - v1.1
  231. # - v2.0
  232. master
  233. v1.0
  234. """
  235. versions_dir = Path("../.github")
  236. versions_dir.mkdir(parents=True, exist_ok=True)
  237. versions_path = versions_dir / "versions.list"
  238. with open(versions_path, 'w', encoding='utf-8') as f:
  239. f.write(versions_content)
  240. print(f"✓ 版本列表文件已创建: {versions_path}")
  241. def main():
  242. """主函数"""
  243. print("🚀 SDK文档项目快速设置工具")
  244. print("此工具将帮助您快速配置新的SDK文档项目")
  245. # 检查是否在正确的目录
  246. if not Path("doc_generator.py").exists():
  247. print("❌ 错误: 请在source目录中运行此脚本")
  248. sys.exit(1)
  249. # 交互式创建配置
  250. config = create_config_interactive()
  251. # 保存配置文件
  252. save_config(config)
  253. # 创建GitHub相关文件
  254. repo_name = config['repository']['name']
  255. create_github_workflow(repo_name)
  256. create_readthedocs_config()
  257. create_versions_list()
  258. print("\n" + "=" * 60)
  259. print("✅ 项目设置完成!")
  260. print("=" * 60)
  261. print("\n📝 下一步操作:")
  262. print("1. 检查并修改 config.yaml 文件")
  263. print("2. 确保项目目录结构正确")
  264. print("3. 运行测试构建:")
  265. print(" python build_local.py --check")
  266. print("4. 构建文档:")
  267. print(" python build_local.py")
  268. print("5. 本地预览:")
  269. print(" python build_local.py --serve")
  270. print("\n🔗 部署相关:")
  271. print("- GitHub Pages: 推送到master分支自动部署")
  272. print("- Read the Docs: 连接仓库后自动构建")
  273. print("\n📚 更多信息请查看 README.md")
  274. if __name__ == "__main__":
  275. main()