/
gen.go
181 lines (148 loc) · 3.83 KB
/
gen.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"fmt"
"golang.org/x/tools/go/packages"
"os"
"sort"
"strings"
)
const lotusCfgPkg = "github.com/filecoin-project/lotus/node/config"
const lotusTypesPath = "node/config/types.go"
const boostTypesSrc = "./node/config/types.go"
const lotusCfgPkgAlias = "lotus_config"
// Generate the struct with all the documentation comments that is used when
// writing out the config file
func run() error {
// Get the path to the lotus types file
cfg := &packages.Config{Mode: packages.NeedFiles | packages.NeedSyntax}
pkgs, err := packages.Load(cfg, lotusCfgPkg)
if err != nil {
return fmt.Errorf("error loading packages: %w", err)
}
if packages.PrintErrors(pkgs) > 0 {
os.Exit(1)
}
lotusTypesSrc := ""
for _, pkg := range pkgs {
for _, goFile := range pkg.GoFiles {
if strings.Contains(goFile, lotusTypesPath) {
lotusTypesSrc = goFile
}
}
}
if lotusTypesSrc == "" {
return fmt.Errorf("could not find %s in package %s", lotusTypesSrc, lotusCfgPkg)
}
// Parse the lotus types file
lotusTypes, err := parseTypesFile(lotusTypesSrc)
if err != nil {
return fmt.Errorf("parsing %s: %w", lotusTypesSrc, err)
}
// Parse the boost types file
boostTypes, err := parseTypesFile(boostTypesSrc)
if err != nil {
return fmt.Errorf("parsing %s: %w", boostTypesSrc, err)
}
// Prefix all the lotus types
for k, v := range lotusTypes {
boostTypes[lotusCfgPkgAlias+"."+k] = v
}
// Write out the types as a map of <type name> => <doc field>
var outt []string
for t := range boostTypes {
outt = append(outt, t)
}
sort.Strings(outt)
fmt.Print(`// Code generated by github.com/filecoin-project/lotus/node/config/cfgdocgen. DO NOT EDIT.
package config
type DocField struct {
Name string
Type string
Comment string
}
var Doc = map[string][]DocField{
`)
for _, typeName := range outt {
typ := boostTypes[typeName]
fmt.Printf("\t\"%s\": []DocField{\n", typeName)
for _, f := range typ {
fmt.Println("\t\t{")
fmt.Printf("\t\t\tName: \"%s\",\n", f.Name)
fmt.Printf("\t\t\tType: \"%s\",\n\n", f.Type)
fmt.Printf("\t\t\tComment: `%s`,\n", f.Comment)
fmt.Println("\t\t},")
}
fmt.Printf("\t},\n")
}
fmt.Println(`}`)
return nil
}
type field struct {
Name string
Type string
Comment string
}
func parseTypesFile(src string) (map[string][]field, error) {
tfb, err := os.ReadFile(src)
if err != nil {
return nil, err
}
// could use the ast lib, but this is simpler
type st int
const (
stGlobal st = iota // looking for typedef
stType st = iota // in typedef
)
lines := strings.Split(string(tfb), "\n")
state := stGlobal
var currentType string
var currentComment []string
out := map[string][]field{}
for l := range lines {
line := strings.TrimSpace(lines[l])
switch state {
case stGlobal:
if strings.HasPrefix(line, "type ") {
currentType = line
currentType = strings.TrimPrefix(currentType, "type")
currentType = strings.TrimSuffix(currentType, "{")
currentType = strings.TrimSpace(currentType)
currentType = strings.TrimSuffix(currentType, "struct")
currentType = strings.TrimSpace(currentType)
currentComment = nil
state = stType
continue
}
case stType:
if strings.HasPrefix(line, "// ") {
cline := strings.TrimSpace(strings.TrimPrefix(line, "//"))
currentComment = append(currentComment, cline)
continue
}
comment := currentComment
currentComment = nil
if strings.HasPrefix(line, "}") {
state = stGlobal
continue
}
f := strings.Fields(line)
if len(f) < 2 { // empty or embedded struct
continue
}
name := f[0]
typ := f[1]
out[currentType] = append(out[currentType], field{
Name: name,
Type: typ,
Comment: strings.Join(comment, "\n"),
})
}
}
return out, nil
}
func main() {
if err := run(); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
}