-
Notifications
You must be signed in to change notification settings - Fork 1
/
docgen.go
124 lines (117 loc) · 3.1 KB
/
docgen.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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/Kunde21/markdownfmt/v2/markdownfmt"
"github.com/gunk/docgen/generate"
"github.com/gunk/docgen/parser"
"github.com/gunk/plugin"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/pluginpb"
)
const modeAppend = "append"
func main() {
plugin.RunMain(new(docPlugin))
}
type docPlugin struct{}
func (p *docPlugin) Generate(req *pluginpb.CodeGeneratorRequest) (*pluginpb.CodeGeneratorResponse, error) {
var (
lang []string
mode, dir string
customHeaderIds generate.CustomHeaderIdsOpt
onlyExternal parser.OnlyExternalOpt
)
if param := req.GetParameter(); param != "" {
ps := strings.Split(param, ",")
for _, p := range ps {
s := strings.Split(p, "=")
if len(s) != 2 {
return nil, fmt.Errorf("could not parse parameter: %s", p)
}
k, v := s[0], s[1]
switch k {
case "lang":
lang = append(lang, v)
case "mode":
if v != modeAppend {
return nil, fmt.Errorf("unknown mode: %s", v)
}
mode = v
case "out":
dir = v
case "custom-header-ids":
boolVal, err := strconv.ParseBool(v)
if err != nil {
return nil, err
}
if boolVal {
customHeaderIds = generate.CustomHeaderIdsOptEnabled
}
case "only_external":
boolVal, err := strconv.ParseBool(v)
if err != nil {
return nil, err
}
if boolVal {
onlyExternal = parser.OnlyExternalEnabled
}
default:
return nil, fmt.Errorf("unknown parameter: %s", k)
}
}
}
// find the source by looping through the protofiles and finding the one
// that matches the FileToGenerate
var source *parser.FileDescWrapper
for _, f := range req.GetProtoFile() {
if strings.Contains(f.GetName(), "all.proto") {
for _, fileToGenerate := range req.FileToGenerate {
if fileToGenerate == f.GetName() {
source = &parser.FileDescWrapper{FileDescriptorProto: f}
break
}
}
}
}
if source == nil {
return nil, fmt.Errorf("no file to generate")
}
base := filepath.Join(filepath.Dir(source.GetName()))
source.DependencyMap = parser.GenerateDependencyMap(source.FileDescriptorProto, req.GetProtoFile())
var buf bytes.Buffer
err := generate.Run(&buf, source, lang, customHeaderIds, onlyExternal)
if err != nil {
// file does not include a service -> just don't do anything
if err == generate.ErrNoServices {
return &pluginpb.CodeGeneratorResponse{
File: []*pluginpb.CodeGeneratorResponse_File{},
}, nil
}
return nil, fmt.Errorf("failed markdown generation: %v", err)
}
if mode == modeAppend {
// Load content from existing all.md
e, err := ioutil.ReadFile(filepath.Join(dir, "all.md"))
if err != nil && !os.IsNotExist(err) {
return nil, err
}
buf = *bytes.NewBuffer(append(e, buf.Bytes()...))
}
formatted, err := markdownfmt.Process("", buf.Bytes())
if err != nil {
return nil, err
}
return &pluginpb.CodeGeneratorResponse{
File: []*pluginpb.CodeGeneratorResponse_File{
{
Name: proto.String(filepath.Join(base, "all.md")),
Content: proto.String(string(formatted)),
},
},
}, nil
}