// Command agidocgen builds the AGI developer documentation into docs/agi/. // // The API reference is generated from the same api.json the Terminal WebApp // uses for its in-app help (src/web/Terminal/docs/api.json), so the two can no // longer drift apart. The prose lives in Markdown next to this file: pages/ // holds the guides and reference/ one page per library, whose function // headings are matched to the api.json entries. The playground runs scripts on // the real Otto interpreter, compiled to WebAssembly from ./playground. // // Run it from this folder: // // go run . # build docs/agi // go run . -serve :8132 # build, then serve docs/ at http://localhost:8132/agi/ // // docs/agi/ is gitignored: it is rebuilt from scratch on every run. package main import ( "errors" "flag" "fmt" "os" "path/filepath" "sort" "strings" "time" ) const markerFile = ".agidocgen" type options struct { Out string // output folder, docs/agi API string // Terminal's api.json AgiGo string // src/mod/agi/agi.go, for AgiVersion Icons []string NoWasm bool Serve string BaseHref string } func main() { opt := options{} flag.StringVar(&opt.Out, "out", filepath.Join("..", "..", "docs", "agi"), "output folder (rebuilt from scratch)") flag.StringVar(&opt.API, "api", filepath.Join("..", "..", "src", "web", "Terminal", "docs", "api.json"), "the API definition file") flag.StringVar(&opt.AgiGo, "agi", filepath.Join("..", "..", "src", "mod", "agi", "agi.go"), "source file holding the AgiVersion constant") flag.BoolVar(&opt.NoWasm, "nowasm", false, "skip building the playground WebAssembly module") flag.StringVar(&opt.Serve, "serve", "", "after building, serve the docs folder on this address (e.g. :8132)") flag.Parse() opt.Icons = []string{"icons", filepath.Join("..", "sitegen", "icons")} start := time.Now() rep, err := build(opt) if err != nil { fmt.Fprintln(os.Stderr, "agidocgen:", err) os.Exit(1) } rep.print(os.Stdout) fmt.Printf("Built %s in %s\n", opt.Out, time.Since(start).Round(time.Millisecond)) if opt.Serve != "" { if err := serve(opt.Serve, filepath.Dir(opt.Out), filepath.Base(opt.Out)); err != nil { fmt.Fprintln(os.Stderr, "agidocgen:", err) os.Exit(1) } } } // build runs the whole pipeline and returns what it noticed along the way. func build(opt options) (*report, error) { rep := &report{} api, err := loadAPI(opt.API) if err != nil { return nil, err } version, err := readAgiVersion(opt.AgiGo) if err != nil { return nil, err } icons, err := loadIcons(opt.Icons) if err != nil { return nil, err } guides, err := loadGuides("pages") if err != nil { return nil, err } refs, err := loadReferences("reference", api, rep) if err != nil { return nil, err } samples, err := loadSamples("samples") if err != nil { return nil, err } if err := prepareOutput(opt.Out); err != nil { return nil, err } site := &siteModel{ Version: version, API: api, Guides: guides, Refs: refs, Samples: samples, Icons: icons, } if err := site.render(opt.Out, rep); err != nil { return nil, err } if opt.NoWasm { rep.notes = append(rep.notes, "Playground WebAssembly skipped (-nowasm); the playground will not run scripts.") } else if err := buildWasm(filepath.Join(opt.Out, "playground")); err != nil { return nil, err } return rep, nil } // prepareOutput empties the output folder. It only ever deletes a folder this // tool created (it carries the marker file), so a wrong -out cannot wipe // anything else. func prepareOutput(out string) error { entries, err := os.ReadDir(out) if errors.Is(err, os.ErrNotExist) { if err := os.MkdirAll(out, 0o755); err != nil { return err } } else if err != nil { return err } if len(entries) > 0 { if _, err := os.Stat(filepath.Join(out, markerFile)); err != nil { return fmt.Errorf("%s is not empty and was not made by agidocgen (no %s); refusing to clear it", out, markerFile) } for _, e := range entries { if err := os.RemoveAll(filepath.Join(out, e.Name())); err != nil { return err } } } return os.WriteFile(filepath.Join(out, markerFile), []byte("Generated by scripts/agidocgen. Everything here is rebuilt on every run.\n"), 0o644) } // report collects the drift between api.json and the Markdown pages, so a // maintainer sees what to add where. type report struct { missingInAPI []string // documented in Markdown, absent from api.json noHeading []string // in api.json, no heading in its reference page noPage []string // api.json sections without a reference page pageOnly []string // reference pages without an api.json section notes []string } func (r *report) print(w *os.File) { section := func(title string, items []string) { if len(items) == 0 { return } sort.Strings(items) fmt.Fprintf(w, "\n%s (%d):\n", title, len(items)) for _, it := range items { fmt.Fprintln(w, " -", it) } } section("Documented in reference/*.md but missing from api.json (Terminal's in-app help)", r.missingInAPI) section("api.json sections without a reference page (drawn from api.json alone)", r.noPage) section("Reference pages without an api.json section", r.pageOnly) if len(r.noHeading) > 0 { fmt.Fprintf(w, "\napi.json functions without a heading in reference/*.md, placed after their api.json neighbour (%d):\n %s\n", len(r.noHeading), strings.Join(r.noHeading, ", ")) } for _, n := range r.notes { fmt.Fprintln(w, "\n"+n) } if len(r.missingInAPI)+len(r.noPage)+len(r.pageOnly) == 0 { fmt.Fprintln(w, "\napi.json and the reference pages agree.") } fmt.Fprintln(w, strings.Repeat("-", 40)) }