修改.h覆盖问题

This commit is contained in:
Robofish 2025-10-18 23:55:43 +08:00
parent 7127b207f8
commit 49545dd232
5 changed files with 52 additions and 8 deletions

View File

@ -1243,7 +1243,9 @@ class bsp(QWidget):
dst_bsp_h = os.path.join(project_path, "User/bsp/bsp.h") dst_bsp_h = os.path.join(project_path, "User/bsp/bsp.h")
os.makedirs(os.path.dirname(dst_bsp_h), exist_ok=True) os.makedirs(os.path.dirname(dst_bsp_h), exist_ok=True)
if os.path.exists(src_bsp_h): if os.path.exists(src_bsp_h):
shutil.copyfile(src_bsp_h, dst_bsp_h) with open(src_bsp_h, 'r', encoding='utf-8') as f:
content = f.read()
save_with_preserve(dst_bsp_h, content)
total = 0 total = 0
success_count = 0 success_count = 0

View File

@ -294,7 +294,9 @@ class component(QWidget):
dst_component_h = os.path.join(project_path, "User/component/component.h") dst_component_h = os.path.join(project_path, "User/component/component.h")
os.makedirs(os.path.dirname(dst_component_h), exist_ok=True) os.makedirs(os.path.dirname(dst_component_h), exist_ok=True)
if os.path.exists(src_component_h): if os.path.exists(src_component_h):
shutil.copyfile(src_component_h, dst_component_h) with open(src_component_h, 'r', encoding='utf-8') as f:
content = f.read()
save_with_preserve(dst_component_h, content)
# 收集所有需要生成的组件和它们的依赖 # 收集所有需要生成的组件和它们的依赖
components_to_generate = set() components_to_generate = set()
@ -367,9 +369,11 @@ class component(QWidget):
shutil.rmtree(dst_path) shutil.rmtree(dst_path)
shutil.copytree(src_path, dst_path) shutil.copytree(src_path, dst_path)
elif os.path.isfile(src_path): elif os.path.isfile(src_path):
# 如果是文件,复制单个文件 # 如果是文件,复制单个文件并保留用户区域
os.makedirs(os.path.dirname(dst_path), exist_ok=True) os.makedirs(os.path.dirname(dst_path), exist_ok=True)
shutil.copyfile(src_path, dst_path) with open(src_path, 'r', encoding='utf-8') as f:
new_content = f.read()
save_with_preserve(dst_path, new_content)
else: else:
# 如果既不是文件也不是目录,跳过 # 如果既不是文件也不是目录,跳过
print(f"跳过不存在的依赖: {dep_path}") print(f"跳过不存在的依赖: {dep_path}")

View File

@ -7,6 +7,38 @@ import shutil
import yaml import yaml
import re import re
def preserve_all_user_regions(new_code, old_code):
""" Preserves all user-defined regions in the new code based on the old code.
This function uses regex to find user-defined regions in the old code and replaces them in the new code.
Args:
new_code (str): The new code content.
old_code (str): The old code content.
Returns:
str: The new code with preserved user-defined regions.
"""
pattern = re.compile(
r"/\*\s*(USER [A-Z0-9_ ]+)\s*BEGIN\s*\*/(.*?)/\*\s*\1\s*END\s*\*/",
re.DOTALL
)
old_regions = {m.group(1): m.group(2) for m in pattern.finditer(old_code or "")}
def repl(m):
region = m.group(1)
old_content = old_regions.get(region)
if old_content is not None:
return m.group(0).replace(m.group(2), old_content)
return m.group(0)
return pattern.sub(repl, new_code)
def save_with_preserve(path, new_code):
"""保存文件并保留用户区域"""
if os.path.exists(path):
with open(path, "r", encoding="utf-8") as f:
old_code = f.read()
new_code = preserve_all_user_regions(new_code, old_code)
os.makedirs(os.path.dirname(path), exist_ok=True)
with open(path, "w", encoding="utf-8") as f:
f.write(new_code)
def load_device_config(config_path): def load_device_config(config_path):
"""加载设备配置""" """加载设备配置"""
if os.path.exists(config_path): if os.path.exists(config_path):
@ -269,9 +301,11 @@ class DeviceSimple(QWidget):
f.write(content) f.write(content)
if file_type == 'header': if file_type == 'header':
# 头文件直接复制,不做修改 # 头文件需要保留用户区域
os.makedirs(os.path.dirname(dst_path), exist_ok=True) os.makedirs(os.path.dirname(dst_path), exist_ok=True)
shutil.copy2(src_path, dst_path) with open(src_path, 'r', encoding='utf-8') as f:
content = f.read()
save_with_preserve(dst_path, content)
elif file_type == 'source': elif file_type == 'source':
# 源文件需要替换BSP设备名称 # 源文件需要替换BSP设备名称

View File

@ -416,14 +416,18 @@ class DataInterface(QWidget):
skipped.append(dst_c) skipped.append(dst_c)
else: else:
os.makedirs(os.path.dirname(dst_c), exist_ok=True) os.makedirs(os.path.dirname(dst_c), exist_ok=True)
shutil.copy2(src_c, dst_c) with open(src_c, 'r', encoding='utf-8') as f:
content = f.read()
save_with_preserve(dst_c, content)
copied.append(dst_c) copied.append(dst_c)
if os.path.exists(src_h): if os.path.exists(src_h):
if os.path.exists(dst_h): if os.path.exists(dst_h):
skipped.append(dst_h) skipped.append(dst_h)
else: else:
os.makedirs(os.path.dirname(dst_h), exist_ok=True) os.makedirs(os.path.dirname(dst_h), exist_ok=True)
shutil.copy2(src_h, dst_h) with open(src_h, 'r', encoding='utf-8') as f:
content = f.read()
save_with_preserve(dst_h, content)
copied.append(dst_h) copied.append(dst_h)
msg = f"已拷贝 {len(copied)} 个文件到 User 目录" msg = f"已拷贝 {len(copied)} 个文件到 User 目录"
if skipped: if skipped: