修复device
This commit is contained in:
parent
a9f13e2607
commit
7127b207f8
@ -44,10 +44,17 @@ def generate_device_header(project_path, enabled_devices):
|
|||||||
from app.tools.code_generator import CodeGenerator
|
from app.tools.code_generator import CodeGenerator
|
||||||
device_dir = CodeGenerator.get_assets_dir("User_code/device")
|
device_dir = CodeGenerator.get_assets_dir("User_code/device")
|
||||||
template_path = os.path.join(device_dir, "device.h")
|
template_path = os.path.join(device_dir, "device.h")
|
||||||
|
dst_path = os.path.join(project_path, "User/device/device.h")
|
||||||
|
|
||||||
# 读取模板文件
|
# 优先读取项目中已存在的文件,如果不存在则使用模板
|
||||||
with open(template_path, 'r', encoding='utf-8') as f:
|
if os.path.exists(dst_path):
|
||||||
content = f.read()
|
# 读取现有文件以保留用户区域
|
||||||
|
with open(dst_path, 'r', encoding='utf-8') as f:
|
||||||
|
content = f.read()
|
||||||
|
else:
|
||||||
|
# 文件不存在时从模板创建
|
||||||
|
with open(template_path, 'r', encoding='utf-8') as f:
|
||||||
|
content = f.read()
|
||||||
|
|
||||||
# 收集所有需要的信号定义
|
# 收集所有需要的信号定义
|
||||||
signals = []
|
signals = []
|
||||||
@ -71,13 +78,12 @@ def generate_device_header(project_path, enabled_devices):
|
|||||||
# 生成信号定义文本
|
# 生成信号定义文本
|
||||||
signals_text = '\n'.join(signals) if signals else '/* No signals defined */'
|
signals_text = '\n'.join(signals) if signals else '/* No signals defined */'
|
||||||
|
|
||||||
# 替换AUTO GENERATED SIGNALS部分
|
# 替换AUTO GENERATED SIGNALS部分,保留其他所有用户区域
|
||||||
pattern = r'/\* AUTO GENERATED SIGNALS BEGIN \*/(.*?)/\* AUTO GENERATED SIGNALS END \*/'
|
pattern = r'/\* AUTO GENERATED SIGNALS BEGIN \*/(.*?)/\* AUTO GENERATED SIGNALS END \*/'
|
||||||
replacement = f'/* AUTO GENERATED SIGNALS BEGIN */\n{signals_text}\n/* AUTO GENERATED SIGNALS END */'
|
replacement = f'/* AUTO GENERATED SIGNALS BEGIN */\n{signals_text}\n/* AUTO GENERATED SIGNALS END */'
|
||||||
content = re.sub(pattern, replacement, content, flags=re.DOTALL)
|
content = re.sub(pattern, replacement, content, flags=re.DOTALL)
|
||||||
|
|
||||||
# 保存文件
|
# 保存文件
|
||||||
dst_path = os.path.join(project_path, "User/device/device.h")
|
|
||||||
os.makedirs(os.path.dirname(dst_path), exist_ok=True)
|
os.makedirs(os.path.dirname(dst_path), exist_ok=True)
|
||||||
with open(dst_path, 'w', encoding='utf-8') as f:
|
with open(dst_path, 'w', encoding='utf-8') as f:
|
||||||
f.write(content)
|
f.write(content)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user