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 app.tools.update_cmake_sources import find_user_c_files, update_cmake_sources,update_cmake_includes
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
# 构建User目录和CMakeLists.txt路径
|
# 构建User目录和CMakeLists.txt路径,规范化路径分隔符
|
||||||
user_dir = os.path.join(self.project_path, "User")
|
user_dir = os.path.normpath(os.path.join(self.project_path, "User"))
|
||||||
cmake_file = os.path.join(self.project_path, "CMakeLists.txt")
|
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目录是否存在
|
# 检查User目录是否存在
|
||||||
if not os.path.exists(user_dir):
|
if not os.path.exists(user_dir):
|
||||||
@ -191,7 +195,9 @@ class CodeGenerateInterface(QWidget):
|
|||||||
return
|
return
|
||||||
|
|
||||||
# 查找User目录下的所有.c文件
|
# 查找User目录下的所有.c文件
|
||||||
|
print("开始查找.c文件...")
|
||||||
c_files = find_user_c_files(user_dir)
|
c_files = find_user_c_files(user_dir)
|
||||||
|
print(f"找到 {len(c_files)} 个.c文件")
|
||||||
|
|
||||||
if not c_files:
|
if not c_files:
|
||||||
InfoBar.warning(
|
InfoBar.warning(
|
||||||
@ -203,16 +209,31 @@ class CodeGenerateInterface(QWidget):
|
|||||||
return
|
return
|
||||||
|
|
||||||
# 更新CMakeLists.txt
|
# 更新CMakeLists.txt
|
||||||
success = update_cmake_sources(cmake_file, c_files)
|
print("开始更新CMakeLists.txt...")
|
||||||
success = update_cmake_includes(cmake_file, user_dir)
|
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(
|
InfoBar.success(
|
||||||
title="配置成功",
|
title="配置成功",
|
||||||
content=f"已成功更新CMakeLists.txt,共添加了 {len(c_files)} 个源文件",
|
content=f"已成功更新CMakeLists.txt,共添加了 {len(c_files)} 个源文件",
|
||||||
parent=self,
|
parent=self,
|
||||||
duration=3000
|
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:
|
else:
|
||||||
InfoBar.error(
|
InfoBar.error(
|
||||||
title="配置失败",
|
title="配置失败",
|
||||||
@ -222,6 +243,7 @@ class CodeGenerateInterface(QWidget):
|
|||||||
)
|
)
|
||||||
|
|
||||||
except ImportError as e:
|
except ImportError as e:
|
||||||
|
print(f"导入错误: {e}")
|
||||||
InfoBar.error(
|
InfoBar.error(
|
||||||
title="导入错误",
|
title="导入错误",
|
||||||
content=f"无法导入cmake配置模块: {str(e)}",
|
content=f"无法导入cmake配置模块: {str(e)}",
|
||||||
@ -229,6 +251,9 @@ class CodeGenerateInterface(QWidget):
|
|||||||
duration=3000
|
duration=3000
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
|
print(f"cmake配置错误: {e}")
|
||||||
|
import traceback
|
||||||
|
traceback.print_exc()
|
||||||
InfoBar.error(
|
InfoBar.error(
|
||||||
title="配置失败",
|
title="配置失败",
|
||||||
content=f"cmake配置过程中出现错误: {str(e)}",
|
content=f"cmake配置过程中出现错误: {str(e)}",
|
||||||
|
|||||||
@ -21,7 +21,8 @@ def find_user_c_files(user_dir):
|
|||||||
for c_file in user_path.rglob("*.c"):
|
for c_file in user_path.rglob("*.c"):
|
||||||
# 获取相对于项目根目录的路径
|
# 获取相对于项目根目录的路径
|
||||||
relative_path = c_file.relative_to(user_path.parent)
|
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()
|
c_files.sort()
|
||||||
@ -44,31 +45,37 @@ def update_cmake_sources(cmake_file, c_files):
|
|||||||
# 按目录分组
|
# 按目录分组
|
||||||
current_dir = ""
|
current_dir = ""
|
||||||
for c_file in c_files:
|
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 file_dir != current_dir:
|
||||||
if current_dir: # 不是第一个目录,添加空行
|
if current_dir: # 不是第一个目录,添加空行
|
||||||
sources_section += "\n"
|
sources_section += "\n"
|
||||||
sources_section += f" # {file_dir} sources\n"
|
sources_section += f" # {file_dir} sources\n"
|
||||||
current_dir = file_dir
|
current_dir = file_dir
|
||||||
|
|
||||||
sources_section += f" {c_file}\n"
|
sources_section += f" {normalized_file}\n"
|
||||||
|
|
||||||
sources_section += ")"
|
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):
|
try:
|
||||||
new_content = re.sub(pattern, sources_section, content, flags=re.DOTALL)
|
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)
|
with open(cmake_file, 'w', encoding='utf-8') as f:
|
||||||
|
f.write(new_content)
|
||||||
print("✅ 成功更新CMakeLists.txt中的源文件列表")
|
|
||||||
return True
|
print("✅ 成功更新CMakeLists.txt中的源文件列表")
|
||||||
else:
|
return True
|
||||||
print("❌ 错误: 在CMakeLists.txt中找不到target_sources部分")
|
else:
|
||||||
|
print("❌ 错误: 在CMakeLists.txt中找不到target_sources部分")
|
||||||
|
return False
|
||||||
|
except re.error as e:
|
||||||
|
print(f"❌ 正则表达式错误: {e}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def update_cmake_includes(cmake_file, user_dir):
|
def update_cmake_includes(cmake_file, user_dir):
|
||||||
@ -89,16 +96,20 @@ def update_cmake_includes(cmake_file, user_dir):
|
|||||||
")"
|
")"
|
||||||
)
|
)
|
||||||
|
|
||||||
# 正则匹配并替换include部分
|
# 使用更安全的正则表达式匹配include部分
|
||||||
pattern = r'# Add include paths\s*\ntarget_include_directories\(\$\{CMAKE_PROJECT_NAME\}\s+PRIVATE\s*\n.*?\)'
|
pattern = r'# Add include paths\s*\ntarget_include_directories\(\$\{CMAKE_PROJECT_NAME\}\s+PRIVATE\s*\n(?:.*?\n)*?\)'
|
||||||
if re.search(pattern, content, re.DOTALL):
|
try:
|
||||||
new_content = re.sub(pattern, include_section, content, flags=re.DOTALL)
|
if re.search(pattern, content, re.DOTALL | re.MULTILINE):
|
||||||
with open(cmake_file, 'w', encoding='utf-8') as f:
|
new_content = re.sub(pattern, include_section, content, flags=re.DOTALL | re.MULTILINE)
|
||||||
f.write(new_content)
|
with open(cmake_file, 'w', encoding='utf-8') as f:
|
||||||
print("✅ 成功更新CMakeLists.txt中的include路径")
|
f.write(new_content)
|
||||||
return True
|
print("✅ 成功更新CMakeLists.txt中的include路径")
|
||||||
else:
|
return True
|
||||||
print("❌ 错误: 在CMakeLists.txt中找不到target_include_directories部分")
|
else:
|
||||||
|
print("❌ 错误: 在CMakeLists.txt中找不到target_include_directories部分")
|
||||||
|
return False
|
||||||
|
except re.error as e:
|
||||||
|
print(f"❌ 正则表达式错误: {e}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user