Plugins that need their own UI and richer themes should not force breaking contract changes later, so the full shape is published now (uiApi 2.0, bridgeApi 1.0) and every entry is marked stable or experimental: - docs/plugin-architecture.md: plugin-owned views (sidebar, vm.tab, settings, dialog) and the bridge methods (§5.6), the finer theme contract with slot catalogue, layers, layout and icons (§5.1), and the contract evolution rules (§6.3). - ui-contract.ts and the new bridgeContract.ts list stable and experimental entries; the build fails if a stable entry is not emitted or an emitted entry is still experimental, and writes bridge-contract.json next to ui-contract.json. - internal/plugin/contract.go mirrors them as catalogs (contribution points, slots with accepted content, view locations, layers, layout, icons, bridge methods and events) and derives HostFeatures; tests keep Go and TypeScript identical and require every contributes field to be catalogued. - Manifests gain views, theme layout and engines.bridgeApi. Unknown slots, locations, layout values and icon names are rejected; experimental use, unknown fields and missing engines ranges are doctor warnings. Declared-but-unrendered entries (vmCards, statusItems, settingsPages, vmTemplates, runtimeSources, runtimes, uiScripts, diagnostics, theme icons and brandMark, panels outside settings.plugin) are now marked experimental. - The panel bridge resolves methods through the catalog, adds hello and refuses experimental methods; panels get theme.changed and locale.changed events; the Sidecar handshake and the Go SDK carry uiApi, bridgeApi and features. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
131 lines
6.7 KiB
JavaScript
131 lines
6.7 KiB
JavaScript
// Checks the UI and bridge contracts (docs/plugin-architecture.md §5.1, §5.6, §6.3)
|
|
// and writes ui-contract.json / bridge-contract.json for plugin authors.
|
|
//
|
|
// Rule "public means implemented": every stable part, state attribute, slot and
|
|
// layer must be emitted by the host, and an experimental entry the host already
|
|
// emits must be promoted to stable in the same change.
|
|
import fs from 'node:fs'
|
|
import path from 'node:path'
|
|
import { fileURLToPath } from 'node:url'
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
|
const rootDir = path.resolve(__dirname, '..')
|
|
const srcDir = path.join(rootDir, 'src')
|
|
|
|
const contractSrc = fs.readFileSync(path.join(srcDir, 'styles', 'ui-contract.ts'), 'utf8')
|
|
const bridgeSrc = fs.readFileSync(path.join(srcDir, 'lib', 'bridgeContract.ts'), 'utf8')
|
|
const tokensCss = fs.readFileSync(path.join(srcDir, 'styles', 'tokens.css'), 'utf8')
|
|
|
|
function extractArray(src, name) {
|
|
const re = new RegExp(`export const ${name} = \\[([\\s\\S]*?)\\] as const`)
|
|
const m = src.match(re)
|
|
if (!m) throw new Error(`Could not parse ${name}`)
|
|
return [...m[1].matchAll(/'([^']+)'/g)].map((x) => x[1])
|
|
}
|
|
function extractVersion(src, name) {
|
|
const m = src.match(new RegExp(`${name}\\s*=\\s*'([^']+)'`))
|
|
if (!m) throw new Error(`Could not parse ${name}`)
|
|
return m[1]
|
|
}
|
|
const esc = (s) => s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
|
|
|
|
const uiApi = extractVersion(contractSrc, 'UI_API_VERSION')
|
|
const stable = {
|
|
parts: extractArray(contractSrc, 'UI_CONTRACT_PARTS'),
|
|
stateAttrs: extractArray(contractSrc, 'UI_CONTRACT_STATE_ATTRS'),
|
|
slots: extractArray(contractSrc, 'UI_CONTRACT_SLOTS'),
|
|
layers: extractArray(contractSrc, 'UI_CONTRACT_LAYERS'),
|
|
tokens: extractArray(contractSrc, 'UI_CONTRACT_TOKENS'),
|
|
}
|
|
const experimental = {
|
|
parts: extractArray(contractSrc, 'UI_CONTRACT_PARTS_EXPERIMENTAL'),
|
|
stateAttrs: extractArray(contractSrc, 'UI_CONTRACT_STATE_ATTRS_EXPERIMENTAL'),
|
|
itemStates: extractArray(contractSrc, 'UI_CONTRACT_ITEM_STATES_EXPERIMENTAL'),
|
|
slots: extractArray(contractSrc, 'UI_CONTRACT_SLOTS_EXPERIMENTAL'),
|
|
layers: extractArray(contractSrc, 'UI_CONTRACT_LAYERS_EXPERIMENTAL'),
|
|
layoutOptions: extractArray(contractSrc, 'UI_CONTRACT_LAYOUT_OPTIONS_EXPERIMENTAL'),
|
|
icons: extractArray(contractSrc, 'UI_CONTRACT_ICONS_EXPERIMENTAL'),
|
|
viewLocations: extractArray(contractSrc, 'UI_CONTRACT_VIEW_LOCATIONS_EXPERIMENTAL'),
|
|
sdkEvents: extractArray(contractSrc, 'UI_CONTRACT_SDK_EVENTS_EXPERIMENTAL'),
|
|
}
|
|
const bridgeApi = extractVersion(bridgeSrc, 'BRIDGE_API_VERSION')
|
|
const bridge = {
|
|
methods: extractArray(bridgeSrc, 'BRIDGE_METHODS'),
|
|
events: extractArray(bridgeSrc, 'BRIDGE_EVENTS'),
|
|
experimentalMethods: extractArray(bridgeSrc, 'BRIDGE_METHODS_EXPERIMENTAL'),
|
|
experimentalEvents: extractArray(bridgeSrc, 'BRIDGE_EVENTS_EXPERIMENTAL'),
|
|
}
|
|
|
|
// Host source the contract is emitted from (contract/SDK definitions excluded).
|
|
function readSources(dir, exts) {
|
|
let buf = ''
|
|
for (const entry of fs.readdirSync(dir, { withFileTypes: true })) {
|
|
const full = path.join(dir, entry.name)
|
|
if (entry.isDirectory()) buf += readSources(full, exts)
|
|
else if (exts.some((e) => entry.name.endsWith(e)) && !/ui-contract\.ts$|bridgeContract\.ts$|\.gen\.ts$/.test(entry.name)) {
|
|
buf += '\n' + fs.readFileSync(full, 'utf8')
|
|
}
|
|
}
|
|
return buf
|
|
}
|
|
const allTsx = readSources(srcDir, ['.tsx'])
|
|
const allCode = readSources(srcDir, ['.ts', '.tsx'])
|
|
|
|
const hasClass = (p) => new RegExp(`(?<![a-zA-Z0-9_-])${esc(p)}(?![a-zA-Z0-9_-])`).test(allTsx)
|
|
const camel = (attr) => attr.replace(/^data-/, '').replace(/-([a-z])/g, (_, c) => c.toUpperCase())
|
|
const hasAttr = (a) => allCode.includes(a) || new RegExp(`dataset\\.${camel(a)}\\b`).test(allCode)
|
|
const hasSlot = (s) => allTsx.includes(`data-rg-slot="${s}"`)
|
|
const hasLayer = (l) => allTsx.includes(`data-rg-layer="${l}"`)
|
|
|
|
const errors = []
|
|
const fail = (msg, list) => { if (list.length) errors.push(`${msg}: ${list.join(', ')}`) }
|
|
|
|
fail('stable parts not emitted by any component', stable.parts.filter((p) => !hasClass(p)))
|
|
fail('stable state attributes never set', stable.stateAttrs.filter((a) => !hasAttr(a)))
|
|
fail('stable slots without a data-rg-slot anchor', stable.slots.filter((s) => !hasSlot(s)))
|
|
fail('stable layers without a data-rg-layer element', stable.layers.filter((l) => !hasLayer(l)))
|
|
fail('stable tokens missing in tokens.css', stable.tokens.filter((tk) => !tokensCss.includes(`${tk}:`)))
|
|
|
|
fail('experimental parts already emitted (promote them to UI_CONTRACT_PARTS)', experimental.parts.filter(hasClass))
|
|
fail('experimental slots already anchored (promote them to UI_CONTRACT_SLOTS)', experimental.slots.filter(hasSlot))
|
|
fail('experimental layers already rendered (promote them to UI_CONTRACT_LAYERS)', experimental.layers.filter(hasLayer))
|
|
|
|
const overlap = (a, b) => a.filter((x) => b.includes(x))
|
|
fail('entries listed as both stable and experimental (parts)', overlap(stable.parts, experimental.parts))
|
|
fail('entries listed as both stable and experimental (state attributes)', overlap(stable.stateAttrs, experimental.stateAttrs))
|
|
fail('entries listed as both stable and experimental (slots)', overlap(stable.slots, experimental.slots))
|
|
fail('entries listed as both stable and experimental (layers)', overlap(stable.layers, experimental.layers))
|
|
fail('bridge methods listed as both stable and experimental', overlap(bridge.methods, bridge.experimentalMethods))
|
|
fail('bridge events listed as both stable and experimental', overlap(bridge.events, bridge.experimentalEvents))
|
|
|
|
const knownParts = [...stable.parts, ...experimental.parts]
|
|
fail('item states on unknown parts', experimental.itemStates.filter((s) => !knownParts.includes(s.split(':')[0])))
|
|
for (const [name, list] of Object.entries({ ...stable, ...experimental, ...bridge })) {
|
|
const dupes = list.filter((x, i) => list.indexOf(x) !== i)
|
|
fail(`duplicate entries in ${name}`, dupes)
|
|
}
|
|
|
|
if (errors.length > 0) {
|
|
for (const e of errors) console.error(`[ui-contract] ERROR: ${e}`)
|
|
process.exit(1)
|
|
}
|
|
|
|
const write = (file, data) => {
|
|
const text = JSON.stringify(data, null, 2) + '\n'
|
|
fs.writeFileSync(path.join(rootDir, file), text, 'utf8')
|
|
const distDir = path.join(rootDir, 'dist')
|
|
if (fs.existsSync(distDir)) fs.writeFileSync(path.join(distDir, file), text, 'utf8')
|
|
}
|
|
write('ui-contract.json', { uiApi, ...stable, experimental })
|
|
write('bridge-contract.json', {
|
|
bridgeApi,
|
|
methods: bridge.methods,
|
|
events: bridge.events,
|
|
experimental: { methods: bridge.experimentalMethods, events: bridge.experimentalEvents },
|
|
})
|
|
console.log(
|
|
`[ui-contract] OK (uiApi=${uiApi}, parts=${stable.parts.length}+${experimental.parts.length}x, ` +
|
|
`tokens=${stable.tokens.length}, slots=${stable.slots.length}+${experimental.slots.length}x, ` +
|
|
`bridgeApi=${bridgeApi}, methods=${bridge.methods.length}+${bridge.experimentalMethods.length}x)`,
|
|
)
|