Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zrpcclient 支持自定义pb和client 输出路径 #118

Merged
merged 6 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .template/client/zrpcclient-go/typed/scope_client.go.tpl
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package {{.Scope}}
package {{.Package}}

import (
"github.com/zeromicro/go-zero/zrpc"
{{ range $v := .Services}}
"{{ $.Module }}/typed/{{$.Scope}}/{{ $v | lower }}"
"{{ $.Module }}/{{$.Scope}}/{{ $v | lower }}"
{{ end }}
)

Expand Down
2 changes: 2 additions & 0 deletions cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ func init() {
genCmd.AddCommand(genZRpcClientCmd)

genZRpcClientCmd.Flags().StringP("output", "o", "zrpcclient-go", "generate rpcclient code")
genZRpcClientCmd.Flags().StringP("pb-dir", "", "", "set output pb dir ")
genZRpcClientCmd.Flags().StringP("client-dir", "", "", "set output client dir ")
genZRpcClientCmd.Flags().StringP("scope", "", "", "set scope name")
genZRpcClientCmd.Flags().StringP("goModule", "", "", "set go module name")
genZRpcClientCmd.Flags().StringP("goVersion", "", "", "set go version, only effect when having goModule flag")
Expand Down
15 changes: 8 additions & 7 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,14 @@ type GenSwaggerConfig struct {
}

type GenZrpcclientConfig struct {
Hooks HooksConfig `mapstructure:"hooks"`

Scope string `mapstructure:"scope"`
Output string `mapstructure:"output"`
GoVersion string `mapstructure:"goVersion"`
GoModule string `mapstructure:"goModule"`
GoPackage string `mapstructure:"goPackage"`
Hooks HooksConfig `mapstructure:"hooks"`
PbDir string `mapstructure:"pb-dir"`
ClientDir string `mapstructure:"client-dir"`
Scope string `mapstructure:"scope"`
Output string `mapstructure:"output"`
GoVersion string `mapstructure:"goVersion"`
GoModule string `mapstructure:"goModule"`
GoPackage string `mapstructure:"goPackage"`
}

type GenDocsConfig struct {
Expand Down
45 changes: 36 additions & 9 deletions internal/gen/genzrpcclient/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,17 @@ type DirContext struct {
OptionGoPackage string
Scope string
Output string
PbDir string
ClientDir string
}

func (d DirContext) GetCall() generator.Dir {
fileName := filepath.Join(d.Output, "typed", d.Scope)
if d.ClientDir != "" {
fileName = filepath.Join(d.Output, d.ClientDir)
}
return generator.Dir{
Filename: filepath.Join(d.Output, "typed", d.Scope),
Filename: fileName,
GetChildPackage: func(childPath string) (string, error) {
return strings.ToLower(childPath), nil
},
Expand Down Expand Up @@ -63,14 +69,22 @@ func (d DirContext) GetSvc() generator.Dir {

func (d DirContext) GetPb() generator.Dir {
return generator.Dir{
Package: filepath.ToSlash(fmt.Sprintf("%s/model/%s/%s", d.ImportBase, d.Scope, strings.TrimPrefix(d.OptionGoPackage, "./"))),
Package: d.packagePath(),
}
}

func (d DirContext) packagePath() string {
packagePath := filepath.ToSlash(fmt.Sprintf("%s/model/%s/%s", d.ImportBase, d.Scope, strings.TrimPrefix(d.OptionGoPackage, "./")))
if d.PbDir != "" {
packagePath = filepath.ToSlash(fmt.Sprintf("%s/%s/%s", d.ImportBase, d.PbDir, strings.TrimPrefix(d.OptionGoPackage, "./")))
}
return packagePath
}

func (d DirContext) GetProtoGo() generator.Dir {
return generator.Dir{
Filename: d.OptionGoPackage,
Package: filepath.ToSlash(fmt.Sprintf("%s/model/%s/%s", d.ImportBase, d.Scope, strings.TrimPrefix(d.OptionGoPackage, "./"))),
Package: d.packagePath(),
}
}

Expand Down Expand Up @@ -102,7 +116,6 @@ func Generate(c config.Config, genModule bool) error {
}

var services []string

for _, fp := range fps {
parser := rpcparser.NewDefaultProtoParser()
parse, err := parser.Parse(fp, true)
Expand All @@ -115,18 +128,23 @@ func Generate(c config.Config, genModule bool) error {
OptionGoPackage: parse.GoPackage,
Scope: c.Gen.Zrpcclient.Scope,
Output: c.Gen.Zrpcclient.Output,
PbDir: c.Gen.Zrpcclient.PbDir,
ClientDir: c.Gen.Zrpcclient.ClientDir,
}
for _, service := range parse.Service {
services = append(services, service.Name)
_ = os.MkdirAll(filepath.Join(dirContext.GetCall().Filename, strings.ToLower(service.Name)), 0o755)
}

pbDir := filepath.Join(c.Gen.Zrpcclient.Output, "model", c.Gen.Zrpcclient.Scope)
if dirContext.PbDir != "" {
pbDir = filepath.Join(c.Gen.Zrpcclient.Output, dirContext.PbDir)
}
// gen pb model
err = os.MkdirAll(filepath.Join(c.Gen.Zrpcclient.Output, "model", c.Gen.Zrpcclient.Scope), 0o755)
err = os.MkdirAll(pbDir, 0o755)
if err != nil {
return err
}
resp, err := execx.Run(fmt.Sprintf("protoc -I%s -I%s --go_out=%s --go-grpc_out=%s %s", baseProtoDir, filepath.Join(baseProtoDir, "third_party"), filepath.Join(c.Gen.Zrpcclient.Output, "model", c.Gen.Zrpcclient.Scope), filepath.Join(c.Gen.Zrpcclient.Output, "model", c.Gen.Zrpcclient.Scope), fp), wd)
resp, err := execx.Run(fmt.Sprintf("protoc -I%s -I%s --go_out=%s --go-grpc_out=%s %s", baseProtoDir, filepath.Join(baseProtoDir, "third_party"), pbDir, pbDir, fp), wd)
if err != nil {
return errors.Errorf("err: [%v], resp: [%s]", err, resp)
}
Expand Down Expand Up @@ -170,15 +188,24 @@ func Generate(c config.Config, genModule bool) error {
}

// generate scope client
scope := "typed/" + c.Gen.Zrpcclient.Scope
if c.Gen.Zrpcclient.PbDir != "" {
scope = c.Gen.Zrpcclient.ClientDir
}
template, err = templatex.ParseTemplate(map[string]any{
"Module": c.Gen.Zrpcclient.GoModule,
"Scope": c.Gen.Zrpcclient.Scope,
"Scope": scope,
"Package": c.Gen.Zrpcclient.Scope,
"Services": services,
}, embeded.ReadTemplateFile(filepath.ToSlash(filepath.Join("client", "zrpcclient-go", "typed", "scope_client.go.tpl"))))
if err != nil {
return err
}
err = os.WriteFile(filepath.Join(c.Gen.Zrpcclient.Output, "typed", c.Gen.Zrpcclient.Scope, fmt.Sprintf("%s_client.go", c.Gen.Zrpcclient.Scope)), template, 0o644)
filePath := filepath.Join(c.Gen.Zrpcclient.Output, "typed", c.Gen.Zrpcclient.Scope, fmt.Sprintf("%s_client.go", c.Gen.Zrpcclient.Scope))
if c.Gen.Zrpcclient.ClientDir != "" {
filePath = filepath.Join(c.Gen.Zrpcclient.Output, c.Gen.Zrpcclient.ClientDir, fmt.Sprintf("%s_client.go", c.Gen.Zrpcclient.Scope))
}
err = os.WriteFile(filePath, template, 0o644)
if err != nil {
return err
}
Expand Down
Loading