Files
coder/scripts/clidocgen/main.go
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

151 lines
3.8 KiB
Go
Raw Normal View History

2023-01-27 15:10:40 -03:00
package main
import (
"cmp"
2023-01-27 15:10:40 -03:00
"encoding/json"
"os"
2023-03-23 17:42:20 -05:00
"path/filepath"
"slices"
2023-01-27 15:10:40 -03:00
"github.com/coder/coder/v2/enterprise/cli"
"github.com/coder/coder/v2/scripts/atomicwrite"
"github.com/coder/coder/v2/scripts/docgenenv"
2023-03-23 17:42:20 -05:00
"github.com/coder/flog"
"github.com/coder/serpent"
2023-01-27 15:10:40 -03:00
)
// cliIndexRoute holds the "Command Line" manifest route's metadata so the
// generated index page can mirror it through the shared docgenenv.FrontMatter
// emitter (see the frontMatter template func in gen.go). main populates it
// before genTree runs.
var cliIndexRoute docgenenv.Route
2023-01-27 15:10:40 -03:00
2023-03-23 17:42:20 -05:00
func deleteEmptyDirs(dir string) error {
return filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
2023-03-23 17:42:20 -05:00
if !info.IsDir() {
return nil
}
ents, err := os.ReadDir(path)
if err != nil {
return err
}
if len(ents) == 0 {
flog.Infof("deleting empty dir\t %v", path)
err = os.Remove(path)
if err != nil {
return err
}
}
return nil
})
}
2023-03-23 17:42:20 -05:00
func main() {
docgenenv.Prepare()
2023-01-27 15:10:40 -03:00
2023-03-23 17:42:20 -05:00
workdir, err := os.Getwd()
2023-01-27 15:10:40 -03:00
if err != nil {
2023-03-23 17:42:20 -05:00
flog.Fatalf("getwd: %v", err)
}
root := (&cli.RootCmd{})
// wroteMap indexes file paths to commands.
2024-03-17 09:45:26 -05:00
wroteMap := make(map[string]*serpent.Command)
2023-03-23 17:42:20 -05:00
var (
docsDir = filepath.Join(workdir, "docs")
cliMarkdownDir = filepath.Join(docsDir, "reference/cli")
2023-03-23 17:42:20 -05:00
)
if d := os.Getenv("DOCS_DIR"); d != "" {
docsDir = d
cliMarkdownDir = filepath.Join(docsDir, "reference/cli")
}
// Load the manifest up front so the generated index page can mirror the
// "Command Line" route's curated metadata (title/description/icon_path)
// instead of the root command name.
manifestPath := filepath.Join(docsDir, "manifest.json")
man, err := docgenenv.LoadManifest(manifestPath)
if err != nil {
flog.Fatalf("%v", err)
}
cmdLine := man.FindRoute("Reference", "Command Line")
if cmdLine == nil {
flog.Fatalf("could not find Command Line route in manifest %q", manifestPath)
}
// Mirror the whole "Command Line" route (minus its nav children) so the
// index page front matter carries every current and future per-page field
// automatically, the same way the API index mirrors its manifest route.
cliIndexRoute = cliIndexRouteFrom(*cmdLine)
2023-03-23 17:42:20 -05:00
cmd, err := root.Command(root.EnterpriseSubcommands())
if err != nil {
flog.Fatalf("creating command: %v", err)
}
err = genTree(
cliMarkdownDir,
cmd,
wroteMap,
)
if err != nil {
flog.Fatalf("generating markdowns: %v", err)
2023-01-27 15:10:40 -03:00
}
2023-03-23 17:42:20 -05:00
// Delete old files
err = filepath.Walk(cliMarkdownDir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
_, ok := wroteMap[path]
if !ok {
flog.Infof("deleting old doc\t %v", path)
if err := os.Remove(path); err != nil {
return err
}
}
return nil
})
2023-01-27 15:10:40 -03:00
if err != nil {
2023-03-23 17:42:20 -05:00
flog.Fatalf("deleting old docs: %v", err)
2023-01-27 15:10:40 -03:00
}
2023-03-23 17:42:20 -05:00
err = deleteEmptyDirs(cliMarkdownDir)
2023-01-27 15:10:40 -03:00
if err != nil {
2023-03-23 17:42:20 -05:00
flog.Fatalf("deleting empty dirs: %v", err)
2023-01-27 15:10:40 -03:00
}
// Rebuild the "Command Line" route's children from the generated pages.
// cmdLine aliases the manifest loaded above, so mutating it updates the
// manifest in place.
cmdLine.Children = nil
for path, cmd := range wroteMap {
relPath, err := filepath.Rel(docsDir, path)
if err != nil {
flog.Fatalf("getting relative path: %v", err)
2023-03-23 17:42:20 -05:00
}
child := cliCommandRoute(cmd)
child.Path = relPath
cmdLine.Children = append(cmdLine.Children, child)
2023-03-23 17:42:20 -05:00
}
// Sort children by title because wroteMap iteration is non-deterministic.
slices.SortFunc(cmdLine.Children, func(a, b docgenenv.Route) int {
return cmp.Compare(a.Title, b.Title)
})
2023-01-27 15:10:40 -03:00
manifestByt, err := json.MarshalIndent(man, "", " ")
2023-01-27 15:10:40 -03:00
if err != nil {
2023-03-23 17:42:20 -05:00
flog.Fatalf("marshaling manifest: %v", err)
2023-01-27 15:10:40 -03:00
}
2023-03-23 17:42:20 -05:00
err = atomicwrite.File(manifestPath, manifestByt)
2023-01-27 15:10:40 -03:00
if err != nil {
2023-03-23 17:42:20 -05:00
flog.Fatalf("writing manifest: %v", err)
2023-01-27 15:10:40 -03:00
}
}