mirror of
https://github.com/zeromicro/go-zero.git
synced 2026-05-13 18:00:00 +08:00
feat(goctl/rpc): support external proto imports with cross-package ty… (#5472)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/zeromicro/go-zero/tools/goctl/rpc/execx"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/rpc/parser"
|
||||
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
||||
)
|
||||
|
||||
@@ -19,19 +20,89 @@ func (g *Generator) GenPb(ctx DirContext, c *ZRpcContext) error {
|
||||
}
|
||||
|
||||
func (g *Generator) 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)
|
||||
protocCmd, err := g.buildProtocCmd(c, pwd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
g.log.Debug("[command]: %s", protocCmd)
|
||||
_, err = execx.Run(protocCmd, pwd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return g.setPbDir(ctx, c)
|
||||
}
|
||||
|
||||
// buildProtocCmd resolves all transitively imported proto files and appends
|
||||
// them to the protoc command so that their pb.go files are also generated.
|
||||
func (g *Generator) buildProtocCmd(c *ZRpcContext, pwd string) (string, error) {
|
||||
// Build the full list of proto search paths (absolute).
|
||||
protoPaths := make([]string, 0, len(c.ProtoPaths)+1)
|
||||
|
||||
// Always include the directory of the source proto so that imports
|
||||
// relative to the source file can be found.
|
||||
srcDir := filepath.Dir(c.Src)
|
||||
if !filepath.IsAbs(srcDir) {
|
||||
srcDir = filepath.Join(pwd, srcDir)
|
||||
}
|
||||
protoPaths = append(protoPaths, srcDir)
|
||||
|
||||
for _, p := range c.ProtoPaths {
|
||||
if !filepath.IsAbs(p) {
|
||||
p = filepath.Join(pwd, p)
|
||||
}
|
||||
protoPaths = append(protoPaths, p)
|
||||
}
|
||||
|
||||
importedFiles, err := parser.ResolveImports(c.Src, protoPaths)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(importedFiles) == 0 {
|
||||
return c.ProtocCmd, nil
|
||||
}
|
||||
|
||||
cmd := c.ProtocCmd
|
||||
for _, f := range importedFiles {
|
||||
// Use the path relative to the best-matching --proto_path entry so that
|
||||
// protoc's source_relative output lands in the correct directory.
|
||||
// e.g. if --proto_path=./ext and the file is ext/common/types.proto,
|
||||
// we pass "common/types.proto" rather than "ext/common/types.proto".
|
||||
rel := relativeToProtoPath(f, protoPaths, pwd)
|
||||
cmd += " " + rel
|
||||
}
|
||||
return cmd, nil
|
||||
}
|
||||
|
||||
// relativeToProtoPath returns the path of f relative to the most specific
|
||||
// (longest) proto_path entry that is a parent of f. Falls back to relative
|
||||
// to pwd when no proto_path matches.
|
||||
func relativeToProtoPath(f string, protoPaths []string, pwd string) string {
|
||||
bestRel := ""
|
||||
bestLen := 0
|
||||
for _, pp := range protoPaths {
|
||||
prefix := pp + string(filepath.Separator)
|
||||
if strings.HasPrefix(f, prefix) && len(pp) > bestLen {
|
||||
if rel, err := filepath.Rel(pp, f); err == nil {
|
||||
bestRel = rel
|
||||
bestLen = len(pp)
|
||||
}
|
||||
}
|
||||
}
|
||||
if bestRel != "" {
|
||||
return bestRel
|
||||
}
|
||||
if rel, err := filepath.Rel(pwd, f); err == nil {
|
||||
return rel
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
func (g *Generator) setPbDir(ctx DirContext, c *ZRpcContext) error {
|
||||
pbDir, err := findPbFile(c.GoOutput, c.Src, false)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user