add whether to generate rpc client option (#3361)

Co-authored-by: admin <admin@admindeMacBook-Pro.local>
master
Mikael 1 year ago committed by GitHub
parent b176d5d434
commit f5f5261556
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -1 +1,2 @@
.vscode .vscode
.idea

@ -226,7 +226,8 @@
"home": "{{.global.home}}", "home": "{{.global.home}}",
"remote": "{{.global.remote}}", "remote": "{{.global.remote}}",
"branch": "{{.global.branch}}", "branch": "{{.global.branch}}",
"verbose": "Enable log output" "verbose": "Enable log output",
"client": "Whether to generate rpc client"
}, },
"template": { "template": {
"short": "Generate proto template", "short": "Generate proto template",
@ -243,7 +244,8 @@
"home": "{{.global.home}}", "home": "{{.global.home}}",
"remote": "{{.global.remote}}", "remote": "{{.global.remote}}",
"branch": "{{.global.branch}}", "branch": "{{.global.branch}}",
"verbose": "Enable log output" "verbose": "Enable log output",
"client": "Whether to generate rpc client"
} }
}, },
"template": { "template": {

@ -44,6 +44,8 @@ var (
VarBoolVerbose bool VarBoolVerbose bool
// VarBoolMultiple describes whether support generating multiple rpc services or not. // VarBoolMultiple describes whether support generating multiple rpc services or not.
VarBoolMultiple bool VarBoolMultiple bool
// VarBoolClient describes whether to generate rpc client
VarBoolClient bool
) )
// RPCNew is to generate rpc greet service, this greet service can speed // RPCNew is to generate rpc greet service, this greet service can speed
@ -88,6 +90,7 @@ func RPCNew(_ *cobra.Command, args []string) error {
ctx.IsGooglePlugin = true ctx.IsGooglePlugin = true
ctx.Output = filepath.Dir(src) ctx.Output = filepath.Dir(src)
ctx.ProtocCmd = fmt.Sprintf("protoc -I=%s %s --go_out=%s --go-grpc_out=%s", filepath.Dir(src), filepath.Base(src), filepath.Dir(src), filepath.Dir(src)) ctx.ProtocCmd = fmt.Sprintf("protoc -I=%s %s --go_out=%s --go-grpc_out=%s", filepath.Dir(src), filepath.Base(src), filepath.Dir(src), filepath.Dir(src))
ctx.IsGenClient = VarBoolClient
grpcOptList := VarStringSliceGoGRPCOpt grpcOptList := VarStringSliceGoGRPCOpt
if len(grpcOptList) > 0 { if len(grpcOptList) > 0 {

@ -102,6 +102,7 @@ func ZRPC(_ *cobra.Command, args []string) error {
ctx.IsGooglePlugin = isGooglePlugin ctx.IsGooglePlugin = isGooglePlugin
ctx.Output = zrpcOut ctx.Output = zrpcOut
ctx.ProtocCmd = strings.Join(protocArgs, " ") ctx.ProtocCmd = strings.Join(protocArgs, " ")
ctx.IsGenClient = VarBoolClient
g := generator.NewGenerator(style, verbose) g := generator.NewGenerator(style, verbose)
return g.Generate(&ctx) return g.Generate(&ctx)
} }

@ -43,6 +43,7 @@ func init() {
newCmdFlags.BoolVarP(&cli.VarBoolVerbose, "verbose", "v") newCmdFlags.BoolVarP(&cli.VarBoolVerbose, "verbose", "v")
newCmdFlags.MarkHidden("go_opt") newCmdFlags.MarkHidden("go_opt")
newCmdFlags.MarkHidden("go-grpc_opt") newCmdFlags.MarkHidden("go-grpc_opt")
newCmdFlags.BoolVarPWithDefaultValue(&cli.VarBoolClient, "client", "c", true)
protocCmdFlags.BoolVarP(&cli.VarBoolMultiple, "multiple", "m") protocCmdFlags.BoolVarP(&cli.VarBoolMultiple, "multiple", "m")
protocCmdFlags.StringSliceVar(&cli.VarStringSliceGoOut, "go_out") protocCmdFlags.StringSliceVar(&cli.VarStringSliceGoOut, "go_out")
@ -63,6 +64,7 @@ func init() {
protocCmdFlags.MarkHidden("go-grpc_opt") protocCmdFlags.MarkHidden("go-grpc_opt")
protocCmdFlags.MarkHidden("plugin") protocCmdFlags.MarkHidden("plugin")
protocCmdFlags.MarkHidden("proto_path") protocCmdFlags.MarkHidden("proto_path")
protocCmdFlags.BoolVarPWithDefaultValue(&cli.VarBoolClient, "client", "c", true)
templateCmdFlags.StringVar(&cli.VarStringOutput, "o") templateCmdFlags.StringVar(&cli.VarStringOutput, "o")
templateCmdFlags.StringVar(&cli.VarStringHome, "home") templateCmdFlags.StringVar(&cli.VarStringHome, "home")

@ -28,6 +28,8 @@ type ZRpcContext struct {
Output string Output string
// Multiple is the flag to indicate whether the proto file is generated in multiple mode. // Multiple is the flag to indicate whether the proto file is generated in multiple mode.
Multiple bool Multiple bool
// Whether to generate rpc client
IsGenClient bool
} }
// Generate generates a rpc service, through the proto file, // Generate generates a rpc service, through the proto file,
@ -100,7 +102,9 @@ func (g *Generator) Generate(zctx *ZRpcContext) error {
return err return err
} }
if zctx.IsGenClient {
err = g.GenCall(dirCtx, proto, g.cfg, zctx) err = g.GenCall(dirCtx, proto, g.cfg, zctx)
}
console.NewColorConsole().MarkDone() console.NewColorConsole().MarkDone()

@ -87,6 +87,7 @@ func mkdir(ctx *ctx.ProjectContext, proto parser.Proto, conf *conf.Config, c *ZR
return filepath.ToSlash(pkg), nil return filepath.ToSlash(pkg), nil
} }
var callClientDir string
if !c.Multiple { if !c.Multiple {
callDir := filepath.Join(ctx.WorkDir, callDir := filepath.Join(ctx.WorkDir,
strings.ToLower(stringx.From(proto.Service[0].Name).ToCamel())) strings.ToLower(stringx.From(proto.Service[0].Name).ToCamel()))
@ -98,23 +99,18 @@ func mkdir(ctx *ctx.ProjectContext, proto parser.Proto, conf *conf.Config, c *ZR
} }
callDir = filepath.Join(ctx.WorkDir, clientDir) callDir = filepath.Join(ctx.WorkDir, clientDir)
} }
inner[call] = Dir{ callClientDir = callDir
Filename: callDir,
Package: filepath.ToSlash(filepath.Join(ctx.Path,
strings.TrimPrefix(callDir, ctx.Dir))),
Base: filepath.Base(callDir),
GetChildPackage: func(childPath string) (string, error) {
return getChildPackage(callDir, childPath)
},
}
} else { } else {
callClientDir = clientDir
}
if c.IsGenClient {
inner[call] = Dir{ inner[call] = Dir{
Filename: clientDir, Filename: callClientDir,
Package: filepath.ToSlash(filepath.Join(ctx.Path, Package: filepath.ToSlash(filepath.Join(ctx.Path,
strings.TrimPrefix(clientDir, ctx.Dir))), strings.TrimPrefix(callClientDir, ctx.Dir))),
Base: filepath.Base(clientDir), Base: filepath.Base(callClientDir),
GetChildPackage: func(childPath string) (string, error) { GetChildPackage: func(childPath string) (string, error) {
return getChildPackage(clientDir, childPath) return getChildPackage(callClientDir, childPath)
}, },
} }
} }

Loading…
Cancel
Save