83 lines
3.0 KiB
Python
83 lines
3.0 KiB
Python
import os
|
|
import re
|
|
|
|
def prompt_for_multiline_input(prompt):
|
|
"""Helper function to get multi-line input from the user."""
|
|
print(f"{prompt} (输入 'END' 结束):")
|
|
lines = []
|
|
while True:
|
|
line = input()
|
|
if line.upper() == 'END':
|
|
break
|
|
lines.append(line)
|
|
return "\\n".join(lines)
|
|
|
|
def prompt_user_for_data():
|
|
"""Prompts the user for project details and returns a dictionary."""
|
|
print("--- 项目概述初始化 ---")
|
|
print("建议先查看 .templates/analysis_prompts.md 准备所需信息。")
|
|
|
|
while True:
|
|
proceed = input("是否继续? (y/n): ").lower()
|
|
if proceed in ['y', 'yes']:
|
|
break
|
|
elif proceed in ['n', 'no']:
|
|
print("操作已取消。")
|
|
return None
|
|
|
|
data = {
|
|
'PROJECT_NAME': input("请输入项目名称: "),
|
|
'PROJECT_INTRODUCTION': prompt_for_multiline_input("请输入项目简介 (多行输入)"),
|
|
'TECH_STACK_TABLE': prompt_for_multiline_input("请输入技术栈表格 (Markdown格式)"),
|
|
'CORE_FEATURES_LIST': prompt_for_multiline_input("请输入核心功能列表 (Markdown格式)"),
|
|
'ARCHITECTURE_DIAGRAM': prompt_for_multiline_input("请输入架构图 (Mermaid格式)"),
|
|
'DEV_STANDARDS': prompt_for_multiline_input("请输入开发环境与规范"),
|
|
'QUICK_START_GUIDE': prompt_for_multiline_input("请输入快速启动指南")
|
|
}
|
|
return data
|
|
|
|
def render_template(template_content, data):
|
|
"""Renders the template with the provided data."""
|
|
content = template_content
|
|
for key, value in data.items():
|
|
placeholder = f"{{{{{key}}}}}"
|
|
content = content.replace(placeholder, value)
|
|
return content
|
|
|
|
def write_output_file(content, output_path="rule-project.md"):
|
|
"""Writes the final content to the output file, with a safety check."""
|
|
if os.path.exists(output_path):
|
|
overwrite = input(f"警告: '{output_path}' 已存在。是否覆盖? (y/n): ").lower()
|
|
if overwrite not in ['y', 'yes']:
|
|
print("操作已取消,未写入文件。")
|
|
return False
|
|
|
|
try:
|
|
with open(output_path, 'w', encoding='utf-8') as f:
|
|
f.write(content)
|
|
print(f"成功生成文档: {output_path}")
|
|
return True
|
|
except IOError as e:
|
|
print(f"错误: 无法写入文件 {output_path}。原因: {e}")
|
|
return False
|
|
|
|
def main():
|
|
"""Main function to run the initializer."""
|
|
template_path = os.path.join('.templates', 'project_overview_template.md')
|
|
|
|
try:
|
|
with open(template_path, 'r', encoding='utf-8') as f:
|
|
template_content = f.read()
|
|
except FileNotFoundError:
|
|
print(f"错误: 模板文件未找到于 '{template_path}'")
|
|
return
|
|
|
|
user_data = prompt_user_for_data()
|
|
if user_data is None:
|
|
return
|
|
|
|
rendered_content = render_template(template_content, user_data)
|
|
write_output_file(rendered_content)
|
|
|
|
if __name__ == "__main__":
|
|
main() |