/
main.go
145 lines (130 loc) · 3.23 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package main
import (
"encoding/gob"
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/fs"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/avamsi/clifr"
"github.com/avamsi/ergo"
"github.com/sanity-io/litter"
"golang.org/x/exp/maps"
)
func onlyElement[T any](s []T) T {
if len(s) == 1 {
return s[0]
}
panic(fmt.Sprintf("want exactly 1 element, got %s", litter.Sdump(s)))
}
func importPath(dir string) string {
cmd := exec.Command("go", "list")
cmd.Dir = dir
return strings.TrimSpace(string(ergo.Must1(cmd.Output())))
}
func docFromField(f *ast.Field) string {
d := strings.TrimSpace(f.Doc.Text())
if d == "" {
d = strings.TrimSpace(f.Comment.Text())
}
return d
}
type doc struct {
Long, Short, Usage string
}
const (
shortDirective = "//clifr:short"
usageDirective = "//clifr:usage"
)
func newDoc(cg *ast.CommentGroup) doc {
if cg == nil {
return doc{}
}
short, usage := "", ""
for _, c := range cg.List {
if strings.HasPrefix(c.Text, shortDirective) {
if short == "" {
short = strings.TrimSpace(strings.TrimPrefix(c.Text, shortDirective))
} else {
panic(fmt.Sprintf("want exactly 1 %s directive, got %s", shortDirective, litter.Sdump(cg)))
}
} else if strings.HasPrefix(c.Text, usageDirective) {
if usage == "" {
usage = strings.TrimSpace(strings.TrimPrefix(c.Text, usageDirective))
} else {
panic(fmt.Sprintf("want exactly 1 %s directive, got %s", usageDirective, litter.Sdump(cg)))
}
}
}
return doc{Long: strings.TrimSpace(cg.Text()), Short: short, Usage: usage}
}
func populateDocsForType(pid string, t ast.Expr, docs map[string]doc) {
if st, ok := t.(*ast.StructType); ok {
for _, f := range st.Fields.List {
for _, n := range f.Names {
id := pid + "." + n.Name
docs[id] = doc{Long: docFromField(f)}
}
}
}
}
func populateDocs(dir string, docs map[string]doc) {
pkgs := ergo.Must1(parser.ParseDir(token.NewFileSet(), dir, nil, parser.ParseComments))
if len(pkgs) == 0 {
return
}
pkg := onlyElement(maps.Values(pkgs))
pkgPath := "main"
if pkg.Name != "main" {
pkgPath = importPath(dir)
}
for _, f := range pkg.Files {
for _, d := range f.Decls {
switch dt := d.(type) {
case *ast.GenDecl:
for _, s := range dt.Specs {
switch st := s.(type) {
case *ast.TypeSpec:
id := pkgPath + "." + st.Name.Name
docs[id] = newDoc(dt.Doc)
populateDocsForType(id, st.Type, docs)
}
}
case *ast.FuncDecl:
if dt.Recv == nil {
// We're only interested in methods for now.
continue
}
pid := pkgPath + "." + onlyElement(dt.Recv.List).Type.(*ast.Ident).Name
id := pid + "." + dt.Name.Name
docs[id] = newDoc(dt.Doc)
for _, p := range dt.Type.Params.List {
populateDocsForType(id, p.Type, docs)
}
}
}
}
}
type Clifr struct{}
func (Clifr) Docs(opts struct{ Out string }) {
docs := map[string]doc{}
cwd := ergo.Must1(os.Getwd())
populateDocs(cwd, docs)
filepath.WalkDir(cwd, func(path string, d fs.DirEntry, err error) error {
ergo.Must0(err)
if d.IsDir() {
populateDocs(path, docs)
}
return nil
})
file := ergo.Must1(os.OpenFile(opts.Out, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644))
defer file.Close()
ergo.Must0(gob.NewEncoder(file).Encode(docs))
}
func main() {
clifr.Execute(Clifr{})
}