Its version is the build module's own, so a new generator is a new build of mesh-build-go and mesh-build-node and a new fingerprint of what they compare; no repository of its own. Moved from seatgen c1fdd7c unchanged but for the import path. Claude-Session: https://claude.ai/code/session_01JG73KJNrH5EgFXMKKYSrig
314 lines
9.0 KiB
Go
314 lines
9.0 KiB
Go
package seatgen
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/seatdef"
|
|
)
|
|
|
|
const tsHeader = "// Code generated by seatgen from the seat definitions; DO NOT EDIT.\n\n"
|
|
|
|
// TSSeatDir is where a seat defined whole has its npm package (ADR 0625 §1): the indexer counts the npm package a
|
|
// lock resolves, so each such seat is a package of its own and a module is rebuilt only for the seats it uses.
|
|
func TSSeatDir(seat string) string { return "ts/seats/" + pkgName(seat) }
|
|
|
|
// TSSeatPackage is the npm package name of a seat defined whole, beside the seat set's package root.
|
|
func TSSeatPackage(root, seat string) string { return root + "-" + seat }
|
|
|
|
const tsConfig = `{
|
|
"compilerOptions": {
|
|
"target": "ES2022",
|
|
"module": "NodeNext",
|
|
"moduleResolution": "NodeNext",
|
|
"declaration": true,
|
|
"outDir": "dist",
|
|
"rootDir": "src",
|
|
"strict": true,
|
|
"skipLibCheck": true,
|
|
"forceConsistentCasingInFileNames": true
|
|
},
|
|
"include": ["src/**/*.ts"]
|
|
}
|
|
`
|
|
|
|
// tsPackage writes one npm package's package.json and tsconfig.json at dir.
|
|
func tsPackage(out map[string][]byte, dir, name, description string) {
|
|
pkg := map[string]any{
|
|
"name": name, "version": "0.1.0", "type": "module",
|
|
"description": description,
|
|
"exports": map[string]any{".": "./dist/index.js"},
|
|
"files": []any{"dist"},
|
|
"scripts": map[string]any{"build": "tsc", "prepare": "tsc"},
|
|
"devDependencies": map[string]any{"typescript": "^5.6.0"},
|
|
}
|
|
raw, _ := json.MarshalIndent(pkg, "", " ")
|
|
out[dir+"/package.json"] = append(raw, '\n')
|
|
out[dir+"/tsconfig.json"] = []byte(tsConfig)
|
|
}
|
|
|
|
// tsFiles writes the npm side: the seat set's package at ts/ (the seat list alone, ADR 0625 §2) and one package per
|
|
// seat defined whole at TSSeatDir (ADR 0625 §1), holding the parts it includes whole and importing nothing outside it.
|
|
func tsFiles(m *Module, out map[string][]byte) error {
|
|
tsPackage(out, "ts", m.TSPackage, "The mesh's seat set, generated from every seat's definition.")
|
|
out["ts/src/index.ts"] = []byte(tsHeader + "export * as seats from \"./seats.js\";\n")
|
|
out["ts/src/seats.ts"] = tsSeatList(m)
|
|
partTypes := map[string]*typer{}
|
|
partFiles := map[string][]byte{}
|
|
for _, name := range sortedKeys(m.Parts) {
|
|
p := m.Parts[name]
|
|
d := seatdef.Definition{Grain: seatdef.Whole, Verbs: p.Verbs, Events: p.Events, Defs: p.Defs}
|
|
tp, err := declareAll(d, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
partTypes[name] = tp
|
|
var b bytes.Buffer
|
|
b.WriteString(tsHeader)
|
|
fmt.Fprintf(&b, "// The part %q several seats include whole: %s\n\n", p.Part, oneLine(p.Description))
|
|
tsNames(&b, "verbs", seatdef.VerbNames(p.Verbs))
|
|
tsNames(&b, "events", seatdef.MessageNames(p.Events))
|
|
tsTypes(&b, tp)
|
|
partFiles[name] = b.Bytes()
|
|
}
|
|
for _, name := range m.Order {
|
|
d := m.Seats[name]
|
|
if d.Grain != seatdef.Whole {
|
|
continue
|
|
}
|
|
dir := TSSeatDir(name)
|
|
tsPackage(out, dir, TSSeatPackage(m.TSPackage, name), fmt.Sprintf("The contract of the seat %s, generated from its definition.", name))
|
|
raw := m.Raw[name]
|
|
fromPart := map[string]string{}
|
|
for _, inc := range raw.Includes {
|
|
for _, t := range partTypes[inc].types {
|
|
fromPart[t.name] = inc
|
|
}
|
|
out[dir+"/src/parts/"+inc+".ts"] = partFiles[inc]
|
|
}
|
|
tp, err := declareAll(d, fromPart)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var b bytes.Buffer
|
|
b.WriteString(tsHeader)
|
|
fmt.Fprintf(&b, "// The contract of the seat %q, defined whole (seats/%s/seat.json): %s\n\n", d.Seat, d.Seat, oneLine(d.Description))
|
|
for _, inc := range raw.Includes {
|
|
fmt.Fprintf(&b, "import type * as %s from \"./parts/%s.js\";\n\n", tsName(inc), inc)
|
|
}
|
|
fmt.Fprintf(&b, "export const seat = %q;\n\n", d.Seat)
|
|
tsNames(&b, "verbs", seatdef.VerbNames(d.Verbs))
|
|
tsNames(&b, "accepts", seatdef.MessageNames(d.Accepts))
|
|
tsNames(&b, "events", seatdef.MessageNames(d.Events))
|
|
if f := d.Family; f != nil {
|
|
fmt.Fprintf(&b, "export const family = { name: %q, maxAgeDays: %d", f.Name, f.MaxAgeDays)
|
|
b.WriteString(" } as const;\n\n")
|
|
}
|
|
for _, e := range d.Events {
|
|
subj := seatdef.EventSubject(d.Seat, e.Name)
|
|
if e.ByCaller {
|
|
fmt.Fprintf(&b, "export function %sSubject(caller: string): string {\n return %q + caller;\n}\n\n", tsName(e.Name), subj+".")
|
|
} else {
|
|
fmt.Fprintf(&b, "export const %sSubject = %q;\n\n", tsName(e.Name), subj)
|
|
}
|
|
}
|
|
for _, a := range d.Accepts {
|
|
subj := seatdef.AcceptSubject(d.Seat, a.Name)
|
|
if a.ByCaller {
|
|
fmt.Fprintf(&b, "export function %sSubject(caller: string): string {\n return %q + caller;\n}\n\n", tsName(a.Name), subj+".")
|
|
} else {
|
|
fmt.Fprintf(&b, "export const %sSubject = %q;\n\n", tsName(a.Name), subj)
|
|
}
|
|
}
|
|
if len(d.HolderEvents) > 0 {
|
|
tsNames(&b, "holderEvents", holderNames(d.HolderEvents))
|
|
for _, e := range d.HolderEvents {
|
|
before, after, _ := strings.Cut(e.Subject, "<holder>")
|
|
fmt.Fprintf(&b, "export function %sHolderSubject(holder: string): string {\n return %q + holder + %q;\n}\n\n",
|
|
tsName(e.Name), before, after)
|
|
}
|
|
}
|
|
for _, n := range sortedKeys(fromPart) {
|
|
fmt.Fprintf(&b, "export type %s = %s.%s;\n", n, tsName(fromPart[n]), n)
|
|
}
|
|
if len(fromPart) > 0 {
|
|
b.WriteString("\n")
|
|
}
|
|
tsTypes(&b, tp)
|
|
out[dir+"/src/index.ts"] = b.Bytes()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func holderNames(es []seatdef.HolderEvent) []string {
|
|
out := make([]string, len(es))
|
|
for i, e := range es {
|
|
out[i] = e.Name
|
|
}
|
|
return out
|
|
}
|
|
|
|
func tsNames(b *bytes.Buffer, what string, names []string) {
|
|
if len(names) == 0 {
|
|
return
|
|
}
|
|
fmt.Fprintf(b, "export const %s = {\n", what)
|
|
for _, n := range names {
|
|
fmt.Fprintf(b, " %s: %q,\n", tsName(n), n)
|
|
}
|
|
b.WriteString("} as const;\n\n")
|
|
}
|
|
|
|
func tsTypes(b *bytes.Buffer, tp *typer) {
|
|
for _, t := range tp.types {
|
|
if d := strings.TrimSpace(t.desc); d != "" {
|
|
fmt.Fprintf(b, "/** %s */\n", oneLine(d))
|
|
}
|
|
if t.t.kind != "struct" {
|
|
fmt.Fprintf(b, "export type %s = %s;\n\n", t.name, tsType(t.t))
|
|
continue
|
|
}
|
|
fmt.Fprintf(b, "export interface %s {\n", t.name)
|
|
for _, f := range t.t.fields {
|
|
if f.desc != "" {
|
|
fmt.Fprintf(b, " /** %s */\n", oneLine(f.desc))
|
|
}
|
|
opt := "?"
|
|
if f.required {
|
|
opt = ""
|
|
}
|
|
fmt.Fprintf(b, " %s%s: %s;\n", strconv.Quote(f.json), opt, tsType(f.t))
|
|
}
|
|
b.WriteString("}\n\n")
|
|
}
|
|
}
|
|
|
|
func tsType(t *typ) string {
|
|
switch t.kind {
|
|
case "string":
|
|
if len(t.enum) > 0 {
|
|
parts := make([]string, len(t.enum))
|
|
for i, e := range t.enum {
|
|
parts[i] = strconv.Quote(e)
|
|
}
|
|
return strings.Join(parts, " | ")
|
|
}
|
|
return "string"
|
|
case "integer", "number":
|
|
return "number"
|
|
case "boolean":
|
|
return "boolean"
|
|
case "object":
|
|
return "Record<string, unknown>"
|
|
case "map":
|
|
return "Record<string, " + tsType(t.elem) + ">"
|
|
case "slice":
|
|
el := tsType(t.elem)
|
|
if strings.Contains(el, "|") {
|
|
el = "(" + el + ")"
|
|
}
|
|
return el + "[]"
|
|
case "ref":
|
|
return t.ref
|
|
}
|
|
return "unknown"
|
|
}
|
|
|
|
// tsSeatList is ts/src/seats.ts: the seat list as data, and every seat's name.
|
|
func tsSeatList(m *Module) []byte {
|
|
var b bytes.Buffer
|
|
b.WriteString(tsHeader)
|
|
b.WriteString(`// The mesh's seat list, generated from every seat's definition (design 26).
|
|
|
|
export interface Verb {
|
|
name: string;
|
|
description: string;
|
|
input?: Record<string, unknown>;
|
|
output?: Record<string, unknown>;
|
|
optional?: boolean;
|
|
replaces?: string[];
|
|
}
|
|
|
|
export interface Seat {
|
|
name: string;
|
|
scope: "mesh" | "node";
|
|
grain: "whole" | "seat-list";
|
|
decision: string;
|
|
delivers?: string;
|
|
replicated?: boolean;
|
|
accepts?: string[];
|
|
emits?: string[];
|
|
pulledByKind?: boolean;
|
|
yields?: boolean;
|
|
heldByTheEngine?: boolean;
|
|
serves?: Verb[];
|
|
}
|
|
|
|
`)
|
|
b.WriteString("export const names = {\n")
|
|
for _, n := range m.Order {
|
|
fmt.Fprintf(&b, " %s: %q,\n", tsName(n), n)
|
|
}
|
|
b.WriteString("} as const;\n\nexport const list: readonly Seat[] = ")
|
|
var list []map[string]any
|
|
for _, n := range m.Order {
|
|
d := m.Seats[n]
|
|
if d.HolderDeclares {
|
|
continue
|
|
}
|
|
e := map[string]any{"name": d.Seat, "scope": d.Scope, "grain": d.Grain, "decision": d.Decision}
|
|
if d.Delivers != "" {
|
|
e["delivers"] = d.Delivers
|
|
}
|
|
if d.Replicated {
|
|
e["replicated"] = true
|
|
}
|
|
if len(d.Accepts) > 0 {
|
|
e["accepts"] = seatdef.MessageNames(d.Accepts)
|
|
}
|
|
if len(d.Events) > 0 {
|
|
e["emits"] = seatdef.MessageNames(d.Events)
|
|
}
|
|
if d.PulledByKind {
|
|
e["pulledByKind"] = true
|
|
}
|
|
if d.Yields {
|
|
e["yields"] = true
|
|
}
|
|
if d.HeldByTheEngine {
|
|
e["heldByTheEngine"] = true
|
|
}
|
|
var verbs []map[string]any
|
|
for _, v := range d.Verbs {
|
|
vm := map[string]any{"name": v.Name, "description": v.Description}
|
|
if v.Input != nil {
|
|
vm["input"] = resolveRefs(v.Input, d.Defs)
|
|
}
|
|
if v.Answer != nil {
|
|
vm["output"] = resolveRefs(v.Answer, d.Defs)
|
|
}
|
|
if v.Optional {
|
|
vm["optional"] = true
|
|
}
|
|
if len(v.Replaces) > 0 {
|
|
vm["replaces"] = v.Replaces
|
|
}
|
|
verbs = append(verbs, vm)
|
|
}
|
|
if len(verbs) > 0 {
|
|
e["serves"] = verbs
|
|
}
|
|
list = append(list, e)
|
|
}
|
|
enc := json.NewEncoder(&b)
|
|
enc.SetEscapeHTML(false)
|
|
enc.SetIndent("", " ")
|
|
_ = enc.Encode(list)
|
|
b.Truncate(b.Len() - 1)
|
|
b.WriteString(";\n")
|
|
return b.Bytes()
|
|
}
|