/
generate.go
89 lines (69 loc) · 1.64 KB
/
generate.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
// Command docs extracts documentation
// from comments in package checks.
package main
import (
"fmt"
"go/doc"
"go/parser"
"go/token"
"os"
"regexp"
"strings"
)
//go:generate go run generate.go
func main() {
fmt.Println("I'll generate some docs")
deleteMarkdownFiles()
// Parse the Go files in the checks package
fset := &token.FileSet{}
mode := parser.ParseComments
pkgs, err := parser.ParseDir(fset, "../checks", nil, mode)
if err != nil {
fmt.Printf("parsing: %s", err)
return
}
c := pkgs["checks"]
computed := doc.New(c, "github.com/tylermumford/localstatus/checks", 0)
// Prepare to grab the doc comments
// of the types which are checks
regex := regexp.MustCompile(`^check = "(\S+)"`)
count := 0
all := []string{}
const fmode = 0666
for _, t := range computed.Types {
if !strings.HasPrefix(t.Doc, "check =") {
continue
}
submatches := regex.FindStringSubmatch(t.Doc)
name := submatches[1]
// Write the doc comments to their own files
err := os.WriteFile(name+".md", []byte(t.Doc), fmode)
if err != nil {
fmt.Printf("writing docs for "+name+": %s\n", err)
continue
}
count += 1
all = append(all, t.Doc)
}
// Write them all to a single file
allJoined := strings.Join(all, "\n----------\n\n")
os.WriteFile("all.md", []byte(allJoined), fmode)
count++
fmt.Printf("generated %d docs\n", count)
}
func deleteMarkdownFiles() {
files, _ := os.ReadDir(".")
count := 0
for _, f := range files {
if !strings.HasSuffix(f.Name(), ".md") {
continue
}
os.Remove(f.Name())
count += 1
}
fmt.Printf("(deleted %d existing docs)\n", count)
}
type docBit struct {
name string
docs string
}