Skip to content

Commit ce922e9

Browse files
committed
build: 修复版本依赖更新导致行末改变的问题
1 parent 300cfcf commit ce922e9

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

!src-dist/plib/assert_version.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import re
1010
from typing import List, Tuple
1111

12-
import plib.utils as utils
1312
import plib.git as git
1413
from plib.semver import Semver, satisfies as semver_satisfies
1514

@@ -87,9 +86,19 @@ def update_assert_version_in_file(
8786
返回:
8887
tuple: (是否有更新, 更新的数量)
8988
"""
90-
# 使用 utils.read_file 读取文件,自动处理编码问题
89+
# 检测文件编码
90+
encoding_used = "gbk" # 默认使用 gbk
9191
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
93102
except Exception as e:
94103
print(f"Warning: Cannot read file {file_path}: {e}")
95104
return False, 0
@@ -122,14 +131,8 @@ def replace_version(match):
122131

123132
if update_count > 0:
124133
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:
133136
f.write(updated_content)
134137
return True, update_count
135138
except Exception as e:

0 commit comments

Comments
 (0)