dsh-stm32 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (82) hide show
  1. package/README.md +127 -29
  2. package/README_EN.md +125 -29
  3. package/client/client.js +682 -0
  4. package/docs/development-notes.md +1385 -5
  5. package/docs/logic-audit.md +607 -0
  6. package/docs/reading-guide.md +738 -0
  7. package/lib/client.js +682 -0
  8. package/lib/commands/register.js +214 -12
  9. package/lib/data/chips.js +134 -4
  10. package/lib/data/clock-f1.js +158 -0
  11. package/lib/data/cmsis-sources.js +316 -0
  12. package/lib/data/f1-ram.generated.js +34 -0
  13. package/lib/data/firmware-packages.js +352 -0
  14. package/lib/data/install-guides.js +190 -0
  15. package/lib/data/search-roots.js +135 -15
  16. package/lib/data/stm32f1.js +186 -0
  17. package/lib/data/toolchain-config.js +310 -0
  18. package/lib/engine/build.js +121 -16
  19. package/lib/engine/cache.js +336 -0
  20. package/lib/engine/clock-plan.js +608 -0
  21. package/lib/engine/cmake-sources.js +428 -0
  22. package/lib/engine/code-patch.js +18 -6
  23. package/lib/engine/debug-session.js +163 -4
  24. package/lib/engine/debug.js +84 -19
  25. package/lib/engine/doctor.js +395 -4
  26. package/lib/engine/fetch.js +287 -0
  27. package/lib/engine/firmware-install.js +668 -0
  28. package/lib/engine/flash.js +145 -15
  29. package/lib/engine/fs-longpath.js +107 -0
  30. package/lib/engine/generate-cache.js +118 -0
  31. package/lib/engine/generate-script.js +30 -8
  32. package/lib/engine/generate.js +296 -14
  33. package/lib/engine/hal-chip.js +230 -0
  34. package/lib/engine/ioc-plan.js +47 -8
  35. package/lib/engine/msys2-install.js +1870 -0
  36. package/lib/engine/probe.js +57 -6
  37. package/lib/engine/project-export.js +129 -0
  38. package/lib/engine/register-clock.js +132 -0
  39. package/lib/engine/register-linker.js +129 -0
  40. package/lib/engine/register-template.js +449 -0
  41. package/lib/engine/regs.js +18 -4
  42. package/lib/engine/route-plan.js +553 -0
  43. package/lib/engine/scaffold.js +303 -72
  44. package/lib/engine/subprocess-runner.js +2 -0
  45. package/lib/engine/tar.js +250 -0
  46. package/lib/engine/text-file.js +150 -0
  47. package/lib/engine/thirdparty.js +243 -0
  48. package/lib/engine/tool-env.js +51 -0
  49. package/lib/engine/toolchain-gate.js +386 -0
  50. package/lib/engine/vscode-config.js +99 -0
  51. package/lib/engine/web-api.js +375 -0
  52. package/lib/engine/web-routes.js +100 -0
  53. package/lib/engine/xcube-catalog.js +102 -0
  54. package/lib/engine/xcube.js +254 -0
  55. package/lib/engine/zip.js +142 -0
  56. package/lib/index.js +44 -2
  57. package/lib/skill/stm32-workflow.js +3 -3
  58. package/lib/tools/build.js +55 -3
  59. package/lib/tools/cache.js +250 -0
  60. package/lib/tools/code-patch.js +29 -2
  61. package/lib/tools/debug-session.js +56 -14
  62. package/lib/tools/debug.js +43 -2
  63. package/lib/tools/doctor.js +16 -3
  64. package/lib/tools/env-install.js +472 -0
  65. package/lib/tools/env-probe.js +6 -2
  66. package/lib/tools/firmware-install.js +255 -0
  67. package/lib/tools/flash.js +41 -2
  68. package/lib/tools/generate.js +96 -10
  69. package/lib/tools/ioc-apply.js +27 -11
  70. package/lib/tools/ioc-inspect.js +4 -2
  71. package/lib/tools/ioc-plan.js +209 -11
  72. package/lib/tools/project-create.js +229 -21
  73. package/lib/tools/project-dir.js +95 -0
  74. package/lib/tools/project-export.js +100 -0
  75. package/lib/tools/regs.js +82 -7
  76. package/lib/tools/route-plan.js +467 -0
  77. package/package.json +11 -4
  78. package/templates/register/CMakeLists.txt +52 -0
  79. package/templates/register/Core/Src/main.c +72 -0
  80. package/templates/scaffold/openocd.cfg +8 -0
  81. package/templates/scaffold/vscode/launch.json +8 -6
  82. package/templates/scaffold/vscode/settings.json +5 -11
