-
Notifications
You must be signed in to change notification settings - Fork 0
/
protoc.go
202 lines (189 loc) · 3.97 KB
/
protoc.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"errors"
"fmt"
"go/build"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"runtime"
"strings"
"github.com/urfave/cli"
)
var (
withBM bool
withGRPC bool
withSwagger bool
withEcode bool
withPHP bool
outPath string
)
func protocAction(ctx *cli.Context) (err error) {
if err = checkProtoc(); err != nil {
return err
}
files := []string(ctx.Args())
if len(files) == 0 {
files, _ = filepath.Glob("*.proto")
}
if !withGRPC && !withBM && !withSwagger && !withEcode && !withPHP {
withBM = true
withGRPC = true
withSwagger = true
withEcode = true
withPHP = true
}
if withBM {
if err = installBMGen(); err != nil {
return
}
if err = genBM(files); err != nil {
return
}
}
if withGRPC {
if err = installGRPCGen(); err != nil {
return err
}
if err = genGRPC(files); err != nil {
return
}
}
if withPHP {
if err = installPHPGen(); err != nil {
return
}
if err = genPHP(files); err != nil {
return
}
}
if withSwagger {
if err = installSwaggerGen(); err != nil {
return
}
if err = genSwagger(files); err != nil {
return
}
}
if withEcode {
if err = installEcodeGen(); err != nil {
return
}
if err = genEcode(files); err != nil {
return
}
}
log.Printf("generate %s success.\n", strings.Join(files, " "))
return nil
}
func checkProtoc() error {
if _, err := exec.LookPath("protoc"); err != nil {
switch runtime.GOOS {
case "darwin":
fmt.Println("brew install protobuf")
cmd := exec.Command("brew", "install", "protobuf")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err = cmd.Run(); err != nil {
return err
}
case "linux":
fmt.Println("snap install --classic protobuf")
cmd := exec.Command("snap", "install", "--classic", "protobuf")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err = cmd.Run(); err != nil {
return err
}
default:
return errors.New("您还没安装protobuf,请进行手动安装:https://github.com/protocolbuffers/protobuf/releases")
}
}
return nil
}
func generate(protoc string, files []string) error {
pwd, _ := os.Getwd()
gosrc := path.Join(gopath(), "src")
ext, err := latestKratos()
if err != nil {
return err
}
line := fmt.Sprintf(protoc, gosrc, ext, pwd)
log.Println(line, strings.Join(files, " "))
args := strings.Split(line, " ")
args = append(args, files...)
cmd := exec.Command(args[0], args[1:]...)
cmd.Dir = pwd
cmd.Env = os.Environ()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func goget(url string) error {
args := strings.Split(url, " ")
cmd := exec.Command(args[0], args[1:]...)
cmd.Env = os.Environ()
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
log.Println(url)
return cmd.Run()
}
func latestKratos() (string, error) {
gopath := gopath()
ext := path.Join(gopath, "src/github.com/go-kratos/kratos/third_party")
if _, err := os.Stat(ext); !os.IsNotExist(err) {
return ext, nil
}
baseMod := path.Join(gopath, "pkg/mod/github.com/go-kratos")
files, err := ioutil.ReadDir(baseMod)
if err != nil {
return "", err
}
for i := len(files) - 1; i >= 0; i-- {
if strings.HasPrefix(files[i].Name(), "kratos@") {
return path.Join(baseMod, files[i].Name(), "third_party"), nil
}
}
return "", errors.New("not found kratos package")
}
func gopath() (gp string) {
gopaths := strings.Split(os.Getenv("GOPATH"), string(filepath.ListSeparator))
if len(gopaths) == 1 && gopaths[0] != "" {
return gopaths[0]
}
pwd, err := os.Getwd()
if err != nil {
return
}
abspwd, err := filepath.Abs(pwd)
if err != nil {
return
}
for _, gopath := range gopaths {
if gopath == "" {
continue
}
absgp, err := filepath.Abs(gopath)
if err != nil {
return
}
if strings.HasPrefix(abspwd, absgp) {
return absgp
}
}
return build.Default.GOPATH
}
func checkPath(path string) error {
if _, err := os.Stat(path); err == nil {
return nil
} else {
err := os.MkdirAll(path, 0711)
if err != nil {
return err
}
}
return nil
}