generate_backend_sourcecode.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 后端源代码拼接脚本
  5. 将 output_sourcecode/backend/ 目录下所有Java文件及配置文件内容拼接生成统一的后端源代码文档
  6. 输出纯源代码内容,不包含额外说明
  7. """
  8. import os
  9. import glob
  10. from datetime import datetime
  11. def get_file_type_priority(file_path):
  12. """
  13. 获取文件类型优先级,用于排序
  14. """
  15. if file_path.endswith('pom.xml'):
  16. return 1
  17. elif file_path.endswith('.yml') or file_path.endswith('.yaml'):
  18. return 2
  19. elif file_path.endswith('.properties'):
  20. return 3
  21. elif file_path.endswith('Application.java'):
  22. return 4
  23. elif 'entity' in file_path.lower():
  24. return 5
  25. elif 'mapper' in file_path.lower():
  26. return 6
  27. elif 'service' in file_path.lower():
  28. return 7
  29. elif 'controller' in file_path.lower():
  30. return 8
  31. elif 'dto' in file_path.lower():
  32. return 9
  33. elif 'vo' in file_path.lower():
  34. return 10
  35. elif 'config' in file_path.lower():
  36. return 11
  37. elif 'util' in file_path.lower():
  38. return 12
  39. else:
  40. return 13
  41. def extract_backend_files(backend_dir):
  42. """
  43. 提取后端目录中的所有源代码文件
  44. """
  45. file_patterns = [
  46. '**/*.java',
  47. '**/*.xml',
  48. '**/*.yml',
  49. '**/*.yaml',
  50. '**/*.properties',
  51. '**/*.sql'
  52. ]
  53. all_files = []
  54. if os.path.exists(backend_dir):
  55. for pattern in file_patterns:
  56. files = glob.glob(os.path.join(backend_dir, pattern), recursive=True)
  57. all_files.extend(files)
  58. # 过滤掉一些不需要的文件
  59. excluded_patterns = [
  60. 'target/',
  61. '.class',
  62. 'test/',
  63. 'Test.java',
  64. '.git/',
  65. 'node_modules/'
  66. ]
  67. filtered_files = []
  68. for file_path in all_files:
  69. should_exclude = False
  70. for pattern in excluded_patterns:
  71. if pattern in file_path:
  72. should_exclude = True
  73. break
  74. if not should_exclude:
  75. filtered_files.append(file_path)
  76. # 按照文件类型和路径排序
  77. filtered_files.sort(key=lambda x: (get_file_type_priority(x), x))
  78. return filtered_files
  79. def get_relative_path(file_path, backend_dir):
  80. """
  81. 获取相对于backend目录的路径
  82. """
  83. return os.path.relpath(file_path, backend_dir)
  84. def generate_backend_sourcecode():
  85. """
  86. 生成后端源代码文档
  87. """
  88. # 定义路径
  89. base_dir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) # 回到项目根目录
  90. backend_dir = os.path.join(base_dir, 'output_sourcecode', 'backend')
  91. output_dir = os.path.join(base_dir, 'output_docs')
  92. output_file = os.path.join(output_dir, '后端源代码.txt')
  93. # 确保输出目录存在
  94. os.makedirs(output_dir, exist_ok=True)
  95. # 获取所有后端文件
  96. backend_files = extract_backend_files(backend_dir)
  97. if not backend_files:
  98. print(f"错误:在 {backend_dir} 目录下没有找到后端源代码文件")
  99. return
  100. print(f"找到 {len(backend_files)} 个后端源代码文件:")
  101. for file_path in backend_files:
  102. rel_path = get_relative_path(file_path, backend_dir)
  103. print(f" - {rel_path}")
  104. # 开始生成文档
  105. print("开始生成后端源代码文档...")
  106. with open(output_file, 'w', encoding='utf-8') as f:
  107. # 处理每个源代码文件
  108. for i, file_path in enumerate(backend_files, 1):
  109. rel_path = get_relative_path(file_path, backend_dir)
  110. print(f"处理文件 {i}/{len(backend_files)}: {rel_path}")
  111. try:
  112. with open(file_path, 'r', encoding='utf-8') as source_f:
  113. file_content = source_f.read()
  114. # 写入文件分隔标识和源代码
  115. f.write(f"=== {rel_path} ===\n")
  116. f.write(file_content)
  117. f.write("\n\n")
  118. except Exception as e:
  119. print(f"处理文件 {rel_path} 时出错: {e}")
  120. f.write(f"=== {rel_path} ===\n")
  121. f.write(f"错误:无法读取文件内容 - {e}\n\n")
  122. print(f"✅ 后端源代码文档生成完成!")
  123. print(f"📁 输出文件: {output_file}")
  124. # 显示文件大小
  125. try:
  126. file_size = os.path.getsize(output_file)
  127. if file_size > 1024 * 1024:
  128. size_str = f"{file_size / (1024 * 1024):.2f} MB"
  129. elif file_size > 1024:
  130. size_str = f"{file_size / 1024:.2f} KB"
  131. else:
  132. size_str = f"{file_size} bytes"
  133. print(f"📊 文件大小: {size_str}")
  134. except:
  135. pass
  136. def main():
  137. """
  138. 主函数
  139. """
  140. print("=" * 60)
  141. print("后端源代码拼接脚本")
  142. print("=" * 60)
  143. try:
  144. generate_backend_sourcecode()
  145. except Exception as e:
  146. print(f"❌ 脚本执行失败: {e}")
  147. return 1
  148. return 0
  149. if __name__ == "__main__":
  150. exit(main())