-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
99 lines (81 loc) 路 2.05 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
// Copyright 2022 Outreach Corporation. All Rights Reserved.
// Description: This file generates documentation for all functions
// by reading the source code and outputing markdown.
package main
import (
"fmt"
"go/build"
"os"
"path/filepath"
"strings"
// We're using embed
_ "embed"
"github.com/pkg/errors"
"github.com/princjef/gomarkdoc"
"github.com/princjef/gomarkdoc/lang"
"github.com/princjef/gomarkdoc/logger"
)
//go:embed functions.md.tpl
var functionsTemplate string
type file struct {
Name string
Contents string
}
// generateMarkdown generates the markdown files for all functions.
func generateMarkdown() ([]file, error) {
// Create a renderer to output data
out, err := gomarkdoc.NewRenderer(
gomarkdoc.WithTemplateOverride("func", functionsTemplate),
)
if err != nil {
return nil, err
}
buildPkg, err := build.ImportDir("../internal/codegen", build.ImportComment)
if err != nil {
return nil, err
}
// Create a documentation package from the build representation of our
// package.
log := logger.New(logger.DebugLevel)
pkg, err := lang.NewPackageFromBuild(log, buildPkg)
if err != nil {
return nil, err
}
files := make([]file, 0)
for _, typ := range pkg.Types() {
if !strings.HasPrefix(typ.Name(), "Tpl") {
continue
}
for _, f := range typ.Methods() {
txt, err := out.Func(f)
if err != nil {
return nil, errors.Wrapf(err, "failed to generate documentation for %s", f.Name())
}
files = append(files, file{
Name: strings.ToLower(strings.TrimPrefix(typ.Name(), "Tpl")) + "." + f.Name(),
Contents: txt,
})
}
}
return files, nil
}
// saveMarkdown writes the markdown files to disk.
func saveMarkdown(files []file) error {
for _, f := range files {
if err := os.WriteFile(filepath.Join("content", "en", "functions", f.Name+".md"), []byte(f.Contents), 0644); err != nil {
return err
}
}
return nil
}
func main() {
files, err := generateMarkdown()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if err := saveMarkdown(files); err != nil {
fmt.Println(err)
os.Exit(1)
}
}