-
Notifications
You must be signed in to change notification settings - Fork 0
/
target.go
89 lines (75 loc) · 2.09 KB
/
target.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
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
)
type target struct {
// 语言
Lang string `default:"go" json:"lang" validate:"oneof=go gogo golang java js dart swift python"`
// 输出目录
Output string `default:"." json:"output"`
// 插件列表
Plugins []string `json:"plugins"`
// 选项
Opt string `json:"opt"`
}
func (t *target) opt(plugin *plugin) (opt []string) {
if "" == t.Opt {
return
}
opt = make([]string, 0, 1)
plugins := t.plugins(plugin)
opt = append(opt, fmt.Sprintf("--%s_opt=%s", t.Lang, t.Opt))
for _, _plugin := range plugins {
opt = append(opt, fmt.Sprintf("--%s_opt=%s", _plugin, t.Opt))
}
return
}
func (t *target) out(plugin *plugin) (out []string) {
out = make([]string, 0, 1)
output := t.output()
plugins := t.plugins(plugin)
switch t.Lang {
case langGo, langGolang, langJava:
out = append(out, fmt.Sprintf("--%s_out=%s", t.Lang, output))
for _, _plugin := range plugins {
out = append(out, fmt.Sprintf("--%s_out=%s", _plugin, output))
}
default:
out = append(out, fmt.Sprintf("--%s_out=%s:%s", t.Lang, strings.Join(plugins, separator), output))
}
return
}
func (t *target) output() (output string) {
output = t.Output
switch {
case langDart == t.Lang && !strings.HasSuffix(output, dartLibFilename):
output = filepath.Join(output, dartLibFilename)
case langJava == t.Lang && !strings.HasSuffix(output, filepath.FromSlash(javaSourceFilename)):
output = filepath.Join(output, filepath.FromSlash(javaSourceFilename))
}
// 转换成绝对路径,防止Protobuf找不到路径报错
output, _ = filepath.Abs(output)
_ = os.MkdirAll(output, os.ModePerm)
return
}
func (t *target) plugins(plugin *plugin) (plugins []string) {
plugins = t.Plugins
if !*plugin.Defaults {
return
}
switch t.Lang {
case langJava:
plugins = append(plugins, "grpc-java", "grpc-gateway")
case langGo, langGogo:
plugins = append(plugins, "go-grpc", "grpc-gateway")
case langDart:
plugins = append(plugins, "generate_kythe_info")
case langJs:
plugins = append(plugins, "binary")
}
plugins = append(plugins, plugin.Plugins...)
return
}