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

@@ -1,8 +1,7 @@
package generator
import (
"os/exec"
"github.com/zeromicro/go-zero/tools/goctl/env"
"github.com/zeromicro/go-zero/tools/goctl/util/console"
)
@@ -25,17 +24,5 @@ func NewDefaultGenerator() Generator {
// Prepare provides environment detection generated by rpc service,
// including go environment, protoc, whether protoc-gen-go is installed or not
func (g *DefaultGenerator) Prepare() error {
_, err := exec.LookPath("go")
if err != nil {
return err
}
_, err = exec.LookPath("protoc")
if err != nil {
return err
}
_, err = exec.LookPath("protoc-gen-go")
return err
return env.Prepare(true, true)
}

View File

@@ -24,6 +24,9 @@ type ZRpcContext struct {
ProtocCmd string
ProtoGenGrpcDir string
ProtoGenGoDir string
IsGooglePlugin bool
GoOutput string
GrpcOutput string
Output string
}

View File

@@ -3,6 +3,8 @@ package generator
import (
"bytes"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"
@@ -19,7 +21,7 @@ const googleProtocGenGoErr = `--go_out: protoc-gen-go: plugins are not supported
// 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, c *ZRpcContext, goOptions ...string) error {
if c != nil {
return g.genPbDirect(c)
return g.genPbDirect(ctx, c)
}
// deprecated: use genPbDirect instead.
@@ -110,13 +112,74 @@ go get -u github.com/golang/protobuf/protoc-gen-go`)
return nil
}
func (g *DefaultGenerator) genPbDirect(c *ZRpcContext) error {
g.log.Debug(c.ProtocCmd)
func (g *DefaultGenerator) genPbDirect(ctx DirContext, c *ZRpcContext) error {
g.log.Debug("[command]: %s", c.ProtocCmd)
pwd, err := os.Getwd()
if err != nil {
return err
}
_, err = execx.Run(c.ProtocCmd, pwd)
return err
if err != nil {
return err
}
return g.setPbDir(ctx, c)
}
func (g *DefaultGenerator) setPbDir(ctx DirContext, c *ZRpcContext) error {
pbDir, err := findPbFile(c.GoOutput, false)
if err != nil {
return err
}
if len(pbDir) == 0 {
return fmt.Errorf("pg.go is not found under %q", c.GoOutput)
}
grpcDir, err := findPbFile(c.GrpcOutput, true)
if err != nil {
return err
}
if len(grpcDir) == 0 {
return fmt.Errorf("_grpc.pb.go is not found in %q", c.GrpcOutput)
}
if pbDir != grpcDir {
return fmt.Errorf("the pb.go and _grpc.pb.go must under the same dir: "+
"\n pb.go: %s\n_grpc.pb.go: %s", pbDir, grpcDir)
}
if pbDir == c.Output {
return fmt.Errorf("the output of pb.go and _grpc.pb.go must not be the same "+
"with --zrpc_out:\npb output: %s\nzrpc out: %s", pbDir, c.Output)
}
ctx.SetPbDir(pbDir, grpcDir)
return nil
}
const (
pbSuffix = "pb.go"
grpcSuffix = "_grpc.pb.go"
)
func findPbFile(current string, grpc bool) (string, error) {
fileSystem := os.DirFS(current)
var ret string
err := fs.WalkDir(fileSystem, ".", func(path string, d fs.DirEntry, err error) error {
if d.IsDir() {
return nil
}
if strings.HasSuffix(path, pbSuffix) {
if grpc {
if strings.HasSuffix(path, grpcSuffix) {
ret = path
return os.ErrExist
}
} else if !strings.HasSuffix(path, grpcSuffix) {
ret = path
return os.ErrExist
}
}
return nil
})
if err == os.ErrExist {
return filepath.Dir(filepath.Join(current, ret)), nil
}
return "", err
}

View File

@@ -0,0 +1,197 @@
package generator
import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
)
func Test_findPbFile(t *testing.T) {
dir := t.TempDir()
protoFile := filepath.Join(dir, "greet.proto")
err := ioutil.WriteFile(protoFile, []byte(`
syntax = "proto3";
package greet;
option go_package="./greet";
message Req{}
message Resp{}
service Greeter {
rpc greet(Req) returns (Resp);
}
`), 0666)
if err != nil {
t.Log(err)
return
}
t.Run("", func(t *testing.T) {
output := t.TempDir()
grpc := filepath.Join(output, "grpc")
err := pathx.MkdirIfNotExist(grpc)
if err != nil {
t.Log(err)
return
}
cmd := exec.Command("protoc", "-I="+filepath.Dir(protoFile), "--go_out="+output, "--go-grpc_out="+grpc, filepath.Base(protoFile))
cmd.Dir = output
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
t.Log(err)
return
}
pbDir, err := findPbFile(output, false)
assert.Nil(t, err)
pbGo := filepath.Join(pbDir, "greet.pb.go")
assert.True(t, pathx.FileExists(pbGo))
grpcDir, err := findPbFile(output, true)
assert.Nil(t, err)
grpcGo := filepath.Join(grpcDir, "greet_grpc.pb.go")
assert.True(t, pathx.FileExists(grpcGo))
})
t.Run("", func(t *testing.T) {
output := t.TempDir()
redirect := filepath.Join(output, "pb")
grpc := filepath.Join(output, "grpc")
err := pathx.MkdirIfNotExist(grpc)
if err != nil {
t.Log(err)
return
}
cmd := exec.Command("protoc", "-I="+filepath.Dir(protoFile), "--go_out="+output,
"--go-grpc_out="+grpc, filepath.Base(protoFile), "--go_opt=M"+filepath.Base(protoFile)+"="+redirect)
cmd.Dir = output
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
t.Log(err)
return
}
pbDir, err := findPbFile(output, false)
assert.Nil(t, err)
pbGo := filepath.Join(pbDir, "greet.pb.go")
assert.True(t, pathx.FileExists(pbGo))
grpcDir, err := findPbFile(output, true)
assert.Nil(t, err)
grpcGo := filepath.Join(grpcDir, "greet_grpc.pb.go")
assert.True(t, pathx.FileExists(grpcGo))
})
t.Run("", func(t *testing.T) {
output := t.TempDir()
pbeRedirect := filepath.Join(output, "redirect")
grpc := filepath.Join(output, "grpc")
grpcRedirect := filepath.Join(grpc, "redirect")
err := pathx.MkdirIfNotExist(grpc)
if err != nil {
t.Log(err)
return
}
cmd := exec.Command("protoc", "-I="+filepath.Dir(protoFile), "--go_out="+output,
"--go-grpc_out="+grpc, filepath.Base(protoFile), "--go_opt=M"+filepath.Base(protoFile)+"="+pbeRedirect,
"--go-grpc_opt=M"+filepath.Base(protoFile)+"="+grpcRedirect)
cmd.Dir = output
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
t.Log(err)
return
}
pbDir, err := findPbFile(output, false)
assert.Nil(t, err)
pbGo := filepath.Join(pbDir, "greet.pb.go")
assert.True(t, pathx.FileExists(pbGo))
grpcDir, err := findPbFile(output, true)
assert.Nil(t, err)
grpcGo := filepath.Join(grpcDir, "greet_grpc.pb.go")
assert.True(t, pathx.FileExists(grpcGo))
})
t.Run("", func(t *testing.T) {
output := t.TempDir()
pbeRedirect := filepath.Join(output, "redirect")
grpc := filepath.Join(output, "grpc")
grpcRedirect := filepath.Join(grpc, "redirect")
err := pathx.MkdirIfNotExist(grpc)
if err != nil {
t.Log(err)
return
}
cmd := exec.Command("protoc", "-I="+filepath.Dir(protoFile), "--go_out="+output,
"--go-grpc_out="+grpc, filepath.Base(protoFile), "--go_opt=M"+filepath.Base(protoFile)+"="+pbeRedirect,
"--go-grpc_opt=M"+filepath.Base(protoFile)+"="+grpcRedirect, "--go_opt=paths=import", "--go-grpc_opt=paths=source_relative")
cmd.Dir = output
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
t.Log(err)
return
}
pbDir, err := findPbFile(output, false)
assert.Nil(t, err)
pbGo := filepath.Join(pbDir, "greet.pb.go")
assert.True(t, pathx.FileExists(pbGo))
grpcDir, err := findPbFile(output, true)
assert.Nil(t, err)
grpcGo := filepath.Join(grpcDir, "greet_grpc.pb.go")
assert.True(t, pathx.FileExists(grpcGo))
})
t.Run("", func(t *testing.T) {
output := t.TempDir()
pbeRedirect := filepath.Join(output, "redirect")
grpc := filepath.Join(output, "grpc")
grpcRedirect := filepath.Join(grpc, "redirect")
err := pathx.MkdirIfNotExist(grpc)
if err != nil {
t.Log(err)
return
}
err = pathx.MkdirIfNotExist(pbeRedirect)
if err != nil {
t.Log(err)
return
}
err = pathx.MkdirIfNotExist(grpcRedirect)
if err != nil {
t.Log(err)
return
}
cmd := exec.Command("protoc", "-I="+filepath.Dir(protoFile), "--go_out="+output,
"--go-grpc_out="+grpc, filepath.Base(protoFile), "--go_opt=M"+filepath.Base(protoFile)+"="+pbeRedirect,
"--go-grpc_opt=M"+filepath.Base(protoFile)+"="+grpcRedirect, "--go_opt=paths=import", "--go-grpc_opt=paths=source_relative",
"--go_out="+pbeRedirect, "--go-grpc_out="+grpcRedirect)
cmd.Dir = output
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
t.Log(err)
return
}
pbDir, err := findPbFile(output, false)
assert.Nil(t, err)
pbGo := filepath.Join(pbDir, "greet.pb.go")
assert.True(t, pathx.FileExists(pbGo))
grpcDir, err := findPbFile(output, true)
assert.Nil(t, err)
grpcGo := filepath.Join(grpcDir, "greet_grpc.pb.go")
assert.True(t, pathx.FileExists(grpcGo))
})
}

