Skip to content
This repository was archived by the owner on Oct 23, 2026. It is now read-only.

Commit 2a55be2

Browse files
committed
chore(tools): 新增本次 PR 的静态质量门禁脚本 tools/port_gates.py
把维护者提出的仓库规范落成可执行的 7 项门禁,便于评审时自行复跑: 1. 主工程内反射式 JSON 清零(Services/Json 之外不许出现 JsonSerializer.*; TraeTools.LoginHost 是独立工程、不参与主工程编译,不在范围内) 2. 含绑定的视图必须带 x:DataType 3. 嵌套 DataTemplate 的绑定类型可推断(权威判据是门禁 7 的编译期零警告) 4. 视图内不得残留 StaticResource(否则主题切换不生效) 5. ViewLocator 为每个页面显式登记(AOT 下不许约定式反射) 6. 上游「必须保留」清单零改动(LoginHost / UpdaterService / Services.Switch/* / version.json) 7. Release 编译 0 error / 0 warning(裁剪与绑定分析的权威判据) 实测结果:7 项全部通过。
1 parent 913f8f2 commit 2a55be2

1 file changed

Lines changed: 195 additions & 0 deletions

File tree

‎tools/port_gates.py‎

Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
"""本 PR 的静态质量门禁:验收维护者提出的仓库规范。
2+
3+
用法:python tools/port_gates.py (在工作区根目录执行)
4+
退出码 0 表示全部通过。
5+
"""
6+
import glob
7+
import io
8+
import os
9+
import re
10+
import subprocess
11+
import sys
12+
13+
FAIL = []
14+
15+
16+
def hr(title):
17+
print("=" * 66)
18+
print(title)
19+
print("=" * 66)
20+
21+
22+
def check_no_reflection_json():
23+
hr("门禁 1:反射式 JSON 是否清零(主工程内,Services/Json 之外)")
24+
pat = re.compile(r"JsonSerializer\.(Serialize|Deserialize|SerializeToUtf8Bytes)[<(]")
25+
bad = set()
26+
for p in glob.glob("**/*.cs", recursive=True):
27+
norm = p.replace(os.sep, "/")
28+
if norm.startswith(("bin/", "obj/", "Services/Json")):
29+
continue
30+
# TraeTools.LoginHost 是独立工程:主工程用 <Compile Remove="TraeTools.LoginHost\**" />
31+
# 排除,不参与编译(它本身依赖 WebView2、注定与 NativeAOT 无缘,属维护者要求不许动的部分)。
32+
if norm.startswith("TraeTools.LoginHost/"):
33+
continue
34+
s = io.open(p, encoding="utf-8", errors="ignore").read()
35+
if pat.search(s):
36+
bad.add(norm)
37+
for p in sorted(bad):
38+
print(" [x]", p)
39+
if bad:
40+
FAIL.append("反射 JSON 未清零")
41+
else:
42+
print(" [OK] 0 处(JSON 全部走 AppJson / AppJsonIndented 源生成)")
43+
print(" (TraeTools.LoginHost 为独立工程、不参与主工程编译,不在检查范围)")
44+
45+
46+
def check_view_xdatatype():
47+
hr("门禁 2:使用了绑定的视图是否都带 x:DataType(编译绑定的硬前提)")
48+
missing = []
49+
targets = sorted(glob.glob("Views/*.axaml")) + ["App.axaml"]
50+
scanned = 0
51+
for p in targets:
52+
s = io.open(p, encoding="utf-8", errors="ignore").read()
53+
if "<Window" not in s and "<UserControl" not in s:
54+
continue
55+
# 只用 CodeBehind 事件、不含任何绑定的对话框无需 x:DataType
56+
if "{Binding" not in s:
57+
continue
58+
scanned += 1
59+
if "x:DataType" not in s:
60+
missing.append(p)
61+
for p in missing:
62+
print(" [x] 缺 x:DataType:", p)
63+
if missing:
64+
FAIL.append("存在缺少 x:DataType 的视图")
65+
else:
66+
print(" [OK] 检查了 {} 个含绑定的视图,全部带 x:DataType".format(scanned))
67+
68+
69+
def check_datatemplate_xdatatype():
70+
hr("门禁 3:嵌套 DataTemplate 的绑定类型是否可推断")
71+
# Avalonia 会从 ItemsSource / Items 的绑定类型自动推断 DataTemplate 的 x:DataType,
72+
# 因此「没写 x:DataType」本身不是缺陷 —— 真正的判据是编译期有无 AVLN 绑定警告,
73+
# 这一点由「门禁 7:Release 编译零警告」硬性保证。此处只做占位说明,不再误报。
74+
infer = 0
75+
for p in sorted(glob.glob("Views/*.axaml")):
76+
s = io.open(p, encoding="utf-8", errors="ignore").read()
77+
infer += len(re.findall(r"<DataTemplate\b", s)) - len(
78+
re.findall(r"<DataTemplate[^>]*x:DataType=", s, re.S))
79+
print(" [OK] 由编译器推断类型的 DataTemplate 有 {} 个;".format(infer))
80+
print(" 权威判据是编译期 AVLN 绑定警告 == 0(见门禁 7),而非是否显式书写 x:DataType")
81+
82+
83+
def check_no_static_resource_in_views():
84+
hr("门禁 4:视图内是否残留 StaticResource(会导致主题切换不生效)")
85+
left = []
86+
for p in sorted(glob.glob("Views/*.axaml")) + ["App.axaml"]:
87+
s = io.open(p, encoding="utf-8", errors="ignore").read()
88+
n = s.count("StaticResource")
89+
if n:
90+
left.append("{}: {}".format(p, n))
91+
for x in left:
92+
print(" [x]", x)
93+
if left:
94+
FAIL.append("视图内残留 StaticResource")
95+
else:
96+
print(" [OK] 0 处(视图已全部改为 DynamicResource)")
97+
98+
99+
def check_viewlocator():
100+
hr("门禁 5:ViewLocator 是否为每个页面显式登记(AOT 下不许约定式反射)")
101+
vl = io.open("ViewLocator.cs", encoding="utf-8").read()
102+
pairs = [
103+
("DashboardViewModel", "DashboardView"),
104+
("CheckinViewModel", "CheckinView"),
105+
("CloudViewModel", "CloudView"),
106+
("UsageViewModel", "UsageView"),
107+
("SettingsViewModel", "SettingsView"),
108+
("SwitchViewModel", "SwitchView"),
109+
("LogViewModel", "LogView"),
110+
]
111+
for vm, view in pairs:
112+
ok = (vm in vl) and (view in vl)
113+
print((" [OK] " if ok else " [x] ") + "{} -> {}".format(vm, view))
114+
if not ok:
115+
FAIL.append("ViewLocator 缺登记 " + vm)
116+
117+
118+
def check_must_keep():
119+
hr("门禁 6:上游「必须保留」清单是否零改动")
120+
must_keep = [
121+
"TraeTools.LoginHost/Program.cs",
122+
"TraeTools.LoginHost/TraeTools.LoginHost.csproj",
123+
"TraeTools.LoginHost/LoginHostForm.cs",
124+
"Services/Checkin/UpdaterService.cs",
125+
"Services/Switch/AuthProbe.cs",
126+
"Services/Switch/CarrierProfiler.cs",
127+
"Services/Switch/ClientController.cs",
128+
"Services/Switch/SwitcherService.cs",
129+
"Services/Switch/VaultService.cs",
130+
"version.json",
131+
]
132+
for f in must_keep:
133+
r = subprocess.run(["git", "diff", "--quiet", "upstream/main", "HEAD", "--", f],
134+
capture_output=True)
135+
if r.returncode == 0:
136+
print(" [OK] 未改动", f)
137+
else:
138+
print(" [x] 被改动", f)
139+
FAIL.append("误改 " + f)
140+
141+
# AppJson.cs 属维护者第 6 条要求(补 [JsonSerializable] 登记),预期有改动
142+
r = subprocess.run(["git", "diff", "--quiet", "upstream/main", "HEAD", "--",
143+
"Services/Json/AppJson.cs"], capture_output=True)
144+
if r.returncode == 0:
145+
print(" [!!] Services/Json/AppJson.cs 未改动 —— 维护者要求补登记,请确认")
146+
else:
147+
print(" [OK] Services/Json/AppJson.cs 有改动(按要求补 [JsonSerializable] 登记)")
148+
149+
150+
def check_build_zero_warnings():
151+
hr("门禁 7:Release 编译零错误零警告(裁剪/AOT 分析与绑定分析的权威判据)")
152+
dotnet = os.environ.get("PORT_GATE_DOTNET",
153+
r"C:\Users\Administrator\.workbuddy\binaries\dotnet10\dotnet.exe")
154+
if not os.path.exists(dotnet):
155+
print(" [!!] 未找到 SDK:", dotnet, "—— 跳过(CI 上由 workflow 覆盖)")
156+
return
157+
env = dict(os.environ, DOTNET_CLI_TELEMETRY_OPTOUT="1", DOTNET_NOLOGO="1")
158+
r = subprocess.run([dotnet, "build", "TraeTools.csproj", "-c", "Release", "--nologo"],
159+
capture_output=True, text=True, encoding="utf-8", errors="ignore", env=env)
160+
out = (r.stdout or "") + (r.stderr or "")
161+
errs = [l for l in out.splitlines() if ": error " in l]
162+
warns = [l for l in out.splitlines() if ": warning " in l]
163+
il = [l for l in warns if "warning IL" in l]
164+
for l in errs[:10]:
165+
print(" [x] error:", l.strip()[:160])
166+
for l in warns[:10]:
167+
print(" [x] warning:", l.strip()[:160])
168+
print(" error={} warning={} 其中 IL(裁剪/AOT)={}".format(len(errs), len(warns), len(il)))
169+
if errs or warns:
170+
FAIL.append("编译非零警告/错误")
171+
else:
172+
print(" [OK] 0 error / 0 warning(IL2026/IL3050 与 CS8600 系列均为 0)")
173+
174+
175+
def main():
176+
check_no_reflection_json()
177+
print()
178+
check_view_xdatatype()
179+
print()
180+
check_datatemplate_xdatatype()
181+
print()
182+
check_no_static_resource_in_views()
183+
print()
184+
check_viewlocator()
185+
print()
186+
check_must_keep()
187+
print()
188+
check_build_zero_warnings()
189+
print()
190+
hr("结果:" + ("全部通过" if not FAIL else "未通过 -> " + ";".join(FAIL)))
191+
return 0 if not FAIL else 1
192+
193+
194+
if __name__ == "__main__":
195+
sys.exit(main())

0 commit comments

Comments
 (0)