@@ -0,0 +1,682 @@
1
+ window.__ModuleLoader__.load({
2
+ id: "dsh-stm32",
3
+ factory: (require) => {
4
+ var module = { exports: {} };
5
+ var exports = module.exports;
6
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
7
+
8
+ /*
9
+ * dsh-stm32 浏览器端设置面板(手写纯 JS,不经 tsc)。
10
+ *
11
+ * 为什么文件第一行就是 load({:宿主客户端模块加载器按这个自执行包装取走并执行本文件,
12
+ * 注释放在包装外面会被当成"包装前有别的语句",与 dsh-client-ui-* 的产物形状不一致。
13
+ *
14
+ * 为什么是纯 JS:本文件由宿主加载器直接执行,不经过插件的 TS 构建链;
15
+ * 写成 TS/JSX 会多出一份由 tsc 决定产物路径的中间层,而面板的正确产物就是
16
+ * client/client.js 原文(scripts/copy-client.mjs 把它原样复制到 lib/client.js)。
17
+ * 所以这里只用手写 React.createElement,且只 require 外壳种子词:
18
+ * react / react/jsx-runtime / primitives。
19
+ *
20
+ * 面板只做三件事:读配置、跑探测、走安装(出计划 → 确认 → 轮询日志)。
21
+ * 数据全部来自 src/engine/web-api.ts 暴露的 /dsh-stm32/api/*;路径按
22
+ * document.baseURI 相对解析(与 web-api 的注释同一口径),宿主把 Web
23
+ * 部署在子路径下也能用。
24
+ */
25
+
26
+ const React = require("react");
27
+ const { useCallback, useEffect, useState } = React;
28
+
29
+ /* ------------------------------ 接口层 ------------------------------ */
30
+
31
+ /** 与 web-api.ts 的 WEB_API_PREFIX 对应,去掉前导 / 以便相对 baseURI 解析。 */
32
+ const API_RELATIVE = "dsh-stm32/api/";
33
+ const POLL_INTERVAL_MS = 1200;
34
+
35
+ function describeError(error) {
36
+ if (error instanceof Error) return error.message;
37
+ return String(error);
38
+ }
39
+
40
+ function apiUrl(path) {
41
+ const base = typeof document === "object" && document !== null && typeof document.baseURI === "string"
42
+ ? document.baseURI
43
+ : "http://127.0.0.1/";
44
+ try {
45
+ return new URL(API_RELATIVE + path, base).toString();
46
+ } catch {
47
+ // 极早的加载时机(document 还没就位):退回根路径,至少不会让面板整个炸掉。
48
+ return "/dsh-stm32/api/" + path;
49
+ }
50
+ }
51
+
52
+ /**
53
+ * 调一次面板接口。返回值刻意统一成 { ok, value } / { ok:false, error }:
54
+ * 面板上每个操作只要关心"成了没成、为什么没成",不用各自解析 HTTP 语义。
55
+ */
56
+ async function callApi(method, path, body) {
57
+ const url = apiUrl(path);
58
+ const init = { method: method, credentials: "same-origin", headers: {} };
59
+ if (body !== undefined) {
60
+ init.headers["content-type"] = "application/json";
61
+ init.body = JSON.stringify(body);
62
+ }
63
+ let response;
64
+ try {
65
+ response = await fetch(url, init);
66
+ } catch (error) {
67
+ return { ok: false, error: "请求接口失败(" + url + "):" + describeError(error) };
68
+ }
69
+ let text = "";
70
+ try {
71
+ text = await response.text();
72
+ } catch (error) {
73
+ return { ok: false, error: "读取接口响应失败:" + describeError(error) };
74
+ }
75
+ let payload;
76
+ try {
77
+ payload = text === "" ? {} : JSON.parse(text);
78
+ } catch {
79
+ return {
80
+ ok: false,
81
+ error: "接口返回的不是 JSON(HTTP " + String(response.status) + "):" + text.slice(0, 200),
82
+ };
83
+ }
84
+ if (payload !== null && typeof payload === "object" && payload.ok === true) return { ok: true, value: payload };
85
+ const message =
86
+ payload !== null && typeof payload === "object" && typeof payload.error === "string" && payload.error !== ""
87
+ ? payload.error
88
+ : "接口返回失败(HTTP " + String(response.status) + ")";
89
+ return { ok: false, error: message };
90
+ }
91
+
92
+ /* ------------------------------ 小工具 ------------------------------ */
93
+
94
+ /** 每行一个目录,空行与重复项丢掉;空数组等于"不覆盖"。 */
95
+ function parseBinDirs(text) {
96
+ const out = [];
97
+ for (const line of String(text ?? "").split(/\r?\n/gu)) {
98
+ const trimmed = line.trim();
99
+ if (trimmed === "") continue;
100
+ if (!out.includes(trimmed)) out.push(trimmed);
101
+ }
102
+ return out;
103
+ }
104
+
105
+ function formatBytes(bytes) {
106
+ const value = typeof bytes === "number" && Number.isFinite(bytes) ? bytes : 0;
107
+ if (value <= 0) return "0 B";
108
+ const mb = value / (1024 * 1024);
109
+ if (mb >= 1) return mb.toFixed(1) + " MB";
110
+ return (value / 1024).toFixed(0) + " KB";
111
+ }
112
+
113
+ function toolSummaryLine(tool) {
114
+ if (tool === null || typeof tool !== "object") return "";
115
+ if (tool.found !== true) return String(tool.hint ?? "");
116
+ const version = typeof tool.version === "string" ? tool.version : "";
117
+ if (version !== "") return version;
118
+ const where = typeof tool.path === "string" ? tool.path : "";
119
+ return where;
120
+ }
121
+
122
+ const CAPABILITIES = [
123
+ ["build", "编译"],
124
+ ["flash", "烧录"],
125
+ ["debug", "调试"],
126
+ ["generate", "生成代码"],
127
+ ];
128
+
129
+ const STEP_ICONS = {
130
+ done: "✔",
131
+ running: "…",
132
+ failed: "✘",
133
+ skipped: "–",
134
+ pending: "·",
135
+ };
136
+
137
+ /* ------------------------------ 样式 ------------------------------ */
138
+
139
+ // 样式全部内联:客户端插件没有外部 CSS 通道(宿主不加载插件的 .css),
140
+ // 颜色尽量跟随 currentColor,深浅主题都不至于看不清。
141
+ const styles = {
142
+ root: {
143
+ display: "flex",
144
+ flexDirection: "column",
145
+ gap: "16px",
146
+ padding: "4px 4px 32px",
147
+ maxWidth: "880px",
148
+ color: "inherit",
149
+ fontSize: "13px",
150
+ lineHeight: "1.55",
151
+ },
152
+ title: { margin: "0", fontSize: "16px", fontWeight: 600 },
153
+ lead: { margin: "0", opacity: 0.72 },
154
+ card: {
155
+ display: "flex",
156
+ flexDirection: "column",
157
+ gap: "10px",
158
+ padding: "14px 16px",
159
+ border: "1px solid color-mix(in srgb, currentColor 18%, transparent)",
160
+ borderRadius: "10px",
161
+ },
162
+ cardHead: { display: "flex", alignItems: "baseline", justifyContent: "space-between", gap: "12px" },
163
+ cardTitle: { margin: "0", fontSize: "13px", fontWeight: 600 },
164
+ cardNote: { margin: "0", opacity: 0.62, fontSize: "12px" },
165
+ row: { display: "flex", alignItems: "center", gap: "8px", flexWrap: "wrap" },
166
+ label: { minWidth: "104px", opacity: 0.8 },
167
+ input: {
168
+ flex: "1 1 320px",
169
+ minWidth: "220px",
170
+ padding: "6px 8px",
171
+ font: "inherit",
172
+ color: "inherit",
173
+ background: "transparent",
174
+ border: "1px solid color-mix(in srgb, currentColor 25%, transparent)",
175
+ borderRadius: "6px",
176
+ },
177
+ textarea: {
178
+ width: "100%",
179
+ boxSizing: "border-box",
180
+ minHeight: "62px",
181
+ padding: "6px 8px",
182
+ font: "inherit",
183
+ color: "inherit",
184
+ background: "transparent",
185
+ border: "1px solid color-mix(in srgb, currentColor 25%, transparent)",
186
+ borderRadius: "6px",
187
+ resize: "vertical",
188
+ },
189
+ button: {
190
+ padding: "5px 12px",
191
+ font: "inherit",
192
+ color: "inherit",
193
+ background: "transparent",
194
+ border: "1px solid color-mix(in srgb, currentColor 30%, transparent)",
195
+ borderRadius: "6px",
196
+ cursor: "pointer",
197
+ },
198
+ buttonPrimary: {
199
+ padding: "5px 12px",
200
+ font: "inherit",
201
+ color: "#ffffff",
202
+ background: "#2563eb",
203
+ border: "1px solid #2563eb",
204
+ borderRadius: "6px",
205
+ cursor: "pointer",
206
+ },
207
+ buttonDanger: {
208
+ padding: "5px 12px",
209
+ font: "inherit",
210
+ color: "#ffffff",
211
+ background: "#b91c1c",
212
+ border: "1px solid #b91c1c",
213
+ borderRadius: "6px",
214
+ cursor: "pointer",
215
+ },
216
+ mono: { fontFamily: "ui-monospace, SFMono-Regular, Consolas, monospace", fontSize: "12px", wordBreak: "break-all" },
217
+ chips: { display: "flex", gap: "8px", flexWrap: "wrap" },
218
+ chipOk: {
219
+ padding: "2px 8px",
220
+ borderRadius: "999px",
221
+ border: "1px solid color-mix(in srgb, currentColor 20%, transparent)",
222
+ background: "color-mix(in srgb, #16a34a 16%, transparent)",
223
+ },
224
+ chipBad: {
225
+ padding: "2px 8px",
226
+ borderRadius: "999px",
227
+ border: "1px solid color-mix(in srgb, currentColor 20%, transparent)",
228
+ background: "color-mix(in srgb, #b91c1c 16%, transparent)",
229
+ },
230
+ list: { display: "flex", flexDirection: "column", gap: "4px", margin: "0", padding: "0", listStyle: "none" },
231
+ toolRow: { display: "flex", gap: "8px", alignItems: "baseline" },
232
+ toolLabel: { minWidth: "220px" },
233
+ toolDetail: { opacity: 0.66 },
234
+ note: { margin: "0", fontSize: "12px", opacity: 0.86, whiteSpace: "pre-wrap" },
235
+ pre: {
236
+ margin: "0",
237
+ padding: "8px 10px",
238
+ maxHeight: "220px",
239
+ overflow: "auto",
240
+ fontFamily: "ui-monospace, SFMono-Regular, Consolas, monospace",
241
+ fontSize: "12px",
242
+ border: "1px solid color-mix(in srgb, currentColor 16%, transparent)",
243
+ borderRadius: "6px",
244
+ whiteSpace: "pre-wrap",
245
+ },
246
+ error: { margin: "0", fontSize: "12px", color: "#dc2626", whiteSpace: "pre-wrap" },
247
+ ok: { margin: "0", fontSize: "12px", color: "#16a34a", whiteSpace: "pre-wrap" },
248
+ muted: { margin: "0", fontSize: "12px", opacity: 0.62 },
249
+ };
250
+
251
+ const e = React.createElement;
252
+
253
+ /* ------------------------------ 纯渲染件 ------------------------------ */
254
+
255
+ function toolRow(tool, index) {
256
+ const found = tool !== null && typeof tool === "object" && tool.found === true;
257
+ const error = tool !== null && typeof tool === "object" && typeof tool.error === "string" ? tool.error : "";
258
+ return e(
259
+ "li",
260
+ { key: String(tool?.id ?? index), style: { display: "flex", flexDirection: "column", gap: "2px" } },
261
+ e(
262
+ "div",
263
+ { style: styles.toolRow },
264
+ e("span", { "aria-hidden": "true" }, found ? "✔" : "✘"),
265
+ e("span", { style: styles.toolLabel }, String(tool?.label ?? tool?.id ?? "")),
266
+ e("span", { style: { opacity: 0.66, wordBreak: "break-all" } }, toolSummaryLine(tool)),
267
+ ),
268
+ error === "" ? null : e("div", { style: styles.muted }, error),
269
+ );
270
+ }
271
+
272
+ function capabilityChips(capabilities) {
273
+ return e(
274
+ "div",
275
+ { style: styles.chips },
276
+ CAPABILITIES.map(([id, label]) => {
277
+ const ok = capabilities !== null && typeof capabilities === "object" && capabilities[id] === true;
278
+ return e("span", { key: id, style: ok ? styles.chipOk : styles.chipBad }, (ok ? "✔ " : "✘ ") + label);
279
+ }),
280
+ );
281
+ }
282
+
283
+ function stepList(steps) {
284
+ if (!Array.isArray(steps) || steps.length === 0) return null;
285
+ return e(
286
+ "ul",
287
+ { style: styles.list },
288
+ steps.map((step, index) =>
289
+ e(
290
+ "li",
291
+ { key: String(step?.id ?? index) },
292
+ e(
293
+ "div",
294
+ null,
295
+ (STEP_ICONS[step?.status] ?? "·") + " " + String(step?.label ?? step?.id ?? ""),
296
+ ),
297
+ String(step?.detail ?? "") === ""
298
+ ? null
299
+ : e("div", { style: styles.muted }, String(step.detail)),
300
+ ),
301
+ ),
302
+ );
303
+ }
304
+
305
+ /* ------------------------------ 面板主体 ------------------------------ */
306
+
307
+ /**
308
+ * 设置页里的「STM32 工具链」一节。
309
+ * 组件自己取数据:注册时不传 locale、不传 inject,宿主只负责把这块内容
310
+ * 摆进设置面板的内容列(settings.section 的所有者只保证一个 close 回调)。
311
+ */
312
+ function Stm32Section() {
313
+ const [snapshot, setSnapshot] = useState(null);
314
+ const [configError, setConfigError] = useState("");
315
+ const [loadingConfig, setLoadingConfig] = useState(true);
316
+
317
+ const [msys64, setMsys64] = useState("");
318
+ const [cubeMxRoot, setCubeMxRoot] = useState("");
319
+ const [binDirsText, setBinDirsText] = useState("");
320
+ const [saving, setSaving] = useState(false);
321
+ const [saveNote, setSaveNote] = useState(null);
322
+
323
+ const [probing, setProbing] = useState(false);
324
+ const [probe, setProbe] = useState(null);
325
+ const [probeError, setProbeError] = useState("");
326
+
327
+ const [planning, setPlanning] = useState(false);
328
+ const [plan, setPlan] = useState(null);
329
+ const [planError, setPlanError] = useState("");
330
+ const [confirmInstall, setConfirmInstall] = useState(false);
331
+ const [starting, setStarting] = useState(false);
332
+
333
+ const [taskId, setTaskId] = useState("");
334
+ const [installState, setInstallState] = useState(null);
335
+ const [installError, setInstallError] = useState("");
336
+
337
+ const applySnapshot = useCallback((next) => {
338
+ if (next === null || typeof next !== "object") return;
339
+ setSnapshot(next);
340
+ const config = next.config !== null && typeof next.config === "object" ? next.config : null;
341
+ setMsys64(config === null ? "" : String(config.msys64 ?? ""));
342
+ setCubeMxRoot(config === null ? "" : String(config.cubeMxRoot ?? ""));
343
+ const dirs = Array.isArray(next.binDirs) ? next.binDirs : [];
344
+ setBinDirsText(dirs.map((item) => String(item)).join("\n"));
345
+ }, []);
346
+
347
+ const loadConfig = useCallback(async () => {
348
+ setLoadingConfig(true);
349
+ const result = await callApi("GET", "config");
350
+ setLoadingConfig(false);
351
+ if (!result.ok) {
352
+ setConfigError(result.error);
353
+ return;
354
+ }
355
+ setConfigError("");
356
+ applySnapshot(result.value.snapshot);
357
+ }, [applySnapshot]);
358
+
359
+ // 打开面板读一次配置:配置是唯一"用户已经决定的"状态,其余都是探测结果。
360
+ useEffect(() => {
361
+ void loadConfig();
362
+ }, [loadConfig]);
363
+
364
+ // 安装任务轮询:宿主把安装跑在后台,面板只读状态。任务结束就停表并重读配置
365
+ // (安装器成功时会把 msys64 / binDirs 写回配置文件)。
366
+ useEffect(() => {
367
+ if (taskId === "") return undefined;
368
+ let cancelled = false;
369
+ let timer = null;
370
+ const tick = async () => {
371
+ const result = await callApi("GET", "install/state?taskId=" + encodeURIComponent(taskId));
372
+ if (cancelled) return;
373
+ if (!result.ok) {
374
+ setInstallError(result.error);
375
+ setTaskId("");
376
+ return;
377
+ }
378
+ const state = result.value.state;
379
+ setInstallState(state);
380
+ setInstallError("");
381
+ if (state !== null && typeof state === "object" && state.status === "running") {
382
+ timer = setTimeout(tick, POLL_INTERVAL_MS);
383
+ return;
384
+ }
385
+ setTaskId("");
386
+ void loadConfig();
387
+ };
388
+ timer = setTimeout(tick, 300);
389
+ return () => {
390
+ cancelled = true;
391
+ if (timer !== null) clearTimeout(timer);
392
+ };
393
+ }, [taskId, loadConfig]);
394
+
395
+ const save = async () => {
396
+ setSaving(true);
397
+ setSaveNote(null);
398
+ const result = await callApi("POST", "config/save", {
399
+ msys64: msys64.trim(),
400
+ cubeMxRoot: cubeMxRoot.trim(),
401
+ binDirs: parseBinDirs(binDirsText),
402
+ });
403
+ setSaving(false);
404
+ if (!result.ok) {
405
+ setSaveNote({ ok: false, text: result.error });
406
+ return;
407
+ }
408
+ if (result.value.snapshot !== undefined) applySnapshot(result.value.snapshot);
409
+ setSaveNote({ ok: true, text: "已保存到 " + String(result.value.path ?? "") });
410
+ };
411
+
412
+ const runProbe = async () => {
413
+ setProbing(true);
414
+ setProbeError("");
415
+ const result = await callApi("POST", "probe", {
416
+ binDirs: parseBinDirs(binDirsText),
417
+ cubeMxRoot: cubeMxRoot.trim(),
418
+ });
419
+ setProbing(false);
420
+ if (!result.ok) {
421
+ setProbeError(result.error);
422
+ return;
423
+ }
424
+ setProbe(result.value.summary);
425
+ };
426
+
427
+ const runPlan = async () => {
428
+ setPlanning(true);
429
+ setPlanError("");
430
+ setConfirmInstall(false);
431
+ const result = await callApi("POST", "install/plan", {});
432
+ setPlanning(false);
433
+ if (!result.ok) {
434
+ setPlanError(result.error);
435
+ return;
436
+ }
437
+ setPlan(result.value.plan);
438
+ };
439
+
440
+ const startInstall = async () => {
441
+ setStarting(true);
442
+ setInstallError("");
443
+ const body = { confirm: true, writePath: true };
444
+ if (plan !== null && typeof plan === "object" && typeof plan.msys64 === "string" && plan.msys64 !== "") {
445
+ body.targetDir = plan.msys64;
446
+ }
447
+ const result = await callApi("POST", "install/start", body);
448
+ setStarting(false);
449
+ setConfirmInstall(false);
450
+ if (!result.ok) {
451
+ setInstallError(result.error);
452
+ return;
453
+ }
454
+ setInstallState(null);
455
+ setTaskId(String(result.value.taskId ?? ""));
456
+ };
457
+
458
+ const configPath = snapshot !== null && typeof snapshot === "object" ? String(snapshot.path ?? "") : "";
459
+ const configExists = snapshot !== null && typeof snapshot === "object" && snapshot.exists === true;
460
+ const configDisabled = snapshot !== null && typeof snapshot === "object" && snapshot.readingDisabled === true;
461
+ const configProblems = snapshot !== null && typeof snapshot === "object" && Array.isArray(snapshot.errors) ? snapshot.errors : [];
462
+
463
+ const configCard = e(
464
+ "section",
465
+ { style: styles.card },
466
+ e(
467
+ "div",
468
+ { style: styles.cardHead },
469
+ e("h3", { style: styles.cardTitle }, "装在哪"),
470
+ e("p", { style: styles.cardNote }, loadingConfig ? "读取配置中…" : configExists ? "配置文件已存在" : "还没有配置文件(保存时创建)"),
471
+ ),
472
+ e("p", { style: styles.mono }, configPath === "" ? "(配置路径未知)" : "配置: " + configPath),
473
+ configDisabled
474
+ ? e("p", { style: styles.muted }, "当前进程用 DSH_STM32_NO_CONFIG 关掉了配置读取,这里的保存仍然会写文件,但本次运行不会生效。")
475
+ : null,
476
+ configProblems.length === 0 ? null : e("p", { style: styles.error }, "配置里有问题:" + configProblems.join(";")),
477
+ configError === "" ? null : e("p", { style: styles.error }, configError),
478
+ e(
479
+ "div",
480
+ { style: styles.row },
481
+ e("label", { style: styles.label, htmlFor: "dsh-stm32-msys64" }, "MSYS2 根"),
482
+ e("input", {
483
+ id: "dsh-stm32-msys64",
484
+ style: styles.input,
485
+ type: "text",
486
+ value: msys64,
487
+ spellCheck: false,
488
+ placeholder: "例如 D:/app/msys64(空 = 不指定)",
489
+ onChange: (event) => setMsys64(event.target.value),
490
+ }),
491
+ ),
492
+ e(
493
+ "div",
494
+ { style: styles.row },
495
+ e("label", { style: styles.label, htmlFor: "dsh-stm32-cubemx" }, "CubeMX 根"),
496
+ e("input", {
497
+ id: "dsh-stm32-cubemx",
498
+ style: styles.input,
499
+ type: "text",
500
+ value: cubeMxRoot,
501
+ spellCheck: false,
502
+ placeholder: "例如 D:/app/STM32CubeMX(空 = 自动找)",
503
+ onChange: (event) => setCubeMxRoot(event.target.value),
504
+ }),
505
+ ),
506
+ e(
507
+ "div",
508
+ { style: { display: "flex", flexDirection: "column", gap: "4px" } },
509
+ e("label", { style: { opacity: 0.8 }, htmlFor: "dsh-stm32-bindirs" }, "额外 bin 目录(每行一个,留空 = 用 MSYS2 推导 / 自动发现)"),
510
+ e("textarea", {
511
+ id: "dsh-stm32-bindirs",
512
+ style: styles.textarea,
513
+ value: binDirsText,
514
+ spellCheck: false,
515
+ placeholder: "D:/app/msys64/ucrt64/bin",
516
+ onChange: (event) => setBinDirsText(event.target.value),
517
+ }),
518
+ ),
519
+ e(
520
+ "div",
521
+ { style: styles.row },
522
+ e("button", { type: "button", style: styles.buttonPrimary, disabled: saving, onClick: () => void save() }, saving ? "保存中…" : "保存配置"),
523
+ saveNote === null ? null : e("p", { style: saveNote.ok ? styles.ok : styles.error }, String(saveNote.text)),
524
+ ),
525
+ e("p", { style: styles.cardNote }, "改完立即生效(插件每次自己读配置文件,不用重开 DSH)。"),
526
+ );
527
+
528
+ const tools = probe !== null && typeof probe === "object" && Array.isArray(probe.tools) ? probe.tools : [];
529
+ const probeCard = e(
530
+ "section",
531
+ { style: styles.card },
532
+ e(
533
+ "div",
534
+ { style: styles.cardHead },
535
+ e("h3", { style: styles.cardTitle }, "缺什么"),
536
+ e("p", { style: styles.cardNote }, "按上面三个框里的值探测;留空就走自动发现。"),
537
+ ),
538
+ e(
539
+ "div",
540
+ { style: styles.row },
541
+ e("button", { type: "button", style: styles.button, disabled: probing, onClick: () => void runProbe() }, probing ? "探测中…" : "探测工具链"),
542
+ ),
543
+ probeError === "" ? null : e("p", { style: styles.error }, probeError),
544
+ probe === null
545
+ ? e("p", { style: styles.muted }, "还没探测过。探测会跑一次各工具的版本命令,不会装任何东西。")
546
+ : e(
547
+ "div",
548
+ { style: { display: "flex", flexDirection: "column", gap: "8px" } },
549
+ e("p", { style: styles.note }, String(probe.message ?? "")),
550
+ capabilityChips(probe.capabilities),
551
+ e("ul", { style: styles.list }, tools.map((tool, index) => toolRow(tool, index))),
552
+ ),
553
+ );
554
+
555
+ const planSteps = plan !== null && typeof plan === "object" ? plan.steps : null;
556
+ const planDownload = plan !== null && typeof plan === "object" ? plan.download : null;
557
+ const installLines =
558
+ installState !== null && typeof installState === "object" && Array.isArray(installState.lines) ? installState.lines : [];
559
+ const installSteps = installState !== null && typeof installState === "object" ? installState.steps : null;
560
+
561
+ const installCard = e(
562
+ "section",
563
+ { style: styles.card },
564
+ e(
565
+ "div",
566
+ { style: styles.cardHead },
567
+ e("h3", { style: styles.cardTitle }, "装工具链"),
568
+ e("p", { style: styles.cardNote }, "先出计划(不下载、不写盘),确认后才真装。"),
569
+ ),
570
+ e(
571
+ "div",
572
+ { style: styles.row },
573
+ e("button", { type: "button", style: styles.button, disabled: planning, onClick: () => void runPlan() }, planning ? "算计划中…" : "出安装计划"),
574
+ plan !== null && !confirmInstall && taskId === ""
575
+ ? e("button", { type: "button", style: styles.buttonDanger, onClick: () => setConfirmInstall(true) }, "开始安装…")
576
+ : null,
577
+ ),
578
+ planError === "" ? null : e("p", { style: styles.error }, planError),
579
+ plan === null
580
+ ? null
581
+ : e(
582
+ "div",
583
+ { style: { display: "flex", flexDirection: "column", gap: "8px" } },
584
+ e("p", { style: styles.note }, String(plan.message ?? "")),
585
+ // 没有可做的步骤时(工具链已完整)只留那一句话:不再画"目标/包/下载"与七行"已跳过",
586
+ // 否则一张"什么都不用装"的卡片看起来比要装的还长。
587
+ Array.isArray(planSteps) && planSteps.length > 0
588
+ ? e(
589
+ "p",
590
+ { style: styles.muted },
591
+ "目标:" + String(plan.msys64 ?? "(未知)") +
592
+ " · 包:" + (Array.isArray(plan.packages) ? plan.packages.join(" ") : "") +
593
+ " · 下载:" + formatBytes(planDownload !== null && typeof planDownload === "object" ? planDownload.bytes : 0) +
594
+ (planDownload !== null && typeof planDownload === "object" && planDownload.cached === true ? "(已缓存)" : ""),
595
+ )
596
+ : null,
597
+ Array.isArray(planSteps) && planSteps.length > 0 ? stepList(planSteps) : null,
598
+ String(plan.nextStep ?? "") === "" ? null : e("p", { style: styles.muted }, "下一步:" + String(plan.nextStep)),
599
+ Array.isArray(plan.notes) && plan.notes.length > 0
600
+ ? e("ul", { style: styles.list }, plan.notes.map((note, index) => e("li", { key: index, style: styles.muted }, String(note))))
601
+ : null,
602
+ ),
603
+ confirmInstall
604
+ ? e(
605
+ "div",
606
+ { style: { ...styles.row, gap: "10px" } },
607
+ e("p", { style: styles.note }, "确认在这台机器上真装?会下载安装包、解压到目标目录,需要几分钟。"),
608
+ e("button", { type: "button", style: styles.buttonDanger, disabled: starting, onClick: () => void startInstall() }, starting ? "启动中…" : "确认安装"),
609
+ e("button", { type: "button", style: styles.button, disabled: starting, onClick: () => setConfirmInstall(false) }, "取消"),
610
+ )
611
+ : null,
612
+ installError === "" ? null : e("p", { style: styles.error }, installError),
613
+ // 结果块用 installState 而不是 taskId 做判据:轮询结束时组件会清掉 taskId 去重读配置,
614
+ // 只看 taskId 的话「安装成功 / 失败」会连同日志一起从面板上消失,用户看不到最终结论。
615
+ taskId === "" && installState === null
616
+ ? null
617
+ : e(
618
+ "div",
619
+ { style: { display: "flex", flexDirection: "column", gap: "8px" } },
620
+ taskId === ""
621
+ ? null
622
+ : e(
623
+ "p",
624
+ { style: styles.muted },
625
+ "安装任务 " + taskId + " · " +
626
+ (installState !== null && typeof installState === "object" ? String(installState.status ?? "running") : "running"),
627
+ ),
628
+ installState !== null && typeof installState === "object" && String(installState.message ?? "") !== ""
629
+ ? e("p", { style: styles.note }, String(installState.message))
630
+ : null,
631
+ stepList(installSteps),
632
+ installLines.length === 0
633
+ ? null
634
+ : e("pre", { style: styles.pre }, installLines.slice(-60).join("\n")),
635
+ ),
636
+ );
637
+
638
+ return e(
639
+ "div",
640
+ { style: styles.root },
641
+ e("h2", { style: styles.title }, "STM32 工具链"),
642
+ e(
643
+ "p",
644
+ { style: styles.lead },
645
+ "这里只负责「这台机器把工具装在哪、缺什么、怎么装」。工程本身的编译 / 烧录 / 调试仍走斜杠命令与工具,面板不碰你的工程。",
646
+ ),
647
+ configCard,
648
+ probeCard,
649
+ installCard,
650
+ );
651
+ }
652
+
653
+ /* ------------------------------ 插件入口 ------------------------------ */
654
+
655
+ /** 客户端插件只声明 slots(注册 settings.section 需要它)。 */
656
+ const inject = ["slots"];
657
+
658
+ function apply(ctx) {
659
+ // ctx.effect 是宿主客户端插件 API:回调里返回的注销函数随插件 fiber 一起释放,
660
+ // 面板卸载/热重载时不会在槽位账本里留下野条目。
661
+ ctx.effect(
662
+ () =>
663
+ ctx.slots.inject("settings.section", () =>
664
+ ctx.slots.register(
665
+ {
666
+ name: "settings.section",
667
+ id: "stm32",
668
+ order: 40,
669
+ label: () => "STM32 工具链",
670
+ },
671
+ Stm32Section,
672
+ ),
673
+ ),
674
+ "dsh-stm32: settings section",
675
+ );
676
+ }
677
+
678
+ exports.apply = apply;
679
+ exports.inject = inject;
680
+ return module.exports;
681
+ },
682
+ });