This commit is contained in:
anqiansong
2022-03-09 19:26:35 +08:00
committed by GitHub
parent 63b7d292c1
commit 4624390e54
11 changed files with 335 additions and 131 deletions

View File

@@ -2,12 +2,10 @@ package cli
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/emicklei/proto"
"github.com/urfave/cli"
"github.com/zeromicro/go-zero/tools/goctl/rpc/generator"
"github.com/zeromicro/go-zero/tools/goctl/util"
@@ -15,7 +13,8 @@ import (
)
var (
errInvalidGrpcOutput = errors.New("ZRPC: missing grpc output")
errInvalidGrpcOutput = errors.New("ZRPC: missing --go-grpc_out")
errInvalidGoOutput = errors.New("ZRPC: missing --go_out")
errInvalidZrpcOutput = errors.New("ZRPC: missing zrpc output, please use --zrpc_out to specify the output")
errInvalidInput = errors.New("ZRPC: missing source")
errMultiInput = errors.New("ZRPC: only one source is expected")
@@ -40,21 +39,29 @@ func ZRPC(c *cli.Context) error {
if err != nil {
return err
}
src := filepath.Dir(source)
goPackage, protoPkg, err := getGoPackage(source)
if err != nil {
return err
}
grpcOut := c.String("go-grpc_out")
goOut := c.String("go_out")
goOpt := c.String("go_opt")
grpcOpt := c.String("go-grpc_opt")
grpcOutList := c.StringSlice("go-grpc_out")
goOutList := c.StringSlice("go_out")
zrpcOut := c.String("zrpc_out")
style := c.String("style")
home := c.String("home")
remote := c.String("remote")
branch := c.String("branch")
if len(grpcOutList) == 0 {
return errInvalidGrpcOutput
}
if len(goOutList) == 0 {
return errInvalidGoOutput
}
goOut := goOutList[len(goOutList)-1]
grpcOut := grpcOutList[len(grpcOutList)-1]
if len(goOut) == 0 {
return errInvalidGrpcOutput
}
if len(zrpcOut) == 0 {
return errInvalidZrpcOutput
}
if len(remote) > 0 {
repo, _ := util.CloneIntoGitHome(remote, branch)
if len(repo) > 0 {
@@ -65,38 +72,11 @@ func ZRPC(c *cli.Context) error {
if len(home) > 0 {
pathx.RegisterGoctlHome(home)
}
if len(goOut) == 0 {
return errInvalidGrpcOutput
}
if len(zrpcOut) == 0 {
return errInvalidZrpcOutput
}
if !filepath.IsAbs(zrpcOut) {
zrpcOut = filepath.Join(pwd, zrpcOut)
}
goOut = removePluginFlag(goOut)
goOut, err = parseOut(src, goOut, goOpt, goPackage)
if err != nil {
return err
}
isGooglePlugin := len(grpcOut) > 0
// If grpcOut is not empty means that user generates grpc code by
// https://google.golang.org/protobuf/cmd/protoc-gen-go and
// https://google.golang.org/grpc/cmd/protoc-gen-go-grpc,
// for details please see https://grpc.io/docs/languages/go/quickstart/
if isGooglePlugin {
grpcOut, err = parseOut(src, grpcOut, grpcOpt, goPackage)
if err != nil {
return err
}
} else {
// Else it means that user generates grpc code by
// https://github.com/golang/protobuf/tree/master/protoc-gen-go
grpcOut = goOut
}
goOut, err = filepath.Abs(goOut)
if err != nil {
return err
@@ -110,23 +90,11 @@ func ZRPC(c *cli.Context) error {
return err
}
if isGooglePlugin && grpcOut != goOut {
return fmt.Errorf("the --go_out and --go-grpc_out must be the same")
}
if goOut == zrpcOut || grpcOut == zrpcOut {
recommendName := goPackage
if len(recommendName) == 0 {
recommendName = protoPkg
}
return fmt.Errorf("the zrpc and grpc output can not be the same, it is recommended to output grpc to the %q",
filepath.Join(goOut, recommendName))
}
var ctx generator.ZRpcContext
ctx.Src = source
ctx.ProtoGenGoDir = goOut
ctx.ProtoGenGrpcDir = grpcOut
ctx.GoOutput = goOut
ctx.GrpcOutput = grpcOut
ctx.IsGooglePlugin = isGooglePlugin
ctx.Output = zrpcOut
ctx.ProtocCmd = strings.Join(protocArgs, " ")
g, err := generator.NewDefaultRPCGenerator(style, generator.WithZRpcContext(&ctx))
@@ -137,52 +105,6 @@ func ZRPC(c *cli.Context) error {
return g.Generate(source, zrpcOut, nil)
}
// parseOut calculates the output place to grpc code, about to calculate logic for details
// please see https://developers.google.com/protocol-buffers/docs/reference/go-generated#invocation.
func parseOut(sourceDir, grpcOut, grpcOpt, goPackage string) (string, error) {
if !filepath.IsAbs(grpcOut) {
grpcOut = filepath.Join(sourceDir, grpcOut)
}
switch grpcOpt {
case "", optImport:
grpcOut = filepath.Join(grpcOut, goPackage)
case optSourceRelative:
grpcOut = filepath.Join(grpcOut)
default:
return "", fmt.Errorf("parseAndSetGrpcOut: unknown path type %q: want %q or %q",
grpcOpt, optImport, optSourceRelative)
}
return grpcOut, nil
}
func getGoPackage(source string) (string, string, error) {
r, err := os.Open(source)
if err != nil {
return "", "", err
}
defer func() {
_ = r.Close()
}()
parser := proto.NewParser(r)
set, err := parser.Parse()
if err != nil {
return "", "", err
}
var goPackage, protoPkg string
proto.Walk(set, proto.WithOption(func(option *proto.Option) {
if option.Name == "go_package" {
goPackage = option.Constant.Source
}
}), proto.WithPackage(func(p *proto.Package) {
protoPkg = p.Name
}))
return goPackage, protoPkg, nil
}
func removeGoctlFlag(args []string) []string {
var ret []string
var step int

View File

@@ -79,6 +79,10 @@ func Test_RemoveGoctlFlag(t *testing.T) {
source: strings.Fields(`protoc foo.proto --go_opt=. --zrpc_out="bar" --style=goZero --home=bar`),
expected: "protoc foo.proto --go_opt=.",
},
{
source: strings.Fields(`protoc --go_opt=. --go-grpc_out=. --zrpc_out=. foo.proto`),
expected: "protoc --go_opt=. --go-grpc_out=. foo.proto",
},
}
for _, e := range testData {
cmd := strings.Join(removeGoctlFlag(e.source), " ")