-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.go
85 lines (71 loc) · 2.1 KB
/
generate.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
/*
* Copyright 2024 hopeio. All rights reserved.
* Licensed under the MIT License that can be found in the LICENSE file.
* @Created by jyb
*/
package main
import (
"fmt"
execi "github.com/hopeio/utils/os/exec"
"github.com/hopeio/utils/os/fs"
_go "github.com/hopeio/utils/sdk/go"
"log"
"os"
"strings"
)
//go:generate mockgen -destination ../protobuf/user/user.mock.go -package user -source ../protobuf/user/user.service_grpc.pb.go UserServiceServer
var (
libProtobufDir, proto string
pwd, gopath, include string
)
func init() {
gopath = os.Getenv("GOPATH")
if strings.HasSuffix(gopath, "/") {
gopath = gopath[:len(gopath)-1]
}
pwd, _ = os.Getwd()
libProtobufDir = _go.GetDepDir(DepProtobuf)
proto = libProtobufDir + "/_proto"
//libGatewayDir := _go.GetDepDir(DepGrpcGateway)
//libGoogleDir := _go.GetDepDir(DepGoogleapis)
include = "-I" + proto
}
func main() {
//single("/content/moment.model.proto")
generate(proto + "/hopeio")
fmt.Println(fs.MoveDirByMode(libProtobufDir+"/hopeio", libProtobufDir, 0))
}
const goOut = "go-patch_out=plugin=go,paths=source_relative"
const grpcOut = "go-patch_out=plugin=go-grpc,paths=source_relative"
const enumOut = "enum_out=paths=source_relative"
const (
goListDir = `go list -m -f {{.Dir}} `
goListDep = `go list -m -f {{.Path}}@{{.Version}} `
DepGoogleapis = "github.com/googleapis/[email protected]"
DepProtobuf = "github.com/hopeio/protobuf"
DepGrpcGateway = "github.com/grpc-ecosystem/grpc-gateway/v2"
DepProtopatch = "github.com/alta/protopatch"
)
var model = []string{goOut, grpcOut, enumOut}
func generate(dir string) {
fileInfos, err := os.ReadDir(dir)
if err != nil {
log.Fatalln(err)
}
var gen bool
for i := range fileInfos {
if fileInfos[i].IsDir() {
generate(dir + "/" + fileInfos[i].Name())
} else if !gen && strings.HasSuffix(fileInfos[i].Name(), ".proto") {
protoc(dir)
}
}
}
func protoc(dir string) {
cmd := "protoc " + include + " " + dir + "/*.proto"
var args string
for _, plugin := range model {
args += " --" + plugin + ":" + libProtobufDir
}
execi.RunWithLog(cmd + args)
}