feat: libs 0004-A — symbol write-path spike (provider save op, writable lib + MEMFS placeholder files, rw mount); fix symbol_editor embind link; e2e write-bridge test; bump kicad
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
parent
d2d2c19a8c
commit
df4de65ba2
8 changed files with 340 additions and 21 deletions
|
|
@ -11,6 +11,7 @@ export const WASM_ASSET_BASE_URL =
|
|||
import type { ProviderConfig, ProviderKind } from "@/wasm/collab";
|
||||
import { remoteLibsSource } from "@/wasm/libs/remote-source";
|
||||
import type { LibsSource } from "@/wasm/libs/source";
|
||||
import { withSpikeWritableLib } from "@/wasm/libs/spike-writable";
|
||||
import { staticLibsSource } from "@/wasm/libs/static-source";
|
||||
|
||||
/**
|
||||
|
|
@ -57,7 +58,22 @@ export function docSourceConfig(): DocSource {
|
|||
*/
|
||||
export function libsSourceConfig(): LibsSource | null {
|
||||
const kind = import.meta.env.VITE_LIBS_SOURCE ?? "remote";
|
||||
if (kind === "off") return null;
|
||||
if (kind === "static") return staticLibsSource();
|
||||
return remoteLibsSource(API_BASE_URL);
|
||||
const base =
|
||||
kind === "off"
|
||||
? null
|
||||
: kind === "static"
|
||||
? staticLibsSource()
|
||||
: remoteLibsSource(API_BASE_URL);
|
||||
|
||||
// 0004-A spike: `?libwrite=1` adds one in-memory writable user lib so the
|
||||
// editor save path has a target before the backend exists. Remove once 0004-C
|
||||
// wires real remote writes.
|
||||
if (
|
||||
typeof window !== "undefined" &&
|
||||
new URLSearchParams(window.location.search).get("libwrite") === "1"
|
||||
) {
|
||||
return withSpikeWritableLib(base, (m) => console.log(m));
|
||||
}
|
||||
|
||||
return base;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,7 +6,7 @@ import {
|
|||
TOOL_NEEDS_CONFIG_SEED,
|
||||
} from "./constants";
|
||||
import { buildSymLibTable, installLibsProvider, type LibsSource } from "./libs/source";
|
||||
import { PCBJAM_LIB_MOUNT } from "./libs/uri";
|
||||
import { libUri, PCBJAM_LIB_MOUNT, PCBJAM_LIB_RW_MOUNT } from "./libs/uri";
|
||||
|
||||
/**
|
||||
* Boot a KiCad tool directly in the main React document — no iframe.
|
||||
|
|
@ -98,11 +98,20 @@ async function doBoot(opts: BootOptions): Promise<void> {
|
|||
// the wasm boots (the table is seeded in preRun below). No source → empty
|
||||
// table, libs disabled.
|
||||
let symLibTable = "(sym_lib_table\n (version 7)\n)\n";
|
||||
// Writable libs get an empty placeholder FILE at their URI (not just the mount
|
||||
// dir): the symbol-editor save path stat()s the lib file after saving
|
||||
// (SetSymModificationTime -> wxFileName::GetModificationTime), which errors on
|
||||
// a non-existent path. The bytes are virtual (served via window.kicadLibs);
|
||||
// this file only satisfies incidental fs checks.
|
||||
let writableLibUris: string[] = [];
|
||||
if (libsSource) {
|
||||
installLibsProvider(libsSource, log);
|
||||
try {
|
||||
const libsList = await libsSource.listLibs();
|
||||
symLibTable = buildSymLibTable(libsList);
|
||||
writableLibUris = libsList
|
||||
.filter((l) => l.writable)
|
||||
.map((l) => libUri(l.id, true));
|
||||
log(`[libs] seeded ${libsList.length} lib(s) into sym-lib-table`);
|
||||
} catch (e) {
|
||||
log(`[libs] listLibs failed, seeding empty table: ${String(e)}`);
|
||||
|
|
@ -177,10 +186,17 @@ async function doBoot(opts: BootOptions): Promise<void> {
|
|||
const seedKicadConfig = () => {
|
||||
const FS = moduleFS();
|
||||
FS.mkdirTree(KICAD_CONFIG_DIR);
|
||||
// libs: the mount point that pcbjam lib URIs (/mnt/pcbjam/<lib>) live under.
|
||||
// A real dir so any incidental existence check on the URI passes; the lib
|
||||
// contents themselves are served virtually via window.kicadLibs.
|
||||
// libs: the mount points that pcbjam lib URIs (/mnt/pcbjam[-rw]/<lib>) live
|
||||
// under. Real dirs so any incidental existence/backup check on the URI
|
||||
// passes; the lib contents themselves are served virtually via
|
||||
// window.kicadLibs (read-only origins and writable user libs).
|
||||
FS.mkdirTree(PCBJAM_LIB_MOUNT);
|
||||
FS.mkdirTree(PCBJAM_LIB_RW_MOUNT);
|
||||
// Empty placeholder file per writable lib so the editor's post-save
|
||||
// file-times stat succeeds (the real bytes are served via window.kicadLibs).
|
||||
for (const uri of writableLibUris) {
|
||||
if (!FS.analyzePath(uri).exists) FS.writeFile(uri, "");
|
||||
}
|
||||
const writeIfAbsent = (path: string, contents: string) => {
|
||||
if (FS.analyzePath(path).exists) return;
|
||||
FS.writeFile(path, contents);
|
||||
|
|
|
|||
|
|
@ -6,11 +6,13 @@ import { libIdFromUri, libUri } from "./uri";
|
|||
* local folder); the WASM-facing provider below is the same regardless.
|
||||
*/
|
||||
export interface LibInfo {
|
||||
/** Opaque id used in the lib-table URI (/mnt/pcbjam/<id>). */
|
||||
/** Opaque id used in the lib-table URI (/mnt/pcbjam[-rw]/<id>). */
|
||||
id: string;
|
||||
/** Display nickname for the sym-lib-table row. */
|
||||
name: string;
|
||||
description?: string | null;
|
||||
/** Writable (user) lib → mounts under /mnt/pcbjam-rw/ and accepts saves. */
|
||||
writable?: boolean;
|
||||
}
|
||||
|
||||
export interface LibItemInfo {
|
||||
|
|
@ -28,6 +30,17 @@ export interface LibsSource {
|
|||
* null if absent. `kind` is 'symbol' for now.
|
||||
*/
|
||||
getItemBody(libId: string, kind: string, name: string): Promise<string | null>;
|
||||
/**
|
||||
* Persist one item body into a writable (user) lib. Optional: read-only
|
||||
* sources omit it (a save into a non-writable source resolves false).
|
||||
* `body` is a complete fork-native `kicad_symbol_lib` s-expr.
|
||||
*/
|
||||
saveItemBody?(
|
||||
libId: string,
|
||||
kind: string,
|
||||
name: string,
|
||||
body: string,
|
||||
): Promise<boolean>;
|
||||
}
|
||||
|
||||
/** The function the WASM `SCH_IO_PCBJAM_LIB` plugin calls via the JS bridge. */
|
||||
|
|
@ -61,8 +74,11 @@ function sexprEscape(s: string): string {
|
|||
export function buildSymLibTable(libsList: LibInfo[]): string {
|
||||
const rows = libsList.map((l) => {
|
||||
const descr = l.description ? sexprEscape(l.description) : "";
|
||||
// Same plugin type ("PCBJAM") for read-only + writable libs; the rw mount
|
||||
// in the URI is what flips writability (plugin IsLibraryWritable).
|
||||
return ` (lib (name "${sexprEscape(l.name)}")(type "PCBJAM")(uri "${libUri(
|
||||
l.id,
|
||||
l.writable,
|
||||
)}")(options "")(descr "${descr}"))`;
|
||||
});
|
||||
return `(sym_lib_table\n (version 7)\n${rows.join("\n")}${
|
||||
|
|
@ -72,9 +88,10 @@ export function buildSymLibTable(libsList: LibInfo[]): string {
|
|||
|
||||
/**
|
||||
* Install `window.kicadLibs` backed by a `LibsSource`. The plugin calls
|
||||
* `request(op, "/mnt/pcbjam/<id>", arg)`:
|
||||
* `request(op, "/mnt/pcbjam[-rw]/<id>", arg)`:
|
||||
* "list" -> JSON {"symbols":[...]} (symbol names in the lib)
|
||||
* "get" -> the item body s-expr (arg = symbol name)
|
||||
* "get" -> the item body s-expr (arg = symbol name; null if absent)
|
||||
* "save" -> "ok" / null (arg = JSON {"name":..,"body":..})
|
||||
*/
|
||||
export function installLibsProvider(
|
||||
source: LibsSource,
|
||||
|
|
@ -100,6 +117,27 @@ export function installLibsProvider(
|
|||
}
|
||||
case "get":
|
||||
return await source.getItemBody(id, "symbol", arg);
|
||||
case "save": {
|
||||
let parsed: { name?: string; body?: string };
|
||||
try {
|
||||
parsed = JSON.parse(arg) as { name?: string; body?: string };
|
||||
} catch {
|
||||
log(`[libs] save: bad JSON arg`);
|
||||
return null;
|
||||
}
|
||||
if (!parsed.name || !parsed.body) return null;
|
||||
if (!source.saveItemBody) {
|
||||
log(`[libs] save: source has no write support (lib=${id})`);
|
||||
return null;
|
||||
}
|
||||
const ok = await source.saveItemBody(
|
||||
id,
|
||||
"symbol",
|
||||
parsed.name,
|
||||
parsed.body,
|
||||
);
|
||||
return ok ? "ok" : null;
|
||||
}
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
78
web/standalone/src/wasm/libs/spike-writable.ts
Normal file
78
web/standalone/src/wasm/libs/spike-writable.ts
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
import type { LibInfo, LibItemInfo, LibsSource } from "./source";
|
||||
|
||||
/**
|
||||
* 0004-A write-path spike (no backend). Wraps a `LibsSource` with ONE in-memory
|
||||
* writable user lib so the editor's save-symbol flow has a target and we can
|
||||
* exercise the full save → enumerate → load round-trip through the same WASM
|
||||
* bridge — without committing to the backend design first. Gated by
|
||||
* `?libwrite=1`. Saved bodies are mirrored onto `window.__pcbjamSaved` so the
|
||||
* Playwright probe can inspect them. Replaced in 0004-C by real remote writes.
|
||||
*/
|
||||
const SPIKE_RW_LIB_ID = "spike-user";
|
||||
const SPIKE_RW_LIB_NAME = "My Symbols (spike)";
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
__pcbjamSaved?: Record<string, string>;
|
||||
}
|
||||
}
|
||||
|
||||
export function withSpikeWritableLib(
|
||||
inner: LibsSource | null,
|
||||
log: (msg: string) => void,
|
||||
): LibsSource {
|
||||
const store = new Map<string, string>(); // symbol name -> body
|
||||
window.__pcbjamSaved = Object.create(null) as Record<string, string>;
|
||||
|
||||
const isSpike = (libId: string) => libId === SPIKE_RW_LIB_ID;
|
||||
|
||||
return {
|
||||
async listLibs(): Promise<LibInfo[]> {
|
||||
// Resilient to a missing backend: the spike must boot standalone (the
|
||||
// writable lib is in-memory), so an unreachable inner source just yields
|
||||
// no origins rather than failing the whole table.
|
||||
let base: LibInfo[] = [];
|
||||
try {
|
||||
base = inner ? await inner.listLibs() : [];
|
||||
} catch (e) {
|
||||
log(`[libs] spike: inner listLibs failed, origins omitted: ${String(e)}`);
|
||||
}
|
||||
return [
|
||||
...base,
|
||||
{ id: SPIKE_RW_LIB_ID, name: SPIKE_RW_LIB_NAME, writable: true },
|
||||
];
|
||||
},
|
||||
|
||||
async listItems(libId: string): Promise<LibItemInfo[]> {
|
||||
if (isSpike(libId))
|
||||
return [...store.keys()].map((name) => ({ kind: "symbol", name }));
|
||||
return inner ? inner.listItems(libId) : [];
|
||||
},
|
||||
|
||||
async getItemBody(
|
||||
libId: string,
|
||||
kind: string,
|
||||
name: string,
|
||||
): Promise<string | null> {
|
||||
if (isSpike(libId)) return store.get(name) ?? null;
|
||||
return inner ? inner.getItemBody(libId, kind, name) : null;
|
||||
},
|
||||
|
||||
async saveItemBody(
|
||||
libId: string,
|
||||
kind: string,
|
||||
name: string,
|
||||
body: string,
|
||||
): Promise<boolean> {
|
||||
if (!isSpike(libId)) {
|
||||
return inner?.saveItemBody
|
||||
? inner.saveItemBody(libId, kind, name, body)
|
||||
: false;
|
||||
}
|
||||
store.set(name, body);
|
||||
window.__pcbjamSaved![name] = body;
|
||||
log(`[libs] spike saved symbol "${name}" (${body.length} bytes)`);
|
||||
return true;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
|
@ -1,20 +1,29 @@
|
|||
/**
|
||||
* pcbjam lib URIs are absolute POSIX paths under this mount. Absolute so that
|
||||
* pcbjam lib URIs are absolute POSIX paths under these mounts. Absolute so that
|
||||
* KiCad's lib-table URI expansion (ExpandURI -> wxFileName::MakeAbsolute) is a
|
||||
* no-op and the path reaches the plugin/provider unmangled — a "scheme://" URI
|
||||
* gets rewritten to "/scheme:/..." against the cwd, which differs per project.
|
||||
*
|
||||
* Two mount roots encode writability without an extra bridge round-trip (the
|
||||
* plugin's IsLibraryWritable is then a cheap prefix check):
|
||||
* /mnt/pcbjam/<id> read-only origins
|
||||
* /mnt/pcbjam-rw/<id> writable user libs
|
||||
*/
|
||||
export const PCBJAM_LIB_MOUNT = "/mnt/pcbjam";
|
||||
export const PCBJAM_LIB_PREFIX = `${PCBJAM_LIB_MOUNT}/`;
|
||||
export const PCBJAM_LIB_RW_MOUNT = "/mnt/pcbjam-rw";
|
||||
export const PCBJAM_LIB_RW_PREFIX = `${PCBJAM_LIB_RW_MOUNT}/`;
|
||||
|
||||
/** The lib-table URI for a lib id. */
|
||||
export function libUri(id: string): string {
|
||||
return `${PCBJAM_LIB_PREFIX}${id}`;
|
||||
/** The lib-table URI for a lib id (writable libs get the rw mount). */
|
||||
export function libUri(id: string, writable = false): string {
|
||||
return `${writable ? PCBJAM_LIB_RW_PREFIX : PCBJAM_LIB_PREFIX}${id}`;
|
||||
}
|
||||
|
||||
/** Recover the lib id from a "/mnt/pcbjam/<id>" URI (arrives unmangled). */
|
||||
/** Recover the lib id from either mount's URI (arrives unmangled). */
|
||||
export function libIdFromUri(uri: string): string | null {
|
||||
return uri.startsWith(PCBJAM_LIB_PREFIX)
|
||||
? uri.slice(PCBJAM_LIB_PREFIX.length)
|
||||
: null;
|
||||
if (uri.startsWith(PCBJAM_LIB_RW_PREFIX))
|
||||
return uri.slice(PCBJAM_LIB_RW_PREFIX.length);
|
||||
if (uri.startsWith(PCBJAM_LIB_PREFIX))
|
||||
return uri.slice(PCBJAM_LIB_PREFIX.length);
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue