mirror of
https://github.com/goldenfishs/MRobot.git
synced 2025-11-02 04:23:10 +08:00
更新win
This commit is contained in:
parent
0e57cd6cd7
commit
a42e2e3832
Binary file not shown.
@ -166,9 +166,13 @@ class CodeGenerateInterface(QWidget):
|
||||
from app.tools.update_cmake_sources import find_user_c_files, update_cmake_sources,update_cmake_includes
|
||||
from pathlib import Path
|
||||
|
||||
# 构建User目录和CMakeLists.txt路径
|
||||
user_dir = os.path.join(self.project_path, "User")
|
||||
cmake_file = os.path.join(self.project_path, "CMakeLists.txt")
|
||||
# 构建User目录和CMakeLists.txt路径,规范化路径分隔符
|
||||
user_dir = os.path.normpath(os.path.join(self.project_path, "User"))
|
||||
cmake_file = os.path.normpath(os.path.join(self.project_path, "CMakeLists.txt"))
|
||||
|
||||
print(f"项目路径: {self.project_path}")
|
||||
print(f"User目录路径: {user_dir}")
|
||||
print(f"CMakeLists.txt路径: {cmake_file}")
|
||||
|
||||
# 检查User目录是否存在
|
||||
if not os.path.exists(user_dir):
|
||||
@ -191,7 +195,9 @@ class CodeGenerateInterface(QWidget):
|
||||
return
|
||||
|
||||
# 查找User目录下的所有.c文件
|
||||
print("开始查找.c文件...")
|
||||
c_files = find_user_c_files(user_dir)
|
||||
print(f"找到 {len(c_files)} 个.c文件")
|
||||
|
||||
if not c_files:
|
||||
InfoBar.warning(
|
||||
@ -203,16 +209,31 @@ class CodeGenerateInterface(QWidget):
|
||||
return
|
||||
|
||||
# 更新CMakeLists.txt
|
||||
success = update_cmake_sources(cmake_file, c_files)
|
||||
success = update_cmake_includes(cmake_file, user_dir)
|
||||
print("开始更新CMakeLists.txt...")
|
||||
sources_success = update_cmake_sources(cmake_file, c_files)
|
||||
includes_success = update_cmake_includes(cmake_file, user_dir)
|
||||
|
||||
if success:
|
||||
if sources_success and includes_success:
|
||||
InfoBar.success(
|
||||
title="配置成功",
|
||||
content=f"已成功更新CMakeLists.txt,共添加了 {len(c_files)} 个源文件",
|
||||
parent=self,
|
||||
duration=3000
|
||||
)
|
||||
elif sources_success:
|
||||
InfoBar.warning(
|
||||
title="部分成功",
|
||||
content=f"源文件更新成功,但include路径更新失败",
|
||||
parent=self,
|
||||
duration=3000
|
||||
)
|
||||
elif includes_success:
|
||||
InfoBar.warning(
|
||||
title="部分成功",
|
||||
content=f"include路径更新成功,但源文件更新失败",
|
||||
parent=self,
|
||||
duration=3000
|
||||
)
|
||||
else:
|
||||
InfoBar.error(
|
||||
title="配置失败",
|
||||
@ -222,6 +243,7 @@ class CodeGenerateInterface(QWidget):
|
||||
)
|
||||
|
||||
except ImportError as e:
|
||||
print(f"导入错误: {e}")
|
||||
InfoBar.error(
|
||||
title="导入错误",
|
||||
content=f"无法导入cmake配置模块: {str(e)}",
|
||||
@ -229,6 +251,9 @@ class CodeGenerateInterface(QWidget):
|
||||
duration=3000
|
||||
)
|
||||
except Exception as e:
|
||||
print(f"cmake配置错误: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
InfoBar.error(
|
||||
title="配置失败",
|
||||
content=f"cmake配置过程中出现错误: {str(e)}",
|
||||
|
||||
@ -21,7 +21,8 @@ def find_user_c_files(user_dir):
|
||||
for c_file in user_path.rglob("*.c"):
|
||||
# 获取相对于项目根目录的路径
|
||||
relative_path = c_file.relative_to(user_path.parent)
|
||||
c_files.append(str(relative_path))
|
||||
# 使用正斜杠确保跨平台兼容性,避免转义字符问题
|
||||
c_files.append(str(relative_path).replace('\\', '/'))
|
||||
|
||||
# 按目录和文件名排序
|
||||
c_files.sort()
|
||||
@ -44,31 +45,37 @@ def update_cmake_sources(cmake_file, c_files):
|
||||
# 按目录分组
|
||||
current_dir = ""
|
||||
for c_file in c_files:
|
||||
file_dir = str(Path(c_file).parent)
|
||||
# 确保路径使用正斜杠,避免转义字符问题
|
||||
normalized_file = c_file.replace('\\', '/')
|
||||
file_dir = str(Path(normalized_file).parent).replace('\\', '/')
|
||||
if file_dir != current_dir:
|
||||
if current_dir: # 不是第一个目录,添加空行
|
||||
sources_section += "\n"
|
||||
sources_section += f" # {file_dir} sources\n"
|
||||
current_dir = file_dir
|
||||
|
||||
sources_section += f" {c_file}\n"
|
||||
sources_section += f" {normalized_file}\n"
|
||||
|
||||
sources_section += ")"
|
||||
|
||||
# 使用正则表达式替换target_sources部分
|
||||
pattern = r'# Add sources to executable\s*\ntarget_sources\(\$\{CMAKE_PROJECT_NAME\}\s+PRIVATE\s*\n.*?\)'
|
||||
# 使用原始字符串避免转义问题,并使用更精确的正则表达式
|
||||
pattern = r'# Add sources to executable\s*\ntarget_sources\(\$\{CMAKE_PROJECT_NAME\}\s+PRIVATE\s*\n(?:.*?\n)*?\)'
|
||||
|
||||
if re.search(pattern, content, re.DOTALL):
|
||||
new_content = re.sub(pattern, sources_section, content, flags=re.DOTALL)
|
||||
|
||||
# 写回文件
|
||||
with open(cmake_file, 'w', encoding='utf-8') as f:
|
||||
f.write(new_content)
|
||||
|
||||
print("✅ 成功更新CMakeLists.txt中的源文件列表")
|
||||
return True
|
||||
else:
|
||||
print("❌ 错误: 在CMakeLists.txt中找不到target_sources部分")
|
||||
try:
|
||||
if re.search(pattern, content, re.DOTALL | re.MULTILINE):
|
||||
new_content = re.sub(pattern, sources_section, content, flags=re.DOTALL | re.MULTILINE)
|
||||
|
||||
# 写回文件
|
||||
with open(cmake_file, 'w', encoding='utf-8') as f:
|
||||
f.write(new_content)
|
||||
|
||||
print("✅ 成功更新CMakeLists.txt中的源文件列表")
|
||||
return True
|
||||
else:
|
||||
print("❌ 错误: 在CMakeLists.txt中找不到target_sources部分")
|
||||
return False
|
||||
except re.error as e:
|
||||
print(f"❌ 正则表达式错误: {e}")
|
||||
return False
|
||||
|
||||
def update_cmake_includes(cmake_file, user_dir):
|
||||
@ -89,16 +96,20 @@ def update_cmake_includes(cmake_file, user_dir):
|
||||
")"
|
||||
)
|
||||
|
||||
# 正则匹配并替换include部分
|
||||
pattern = r'# Add include paths\s*\ntarget_include_directories\(\$\{CMAKE_PROJECT_NAME\}\s+PRIVATE\s*\n.*?\)'
|
||||
if re.search(pattern, content, re.DOTALL):
|
||||
new_content = re.sub(pattern, include_section, content, flags=re.DOTALL)
|
||||
with open(cmake_file, 'w', encoding='utf-8') as f:
|
||||
f.write(new_content)
|
||||
print("✅ 成功更新CMakeLists.txt中的include路径")
|
||||
return True
|
||||
else:
|
||||
print("❌ 错误: 在CMakeLists.txt中找不到target_include_directories部分")
|
||||
# 使用更安全的正则表达式匹配include部分
|
||||
pattern = r'# Add include paths\s*\ntarget_include_directories\(\$\{CMAKE_PROJECT_NAME\}\s+PRIVATE\s*\n(?:.*?\n)*?\)'
|
||||
try:
|
||||
if re.search(pattern, content, re.DOTALL | re.MULTILINE):
|
||||
new_content = re.sub(pattern, include_section, content, flags=re.DOTALL | re.MULTILINE)
|
||||
with open(cmake_file, 'w', encoding='utf-8') as f:
|
||||
f.write(new_content)
|
||||
print("✅ 成功更新CMakeLists.txt中的include路径")
|
||||
return True
|
||||
else:
|
||||
print("❌ 错误: 在CMakeLists.txt中找不到target_include_directories部分")
|
||||
return False
|
||||
except re.error as e:
|
||||
print(f"❌ 正则表达式错误: {e}")
|
||||
return False
|
||||
|
||||
def main():
|
||||
|
||||
Loading…
Reference in New Issue
Block a user