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.
- package/README.md +127 -29
- package/README_EN.md +125 -29
- package/client/client.js +682 -0
- package/docs/development-notes.md +1385 -5
- package/docs/logic-audit.md +607 -0
- package/docs/reading-guide.md +738 -0
- package/lib/client.js +682 -0
- package/lib/commands/register.js +214 -12
- package/lib/data/chips.js +134 -4
- package/lib/data/clock-f1.js +158 -0
- package/lib/data/cmsis-sources.js +316 -0
- package/lib/data/f1-ram.generated.js +34 -0
- package/lib/data/firmware-packages.js +352 -0
- package/lib/data/install-guides.js +190 -0
- package/lib/data/search-roots.js +135 -15
- package/lib/data/stm32f1.js +186 -0
- package/lib/data/toolchain-config.js +310 -0
- package/lib/engine/build.js +121 -16
- package/lib/engine/cache.js +336 -0
- package/lib/engine/clock-plan.js +608 -0
- package/lib/engine/cmake-sources.js +428 -0
- package/lib/engine/code-patch.js +18 -6
- package/lib/engine/debug-session.js +163 -4
- package/lib/engine/debug.js +84 -19
- package/lib/engine/doctor.js +395 -4
- package/lib/engine/fetch.js +287 -0
- package/lib/engine/firmware-install.js +668 -0
- package/lib/engine/flash.js +145 -15
- package/lib/engine/fs-longpath.js +107 -0
- package/lib/engine/generate-cache.js +118 -0
- package/lib/engine/generate-script.js +30 -8
- package/lib/engine/generate.js +296 -14
- package/lib/engine/hal-chip.js +230 -0
- package/lib/engine/ioc-plan.js +47 -8
- package/lib/engine/msys2-install.js +1870 -0
- package/lib/engine/probe.js +57 -6
- package/lib/engine/project-export.js +129 -0
- package/lib/engine/register-clock.js +132 -0
- package/lib/engine/register-linker.js +129 -0
- package/lib/engine/register-template.js +449 -0
- package/lib/engine/regs.js +18 -4
- package/lib/engine/route-plan.js +553 -0
- package/lib/engine/scaffold.js +303 -72
- package/lib/engine/subprocess-runner.js +2 -0
- package/lib/engine/tar.js +250 -0
- package/lib/engine/text-file.js +150 -0
- package/lib/engine/thirdparty.js +243 -0
- package/lib/engine/tool-env.js +51 -0
- package/lib/engine/toolchain-gate.js +386 -0
- package/lib/engine/vscode-config.js +99 -0
- package/lib/engine/web-api.js +375 -0
- package/lib/engine/web-routes.js +100 -0
- package/lib/engine/xcube-catalog.js +102 -0
- package/lib/engine/xcube.js +254 -0
- package/lib/engine/zip.js +142 -0
- package/lib/index.js +44 -2
- package/lib/skill/stm32-workflow.js +3 -3
- package/lib/tools/build.js +55 -3
- package/lib/tools/cache.js +250 -0
- package/lib/tools/code-patch.js +29 -2
- package/lib/tools/debug-session.js +56 -14
- package/lib/tools/debug.js +43 -2
- package/lib/tools/doctor.js +16 -3
- package/lib/tools/env-install.js +472 -0
- package/lib/tools/env-probe.js +6 -2
- package/lib/tools/firmware-install.js +255 -0
- package/lib/tools/flash.js +41 -2
- package/lib/tools/generate.js +96 -10
- package/lib/tools/ioc-apply.js +27 -11
- package/lib/tools/ioc-inspect.js +4 -2
- package/lib/tools/ioc-plan.js +209 -11
- package/lib/tools/project-create.js +229 -21
- package/lib/tools/project-dir.js +95 -0
- package/lib/tools/project-export.js +100 -0
- package/lib/tools/regs.js +82 -7
- package/lib/tools/route-plan.js +467 -0
- package/package.json +11 -4
- package/templates/register/CMakeLists.txt +52 -0
- package/templates/register/Core/Src/main.c +72 -0
- package/templates/scaffold/openocd.cfg +8 -0
- package/templates/scaffold/vscode/launch.json +8 -6
- package/templates/scaffold/vscode/settings.json +5 -11
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* L2:给子进程准备环境变量(主要是 PATH)。
|
|
3
|
+
*
|
|
4
|
+
* 为什么单独有它:binDirs(配置 / msys64 推导出来的工具目录)以前只用来**找可执行文件**,
|
|
5
|
+
* 找到之后 spawn 的还是宿主进程的 env。于是出现这种情况——用户刚用安装器把工具装好、
|
|
6
|
+
* PATH 也写进了 HKCU,但 DSH 宿主进程还没重启(进程 env 还是旧的):
|
|
7
|
+
* 门禁按目录找到了 gcc,cmake 却在内部调 `arm-none-eabi-gcc` 时 not found。
|
|
8
|
+
* 所以凡是"跑起来之后还要再调别的工具"的子进程(cmake → gcc/ninja/objcopy),都要带上这份 PATH。
|
|
9
|
+
*/
|
|
10
|
+
import path from "node:path";
|
|
11
|
+
function dedupe(entries) {
|
|
12
|
+
return [...new Set(entries.map((entry) => entry.trim()).filter((entry) => entry !== ""))];
|
|
13
|
+
}
|
|
14
|
+
/** 找出 env 里 PATH 的实际键名(Windows 上常见是 `Path`,大小写不固定)。 */
|
|
15
|
+
function pathKey(env) {
|
|
16
|
+
for (const key of Object.keys(env)) {
|
|
17
|
+
if (key.toLowerCase() === "path")
|
|
18
|
+
return key;
|
|
19
|
+
}
|
|
20
|
+
return "PATH";
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* 复制一份 env,并把 binDirs 放到 PATH 最前面(其余原样保留)。
|
|
24
|
+
*
|
|
25
|
+
* - 不改调用方的 env 对象;
|
|
26
|
+
* - binDirs 为空时原样返回副本(不凭空造 PATH);
|
|
27
|
+
* - Windows 大小写不敏感:只留一个 PATH 键,避免同时出现 PATH 与 Path 导致子进程看不到工具。
|
|
28
|
+
*/
|
|
29
|
+
export function envWithBinDirs(binDirs, base = process.env) {
|
|
30
|
+
const env = { ...base };
|
|
31
|
+
const clean = dedupe(binDirs);
|
|
32
|
+
if (clean.length === 0)
|
|
33
|
+
return env;
|
|
34
|
+
const key = pathKey(base);
|
|
35
|
+
for (const existing of Object.keys(env)) {
|
|
36
|
+
if (existing !== key && existing.toLowerCase() === "path")
|
|
37
|
+
delete env[existing];
|
|
38
|
+
}
|
|
39
|
+
const current = base[key] ?? "";
|
|
40
|
+
env[key] = clean.join(path.delimiter) + (current === "" ? "" : path.delimiter + current);
|
|
41
|
+
return env;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* 找可执行文件时该看哪些目录:显式给的 binDirs 在前,PATH 里的目录在后(去重、去空)。
|
|
45
|
+
*
|
|
46
|
+
* 与门禁的顺序一致——配置里点名的目录优先,PATH 兜底;不忽略 PATH(用户机器上工具常常就在 PATH 里)。
|
|
47
|
+
*/
|
|
48
|
+
export function toolSearchDirs(binDirs, base = process.env) {
|
|
49
|
+
const fromPath = (base[pathKey(base)] ?? "").split(path.delimiter);
|
|
50
|
+
return dedupe([...binDirs.filter((dir) => dir.trim() !== ""), ...fromPath]);
|
|
51
|
+
}
|
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 工具链门禁(L2):干这件事之前,先确认这台机器干得了。
|
|
3
|
+
*
|
|
4
|
+
* 现状是靠"跑起来才报错"发现问题:stm32_build 会真的起 cmake,缺 ninja 时用户在
|
|
5
|
+
* 一屏 cmake 输出里找原因。这里改成事前一次轻量判断:
|
|
6
|
+
* - 快路径只做文件存在性检查(resolveFromDirs,不 spawn),命中就零成本放行;
|
|
7
|
+
* - 没命中才跑 probeAll(一次版本命令,60 s 进程内缓存),拿到版本号和安装建议。
|
|
8
|
+
*
|
|
9
|
+
* 三个档位由工具参数 envCheck 决定:block(默认,缺就拒绝干活)/ warn(只警告)/ off(跳过)。
|
|
10
|
+
* 拒绝时返回的是一句人话 + 一条可粘贴的安装动作,不是一个退出码。
|
|
11
|
+
*/
|
|
12
|
+
import { resolveSearchConfig, searchConfigWarnings, } from "../data/search-roots.js";
|
|
13
|
+
import { readToolchainConfig, toolchainConfigPath } from "../data/toolchain-config.js";
|
|
14
|
+
import { COMPONENT_INSTALLABILITY, guideLines, guidesFor, } from "../data/install-guides.js";
|
|
15
|
+
import { buildTargets, probeTarget, resolveFromDirs, } from "./probe.js";
|
|
16
|
+
/** 能力 → 必需组件(id 与 probe.buildTargets 一致)。 */
|
|
17
|
+
export const CAPABILITY_COMPONENTS = {
|
|
18
|
+
build: ["gcc", "cmake", "ninja"],
|
|
19
|
+
flash: ["openocd"],
|
|
20
|
+
debug: ["openocd", "gdb"],
|
|
21
|
+
generate: ["cubemx", "cubemx-java"],
|
|
22
|
+
};
|
|
23
|
+
const CAPABILITY_LABEL = {
|
|
24
|
+
build: "编译",
|
|
25
|
+
flash: "烧录",
|
|
26
|
+
debug: "调试",
|
|
27
|
+
generate: "生成代码",
|
|
28
|
+
};
|
|
29
|
+
const PROBE_CACHE_TTL_MS = 60_000;
|
|
30
|
+
const probeCache = new Map();
|
|
31
|
+
const smokeCache = new Map();
|
|
32
|
+
/**
|
|
33
|
+
* 快路径命中后要"真跑一次"的主组件:每种能力挑一个最能代表它的。
|
|
34
|
+
*
|
|
35
|
+
* 为什么只挑一个:快路径的价值是零 spawn,全量 probe 就退化成慢路径了。
|
|
36
|
+
* 而"文件在、跑不起来"(半截下载、被杀软隔离、架构不符、0 字节)只要核心那一个坏了就够判断,
|
|
37
|
+
* 剩下的组件坏掉时慢路径照样会兜住。
|
|
38
|
+
*/
|
|
39
|
+
const CAPABILITY_PRIMARY = {
|
|
40
|
+
build: "gcc",
|
|
41
|
+
flash: "openocd",
|
|
42
|
+
debug: "gdb",
|
|
43
|
+
generate: "cubemx-java",
|
|
44
|
+
};
|
|
45
|
+
/** 单测 / 换机器后清缓存。 */
|
|
46
|
+
export function resetGateCache() {
|
|
47
|
+
probeCache.clear();
|
|
48
|
+
smokeCache.clear();
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* 轻量确认:跑一次版本命令,证明"找到的这东西真的能跑"(审计 B13)。
|
|
52
|
+
*
|
|
53
|
+
* 带 60 s 缓存——同一轮里连着调几个工具时不该反复 spawn。失败时把 probe 的 error 原样带出去,
|
|
54
|
+
* 调用方据此写一条警告("文件在 <path>,但版本命令退出码 1 …"),而不是笼统说"缺 gcc"。
|
|
55
|
+
*/
|
|
56
|
+
async function smokeTarget(subprocess, target, config, timeoutMs, signal) {
|
|
57
|
+
const key = ["smoke", target.id, config.binDirs.join(";")].join("|");
|
|
58
|
+
const cached = smokeCache.get(key);
|
|
59
|
+
if (cached !== undefined && Date.now() - cached.at < PROBE_CACHE_TTL_MS)
|
|
60
|
+
return cached.result;
|
|
61
|
+
const options = { timeoutMs };
|
|
62
|
+
if (signal !== undefined)
|
|
63
|
+
options.signal = signal;
|
|
64
|
+
const result = await probeTarget(subprocess, target, config, options);
|
|
65
|
+
smokeCache.set(key, { at: Date.now(), result });
|
|
66
|
+
return result;
|
|
67
|
+
}
|
|
68
|
+
/** 容错解析 envCheck:缺省 block,写了不认识的值也按 block(保守)。 */
|
|
69
|
+
export function normalizeEnvCheck(value) {
|
|
70
|
+
if (value === "off" || value === "warn" || value === "block")
|
|
71
|
+
return value;
|
|
72
|
+
if (typeof value === "string") {
|
|
73
|
+
const lowered = value.trim().toLowerCase();
|
|
74
|
+
if (lowered === "off" || lowered === "warn" || lowered === "block")
|
|
75
|
+
return lowered;
|
|
76
|
+
}
|
|
77
|
+
return "block";
|
|
78
|
+
}
|
|
79
|
+
function targetById(targets, id) {
|
|
80
|
+
return targets.find((target) => target.id === id);
|
|
81
|
+
}
|
|
82
|
+
/** 纯文件存在性判断:不走 PATH、不 spawn。命中就说明"这个目录里确实有"。 */
|
|
83
|
+
function locateFast(target, config) {
|
|
84
|
+
for (const candidate of target.candidates) {
|
|
85
|
+
const hit = resolveFromDirs(candidate, config.binDirs);
|
|
86
|
+
if (hit !== null)
|
|
87
|
+
return hit;
|
|
88
|
+
}
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
/** 组件 → 安装方式;表里没有的按 auto(pacman 能补的居多)。 */
|
|
92
|
+
function installabilityOf(id) {
|
|
93
|
+
const entry = COMPONENT_INSTALLABILITY[id];
|
|
94
|
+
return entry === undefined ? "auto" : entry;
|
|
95
|
+
}
|
|
96
|
+
function toMissing(target, probe) {
|
|
97
|
+
const missing = {
|
|
98
|
+
id: target.id,
|
|
99
|
+
label: target.label,
|
|
100
|
+
hint: target.hint,
|
|
101
|
+
impact: target.impact,
|
|
102
|
+
install: target.install,
|
|
103
|
+
verify: target.verify,
|
|
104
|
+
installability: installabilityOf(target.id),
|
|
105
|
+
};
|
|
106
|
+
const error = probe?.error ?? "";
|
|
107
|
+
if (error !== "")
|
|
108
|
+
missing.error = error;
|
|
109
|
+
return missing;
|
|
110
|
+
}
|
|
111
|
+
function joinLabels(items) {
|
|
112
|
+
return items.map((item) => item.label).join("、");
|
|
113
|
+
}
|
|
114
|
+
function buildMessage(capability, mode, missing) {
|
|
115
|
+
const label = CAPABILITY_LABEL[capability];
|
|
116
|
+
if (missing.length === 0) {
|
|
117
|
+
return {
|
|
118
|
+
message: "工具链齐全," + label + "可以开始。",
|
|
119
|
+
nextStep: "直接重跑当前工具即可。",
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
const names = joinLabels(missing);
|
|
123
|
+
const effect = missing.map((item) => item.hint).join(";");
|
|
124
|
+
if (mode === "warn") {
|
|
125
|
+
return {
|
|
126
|
+
message: "⚠ 工具链不齐(缺 " + names + "),按 envCheck=warn 仍然继续," + label + "大概率会失败。",
|
|
127
|
+
nextStep: "失败原因大概率就是这个;装好再重跑,或把 envCheck 设成 block 先拦住。",
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
const auto = missing.filter((item) => item.installability === "auto");
|
|
131
|
+
const manual = missing.filter((item) => item.installability === "manual");
|
|
132
|
+
let nextStep;
|
|
133
|
+
if (manual.length === 0) {
|
|
134
|
+
nextStep =
|
|
135
|
+
"要我装吗?回一句「装」,我先跑 stm32_env_install 出计划(只列大小与步骤、不下载),确认后才真装;"
|
|
136
|
+
+ "想自己装就看下面 missing[0].install 的第一条。";
|
|
137
|
+
}
|
|
138
|
+
else if (auto.length === 0) {
|
|
139
|
+
nextStep =
|
|
140
|
+
"这几项插件不代装(CubeMX 是 GUI 向导 + 要 ST 账号,固件包只由 CubeMX 的包管理器维护):"
|
|
141
|
+
+ "照着 guides[].steps 走,装完把路径写进配置再重跑;离线机器可以整目录拷过来(看 guides[].portable)。";
|
|
142
|
+
}
|
|
143
|
+
else {
|
|
144
|
+
nextStep =
|
|
145
|
+
"能自动装的(" + joinLabels(auto) + ")回一句「装」,我先出计划(不下载);"
|
|
146
|
+
+ joinLabels(manual) + " 得你自己装,步骤见 guides[].steps。";
|
|
147
|
+
}
|
|
148
|
+
return {
|
|
149
|
+
message: "缺 " + names + ",这次没有开始" + label + ":" + effect + "。",
|
|
150
|
+
nextStep,
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
function buildDetail(capability, mode, checked, missing, msys64, configPath, cacheHit, warnings, guides) {
|
|
154
|
+
const lines = [];
|
|
155
|
+
lines.push("能力:" + CAPABILITY_LABEL[capability] + "(" + capability + "),envCheck=" + mode);
|
|
156
|
+
lines.push("必需组件:" + checked.join("、"));
|
|
157
|
+
if (missing.length === 0) {
|
|
158
|
+
lines.push("结果:齐全。");
|
|
159
|
+
}
|
|
160
|
+
else {
|
|
161
|
+
lines.push("结果:缺 " + String(missing.length) + " 项 ——");
|
|
162
|
+
for (const item of missing) {
|
|
163
|
+
lines.push(" · " + item.label + ":" + item.hint);
|
|
164
|
+
lines.push(" 方式:" + (item.installability === "auto" ? "自动(插件可代装:stm32_env_install)" : "手动(插件不代装,按下面的指引)"));
|
|
165
|
+
lines.push(" 影响:" + item.impact);
|
|
166
|
+
lines.push(" 安装:" + (item.install[0] ?? "(无建议)"));
|
|
167
|
+
lines.push(" 自检:" + item.verify);
|
|
168
|
+
}
|
|
169
|
+
for (const guide of guides) {
|
|
170
|
+
for (const line of guideLines(guide))
|
|
171
|
+
lines.push(line);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
lines.push("MSYS2 配置:" + (msys64 === "" ? "未配置" : msys64) + "(" + configPath + ")");
|
|
175
|
+
for (const warning of warnings)
|
|
176
|
+
lines.push("⚠ " + warning);
|
|
177
|
+
if (cacheHit)
|
|
178
|
+
lines.push("(探测结果来自 60 s 缓存)");
|
|
179
|
+
return lines.join("\n");
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* 判断并给出结论。不抛异常:宿主缺 subprocess 时按"只做了文件存在性检查"处理。
|
|
183
|
+
*/
|
|
184
|
+
export async function ensureToolchain(ctx, request) {
|
|
185
|
+
const started = Date.now();
|
|
186
|
+
const capability = request.capability;
|
|
187
|
+
const mode = normalizeEnvCheck(request.envCheck);
|
|
188
|
+
const checked = [...CAPABILITY_COMPONENTS[capability]];
|
|
189
|
+
const configPath = toolchainConfigPath(process.env);
|
|
190
|
+
const userConfig = readToolchainConfig(process.env).config;
|
|
191
|
+
const msys64 = userConfig?.msys64 ?? "";
|
|
192
|
+
const overrides = {};
|
|
193
|
+
if (request.binDirs !== undefined)
|
|
194
|
+
overrides.binDirs = request.binDirs;
|
|
195
|
+
if (request.cubeMxRoots !== undefined)
|
|
196
|
+
overrides.cubeMxRoots = request.cubeMxRoots;
|
|
197
|
+
const config = resolveSearchConfig(process.env, overrides);
|
|
198
|
+
const warnings = searchConfigWarnings(process.env).map((warning) => warning.message);
|
|
199
|
+
if (mode === "off") {
|
|
200
|
+
const { message, nextStep } = buildMessage(capability, mode, []);
|
|
201
|
+
return {
|
|
202
|
+
ok: true,
|
|
203
|
+
capability,
|
|
204
|
+
mode,
|
|
205
|
+
checked,
|
|
206
|
+
missing: [],
|
|
207
|
+
message: message + "(envCheck=off,没有检查)",
|
|
208
|
+
nextStep,
|
|
209
|
+
msys64,
|
|
210
|
+
configPath,
|
|
211
|
+
cacheHit: false,
|
|
212
|
+
durationMs: Date.now() - started,
|
|
213
|
+
detail: "envCheck=off:跳过工具链检查。",
|
|
214
|
+
warnings,
|
|
215
|
+
installability: "none",
|
|
216
|
+
guides: [],
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
const targets = buildTargets(config);
|
|
220
|
+
const wanted = [];
|
|
221
|
+
for (const id of checked) {
|
|
222
|
+
const target = targetById(targets, id);
|
|
223
|
+
if (target !== undefined)
|
|
224
|
+
wanted.push(target);
|
|
225
|
+
}
|
|
226
|
+
// 快路径:文件都在就直接放行;放行前对"主组件"跑一次版本命令(审计 B13)。
|
|
227
|
+
// 只做 existsSync 会放过"文件在、跑不起来"的机器:0 字节 / 半截下载 / 被杀软隔离 / 架构不符。
|
|
228
|
+
const unresolved = [];
|
|
229
|
+
for (const target of wanted) {
|
|
230
|
+
if (locateFast(target, config) === null)
|
|
231
|
+
unresolved.push(target);
|
|
232
|
+
}
|
|
233
|
+
if (unresolved.length === 0) {
|
|
234
|
+
const fastOk = () => ({
|
|
235
|
+
ok: true,
|
|
236
|
+
capability,
|
|
237
|
+
mode,
|
|
238
|
+
checked,
|
|
239
|
+
missing: [],
|
|
240
|
+
message: "工具链齐全(" + wanted.map((target) => target.label).join("、") + ")," + CAPABILITY_LABEL[capability] + "可以开始。",
|
|
241
|
+
nextStep: "直接继续。",
|
|
242
|
+
msys64,
|
|
243
|
+
configPath,
|
|
244
|
+
cacheHit: false,
|
|
245
|
+
durationMs: Date.now() - started,
|
|
246
|
+
detail: buildDetail(capability, mode, checked, [], msys64, configPath, false, warnings, []),
|
|
247
|
+
warnings,
|
|
248
|
+
installability: "none",
|
|
249
|
+
guides: [],
|
|
250
|
+
});
|
|
251
|
+
const primary = wanted.find((target) => target.id === CAPABILITY_PRIMARY[capability]);
|
|
252
|
+
const smokeSubprocess = ctx.get("subprocess");
|
|
253
|
+
if (primary === undefined || smokeSubprocess === undefined)
|
|
254
|
+
return fastOk();
|
|
255
|
+
const smoke = await smokeTarget(smokeSubprocess, primary, config, request.timeoutMs ?? 15_000, request.signal);
|
|
256
|
+
if (smoke.found)
|
|
257
|
+
return fastOk();
|
|
258
|
+
// 文件在、跑不起来 → 照样按缺件处理,并把"为什么"写成一句人话(detail 里会带 ⚠ 打印)。
|
|
259
|
+
warnings.push(primary.label + " 的文件在(" + (smoke.path === "" ? primary.candidates[0] ?? "" : smoke.path) +
|
|
260
|
+
"),但版本命令没跑通:" + (smoke.error === "" ? "退出码异常" : smoke.error) +
|
|
261
|
+
"。常见原因是下载不完整、被杀毒软件隔离、或拿到的不是本机架构的可执行文件。");
|
|
262
|
+
unresolved.push(primary);
|
|
263
|
+
}
|
|
264
|
+
// 慢路径:解析 + 一次版本命令(可能从 PATH 里找到),带 60 s 缓存。
|
|
265
|
+
const cacheKey = [
|
|
266
|
+
capability,
|
|
267
|
+
config.binDirs.join(";"),
|
|
268
|
+
config.cubeMxRoots.join(";"),
|
|
269
|
+
].join("|");
|
|
270
|
+
let cacheHit = false;
|
|
271
|
+
let probed = [];
|
|
272
|
+
const cached = probeCache.get(cacheKey);
|
|
273
|
+
if (cached !== undefined && Date.now() - cached.at < PROBE_CACHE_TTL_MS) {
|
|
274
|
+
cacheHit = true;
|
|
275
|
+
probed = cached.results;
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
const subprocess = ctx.get("subprocess");
|
|
279
|
+
if (subprocess === undefined) {
|
|
280
|
+
probed = unresolved.map((target) => ({
|
|
281
|
+
id: target.id,
|
|
282
|
+
label: target.label,
|
|
283
|
+
found: false,
|
|
284
|
+
path: "",
|
|
285
|
+
version: "",
|
|
286
|
+
required: target.required,
|
|
287
|
+
hint: target.hint,
|
|
288
|
+
impact: target.impact,
|
|
289
|
+
install: target.install,
|
|
290
|
+
verify: target.verify,
|
|
291
|
+
error: "宿主没有提供 subprocess 服务,只能做文件存在性检查",
|
|
292
|
+
}));
|
|
293
|
+
}
|
|
294
|
+
else {
|
|
295
|
+
const options = {
|
|
296
|
+
timeoutMs: request.timeoutMs ?? 15_000,
|
|
297
|
+
};
|
|
298
|
+
if (request.signal !== undefined)
|
|
299
|
+
options.signal = request.signal;
|
|
300
|
+
for (const target of wanted) {
|
|
301
|
+
probed.push(await probeTarget(subprocess, target, config, options));
|
|
302
|
+
if (request.signal?.aborted === true)
|
|
303
|
+
break;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
// A27:被取消时探测没跑完,缓存它会把「没探到」当成「缺件」记 60 秒,
|
|
307
|
+
// 用户接着会被劝去重装一份已经装好的工具链。
|
|
308
|
+
if (request.signal?.aborted === true) {
|
|
309
|
+
return {
|
|
310
|
+
ok: false,
|
|
311
|
+
capability,
|
|
312
|
+
mode,
|
|
313
|
+
checked,
|
|
314
|
+
missing: [],
|
|
315
|
+
message: "工具链检查被取消:这次没探完,也不缓存结果,下次调用会重新探一遍。",
|
|
316
|
+
nextStep: "重新调用一次即可。",
|
|
317
|
+
msys64,
|
|
318
|
+
configPath,
|
|
319
|
+
cacheHit: false,
|
|
320
|
+
durationMs: Date.now() - started,
|
|
321
|
+
detail: "signal 已中止:探测结果不完整,按审计 A27 不写入 60 s 缓存。",
|
|
322
|
+
warnings,
|
|
323
|
+
installability: "none",
|
|
324
|
+
guides: [],
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
probeCache.set(cacheKey, { at: Date.now(), results: probed });
|
|
328
|
+
}
|
|
329
|
+
const missing = [];
|
|
330
|
+
for (const target of wanted) {
|
|
331
|
+
// 快路径已经确认过存在的组件不重复判缺:probe 失败只可能是版本命令本身有问题。
|
|
332
|
+
if (!unresolved.includes(target))
|
|
333
|
+
continue;
|
|
334
|
+
const probe = probed.find((item) => item.id === target.id);
|
|
335
|
+
if (probe !== undefined && probe.found)
|
|
336
|
+
continue;
|
|
337
|
+
missing.push(toMissing(target, probe));
|
|
338
|
+
}
|
|
339
|
+
const { message, nextStep } = buildMessage(capability, mode, missing);
|
|
340
|
+
const guides = guidesFor(missing.map((item) => item.id));
|
|
341
|
+
const installability = missing.length === 0 ? "none" : missing.some((item) => item.installability === "manual") ? "manual" : "auto";
|
|
342
|
+
return {
|
|
343
|
+
ok: missing.length === 0,
|
|
344
|
+
capability,
|
|
345
|
+
mode,
|
|
346
|
+
checked,
|
|
347
|
+
missing,
|
|
348
|
+
message,
|
|
349
|
+
nextStep,
|
|
350
|
+
msys64,
|
|
351
|
+
configPath,
|
|
352
|
+
cacheHit,
|
|
353
|
+
durationMs: Date.now() - started,
|
|
354
|
+
detail: buildDetail(capability, mode, checked, missing, msys64, configPath, cacheHit, warnings, guides),
|
|
355
|
+
warnings,
|
|
356
|
+
installability,
|
|
357
|
+
guides,
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
/** 报告里的 notes 行:只在 blocked/warn 时写,免得成功路径噪音。 */
|
|
361
|
+
export function gateReportNotes(result) {
|
|
362
|
+
// 配置告警与成败无关:即使这次齐全,也值得让用户知道"你配的某个路径本机没有"。
|
|
363
|
+
if (result.ok)
|
|
364
|
+
return result.warnings.map((warning) => "⚠ " + warning);
|
|
365
|
+
const lines = [];
|
|
366
|
+
lines.push(result.message);
|
|
367
|
+
for (const item of result.missing) {
|
|
368
|
+
lines.push("缺 " + item.label + ":" + item.hint);
|
|
369
|
+
if (item.install[0] !== undefined)
|
|
370
|
+
lines.push(" 安装:" + item.install[0]);
|
|
371
|
+
}
|
|
372
|
+
if (result.nextStep !== "")
|
|
373
|
+
lines.push(result.nextStep);
|
|
374
|
+
// manual 缺件:卡片上就给前两步,够用户判断"这事要不要我自己动手"。
|
|
375
|
+
for (const guide of result.guides) {
|
|
376
|
+
for (const line of guideLines(guide, 2))
|
|
377
|
+
lines.push(line);
|
|
378
|
+
}
|
|
379
|
+
for (const warning of result.warnings)
|
|
380
|
+
lines.push("⚠ " + warning);
|
|
381
|
+
return lines;
|
|
382
|
+
}
|
|
383
|
+
/** 一句话给别的工具拼失败报告用(tool card 第一行)。 */
|
|
384
|
+
export function gateBlockedMessage(result) {
|
|
385
|
+
return result.message;
|
|
386
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* L2:把模板里的 .vscode 配置填成"这台机器能用的"。
|
|
3
|
+
*
|
|
4
|
+
* 背景(审计 B1/B6):模板快照是从开发者机器上拷的,里面有
|
|
5
|
+
* svdPath: D:/JingYu/...、armToolchainPath/gdbPath: D:/app/msys64/ucrt64/bin、
|
|
6
|
+
* settings.json 里的 cube-cmake / ${env:CUBE_BUNDLE_PATH} / 钉死版本的 clangd query-driver。
|
|
7
|
+
* 换台机器(或换个用户)这些路径全是死的:按 F5 调试直接报找不到 gdb,CMake 面板认不出编译器,
|
|
8
|
+
* 还顺带把开发机的用户名泄露到每个新工程里。
|
|
9
|
+
*
|
|
10
|
+
* 所以模板里只留占位符,写盘时按本机实际情况填:
|
|
11
|
+
* - 找得到 gdb / cmake → 填绝对路径(cortex-debug 与 CMake Tools 都吃绝对路径);
|
|
12
|
+
* - 找不到 → **整行删掉**(让扩展回退到 PATH),并把缺什么回报给调用方写进 warnings。
|
|
13
|
+
* 不写空字符串:cortex-debug 拿到 "" 会当成"用这个路径",比不写更糟。
|
|
14
|
+
*/
|
|
15
|
+
import { existsSync } from "node:fs";
|
|
16
|
+
import path from "node:path";
|
|
17
|
+
import { toolSearchDirs } from "./tool-env.js";
|
|
18
|
+
/** 模板占位符(改了模板记得同步这里)。 */
|
|
19
|
+
export const VSCODE_PLACEHOLDERS = {
|
|
20
|
+
gdb: "__GDB_PATH__",
|
|
21
|
+
toolchain: "__ARM_TOOLCHAIN_PATH__",
|
|
22
|
+
cmake: "__CMAKE_PATH__",
|
|
23
|
+
svd: "__SVD_PATH__",
|
|
24
|
+
};
|
|
25
|
+
function findExecutable(names, dirs) {
|
|
26
|
+
for (const dir of dirs) {
|
|
27
|
+
for (const name of names) {
|
|
28
|
+
const abs = path.join(dir, name);
|
|
29
|
+
if (existsSync(abs))
|
|
30
|
+
return abs;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* 探测本机的调试/构建工具路径。
|
|
37
|
+
*
|
|
38
|
+
* 顺序:显式 binDirs(配置里的 msys64 等)优先,PATH 兜底——与门禁同一套优先级,
|
|
39
|
+
* 免得出现"门禁说齐全、launch.json 却是空的"。
|
|
40
|
+
*/
|
|
41
|
+
export function detectVscodeFill(binDirs, base = process.env) {
|
|
42
|
+
const dirs = toolSearchDirs(binDirs, base);
|
|
43
|
+
const gdb = findExecutable(["gdb-multiarch.exe", "gdb-multiarch", "arm-none-eabi-gdb.exe", "arm-none-eabi-gdb"], dirs);
|
|
44
|
+
const cmake = findExecutable(["cmake.exe", "cmake"], dirs);
|
|
45
|
+
return {
|
|
46
|
+
gdbPath: gdb ?? "",
|
|
47
|
+
armToolchainPath: gdb === null ? "" : path.dirname(gdb),
|
|
48
|
+
cmakePath: cmake ?? "",
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* 把占位符换成实际值;值为空的那一行整行删掉。
|
|
53
|
+
*
|
|
54
|
+
* 返回被删掉的占位符,调用方据此写 warnings("没找到 gdb,新工程的 .vscode 里没写 gdbPath")。
|
|
55
|
+
* 模板是 JSONC(带注释、可带尾逗号),所以按行处理不会破坏语法:一行只放一个属性时删行即合法。
|
|
56
|
+
*/
|
|
57
|
+
export function fillVscodeTemplate(text, fill) {
|
|
58
|
+
const values = {
|
|
59
|
+
[VSCODE_PLACEHOLDERS.gdb]: fill.gdbPath,
|
|
60
|
+
[VSCODE_PLACEHOLDERS.toolchain]: fill.armToolchainPath,
|
|
61
|
+
[VSCODE_PLACEHOLDERS.cmake]: fill.cmakePath,
|
|
62
|
+
};
|
|
63
|
+
const dropped = [];
|
|
64
|
+
let filled = text;
|
|
65
|
+
for (const [token, rawValue] of Object.entries(values)) {
|
|
66
|
+
if (rawValue !== "") {
|
|
67
|
+
// 这些值都嵌在 JSON 字符串里:Windows 路径是单反斜杠,直接插会得到 `"D:\app\..."`
|
|
68
|
+
// 这种非法转义(VS Code 打不开 launch.json)。统一转正斜杠:cortex-debug / CMake Tools 都吃。
|
|
69
|
+
const value = rawValue.replace(/\\/gu, "/");
|
|
70
|
+
filled = filled.split(token).join(value);
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (!filled.includes(token))
|
|
74
|
+
continue;
|
|
75
|
+
dropped.push(token);
|
|
76
|
+
filled = filled
|
|
77
|
+
.split(/\r?\n/u)
|
|
78
|
+
.filter((line) => !line.includes(token))
|
|
79
|
+
.join("\n");
|
|
80
|
+
}
|
|
81
|
+
return { text: filled, dropped };
|
|
82
|
+
}
|
|
83
|
+
/** 被删掉的占位符 → 一句人话(写进 scaffold 的 warnings)。 */
|
|
84
|
+
export function describeDroppedPlaceholder(token) {
|
|
85
|
+
if (token === VSCODE_PLACEHOLDERS.gdb) {
|
|
86
|
+
return "本机没找到 gdb(gdb-multiarch / arm-none-eabi-gdb),新工程 .vscode/launch.json 里没写 gdbPath:" +
|
|
87
|
+
"在 VS Code 里按 F5 调试前要自己补,或把 msys64 的 bin 加进 PATH 后重建工程。";
|
|
88
|
+
}
|
|
89
|
+
if (token === VSCODE_PLACEHOLDERS.toolchain) {
|
|
90
|
+
return "本机没找到 gdb 目录,新工程 .vscode/launch.json 里没写 armToolchainPath:cortex-debug 会去 PATH 里找 arm-none-eabi-objdump。";
|
|
91
|
+
}
|
|
92
|
+
if (token === VSCODE_PLACEHOLDERS.cmake) {
|
|
93
|
+
return "本机没找到 cmake,新工程 .vscode/settings.json 里没写 cmake.cmakePath:CMake Tools 会回退到 PATH 里的 cmake。";
|
|
94
|
+
}
|
|
95
|
+
if (token === VSCODE_PLACEHOLDERS.svd) {
|
|
96
|
+
return "新工程 .vscode/launch.json 里没写 svdPath(插件不代替你选 SVD 文件):想在外设寄存器视图里看值,自己加一行 svdPath 指向对应芯片的 .svd。";
|
|
97
|
+
}
|
|
98
|
+
return "模板占位符 " + token + " 没有值,那一行已删掉。";
|
|
99
|
+
}
|