Skip to content

Commit

Permalink
Revert "zrpcclient 支持自定义pb和client 输出路径 (#118)"
Browse files Browse the repository at this point in the history
This reverts commit b424cd2.
  • Loading branch information
jaronnie authored Dec 4, 2024
1 parent b424cd2 commit 408f209
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 48 deletions.
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 {{.Package}}
package {{.Scope}}

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

Expand Down
2 changes: 0 additions & 2 deletions cmd/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,6 @@ 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: 7 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,13 @@ type GenSwaggerConfig struct {
}

type GenZrpcclientConfig struct {
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"`
Hooks HooksConfig `mapstructure:"hooks"`

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: 9 additions & 36 deletions internal/gen/genzrpcclient/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,11 @@ 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: fileName,
Filename: filepath.Join(d.Output, "typed", d.Scope),
GetChildPackage: func(childPath string) (string, error) {
return strings.ToLower(childPath), nil
},
Expand Down Expand Up @@ -69,22 +63,14 @@ func (d DirContext) GetSvc() generator.Dir {

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

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: d.packagePath(),
Package: filepath.ToSlash(fmt.Sprintf("%s/model/%s/%s", d.ImportBase, d.Scope, strings.TrimPrefix(d.OptionGoPackage, "./"))),
}
}

Expand Down Expand Up @@ -116,6 +102,7 @@ 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 @@ -128,23 +115,18 @@ 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(pbDir, 0o755)
err = os.MkdirAll(filepath.Join(c.Gen.Zrpcclient.Output, "model", c.Gen.Zrpcclient.Scope), 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"), pbDir, pbDir, 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"), filepath.Join(c.Gen.Zrpcclient.Output, "model", c.Gen.Zrpcclient.Scope), filepath.Join(c.Gen.Zrpcclient.Output, "model", c.Gen.Zrpcclient.Scope), fp), wd)
if err != nil {
return errors.Errorf("err: [%v], resp: [%s]", err, resp)
}
Expand Down Expand Up @@ -188,24 +170,15 @@ 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": scope,
"Package": c.Gen.Zrpcclient.Scope,
"Scope": 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
}
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)
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)
if err != nil {
return err
}
Expand Down

0 comments on commit 408f209

Please sign in to comment.