更新win

This commit is contained in:
2025-10-03 22:10:51 +08:00
parent 0e57cd6cd7
commit a42e2e3832
3 changed files with 68 additions and 32 deletions

View File

@@ -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():