修改.h覆盖问题
This commit is contained in:
parent
7127b207f8
commit
49545dd232
Binary file not shown.
@ -1243,7 +1243,9 @@ class bsp(QWidget):
|
||||
dst_bsp_h = os.path.join(project_path, "User/bsp/bsp.h")
|
||||
os.makedirs(os.path.dirname(dst_bsp_h), exist_ok=True)
|
||||
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
|
||||
success_count = 0
|
||||
|
||||
@ -294,7 +294,9 @@ class component(QWidget):
|
||||
dst_component_h = os.path.join(project_path, "User/component/component.h")
|
||||
os.makedirs(os.path.dirname(dst_component_h), exist_ok=True)
|
||||
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()
|
||||
@ -367,9 +369,11 @@ class component(QWidget):
|
||||
shutil.rmtree(dst_path)
|
||||
shutil.copytree(src_path, dst_path)
|
||||
elif os.path.isfile(src_path):
|
||||
# 如果是文件,复制单个文件
|
||||
# 如果是文件,复制单个文件并保留用户区域
|
||||
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:
|
||||
# 如果既不是文件也不是目录,跳过
|
||||
print(f"跳过不存在的依赖: {dep_path}")
|
||||
|
||||
@ -7,6 +7,38 @@ import shutil
|
||||
import yaml
|
||||
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):
|
||||
"""加载设备配置"""
|
||||
if os.path.exists(config_path):
|
||||
@ -269,9 +301,11 @@ class DeviceSimple(QWidget):
|
||||
f.write(content)
|
||||
|
||||
if file_type == 'header':
|
||||
# 头文件直接复制,不做修改
|
||||
# 头文件需要保留用户区域
|
||||
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':
|
||||
# 源文件需要替换BSP设备名称
|
||||
|
||||
@ -416,14 +416,18 @@ class DataInterface(QWidget):
|
||||
skipped.append(dst_c)
|
||||
else:
|
||||
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)
|
||||
if os.path.exists(src_h):
|
||||
if os.path.exists(dst_h):
|
||||
skipped.append(dst_h)
|
||||
else:
|
||||
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)
|
||||
msg = f"已拷贝 {len(copied)} 个文件到 User 目录"
|
||||
if skipped:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user