96 lines
3.1 KiB
Python
96 lines
3.1 KiB
Python
|
|
"""
|
|||
|
|
代码生成控制器
|
|||
|
|
协调代码生成的UI和业务逻辑
|
|||
|
|
"""
|
|||
|
|
from typing import List, Optional
|
|||
|
|
from pathlib import Path
|
|||
|
|
|
|||
|
|
from services.code_generator_service import CodeGeneratorService
|
|||
|
|
from services.graph_service import GraphService
|
|||
|
|
from services.mapping_service import MappingService
|
|||
|
|
from models.message import Message
|
|||
|
|
from config import ProtocolType, SerializationType, LanguageStandard
|
|||
|
|
from utils.logger import get_logger
|
|||
|
|
|
|||
|
|
logger = get_logger(__name__)
|
|||
|
|
|
|||
|
|
|
|||
|
|
class CodeGenController:
|
|||
|
|
"""代码生成控制器类"""
|
|||
|
|
|
|||
|
|
def __init__(self):
|
|||
|
|
self.codegen_service = CodeGeneratorService()
|
|||
|
|
self.graph_service = GraphService()
|
|||
|
|
self.mapping_service = MappingService()
|
|||
|
|
|
|||
|
|
def generate_code(self, config: dict) -> tuple[bool, str, dict]:
|
|||
|
|
"""
|
|||
|
|
生成代码
|
|||
|
|
|
|||
|
|
Args:
|
|||
|
|
config: 代码生成配置字典
|
|||
|
|
|
|||
|
|
Returns:
|
|||
|
|
(是否成功, 消息, 生成的文件字典)
|
|||
|
|
"""
|
|||
|
|
try:
|
|||
|
|
# 解析配置
|
|||
|
|
source_message = config['source_message']
|
|||
|
|
target_message = config['target_message']
|
|||
|
|
protocol = ProtocolType(config['protocol'])
|
|||
|
|
serialization = SerializationType(config['serialization'])
|
|||
|
|
language_standard = LanguageStandard(config.get('language_standard', 'C++17'))
|
|||
|
|
output_dir = Path(config.get('output_dir', '')) if config.get('output_dir') else None
|
|||
|
|
|
|||
|
|
# 获取映射图
|
|||
|
|
graph = self.mapping_service.get_mapping_graph()
|
|||
|
|
if not graph:
|
|||
|
|
return False, "映射图不存在", {}
|
|||
|
|
|
|||
|
|
# 查找映射路径
|
|||
|
|
source_field_id = config.get('source_field_id')
|
|||
|
|
target_field_id = config.get('target_field_id')
|
|||
|
|
|
|||
|
|
if source_field_id and target_field_id:
|
|||
|
|
path_result = self.graph_service.find_shortest_path(
|
|||
|
|
graph, source_field_id, target_field_id
|
|||
|
|
)
|
|||
|
|
if not path_result:
|
|||
|
|
return False, "未找到映射路径", {}
|
|||
|
|
|
|||
|
|
path, _ = path_result
|
|||
|
|
mapping_path = self.graph_service.get_path_mappings(graph, path)
|
|||
|
|
else:
|
|||
|
|
mapping_path = []
|
|||
|
|
|
|||
|
|
# 生成代码
|
|||
|
|
return self.codegen_service.generate_converter_code(
|
|||
|
|
source_message,
|
|||
|
|
target_message,
|
|||
|
|
mapping_path,
|
|||
|
|
protocol,
|
|||
|
|
serialization,
|
|||
|
|
language_standard,
|
|||
|
|
output_dir
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
logger.error(f"Controller: Failed to generate code: {e}")
|
|||
|
|
return False, f"代码生成失败: {str(e)}", {}
|
|||
|
|
|
|||
|
|
def preview_code(self, config: dict) -> tuple[bool, str, dict]:
|
|||
|
|
"""
|
|||
|
|
预览代码(不写入文件)
|
|||
|
|
|
|||
|
|
Args:
|
|||
|
|
config: 代码生成配置字典
|
|||
|
|
|
|||
|
|
Returns:
|
|||
|
|
(是否成功, 消息, 生成的代码字典)
|
|||
|
|
"""
|
|||
|
|
# 不指定输出目录,只生成代码内容
|
|||
|
|
config_copy = config.copy()
|
|||
|
|
config_copy['output_dir'] = None
|
|||
|
|
|
|||
|
|
return self.generate_code(config_copy)
|