|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +AssertVersion 版本更新工具模块 |
| 4 | +
|
| 5 | +提供更新 Lua 文件中 AssertVersion 调用版本号的功能 |
| 6 | +""" |
| 7 | + |
| 8 | +import os |
| 9 | +import re |
| 10 | +from typing import List, Tuple |
| 11 | + |
| 12 | +import plib.git as git |
| 13 | +from plib.semver import Semver, satisfies as semver_satisfies |
| 14 | + |
| 15 | + |
| 16 | +def should_update_version_constraint( |
| 17 | + constraint: str, new_version: str, force_update: bool = False |
| 18 | +) -> Tuple[bool, str]: |
| 19 | + """ |
| 20 | + 判断版本约束是否需要更新 |
| 21 | +
|
| 22 | + 参数: |
| 23 | + constraint: 当前版本约束 |
| 24 | + new_version: 新版本号 |
| 25 | + force_update: 是否强制更新(忽略约束检查) |
| 26 | +
|
| 27 | + 返回: |
| 28 | + tuple: (是否需要更新, 新的约束字符串) |
| 29 | + """ |
| 30 | + constraint = constraint.strip().strip("'\"") |
| 31 | + |
| 32 | + # 不更新通配符版本要求,即使在强制更新模式下 |
| 33 | + if constraint == "*": |
| 34 | + return False, constraint |
| 35 | + |
| 36 | + if force_update: |
| 37 | + # 强制更新模式,一律更新为 ^ 格式 |
| 38 | + new_constraint = f"^{new_version}" |
| 39 | + return True, new_constraint |
| 40 | + |
| 41 | + try: |
| 42 | + # 使用我们的 semver 模块检查新版本是否满足约束 |
| 43 | + satisfies = semver_satisfies(new_version, constraint) |
| 44 | + |
| 45 | + if not satisfies: |
| 46 | + # 需要更新约束,统一使用 ^ 格式 |
| 47 | + new_constraint = f"^{new_version}" |
| 48 | + return True, new_constraint |
| 49 | + |
| 50 | + return False, constraint |
| 51 | + |
| 52 | + except Exception as e: |
| 53 | + print(f"Warning: Cannot process version constraint '{constraint}': {e}") |
| 54 | + return False, constraint |
| 55 | + |
| 56 | + |
| 57 | +def find_lua_files(directory: str) -> List[str]: |
| 58 | + """ |
| 59 | + 递归查找所有Lua文件 |
| 60 | +
|
| 61 | + 参数: |
| 62 | + directory: 要搜索的目录 |
| 63 | +
|
| 64 | + 返回: |
| 65 | + Lua文件路径列表 |
| 66 | + """ |
| 67 | + lua_files = [] |
| 68 | + for root, dirs, files in os.walk(directory): |
| 69 | + for file in files: |
| 70 | + if file.endswith(".lua"): |
| 71 | + lua_files.append(os.path.join(root, file)) |
| 72 | + return lua_files |
| 73 | + |
| 74 | + |
| 75 | +def update_assert_version_in_file( |
| 76 | + file_path: str, new_version: str, force_update: bool = False |
| 77 | +) -> Tuple[bool, int]: |
| 78 | + """ |
| 79 | + 更新单个文件中的AssertVersion调用 |
| 80 | +
|
| 81 | + 参数: |
| 82 | + file_path: 要更新的文件路径 |
| 83 | + new_version: 新版本号 |
| 84 | + force_update: 是否强制更新(忽略约束检查) |
| 85 | +
|
| 86 | + 返回: |
| 87 | + tuple: (是否有更新, 更新的数量) |
| 88 | + """ |
| 89 | + # 检测文件编码 |
| 90 | + encoding_used = "gbk" # 默认使用 gbk |
| 91 | + try: |
| 92 | + with open(file_path, "r", encoding="gbk", newline="") as f: |
| 93 | + content = f.read() |
| 94 | + except UnicodeDecodeError: |
| 95 | + encoding_used = "utf-8" |
| 96 | + try: |
| 97 | + with open(file_path, "r", encoding="utf-8", newline="") as f: |
| 98 | + content = f.read() |
| 99 | + except Exception as e: |
| 100 | + print(f"Warning: Cannot read file {file_path}: {e}") |
| 101 | + return False, 0 |
| 102 | + except Exception as e: |
| 103 | + print(f"Warning: Cannot read file {file_path}: {e}") |
| 104 | + return False, 0 |
| 105 | + |
| 106 | + # 匹配 AssertVersion 调用的正则表达式 |
| 107 | + # 匹配类似:X.AssertVersion(MODULE_NAME, _L[MODULE_NAME], '^27.0.0') |
| 108 | + pattern = r'(\w+\.AssertVersion\s*\([^,]+,\s*[^,]+,\s*["\'])([^"\']+)(["\'])' |
| 109 | + |
| 110 | + updated_content = content |
| 111 | + update_count = 0 |
| 112 | + |
| 113 | + def replace_version(match): |
| 114 | + nonlocal update_count |
| 115 | + prefix = match.group(1) |
| 116 | + version_constraint = match.group(2) |
| 117 | + suffix = match.group(3) |
| 118 | + |
| 119 | + should_update, new_constraint = should_update_version_constraint( |
| 120 | + version_constraint, new_version, force_update |
| 121 | + ) |
| 122 | + |
| 123 | + if should_update: |
| 124 | + update_count += 1 |
| 125 | + print(f" {file_path}: {version_constraint} -> {new_constraint}") |
| 126 | + return f"{prefix}{new_constraint}{suffix}" |
| 127 | + |
| 128 | + return match.group(0) |
| 129 | + |
| 130 | + updated_content = re.sub(pattern, replace_version, updated_content) |
| 131 | + |
| 132 | + if update_count > 0: |
| 133 | + try: |
| 134 | + # 使用相同的编码和 newline="" 参数写回文件,保持原始行尾符 |
| 135 | + with open(file_path, "w", encoding=encoding_used, newline="") as f: |
| 136 | + f.write(updated_content) |
| 137 | + return True, update_count |
| 138 | + except Exception as e: |
| 139 | + print(f"Error writing file {file_path}: {e}") |
| 140 | + return False, 0 |
| 141 | + |
| 142 | + return False, 0 |
| 143 | + |
| 144 | + |
| 145 | +def update_assert_version( |
| 146 | + new_version: str, |
| 147 | + diff_ver: str = None, |
| 148 | + force_changed: bool = False, |
| 149 | + scan_all_if_no_changes: bool = True, |
| 150 | +) -> Tuple[int, int]: |
| 151 | + """ |
| 152 | + 更新 Lua 文件中的 AssertVersion 调用到指定版本 |
| 153 | +
|
| 154 | + 参数: |
| 155 | + new_version: 新版本号 |
| 156 | + diff_ver: 指定对比版本(可选) |
| 157 | + force_changed: 是否强制更新(忽略约束检查) |
| 158 | + scan_all_if_no_changes: 当没有变更时是否扫描所有文件 |
| 159 | +
|
| 160 | + 返回: |
| 161 | + tuple: (总更新文件数, 总更新次数) |
| 162 | + """ |
| 163 | + # 验证新版本格式 |
| 164 | + try: |
| 165 | + Semver(new_version) |
| 166 | + except Exception: |
| 167 | + print(f"Error: Invalid version format: {new_version}") |
| 168 | + return 0, 0 |
| 169 | + |
| 170 | + # 获取当前目录 |
| 171 | + current_dir = os.getcwd() |
| 172 | + print(f"Scanning Lua files in: {current_dir}") |
| 173 | + print(f"Target version: {new_version}") |
| 174 | + |
| 175 | + # 如果指定了 diff 参数或 force_changed,使用 git.get_version_info 获取版本信息 |
| 176 | + if diff_ver is not None or force_changed: |
| 177 | + print("Getting version information...") |
| 178 | + |
| 179 | + # 使用 git.get_version_info 获取版本信息 |
| 180 | + version_info = git.get_version_info(diff_ver=diff_ver) |
| 181 | + |
| 182 | + base_hash = version_info.get("previous_hash", "") |
| 183 | + changed_folders = version_info.get("changed_addon_folders", []) |
| 184 | + |
| 185 | + if base_hash: |
| 186 | + print(f"Base version: {version_info.get('previous', '')} ({base_hash})") |
| 187 | + |
| 188 | + if changed_folders: |
| 189 | + print(f"Changed addon folders: {', '.join(changed_folders)}") |
| 190 | + # 只处理变更的子插件文件夹中的 Lua 文件 |
| 191 | + lua_files = [] |
| 192 | + for folder in changed_folders: |
| 193 | + folder_lua_files = find_lua_files(folder) |
| 194 | + lua_files.extend(folder_lua_files) |
| 195 | + |
| 196 | + # 使用传入的 force_changed 参数 |
| 197 | + force_update = force_changed |
| 198 | + else: |
| 199 | + print("No changed addon folders found") |
| 200 | + if scan_all_if_no_changes: |
| 201 | + # 没有变更时,扫描所有 Lua 文件 |
| 202 | + lua_files = find_lua_files(current_dir) |
| 203 | + force_update = False |
| 204 | + else: |
| 205 | + # 没有变更时,不处理任何文件 |
| 206 | + lua_files = [] |
| 207 | + force_update = False |
| 208 | + else: |
| 209 | + # 没有指定 diff 参数,扫描所有 Lua 文件 |
| 210 | + lua_files = find_lua_files(current_dir) |
| 211 | + force_update = False |
| 212 | + |
| 213 | + if not lua_files: |
| 214 | + print("No Lua files to process") |
| 215 | + return 0, 0 |
| 216 | + |
| 217 | + print(f"Found {len(lua_files)} Lua files") |
| 218 | + print() |
| 219 | + |
| 220 | + total_files_updated = 0 |
| 221 | + total_updates = 0 |
| 222 | + |
| 223 | + # 处理每个文件 |
| 224 | + for file_path in lua_files: |
| 225 | + file_updated, update_count = update_assert_version_in_file( |
| 226 | + file_path, new_version, force_update |
| 227 | + ) |
| 228 | + if file_updated: |
| 229 | + total_files_updated += 1 |
| 230 | + total_updates += update_count |
| 231 | + |
| 232 | + print() |
| 233 | + print("AssertVersion Update Summary:") |
| 234 | + print(f" Files updated: {total_files_updated}") |
| 235 | + print(f" Total updates: {total_updates}") |
| 236 | + |
| 237 | + if total_updates > 0: |
| 238 | + print( |
| 239 | + f"Successfully updated {total_updates} AssertVersion calls in {total_files_updated} files" |
| 240 | + ) |
| 241 | + else: |
| 242 | + print("No AssertVersion calls needed to be updated") |
| 243 | + |
| 244 | + return total_files_updated, total_updates |
| 245 | + |
| 246 | + |
| 247 | +def update_assert_version_for_changed_addons( |
| 248 | + new_version: str, diff_ver: str = None, force_changed: bool = True |
| 249 | +) -> Tuple[int, int]: |
| 250 | + """ |
| 251 | + 更新变更子插件中的 AssertVersion 调用到指定版本 |
| 252 | +
|
| 253 | + 参数: |
| 254 | + new_version: 新版本号 |
| 255 | + diff_ver: 指定对比版本(可选) |
| 256 | + force_changed: 是否强制更新变更的子插件(默认为 True) |
| 257 | +
|
| 258 | + 返回: |
| 259 | + tuple: (总更新文件数, 总更新次数) |
| 260 | + """ |
| 261 | + return update_assert_version( |
| 262 | + new_version, |
| 263 | + diff_ver=diff_ver, |
| 264 | + force_changed=force_changed, |
| 265 | + scan_all_if_no_changes=False, |
| 266 | + ) |
0 commit comments