@turingcode/cli 0.3.4 → 0.3.5
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/dist/cli/create-cli.js +5 -3
- package/dist/cli/index.js +1 -1
- package/dist/core/exact/fraction.js +2 -0
- package/dist/core/files.js +2 -2
- package/dist/core/json-shape.js +1 -0
- package/dist/core/query.js +2 -2
- package/dist/core/tables/csv.d.ts +13 -2
- package/dist/core/tables/csv.js +54 -25
- package/dist/core/tables/markdown.js +8 -2
- package/dist/features/contract/diagnostics.d.ts +3 -0
- package/dist/features/contract/diagnostics.js +7 -4
- package/dist/features/contract/load.js +4 -4
- package/dist/features/contract/rules.d.ts +12 -0
- package/dist/features/contract/rules.js +31 -4
- package/dist/features/debug/clear_breakpoint.d.ts +1 -0
- package/dist/features/debug/clear_breakpoint.js +1 -1
- package/dist/features/debug/command.js +1 -1
- package/dist/features/debug/continue.d.ts +1 -0
- package/dist/features/debug/continue.js +2 -2
- package/dist/features/debug/debug_start.d.ts +1 -0
- package/dist/features/debug/debug_start.js +1 -1
- package/dist/features/debug/debug_stop.d.ts +1 -0
- package/dist/features/debug/debug_stop.js +1 -1
- package/dist/features/debug/evaluate.d.ts +1 -0
- package/dist/features/debug/evaluate.js +5 -5
- package/dist/features/debug/get_stack.d.ts +1 -0
- package/dist/features/debug/get_stack.js +1 -1
- package/dist/features/debug/get_variables.d.ts +1 -0
- package/dist/features/debug/get_variables.js +2 -2
- package/dist/features/debug/halt.d.ts +1 -0
- package/dist/features/debug/halt.js +1 -1
- package/dist/features/debug/set_breakpoint.d.ts +1 -0
- package/dist/features/debug/set_breakpoint.js +1 -1
- package/dist/features/debug/step.d.ts +1 -0
- package/dist/features/debug/step.js +2 -2
- package/dist/features/install-mods/install.js +4 -3
- package/dist/features/install-skills/install.d.ts +1 -0
- package/dist/features/install-skills/install.js +1 -1
- package/dist/features/probe/command.d.ts +1 -1
- package/dist/features/probe/command.js +103 -35
- package/dist/features/probe/kill_all_debuggers.d.ts +1 -0
- package/dist/features/probe/kill_all_debuggers.js +5 -5
- package/dist/features/probe/list_probes.d.ts +1 -0
- package/dist/features/probe/list_probes.js +1 -1
- package/dist/features/probe/program_project.d.ts +1 -0
- package/dist/features/probe/program_project.js +1 -1
- package/dist/features/probe/read_fault_status.d.ts +1 -0
- package/dist/features/probe/read_fault_status.js +1 -1
- package/dist/features/probe/read_memory.d.ts +1 -0
- package/dist/features/probe/read_memory.js +2 -2
- package/dist/features/probe/read_peripheral.d.ts +1 -0
- package/dist/features/probe/read_peripheral.js +7 -5
- package/dist/features/probe/reset_probe_usb.d.ts +1 -0
- package/dist/features/probe/reset_probe_usb.js +1 -1
- package/dist/features/probe/reset_target.d.ts +1 -0
- package/dist/features/probe/reset_target.js +1 -1
- package/dist/features/probe/target_info.d.ts +1 -0
- package/dist/features/probe/target_info.js +1 -1
- package/dist/features/project/analysis_static.d.ts +1 -0
- package/dist/features/project/analysis_static.js +1 -1
- package/dist/features/project/analysis_syntax_check.d.ts +1 -0
- package/dist/features/project/analysis_syntax_check.js +1 -1
- package/dist/features/project/project_build.d.ts +1 -0
- package/dist/features/project/project_build.js +1 -1
- package/dist/features/project/project_download_template.d.ts +1 -0
- package/dist/features/project/project_download_template.js +4 -4
- package/dist/features/project/project_sync_to_server.js +1 -1
- package/dist/features/project/stm32_cubetools_download_drivers.d.ts +1 -0
- package/dist/features/project/stm32_cubetools_download_drivers.js +2 -2
- package/dist/features/status/get_status.d.ts +1 -0
- package/dist/features/status/get_status.js +1 -1
- package/dist/features/table/check.js +5 -5
- package/dist/features/table/read.js +9 -9
- package/dist/features/tool/command.d.ts +2 -2
- package/dist/features/tool/command.js +95 -66
- package/dist/lib.d.ts +4 -0
- package/dist/lib.js +4 -2
- package/dist/stm32/actions/DebugAction.d.ts +4 -2
- package/dist/stm32/actions/DebugAction.js +6 -5
- package/dist/stm32/actions/DebugActionInfo.d.ts +4 -0
- package/dist/stm32/actions/DebugActionInfo.js +4 -2
- package/dist/stm32/actions/DebugActionListProbes.d.ts +1 -0
- package/dist/stm32/actions/DebugActionListProbes.js +1 -1
- package/dist/stm32/actions/DebugActionProgram.d.ts +8 -2
- package/dist/stm32/actions/DebugActionProgram.js +9 -7
- package/dist/stm32/actions/DebugActionRead.d.ts +8 -7
- package/dist/stm32/actions/DebugActionRead.js +5 -3
- package/dist/stm32/actions/DebugActionReset.d.ts +2 -1
- package/dist/stm32/actions/DebugActionReset.js +1 -1
- package/dist/stm32/actions/DebugActionServer.d.ts +4 -0
- package/dist/stm32/actions/DebugActionServer.js +4 -2
- package/dist/stm32/analysis.d.ts +1 -0
- package/dist/stm32/build.d.ts +4 -4
- package/dist/stm32/build.js +1 -1
- package/dist/stm32/context.js +2 -2
- package/dist/stm32/daemon.d.ts +8 -6
- package/dist/stm32/daemon.js +25 -18
- package/dist/stm32/regen.js +14 -8
- package/dist/stm32/session.d.ts +9 -6
- package/dist/stm32/session.js +11 -10
- package/dist/stm32/sync.d.ts +1 -0
- package/dist/stm32/sync.js +5 -3
- package/dist/stm32/tool.d.ts +5 -3
- package/dist/stm32/toolCommand.d.ts +4 -4
- package/dist/stm32/toolCommand.js +1 -3
- package/dist/stm32/utils/discover_tools.d.ts +2 -1
- package/dist/stm32/utils/discover_tools.js +1 -1
- package/dist/stm32/utils/downloadzip_utils.js +4 -4
- package/dist/stm32/utils/faultUtils.d.ts +2 -1
- package/dist/stm32/utils/faultUtils.js +2 -2
- package/dist/stm32/utils/listSyncFiles.js +12 -10
- package/dist/stm32/utils/mcu_utils.d.ts +5 -6
- package/dist/stm32/utils/mcu_utils.js +10 -9
- package/dist/stm32/utils/project_utils.d.ts +1 -1
- package/dist/stm32/utils/project_utils.js +5 -3
- package/dist/stm32/utils/usb_utils.js +2 -2
- package/dist/stm32/utils/user_code.d.ts +4 -1
- package/dist/stm32/utils/user_code.js +5 -3
- package/dist/stm32/utils/utils_mutex.js +34 -14
- package/dist/stm32/validate.d.ts +1 -0
- package/dist/stm32/validate.js +1 -1
- package/mods/claude/turingcode-skills/skills/turingcode-cli/SKILL.md +17 -23
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/SKILL.md +4 -0
- package/mods/claude/turingcode-skills/skills/turingcode-firmware-code/references/input-and-bounds.md +0 -13
- package/package.json +1 -1
- package/mods/claude/turingcode-skills/skills/turingcode-cli/references/board-and-debug.md +0 -22
- package/mods/claude/turingcode-skills/skills/turingcode-cli/references/build-and-check.md +0 -19
- package/mods/claude/turingcode-skills/skills/turingcode-cli/references/new-project.md +0 -9
package/dist/stm32/daemon.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Debug daemon: a background turingcode process per project that holds the probe-rs debug session between CLI runs.
|
|
3
|
+
* `debug start` launches it; probe and debug commands forward their tool call to it over a user-only local socket
|
|
4
|
+
* (Unix socket in <project>/.turingcode, a per-user, per-project named pipe on Windows; access is checked with the
|
|
5
|
+
* token file next to it). It exits on its own once no debug session is running.
|
|
6
|
+
*/
|
|
5
7
|
import { spawn } from 'node:child_process';
|
|
6
8
|
import { randomBytes, timingSafeEqual } from 'node:crypto';
|
|
7
9
|
import { closeSync, mkdirSync, openSync } from 'node:fs';
|
|
@@ -14,8 +16,8 @@ import { debugSessionActive, stopSession } from './session.js';
|
|
|
14
16
|
import { sha256 } from './utils/hash_utils.js';
|
|
15
17
|
import { setProbeLockFile } from './utils/utils_mutex.js';
|
|
16
18
|
import { safetyCheck, validateArgs } from './validate.js';
|
|
17
|
-
export const PING = 'ping';
|
|
18
|
-
const IDLE_EXIT_MS = 5000;
|
|
19
|
+
export const PING = 'ping'; /** reserved tool: replies {"session": <true while a debug session runs>} */
|
|
20
|
+
const IDLE_EXIT_MS = 5000; /** the daemon exits once no request ran for this long without a debug session */
|
|
19
21
|
const START_TIMEOUT_MS = 10_000;
|
|
20
22
|
/**
|
|
21
23
|
* Daemon files of one project, all inside <project>/.turingcode. Windows paths are lowercased so "c:" and "C:" give the same pipe.
|
|
@@ -35,7 +37,8 @@ export function daemonPaths(root) {
|
|
|
35
37
|
* @param tool tool name, or PING
|
|
36
38
|
* @param args tool arguments
|
|
37
39
|
* @param cwd the caller's working directory
|
|
38
|
-
* @returns the daemon's response,
|
|
40
|
+
* @returns the daemon's response, an error response when it closed the connection after the request was sent, or
|
|
41
|
+
* undefined when no daemon is running (the request was never sent)
|
|
39
42
|
*/
|
|
40
43
|
export async function forwardToDaemon(paths, tool, args, cwd) {
|
|
41
44
|
const token = (await readFile(paths.token, 'utf8').catch(() => '')).trim();
|
|
@@ -45,8 +48,12 @@ export async function forwardToDaemon(paths, tool, args, cwd) {
|
|
|
45
48
|
return new Promise((resolve) => {
|
|
46
49
|
const sock = connect(paths.socket);
|
|
47
50
|
let buf = '';
|
|
51
|
+
let sent = false; /** once sent, a close without reply is a daemon failure, not "no daemon": running locally would repeat the command */
|
|
48
52
|
sock.setEncoding('utf8');
|
|
49
|
-
sock.on('connect', () =>
|
|
53
|
+
sock.on('connect', () => {
|
|
54
|
+
sent = true;
|
|
55
|
+
sock.write(JSON.stringify(req) + '\n');
|
|
56
|
+
});
|
|
50
57
|
sock.on('data', (d) => {
|
|
51
58
|
buf += d;
|
|
52
59
|
const nl = buf.indexOf('\n');
|
|
@@ -60,8 +67,8 @@ export async function forwardToDaemon(paths, tool, args, cwd) {
|
|
|
60
67
|
resolve({ id: 1, ok: false, error: 'Bad reply from the debug daemon' });
|
|
61
68
|
}
|
|
62
69
|
});
|
|
63
|
-
sock.on('error', () =>
|
|
64
|
-
sock.on('close', () => resolve(undefined));
|
|
70
|
+
sock.on('error', () => { }); /** close follows */
|
|
71
|
+
sock.on('close', () => resolve(sent ? { id: 1, ok: false, error: 'The debug daemon closed the connection before replying' } : undefined));
|
|
65
72
|
});
|
|
66
73
|
}
|
|
67
74
|
/**
|
|
@@ -110,15 +117,15 @@ export async function runDaemon(root, tools) {
|
|
|
110
117
|
if (process.platform !== 'win32')
|
|
111
118
|
await chmod(paths.dir, 0o700);
|
|
112
119
|
if (await pingDaemon(paths))
|
|
113
|
-
return;
|
|
120
|
+
return; /** another daemon serves this project */
|
|
114
121
|
if (process.platform !== 'win32')
|
|
115
|
-
await unlink(paths.socket).catch(() => { });
|
|
122
|
+
await unlink(paths.socket).catch(() => { }); /** stale socket of a crashed daemon */
|
|
116
123
|
setProbeLockFile(paths.lock);
|
|
117
124
|
const token = randomBytes(32).toString('hex');
|
|
118
125
|
let running = 0;
|
|
119
126
|
let lastActivity = Date.now();
|
|
120
|
-
let stopping = false;
|
|
121
|
-
|
|
127
|
+
let stopping = false; /** set once idle: requests on still-open connections are dropped, so no session starts in a closing daemon */
|
|
128
|
+
/** Checks the token and arguments, then runs one tool call. */
|
|
122
129
|
const handle = async (req) => {
|
|
123
130
|
const given = Buffer.from(String(req.token));
|
|
124
131
|
if (given.length !== token.length || !timingSafeEqual(given, Buffer.from(token)))
|
|
@@ -146,7 +153,7 @@ export async function runDaemon(root, tools) {
|
|
|
146
153
|
};
|
|
147
154
|
const server = createServer((sock) => {
|
|
148
155
|
let buf = '';
|
|
149
|
-
let queue = Promise.resolve();
|
|
156
|
+
let queue = Promise.resolve(); /** replies go out in request order */
|
|
150
157
|
sock.setEncoding('utf8');
|
|
151
158
|
sock.on('error', () => { });
|
|
152
159
|
sock.on('data', (d) => {
|
|
@@ -178,11 +185,11 @@ export async function runDaemon(root, tools) {
|
|
|
178
185
|
await new Promise((resolve, reject) => (server.once('error', reject), server.listen(paths.socket, resolve)));
|
|
179
186
|
}
|
|
180
187
|
catch {
|
|
181
|
-
return;
|
|
188
|
+
return; /** another daemon won the race to listen; its token file stays untouched */
|
|
182
189
|
}
|
|
183
190
|
if (process.platform !== 'win32')
|
|
184
191
|
await chmod(paths.socket, 0o600);
|
|
185
|
-
|
|
192
|
+
/** The token goes in only once listening, by rename, so clients never read a half-written or another daemon's token. */
|
|
186
193
|
const tmp = `${paths.token}.${process.pid}`;
|
|
187
194
|
await writeFile(tmp, token, { mode: 0o600 });
|
|
188
195
|
await rename(tmp, paths.token);
|
|
@@ -195,7 +202,7 @@ export async function runDaemon(root, tools) {
|
|
|
195
202
|
}, 500);
|
|
196
203
|
});
|
|
197
204
|
stopping = true;
|
|
198
|
-
const closed = new Promise((resolve) => server.close(() => resolve()));
|
|
205
|
+
const closed = new Promise((resolve) => server.close(() => resolve())); /** stops accepting now, frees the socket name */
|
|
199
206
|
await unlink(paths.token).catch(() => { });
|
|
200
207
|
await stopSession();
|
|
201
208
|
await closed;
|
package/dist/stm32/regen.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Safe CubeMX regeneration: CubeMX keeps only the code inside USER CODE sections, so before a regen this compares each
|
|
3
|
+
* file that has USER CODE markers with the version CubeMX generated last time (the baseline in .turingcode/generated/)
|
|
4
|
+
* and shows, as a diff, the code that regen would remove. Used by `turingcode project regen` and, as a library, by the
|
|
5
|
+
* VS Code extension.
|
|
6
|
+
*/
|
|
5
7
|
import { copyFile, mkdir, readdir, readFile, rm, writeFile } from 'node:fs/promises';
|
|
6
8
|
import { dirname, join } from 'node:path';
|
|
7
9
|
import { scanUserSections } from './utils/user_code.js';
|
|
@@ -94,7 +96,7 @@ export async function takeServerIoc(call, project) {
|
|
|
94
96
|
const { root } = project;
|
|
95
97
|
const notes = [];
|
|
96
98
|
const tmp = join(root, '.turingcode', `ioc-${process.pid}`);
|
|
97
|
-
|
|
99
|
+
/** A pull that is always cancelled: it only fetches the server's versions of the files that differ. */
|
|
98
100
|
await syncProjectFromServer(call, project, async (files, zip) => {
|
|
99
101
|
const iocs = files.filter((f) => f.endsWith('.ioc'));
|
|
100
102
|
if (!iocs.length)
|
|
@@ -189,7 +191,7 @@ export async function regenerateProject(call, project, force = false) {
|
|
|
189
191
|
const iocNotes = await takeServerIoc(call, project);
|
|
190
192
|
const synced = [...iocNotes, await syncProjectToServer(call, project)].join('\n');
|
|
191
193
|
const regen = await call('project_regenerate', { project_name: name }, 125_000);
|
|
192
|
-
|
|
194
|
+
/** A failed regen comes back as plain text, not as a tool error; pulling then would save unregenerated files as the baseline. */
|
|
193
195
|
if (!/^# Regenerated project /m.test(regen))
|
|
194
196
|
throw new Error(`${regen.trim()}\n\nRegen failed on the server; your local files and the baseline are unchanged. Run it again.`);
|
|
195
197
|
const removed = [];
|
|
@@ -197,8 +199,12 @@ export async function regenerateProject(call, project, force = false) {
|
|
|
197
199
|
const pulled = await syncProjectFromServer(call, project, async (files, zip) => {
|
|
198
200
|
await extractZip(zip, tmp);
|
|
199
201
|
try {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
+
/** source files without markers too: their code would be overwritten without a USER CODE section to keep it */
|
|
203
|
+
for (const f of files.filter((x) => /\.(c|h|cpp|hpp)$/.test(x) && !x.startsWith('Drivers/'))) {
|
|
204
|
+
const mine = local.get(f) ?? (await readFile(join(root, f), 'utf8').catch(() => undefined))?.replace(/\r\n/g, '\n');
|
|
205
|
+
if (mine === undefined)
|
|
206
|
+
continue; /** new file from the server */
|
|
207
|
+
const diff = checkAfterRegen(f, mine, await readFile(join(tmp, f), 'utf8'), local.has(f) && !unbaselined.has(f));
|
|
202
208
|
if (diff)
|
|
203
209
|
removed.push(diff);
|
|
204
210
|
}
|
package/dist/stm32/session.d.ts
CHANGED
|
@@ -1,18 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* probe-rs debug session shared by the tools in this folder: a DAP client over `probe-rs dap-server` (stdio).
|
|
3
|
+
*/
|
|
1
4
|
import { ChildProcess } from 'node:child_process';
|
|
2
5
|
export interface Breakpoint {
|
|
3
6
|
line: number;
|
|
4
7
|
condition?: string;
|
|
5
8
|
}
|
|
6
9
|
export interface StartOptions {
|
|
7
|
-
root: string;
|
|
8
|
-
chip: string;
|
|
9
|
-
elf: string;
|
|
10
|
-
flash: boolean;
|
|
10
|
+
root: string; /** project root */
|
|
11
|
+
chip: string; /** probe-rs target name */
|
|
12
|
+
elf: string; /** ELF with the debug info of the running firmware */
|
|
13
|
+
flash: boolean; /** flash the ELF and halt after reset, instead of attaching */
|
|
11
14
|
}
|
|
12
15
|
export declare class DebugSession {
|
|
13
16
|
readonly opts: StartOptions;
|
|
14
17
|
private proc;
|
|
15
|
-
readonly breakpoints: Map<string, Breakpoint[]>;
|
|
18
|
+
readonly breakpoints: Map<string, Breakpoint[]>; /** project-relative file (forward slashes) -> breakpoints */
|
|
16
19
|
capabilities: any;
|
|
17
20
|
state: 'running' | 'halted';
|
|
18
21
|
stopReason: string;
|
|
@@ -21,7 +24,7 @@ export declare class DebugSession {
|
|
|
21
24
|
private buf;
|
|
22
25
|
private pending;
|
|
23
26
|
private waiters;
|
|
24
|
-
private log;
|
|
27
|
+
private log; /** probe-rs output and stderr, for error messages */
|
|
25
28
|
private elfData?;
|
|
26
29
|
constructor(opts: StartOptions, proc: ChildProcess);
|
|
27
30
|
/**
|
package/dist/stm32/session.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* probe-rs debug session shared by the tools in this folder: a DAP client over `probe-rs dap-server` (stdio).
|
|
3
|
+
*/
|
|
3
4
|
import { spawn } from 'node:child_process';
|
|
4
5
|
import { existsSync } from 'node:fs';
|
|
5
6
|
import { readFile } from 'node:fs/promises';
|
|
@@ -9,7 +10,7 @@ import { DebugActionServer } from './actions/DebugActionServer.js';
|
|
|
9
10
|
export class DebugSession {
|
|
10
11
|
opts;
|
|
11
12
|
proc;
|
|
12
|
-
breakpoints = new Map();
|
|
13
|
+
breakpoints = new Map(); /** project-relative file (forward slashes) -> breakpoints */
|
|
13
14
|
capabilities = {};
|
|
14
15
|
state = 'running';
|
|
15
16
|
stopReason = '';
|
|
@@ -18,12 +19,12 @@ export class DebugSession {
|
|
|
18
19
|
buf = Buffer.alloc(0);
|
|
19
20
|
pending = new Map();
|
|
20
21
|
waiters = [];
|
|
21
|
-
log = [];
|
|
22
|
+
log = []; /** probe-rs output and stderr, for error messages */
|
|
22
23
|
elfData;
|
|
23
24
|
constructor(opts, proc) {
|
|
24
25
|
this.opts = opts;
|
|
25
26
|
this.proc = proc;
|
|
26
|
-
proc.stdin.on('error', () => undefined);
|
|
27
|
+
proc.stdin.on('error', () => undefined); /** writes after probe-rs exited; onExit reports it */
|
|
27
28
|
proc.stdout.on('data', (chunk) => this.onData(chunk));
|
|
28
29
|
proc.stderr.on('data', (chunk) => this.addLog(chunk.toString()));
|
|
29
30
|
proc.on('exit', (code) => this.onExit(`probe-rs dap-server exited (code ${code})`));
|
|
@@ -93,11 +94,11 @@ export class DebugSession {
|
|
|
93
94
|
if (end < 0)
|
|
94
95
|
continue;
|
|
95
96
|
const start = this.elfData.lastIndexOf(0, end) + 1;
|
|
96
|
-
const prefix = this.elfData.subarray(start, end + 1).toString('utf8');
|
|
97
|
+
const prefix = this.elfData.subarray(start, end + 1).toString('utf8'); /** ends with sep */
|
|
97
98
|
return prefix + rel.replace(/\//g, sep);
|
|
98
99
|
}
|
|
99
100
|
}
|
|
100
|
-
return join(this.opts.root, rel);
|
|
101
|
+
return join(this.opts.root, rel); /** built locally or with relative paths */
|
|
101
102
|
}
|
|
102
103
|
/**
|
|
103
104
|
* Converts a path from the debug info to a project-relative one, by finding the longest tail of it that exists in the project.
|
|
@@ -137,7 +138,7 @@ export class DebugSession {
|
|
|
137
138
|
*/
|
|
138
139
|
async stop() {
|
|
139
140
|
await this.request('disconnect', { terminateDebuggee: false }, 3000).catch(() => undefined);
|
|
140
|
-
|
|
141
|
+
/** killing probe-rs while it still closes the probe can leave an ST-LINK in DEV_USB_COMM_ERR until replugged */
|
|
141
142
|
if (this.proc.exitCode === null) {
|
|
142
143
|
const exited = new Promise((done) => this.proc.once('exit', done));
|
|
143
144
|
this.proc.stdin.end();
|
|
@@ -300,13 +301,13 @@ export async function startSession(opts) {
|
|
|
300
301
|
}),
|
|
301
302
|
coreConfigs: [{ coreIndex: 0, programBinary: opts.elf }],
|
|
302
303
|
};
|
|
303
|
-
|
|
304
|
+
/** DAP order: launch/attach, wait for "initialized", then configurationDone; the launch reply may come before or after it */
|
|
304
305
|
const initialized = s.waitEvent('initialized', 60_000);
|
|
305
306
|
const launched = s.request(opts.flash ? 'launch' : 'attach', config, 180_000);
|
|
306
307
|
await Promise.race([initialized, launched]);
|
|
307
308
|
await s.request('configurationDone');
|
|
308
309
|
await launched;
|
|
309
|
-
|
|
310
|
+
/** Let the first stop (halt after reset) arrive before reporting the state */
|
|
310
311
|
if (s.state !== 'halted')
|
|
311
312
|
await s.waitEvent('stopped', opts.flash ? 5000 : 1000);
|
|
312
313
|
return s;
|
package/dist/stm32/sync.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Project } from './tool.js';
|
|
2
|
+
/** Calls a TuringCode server tool and returns its text; throws on a tool error. */
|
|
2
3
|
export type ServerCall = (name: string, args: object, timeoutMs?: number) => Promise<string>;
|
|
3
4
|
/**
|
|
4
5
|
* Syncs the project's files to projects/<name> on the server through the signed link of the sync_project tool:
|
package/dist/stm32/sync.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Project sync with the TuringCode server through the signed links of its sync tools. Used by the project commands
|
|
3
|
+
* and, as a library, by the VS Code extension (which adds progress and confirmation UI around it).
|
|
4
|
+
*/
|
|
3
5
|
import { readFile } from 'node:fs/promises';
|
|
4
6
|
import { join } from 'node:path';
|
|
5
7
|
import { extractZip, fetchOrThrow, syncLink, zipEntryNames, zipFiles } from './utils/downloadzip_utils.js';
|
|
@@ -28,7 +30,7 @@ export async function syncProjectToServer(call, project, forceFullSync = false)
|
|
|
28
30
|
if (forceFullSync)
|
|
29
31
|
msg = await upload([...data.keys()]);
|
|
30
32
|
else {
|
|
31
|
-
|
|
33
|
+
/** send the manifest, the server replies what it needs */
|
|
32
34
|
const manifest = Object.fromEntries([...data].map(([name, buf]) => [name, sha256(buf)]));
|
|
33
35
|
const reply = await (await fetchOrThrow(link, { method: 'POST', body: toSha256sum(manifest) })).text();
|
|
34
36
|
const lines = reply.split('\n');
|
package/dist/stm32/tool.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
/** Tool definitions shared by the project, probe, debug and status commands, and the context their handlers get. */
|
|
1
2
|
export type Prop = {
|
|
2
3
|
type: 'string' | 'integer' | 'number' | 'boolean';
|
|
3
4
|
description?: string;
|
|
@@ -11,9 +12,10 @@ export interface Project {
|
|
|
11
12
|
root: string;
|
|
12
13
|
name: string;
|
|
13
14
|
}
|
|
15
|
+
/** What a handler gets from its caller; server calls and builds are in context.ts. */
|
|
14
16
|
export interface ToolContext {
|
|
15
|
-
cwd: string;
|
|
16
|
-
socketPath: string;
|
|
17
|
+
cwd: string; /** caller's working directory */
|
|
18
|
+
socketPath: string; /** debug daemon socket of the project */
|
|
17
19
|
debugActive(): boolean;
|
|
18
20
|
}
|
|
19
21
|
export interface ToolDef {
|
|
@@ -25,7 +27,7 @@ export interface ToolDef {
|
|
|
25
27
|
required?: string[];
|
|
26
28
|
additionalProperties: boolean;
|
|
27
29
|
};
|
|
28
|
-
needsProbe: boolean;
|
|
30
|
+
needsProbe: boolean; /** refused while a debug session owns the probe */
|
|
29
31
|
run: (args: any, ctx: ToolContext) => Promise<string>;
|
|
30
32
|
}
|
|
31
33
|
/**
|
|
@@ -9,12 +9,12 @@ import { ToolDef } from "./tool.js";
|
|
|
9
9
|
*/
|
|
10
10
|
export type RunMode = "local" | "probe" | "session" | "start";
|
|
11
11
|
export interface ToolCommandSpec {
|
|
12
|
-
command: string;
|
|
12
|
+
command: string; /** subcommand name, e.g. "read" */
|
|
13
13
|
tool: ToolDef;
|
|
14
14
|
mode: RunMode;
|
|
15
|
-
server?: boolean;
|
|
16
|
-
examples: [string, string][];
|
|
17
|
-
aliases?: Record<string, string>;
|
|
15
|
+
server?: boolean; /** calls the Turing Code server, so its help shows the token help */
|
|
16
|
+
examples: [string, string][]; /** [command line, what it does] */
|
|
17
|
+
aliases?: Record<string, string>; /** flag -> short alias, e.g. { build: "b" } */
|
|
18
18
|
}
|
|
19
19
|
/**
|
|
20
20
|
* Converts a tool argument name to its flag: `force_full_sync` -> `force-full-sync`.
|
|
@@ -69,7 +69,7 @@ export function toolCommand(spec) {
|
|
|
69
69
|
const { properties, required = [] } = spec.tool.inputSchema;
|
|
70
70
|
const options = Object.fromEntries(Object.entries(properties).map(([name, p]) => [flagName(name), {
|
|
71
71
|
type: p.type === "integer" || p.type === "number" ? "number" : p.type,
|
|
72
|
-
|
|
72
|
+
/** the CLI hides yargs' type and choices tags, so the allowed values go in the text */
|
|
73
73
|
describe: [p.description, p.enum && `one of ${p.enum.filter(Boolean).join(", ")}`, required.includes(name) ? "required" : undefined]
|
|
74
74
|
.filter(Boolean).join("; ") || undefined,
|
|
75
75
|
...(p.enum && { choices: p.enum }),
|
|
@@ -85,8 +85,6 @@ export function toolCommand(spec) {
|
|
|
85
85
|
c = c.example(line, what);
|
|
86
86
|
return c.epilogue([
|
|
87
87
|
description,
|
|
88
|
-
"Prints the result as text.",
|
|
89
|
-
"Exit: 0 ok, 1 failed (error on stderr), 2 bad arguments.",
|
|
90
88
|
...(spec.server ? [TOKEN_HELP] : []),
|
|
91
89
|
].join("\n"));
|
|
92
90
|
},
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
/** Finds the STM32 command line tools installed on this machine. */
|
|
1
2
|
export type SystemToolId = 'probe-rs' | 'STM32_Programmer_CLI' | 'STM32CubeIDE' | 'STM32CubeMX';
|
|
2
3
|
export interface DiscoveredSystemTool {
|
|
3
4
|
id: SystemToolId;
|
|
4
|
-
path: string;
|
|
5
|
+
path: string; /** executable */
|
|
5
6
|
}
|
|
6
7
|
/**
|
|
7
8
|
* Finds one tool: its environment variable, then PATH, then its default install locations.
|
|
@@ -96,7 +96,7 @@ export async function findTool(id) {
|
|
|
96
96
|
];
|
|
97
97
|
return candidates.find((c) => c && existsSync(c));
|
|
98
98
|
}
|
|
99
|
-
let cached_tools;
|
|
99
|
+
let cached_tools; /** last discovery, shared by every action */
|
|
100
100
|
/**
|
|
101
101
|
* Finds every known tool installed on this machine. The result is cached; pass refresh to look again
|
|
102
102
|
* (e.g. after the user installed a tool).
|
|
@@ -27,16 +27,16 @@ export async function extractZip(data, parentDir, overwrite = true) {
|
|
|
27
27
|
const zip = join(tmpdir(), `turingcode-${randomBytes(8).toString("hex")}.zip`);
|
|
28
28
|
try {
|
|
29
29
|
await writeFile(zip, data);
|
|
30
|
-
await mkdir(parentDir, { recursive: true });
|
|
30
|
+
await mkdir(parentDir, { recursive: true }); /** tar -C, unlike unzip -d, needs the folder to exist */
|
|
31
31
|
try {
|
|
32
32
|
await run("unzip", [overwrite ? "-o" : "-n", zip, "-d", parentDir], { windowsHide: true });
|
|
33
33
|
}
|
|
34
34
|
catch (e) {
|
|
35
35
|
if (e?.code === 1)
|
|
36
|
-
return;
|
|
36
|
+
return; /** unzip exit 1 = warnings, extraction succeeded */
|
|
37
37
|
if (e?.code !== "ENOENT")
|
|
38
38
|
throw new Error(`unzip failed: ${String(e?.stderr || e?.message).trim()}`);
|
|
39
|
-
|
|
39
|
+
/** no unzip installed: tar (bsdtar on Windows and macOS) also reads zips */
|
|
40
40
|
try {
|
|
41
41
|
await run("tar", ["-xf", zip, "-C", parentDir, ...(overwrite ? [] : ["-k"])], { windowsHide: true });
|
|
42
42
|
}
|
|
@@ -144,7 +144,7 @@ export async function zipFiles(files) {
|
|
|
144
144
|
const fileName = Buffer.from(name, "utf8");
|
|
145
145
|
const packed = packedAll[i];
|
|
146
146
|
const crc = crc32(data);
|
|
147
|
-
|
|
147
|
+
/** version 20, flag 0x0800 = UTF-8 names, method 8 = deflate, DOS time/date 0 (1980-01-01) */
|
|
148
148
|
const local = Buffer.alloc(30);
|
|
149
149
|
local.writeUInt32LE(0x04034b50, 0);
|
|
150
150
|
local.writeUInt16LE(20, 4);
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
/** Cortex-M fault status registers (SCB), read in one 6-word block from CFSR. */
|
|
2
|
+
export declare const FAULT_REGS_ADDRESS = 3758157096; /** CFSR, HFSR, DFSR, MMFAR, BFAR, AFSR */
|
|
2
3
|
/**
|
|
3
4
|
* Formats a number as 0x-prefixed, zero-padded hex.
|
|
4
5
|
* @param value number to format
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
export const FAULT_REGS_ADDRESS = 0xe000ed28;
|
|
1
|
+
/** Cortex-M fault status registers (SCB), read in one 6-word block from CFSR. */
|
|
2
|
+
export const FAULT_REGS_ADDRESS = 0xe000ed28; /** CFSR, HFSR, DFSR, MMFAR, BFAR, AFSR */
|
|
3
3
|
const CFSR_BITS = [
|
|
4
4
|
[0, "IACCVIOL", "MemManage: instruction fetch from a no-execute or MPU-protected region"],
|
|
5
5
|
[1, "DACCVIOL", "MemManage: data access to an MPU-protected region"],
|
|
@@ -6,7 +6,7 @@ import { loadProjectSettings } from "./project_utils.js";
|
|
|
6
6
|
const run = promisify(execFile);
|
|
7
7
|
const LS_FILES = ["ls-files", "--cached", "--others", "--exclude-standard", "-z"];
|
|
8
8
|
const MAX_FILE_SIZE = 10 * 1024 * 1024;
|
|
9
|
-
const ALWAYS_SKIPPED = /\.(zip|gz|gzip|rar)$/i;
|
|
9
|
+
const ALWAYS_SKIPPED = /\.(zip|gz|gzip|rar)$/i; /** archives, even when listed in included_files */
|
|
10
10
|
/**
|
|
11
11
|
* Path rules for listed files (included_files bypass them): skips build/, dot folders, .env files,
|
|
12
12
|
* .csv and .md files except README.md.
|
|
@@ -15,11 +15,10 @@ const ALWAYS_SKIPPED = /\.(zip|gz|gzip|rar)$/i; // archives, even when listed in
|
|
|
15
15
|
*/
|
|
16
16
|
function isSyncablePre(rel) {
|
|
17
17
|
return (!rel.startsWith("build/") &&
|
|
18
|
-
!/(^|\/)\.[^/]*\//.test(rel) &&
|
|
19
|
-
!/(^|\/)\.env(\.[^/]*)?$/.test(rel) &&
|
|
20
|
-
!/\.csv$/i.test(rel) &&
|
|
21
|
-
(!/\.md$/i.test(rel) || /(^|\/)README\.md$/i.test(rel))
|
|
22
|
-
);
|
|
18
|
+
!/(^|\/)\.[^/]*\//.test(rel) && /** dot folders at any depth: .git, .turingcode, .codex, .vscode, ... */
|
|
19
|
+
!/(^|\/)\.env(\.[^/]*)?$/.test(rel) && /** .env, .env.local, ... */
|
|
20
|
+
!/\.csv$/i.test(rel) && /** data dumps */
|
|
21
|
+
(!/\.md$/i.test(rel) || /(^|\/)README\.md$/i.test(rel)) /** docs, except READMEs */);
|
|
23
22
|
}
|
|
24
23
|
/**
|
|
25
24
|
* Final check applied to every file, included_files too: not an archive, still exists and small enough to sync;
|
|
@@ -32,8 +31,8 @@ async function isSyncableFinal(root, rel) {
|
|
|
32
31
|
if (ALWAYS_SKIPPED.test(rel))
|
|
33
32
|
return false;
|
|
34
33
|
const st = await stat(join(root, rel)).catch(() => undefined);
|
|
35
|
-
if (!st)
|
|
36
|
-
return false;
|
|
34
|
+
if (!st?.isFile())
|
|
35
|
+
return false; /** deleted-but-tracked files are skipped, and folders */
|
|
37
36
|
if (st.size <= MAX_FILE_SIZE)
|
|
38
37
|
return true;
|
|
39
38
|
console.warn(`[sync] skipping ${rel}: larger than 10 MB`);
|
|
@@ -48,7 +47,7 @@ async function gitListFiles(root) {
|
|
|
48
47
|
const listed = await list([]).catch(() => []);
|
|
49
48
|
if (listed.length)
|
|
50
49
|
return listed;
|
|
51
|
-
|
|
50
|
+
/** Not a repo, or ignored by a parent repo: a throwaway git dir in .turingcode still applies the project's .gitignore files. */
|
|
52
51
|
const gitDir = join(root, ".turingcode", `git-${process.pid}`);
|
|
53
52
|
try {
|
|
54
53
|
await run("git", ["init", "-q", "--bare", gitDir], { windowsHide: true });
|
|
@@ -87,7 +86,10 @@ export async function listSyncFiles(root) {
|
|
|
87
86
|
all = await walkFiles(root);
|
|
88
87
|
}
|
|
89
88
|
else {
|
|
90
|
-
|
|
89
|
+
/** git lists a submodule or nested repo as its folder: sync the files in it.
|
|
90
|
+
* shortcut: the nested repo's own .gitignore is not applied; list it with gitListFiles if its build output gets synced. */
|
|
91
|
+
const expanded = await Promise.all(all.map(async (p) => ((await stat(join(root, p)).catch(() => undefined))?.isDirectory() ? walkFiles(root, p) : [p])));
|
|
92
|
+
all = [...new Set([...expanded.flat(), ...(await walkFiles(root, "Drivers"))])];
|
|
91
93
|
}
|
|
92
94
|
const filtered = all.filter(isSyncablePre);
|
|
93
95
|
const { included_files } = await loadProjectSettings(root);
|
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* @
|
|
5
|
-
* @returns
|
|
2
|
+
* Returns the CubeMX MCU name (e.g. STM32G474RETx) from a project_template or project_create reply.
|
|
3
|
+
* @param text the server reply
|
|
4
|
+
* @returns the MCU name, or undefined when the reply names none
|
|
6
5
|
*/
|
|
7
6
|
export declare function getMcuFromText(text: string): string | undefined;
|
|
8
7
|
/**
|
|
@@ -26,8 +25,8 @@ export declare function hasSingleBankFlashAlgo(mcu: string): boolean;
|
|
|
26
25
|
export declare function getMcuFromIoc(root: string): Promise<string>;
|
|
27
26
|
/** What STM32_Programmer_CLI reports about the connected target. */
|
|
28
27
|
export interface TargetInfo {
|
|
29
|
-
deviceId?: string;
|
|
30
|
-
name?: string;
|
|
28
|
+
deviceId?: string; /** e.g. 0x422 */
|
|
29
|
+
name?: string; /** e.g. STM32F302xB-xC/STM32F303xB-xC/F358xx */
|
|
31
30
|
flashKb?: number;
|
|
32
31
|
cpu?: string;
|
|
33
32
|
voltage?: string;
|
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { readFile, readdir } from "node:fs/promises";
|
|
2
2
|
import { join } from "node:path";
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
/**
|
|
4
|
+
* Server reply formats that name the resolved CubeMX MCU
|
|
5
|
+
* Error replies are left out.
|
|
6
|
+
*/
|
|
5
7
|
const MCU_PATTERNS = [
|
|
6
|
-
/^Barebone project template for (STM32\w+) \(/m,
|
|
7
|
-
/^Created project '[^']*' \((STM32\w+), /m,
|
|
8
|
+
/^Barebone project template for (STM32\w+) \(/m, /** PROJECT_TEMPLATE_READY */
|
|
9
|
+
/^Created project '[^']*' \((STM32\w+), /m, /** PROJECT_CREATED */
|
|
8
10
|
];
|
|
9
11
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* @
|
|
13
|
-
* @returns
|
|
12
|
+
* Returns the CubeMX MCU name (e.g. STM32G474RETx) from a project_template or project_create reply.
|
|
13
|
+
* @param text the server reply
|
|
14
|
+
* @returns the MCU name, or undefined when the reply names none
|
|
14
15
|
*/
|
|
15
16
|
export function getMcuFromText(text) {
|
|
16
17
|
for (const re of MCU_PATTERNS) {
|
|
@@ -68,7 +69,7 @@ export function parseTargetInfo(text) {
|
|
|
68
69
|
voltage: get('Voltage'),
|
|
69
70
|
};
|
|
70
71
|
}
|
|
71
|
-
|
|
72
|
+
/** The flash size letter of a CubeMX MCU name (the 11th character, e.g. STM32F303C[C]Tx) by size in KB */
|
|
72
73
|
const FLASH_CODES = { 16: '4', 32: '6', 64: '8', 128: 'B', 192: 'Z', 256: 'C', 384: 'D', 512: 'E', 768: 'F', 1024: 'G', 1536: 'H', 2048: 'I' };
|
|
73
74
|
/**
|
|
74
75
|
* The product lines of a device name, e.g. `STM32F302xB-xC/STM32F303xB-xC/F358xx` -> F302, F303, F358.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export interface ProjectSettings {
|
|
2
|
-
included_files: string[];
|
|
2
|
+
included_files: string[]; /** posix paths relative to the project root; files or folders synced even when otherwise excluded */
|
|
3
3
|
}
|
|
4
4
|
/**
|
|
5
5
|
* Loads <root>/.turingcode/settings.json, filling missing keys with defaults.
|
|
@@ -54,7 +54,7 @@ export async function findProject(start = process.cwd()) {
|
|
|
54
54
|
break;
|
|
55
55
|
}
|
|
56
56
|
const holds = async (dir, test) => (await readdir(dir, { withFileTypes: true }).catch(() => [])).some((e) => test(e.name, e.isDirectory()));
|
|
57
|
-
|
|
57
|
+
/** the home folder's .turingcode/ holds user settings, not a project, so it never marks one */
|
|
58
58
|
const home = resolve(homedir());
|
|
59
59
|
for (const dir of ancestors) {
|
|
60
60
|
const marked = await holds(dir, (n, isDir) => isDir ? n === ".turingcode" && dir !== home && dirname(dir) !== dir : /^(\.mxproject|.+\.ioc)$/.test(n));
|
|
@@ -86,8 +86,10 @@ export async function findStm32ProjectIn(root) {
|
|
|
86
86
|
throw new Error(`Several STM32 projects found in ${root}:\n${roots.join("\n")}`);
|
|
87
87
|
return { root: roots[0], name: basename(roots[0]) };
|
|
88
88
|
}
|
|
89
|
-
|
|
90
|
-
|
|
89
|
+
/**
|
|
90
|
+
* Folders never searched for a .ioc: generated or vendored trees that can hold many files, and dot folders
|
|
91
|
+
* (.git, and .turingcode whose regen baseline holds a copy of the .ioc).
|
|
92
|
+
*/
|
|
91
93
|
const SKIPPED_DIRS = /^(Drivers|build|node_modules|\..*)$/;
|
|
92
94
|
/**
|
|
93
95
|
* Lists dir and every folder below it that holds a .ioc or .mxproject file, without walking SKIPPED_DIRS.
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { execFile } from 'node:child_process';
|
|
2
2
|
import { promisify } from 'node:util';
|
|
3
3
|
const run = promisify(execFile);
|
|
4
|
-
|
|
4
|
+
/** ST-LINK hardware IDs: V2, V2-1, V3 (three USB configurations), V3SET. */
|
|
5
5
|
const STLINK_IDS = ['3748', '374B', '374E', '374F', '3753', '3754'].map((pid) => `USB\\VID_0483&PID_${pid}`);
|
|
6
6
|
/**
|
|
7
7
|
* Restarts every connected ST-LINK USB device, waking one stuck in USB sleep.
|
|
@@ -12,7 +12,7 @@ export async function resetStlinkUsb() {
|
|
|
12
12
|
throw new Error('USB reset is only supported on Windows (pnputil)');
|
|
13
13
|
const restarted = [];
|
|
14
14
|
for (const hwid of STLINK_IDS) {
|
|
15
|
-
|
|
15
|
+
/** pnputil exits non-zero when no device matches. */
|
|
16
16
|
const { stdout } = await run('pnputil', ['/enum-devices', '/connected', '/deviceid', hwid], { windowsHide: true }).catch(() => ({ stdout: '' }));
|
|
17
17
|
for (const [, id] of stdout.matchAll(/^Instance ID:\s+(.+)$/gm)) {
|
|
18
18
|
await run('pnputil', ['/restart-device', id.trim()], { windowsHide: true });
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CubeMX keeps only the code between the USER CODE BEGIN and USER CODE END markers when it regenerates a file.
|
|
3
|
+
*/
|
|
1
4
|
export interface UserSection {
|
|
2
5
|
name: string;
|
|
3
|
-
start: number;
|
|
6
|
+
start: number; /** body is text.slice(start, end): the lines between the two markers */
|
|
4
7
|
end: number;
|
|
5
8
|
}
|
|
6
9
|
/**
|