main.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. // Command agidocgen builds the AGI developer documentation into docs/agi/.
  2. //
  3. // The API reference is generated from the same api.json the Terminal WebApp
  4. // uses for its in-app help (src/web/Terminal/docs/api.json), so the two can no
  5. // longer drift apart. The prose lives in Markdown next to this file: pages/
  6. // holds the guides and reference/ one page per library, whose function
  7. // headings are matched to the api.json entries. The playground runs scripts on
  8. // the real Otto interpreter, compiled to WebAssembly from ./playground.
  9. //
  10. // Run it from this folder:
  11. //
  12. // go run . # build docs/agi
  13. // go run . -serve :8132 # build, then serve docs/ at http://localhost:8132/agi/
  14. //
  15. // docs/agi/ is gitignored: it is rebuilt from scratch on every run.
  16. package main
  17. import (
  18. "errors"
  19. "flag"
  20. "fmt"
  21. "os"
  22. "path/filepath"
  23. "sort"
  24. "strings"
  25. "time"
  26. )
  27. const markerFile = ".agidocgen"
  28. type options struct {
  29. Out string // output folder, docs/agi
  30. API string // Terminal's api.json
  31. AgiGo string // src/mod/agi/agi.go, for AgiVersion
  32. Icons []string
  33. NoWasm bool
  34. Serve string
  35. BaseHref string
  36. }
  37. func main() {
  38. opt := options{}
  39. flag.StringVar(&opt.Out, "out", filepath.Join("..", "..", "docs", "agi"), "output folder (rebuilt from scratch)")
  40. flag.StringVar(&opt.API, "api", filepath.Join("..", "..", "src", "web", "Terminal", "docs", "api.json"), "the API definition file")
  41. flag.StringVar(&opt.AgiGo, "agi", filepath.Join("..", "..", "src", "mod", "agi", "agi.go"), "source file holding the AgiVersion constant")
  42. flag.BoolVar(&opt.NoWasm, "nowasm", false, "skip building the playground WebAssembly module")
  43. flag.StringVar(&opt.Serve, "serve", "", "after building, serve the docs folder on this address (e.g. :8132)")
  44. flag.Parse()
  45. opt.Icons = []string{"icons", filepath.Join("..", "sitegen", "icons")}
  46. start := time.Now()
  47. rep, err := build(opt)
  48. if err != nil {
  49. fmt.Fprintln(os.Stderr, "agidocgen:", err)
  50. os.Exit(1)
  51. }
  52. rep.print(os.Stdout)
  53. fmt.Printf("Built %s in %s\n", opt.Out, time.Since(start).Round(time.Millisecond))
  54. if opt.Serve != "" {
  55. if err := serve(opt.Serve, filepath.Dir(opt.Out), filepath.Base(opt.Out)); err != nil {
  56. fmt.Fprintln(os.Stderr, "agidocgen:", err)
  57. os.Exit(1)
  58. }
  59. }
  60. }
  61. // build runs the whole pipeline and returns what it noticed along the way.
  62. func build(opt options) (*report, error) {
  63. rep := &report{}
  64. api, err := loadAPI(opt.API)
  65. if err != nil {
  66. return nil, err
  67. }
  68. version, err := readAgiVersion(opt.AgiGo)
  69. if err != nil {
  70. return nil, err
  71. }
  72. icons, err := loadIcons(opt.Icons)
  73. if err != nil {
  74. return nil, err
  75. }
  76. guides, err := loadGuides("pages")
  77. if err != nil {
  78. return nil, err
  79. }
  80. refs, err := loadReferences("reference", api, rep)
  81. if err != nil {
  82. return nil, err
  83. }
  84. samples, err := loadSamples("samples")
  85. if err != nil {
  86. return nil, err
  87. }
  88. if err := prepareOutput(opt.Out); err != nil {
  89. return nil, err
  90. }
  91. site := &siteModel{
  92. Version: version,
  93. API: api,
  94. Guides: guides,
  95. Refs: refs,
  96. Samples: samples,
  97. Icons: icons,
  98. }
  99. if err := site.render(opt.Out, rep); err != nil {
  100. return nil, err
  101. }
  102. if opt.NoWasm {
  103. rep.notes = append(rep.notes, "Playground WebAssembly skipped (-nowasm); the playground will not run scripts.")
  104. } else if err := buildWasm(filepath.Join(opt.Out, "playground")); err != nil {
  105. return nil, err
  106. }
  107. return rep, nil
  108. }
  109. // prepareOutput empties the output folder. It only ever deletes a folder this
  110. // tool created (it carries the marker file), so a wrong -out cannot wipe
  111. // anything else.
  112. func prepareOutput(out string) error {
  113. entries, err := os.ReadDir(out)
  114. if errors.Is(err, os.ErrNotExist) {
  115. if err := os.MkdirAll(out, 0o755); err != nil {
  116. return err
  117. }
  118. } else if err != nil {
  119. return err
  120. }
  121. if len(entries) > 0 {
  122. if _, err := os.Stat(filepath.Join(out, markerFile)); err != nil {
  123. return fmt.Errorf("%s is not empty and was not made by agidocgen (no %s); refusing to clear it", out, markerFile)
  124. }
  125. for _, e := range entries {
  126. if err := os.RemoveAll(filepath.Join(out, e.Name())); err != nil {
  127. return err
  128. }
  129. }
  130. }
  131. return os.WriteFile(filepath.Join(out, markerFile), []byte("Generated by scripts/agidocgen. Everything here is rebuilt on every run.\n"), 0o644)
  132. }
  133. // report collects the drift between api.json and the Markdown pages, so a
  134. // maintainer sees what to add where.
  135. type report struct {
  136. missingInAPI []string // documented in Markdown, absent from api.json
  137. noHeading []string // in api.json, no heading in its reference page
  138. noPage []string // api.json sections without a reference page
  139. pageOnly []string // reference pages without an api.json section
  140. notes []string
  141. }
  142. func (r *report) print(w *os.File) {
  143. section := func(title string, items []string) {
  144. if len(items) == 0 {
  145. return
  146. }
  147. sort.Strings(items)
  148. fmt.Fprintf(w, "\n%s (%d):\n", title, len(items))
  149. for _, it := range items {
  150. fmt.Fprintln(w, " -", it)
  151. }
  152. }
  153. section("Documented in reference/*.md but missing from api.json (Terminal's in-app help)", r.missingInAPI)
  154. section("api.json sections without a reference page (drawn from api.json alone)", r.noPage)
  155. section("Reference pages without an api.json section", r.pageOnly)
  156. if len(r.noHeading) > 0 {
  157. 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, ", "))
  158. }
  159. for _, n := range r.notes {
  160. fmt.Fprintln(w, "\n"+n)
  161. }
  162. if len(r.missingInAPI)+len(r.noPage)+len(r.pageOnly) == 0 {
  163. fmt.Fprintln(w, "\napi.json and the reference pages agree.")
  164. }
  165. fmt.Fprintln(w, strings.Repeat("-", 40))
  166. }