View File

@@ -38,6 +38,7 @@ type (
GetProtoGo() Dir
GetMain() Dir
GetServiceName() stringx.String
SetPbDir(pbDir, grpcDir string)
}
// Dir defines a directory
@@ -50,6 +51,7 @@ type (
defaultDirContext struct {
inner map[string]Dir
serviceName stringx.String
ctx *ctx.ProjectContext
}
)
@@ -134,11 +136,26 @@ func mkdir(ctx *ctx.ProjectContext, proto parser.Proto, _ *conf.Config, c *ZRpcC
}
serviceName := strings.TrimSuffix(proto.Name, filepath.Ext(proto.Name))
return &defaultDirContext{
ctx: ctx,
inner: inner,
serviceName: stringx.From(strings.ReplaceAll(serviceName, "-", "")),
}, nil
}
func (d *defaultDirContext) SetPbDir(pbDir, grpcDir string) {
d.inner[pb] = Dir{
Filename: pbDir,
Package: filepath.ToSlash(filepath.Join(d.ctx.Path, strings.TrimPrefix(pbDir, d.ctx.Dir))),
Base: filepath.Base(pbDir),
}
d.inner[protoGo] = Dir{
Filename: grpcDir,
Package: filepath.ToSlash(filepath.Join(d.ctx.Path, strings.TrimPrefix(grpcDir, d.ctx.Dir))),
Base: filepath.Base(grpcDir),
}
}
func (d *defaultDirContext) GetCall() Dir {
return d.inner[call]
}