|
9 | 9 | import re |
10 | 10 | from typing import List, Tuple |
11 | 11 |
|
12 | | -import plib.utils as utils |
13 | 12 | import plib.git as git |
14 | 13 | from plib.semver import Semver, satisfies as semver_satisfies |
15 | 14 |
|
@@ -87,9 +86,19 @@ def update_assert_version_in_file( |
87 | 86 | 返回: |
88 | 87 | tuple: (是否有更新, 更新的数量) |
89 | 88 | """ |
90 | | - # 使用 utils.read_file 读取文件,自动处理编码问题 |
| 89 | + # 检测文件编码 |
| 90 | + encoding_used = "gbk" # 默认使用 gbk |
91 | 91 | try: |
92 | | - content = utils.read_file(file_path) |
| 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 |
93 | 102 | except Exception as e: |
94 | 103 | print(f"Warning: Cannot read file {file_path}: {e}") |
95 | 104 | return False, 0 |
@@ -122,14 +131,8 @@ def replace_version(match): |
122 | 131 |
|
123 | 132 | if update_count > 0: |
124 | 133 | try: |
125 | | - # 自动检测编码写回文件 |
126 | | - encoding_used = "gbk" # 默认使用 gbk |
127 | | - try: |
128 | | - content.encode("gbk") |
129 | | - except UnicodeEncodeError: |
130 | | - encoding_used = "utf-8" |
131 | | - |
132 | | - with open(file_path, "w", encoding=encoding_used) as f: |
| 134 | + # 使用相同的编码和 newline="" 参数写回文件,保持原始行尾符 |
| 135 | + with open(file_path, "w", encoding=encoding_used, newline="") as f: |
133 | 136 | f.write(updated_content) |
134 | 137 | return True, update_count |
135 | 138 | except Exception as e: |
|
0 commit comments