|
|
@ -2,45 +2,36 @@ package generator
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/tal-tech/go-zero/core/collection"
|
|
|
|
"github.com/tal-tech/go-zero/core/collection"
|
|
|
|
conf "github.com/tal-tech/go-zero/tools/goctl/config"
|
|
|
|
conf "github.com/tal-tech/go-zero/tools/goctl/config"
|
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
|
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
|
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
|
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
|
|
|
|
|
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/util"
|
|
|
|
|
|
|
|
"github.com/tal-tech/go-zero/tools/goctl/vars"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
const googleProtocGenGoErr = `--go_out: protoc-gen-go: plugins are not supported; use 'protoc --go-grpc_out=...' to generate gRPC`
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// GenPb generates the pb.go file, which is a layer of packaging for protoc to generate gprc,
|
|
|
|
// GenPb generates the pb.go file, which is a layer of packaging for protoc to generate gprc,
|
|
|
|
// but the commands and flags in protoc are not completely joined in goctl. At present, proto_path(-I) is introduced
|
|
|
|
// but the commands and flags in protoc are not completely joined in goctl. At present, proto_path(-I) is introduced
|
|
|
|
func (g *DefaultGenerator) GenPb(ctx DirContext, protoImportPath []string, proto parser.Proto, _ *conf.Config, goOptions ...string) error {
|
|
|
|
func (g *DefaultGenerator) GenPb(ctx DirContext, protoImportPath []string, proto parser.Proto, _ *conf.Config, goOptions ...string) error {
|
|
|
|
dir := ctx.GetPb()
|
|
|
|
dir := ctx.GetPb()
|
|
|
|
cw := new(bytes.Buffer)
|
|
|
|
cw := new(bytes.Buffer)
|
|
|
|
directory, base := filepath.Split(proto.Src)
|
|
|
|
directory, _ := filepath.Split(proto.Src)
|
|
|
|
directory = filepath.Clean(directory)
|
|
|
|
directory = filepath.Clean(directory)
|
|
|
|
cw.WriteString("protoc ")
|
|
|
|
cw.WriteString("protoc ")
|
|
|
|
protoImportPathSet := collection.NewSet()
|
|
|
|
protoImportPathSet := collection.NewSet()
|
|
|
|
isSamePackage := true
|
|
|
|
|
|
|
|
for _, ip := range protoImportPath {
|
|
|
|
for _, ip := range protoImportPath {
|
|
|
|
pip := " --proto_path=" + ip
|
|
|
|
pip := " --proto_path=" + ip
|
|
|
|
if protoImportPathSet.Contains(pip) {
|
|
|
|
if protoImportPathSet.Contains(pip) {
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
abs, err := filepath.Abs(ip)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if abs == directory {
|
|
|
|
|
|
|
|
isSamePackage = true
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
isSamePackage = false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protoImportPathSet.AddStr(pip)
|
|
|
|
protoImportPathSet.AddStr(pip)
|
|
|
|
cw.WriteString(pip)
|
|
|
|
cw.WriteString(pip)
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -56,50 +47,88 @@ func (g *DefaultGenerator) GenPb(ctx DirContext, protoImportPath []string, proto
|
|
|
|
cw.WriteString(" --go_out=plugins=grpc:" + dir.Filename)
|
|
|
|
cw.WriteString(" --go_out=plugins=grpc:" + dir.Filename)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Compatible with version 1.4.0,github.com/golang/protobuf/protoc-gen-go@v1.4.0
|
|
|
|
return g.generatePbWithVersion132(cw.String())
|
|
|
|
// --go_opt usage please see https://developers.google.com/protocol-buffers/docs/reference/go-generated#package
|
|
|
|
}
|
|
|
|
optSet := collection.NewSet()
|
|
|
|
|
|
|
|
for _, op := range goOptions {
|
|
|
|
|
|
|
|
opt := " --go_opt=" + op
|
|
|
|
|
|
|
|
if optSet.Contains(opt) {
|
|
|
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
optSet.AddStr(op)
|
|
|
|
// generatePbWithVersion132 generates pb.go by specifying protoc-gen-go@1.3.2 version
|
|
|
|
cw.WriteString(" --go_opt=" + op)
|
|
|
|
func (g *DefaultGenerator) generatePbWithVersion132(cmd string) error {
|
|
|
|
|
|
|
|
goctlHome, err := util.GetGoctlHome()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var currentFileOpt string
|
|
|
|
err = util.MkdirIfNotExist(goctlHome)
|
|
|
|
if !isSamePackage || (len(proto.GoPackage) > 0 && proto.GoPackage != proto.Package.Name) {
|
|
|
|
if err != nil {
|
|
|
|
if filepath.IsAbs(proto.GoPackage) {
|
|
|
|
return err
|
|
|
|
currentFileOpt = " --go_opt=M" + base + "=" + proto.GoPackage
|
|
|
|
|
|
|
|
} else if strings.Contains(proto.GoPackage, string(filepath.Separator)) {
|
|
|
|
|
|
|
|
currentFileOpt = " --go_opt=M" + base + "=./" + proto.GoPackage
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
currentFileOpt = " --go_opt=M" + base + "=../" + proto.GoPackage
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
currentFileOpt = " --go_opt=M" + base + "=."
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if !optSet.Contains(currentFileOpt) {
|
|
|
|
goctlHomeBin := filepath.Join(goctlHome, "bin")
|
|
|
|
cw.WriteString(currentFileOpt)
|
|
|
|
err = util.MkdirIfNotExist(goctlHomeBin)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
command := cw.String()
|
|
|
|
protocGenGo := filepath.Join(goctlHome, "bin", "protoc-gen-go")
|
|
|
|
g.log.Debug(command)
|
|
|
|
g.log.Debug("checking protoc-gen-go state ...")
|
|
|
|
_, err := execx.Run(command, "")
|
|
|
|
goGetCmd := "\ngo install github.com/golang/protobuf/protoc-gen-go@v1.3.2"
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
if strings.Contains(err.Error(), googleProtocGenGoErr) {
|
|
|
|
|
|
|
|
return errors.New(`Unsupported plugin protoc-gen-go which installed from the following source:
|
|
|
|
|
|
|
|
google.golang.org/protobuf/cmd/protoc-gen-go,
|
|
|
|
|
|
|
|
github.com/protocolbuffers/protobuf-go/cmd/protoc-gen-go;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Please replace it by the following command, we recommend to use version before v1.3.5:
|
|
|
|
if util.FileExists(protocGenGo) {
|
|
|
|
go get -u github.com/golang/protobuf/protoc-gen-go`)
|
|
|
|
g.log.Success("protoc-gen-go exists ...")
|
|
|
|
|
|
|
|
goGetCmd = ""
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
g.log.Error("missing protoc-gen-go: downloading ...")
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
goos := runtime.GOOS
|
|
|
|
|
|
|
|
switch goos {
|
|
|
|
|
|
|
|
case vars.OsLinux, vars.OsMac:
|
|
|
|
|
|
|
|
cmd = getUnixLikeCmd(goctlHome, goctlHomeBin, goGetCmd, cmd)
|
|
|
|
|
|
|
|
g.log.Debug("%s", cmd)
|
|
|
|
|
|
|
|
case vars.OsWindows:
|
|
|
|
|
|
|
|
cmd = getWindowsCmd(goctlHome, goctlHomeBin, goGetCmd, cmd)
|
|
|
|
|
|
|
|
// Do not support to execute commands in context, the solution is created
|
|
|
|
|
|
|
|
// a batch file to execute it on Windows.
|
|
|
|
|
|
|
|
batFile, err := createBatchFile(goctlHome, cmd)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
g.log.Debug("%s", cmd)
|
|
|
|
|
|
|
|
cmd = batFile
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
|
|
|
return fmt.Errorf("unsupported os: %s", goos)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_, err = execx.Run(cmd, "")
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func getUnixLikeCmd(goctlHome, goctlHomeBin, goGetCmd, cmd string) string {
|
|
|
|
|
|
|
|
return fmt.Sprintf(`export GOPATH=%s
|
|
|
|
|
|
|
|
export GOBIN=%s
|
|
|
|
|
|
|
|
export PATH=$PATH:$GOPATH:$GOBIN
|
|
|
|
|
|
|
|
export GO111MODULE=on
|
|
|
|
|
|
|
|
export GOPROXY=https://goproxy.cn %s
|
|
|
|
|
|
|
|
%s`, goctlHome, goctlHomeBin, goGetCmd, cmd)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func getWindowsCmd(goctlHome, goctlHomeBin, goGetCmd, cmd string) string {
|
|
|
|
|
|
|
|
return fmt.Sprintf(`set GOPATH=%s
|
|
|
|
|
|
|
|
set GOBIN=%s
|
|
|
|
|
|
|
|
set path=%s
|
|
|
|
|
|
|
|
set GO111MODULE=on
|
|
|
|
|
|
|
|
set GOPROXY=https://goproxy.cn %s
|
|
|
|
|
|
|
|
%s`, goctlHome, goctlHomeBin, "%path%;"+goctlHome+";"+goctlHomeBin, goGetCmd, cmd)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func createBatchFile(goctlHome, cmd string) (string, error) {
|
|
|
|
|
|
|
|
batFile := filepath.Join(goctlHome, ".generate.bat")
|
|
|
|
|
|
|
|
if !util.FileExists(batFile) {
|
|
|
|
|
|
|
|
err := ioutil.WriteFile(batFile, []byte(cmd), os.ModePerm)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return "", err
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
|
|
|
|
|
|
|
|
return batFile, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|