2020-11-05 14:12:47 +08:00
|
|
|
package generator
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"path/filepath"
|
|
|
|
|
|
2022-01-25 23:15:07 +08:00
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/rpc/parser"
|
|
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/util/console"
|
|
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/util/ctx"
|
|
|
|
|
"github.com/zeromicro/go-zero/tools/goctl/util/pathx"
|
2020-11-05 14:12:47 +08:00
|
|
|
)
|
|
|
|
|
|
2022-01-11 20:34:25 +08:00
|
|
|
type ZRpcContext struct {
|
2024-07-25 20:11:56 +09:00
|
|
|
// Src is the source file of the proto.
|
2022-07-21 12:47:46 +08:00
|
|
|
Src string
|
2024-07-25 20:11:56 +09:00
|
|
|
// ProtocCmd is the command to generate proto files.
|
2022-07-21 12:47:46 +08:00
|
|
|
ProtocCmd string
|
|
|
|
|
// ProtoGenGrpcDir is the directory to store the generated proto files.
|
2022-01-11 20:34:25 +08:00
|
|
|
ProtoGenGrpcDir string
|
2022-07-21 12:47:46 +08:00
|
|
|
// ProtoGenGoDir is the directory to store the generated go files.
|
|
|
|
|
ProtoGenGoDir string
|
|
|
|
|
// IsGooglePlugin is the flag to indicate whether the proto file is generated by google plugin.
|
|
|
|
|
IsGooglePlugin bool
|
|
|
|
|
// GoOutput is the output directory of the generated go files.
|
|
|
|
|
GoOutput string
|
|
|
|
|
// GrpcOutput is the output directory of the generated grpc files.
|
|
|
|
|
GrpcOutput string
|
|
|
|
|
// Output is the output directory of the generated files.
|
|
|
|
|
Output string
|
|
|
|
|
// Multiple is the flag to indicate whether the proto file is generated in multiple mode.
|
|
|
|
|
Multiple bool
|
2023-06-17 20:52:49 +08:00
|
|
|
// Whether to generate rpc client
|
|
|
|
|
IsGenClient bool
|
2025-08-31 16:40:49 +08:00
|
|
|
// Module is the custom module name for go.mod
|
|
|
|
|
Module string
|
2020-11-05 14:12:47 +08:00
|
|
|
}
|
|
|
|
|
|
2022-07-21 12:47:46 +08:00
|
|
|
// Generate generates a rpc service, through the proto file,
|
2021-02-26 16:11:47 +08:00
|
|
|
// code storage directory, and proto import parameters to control
|
|
|
|
|
// the source file and target location of the rpc service that needs to be generated
|
2022-03-19 22:50:22 +08:00
|
|
|
func (g *Generator) Generate(zctx *ZRpcContext) error {
|
|
|
|
|
abs, err := filepath.Abs(zctx.Output)
|
2020-11-05 14:12:47 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-03 21:32:40 +08:00
|
|
|
err = pathx.MkdirIfNotExist(abs)
|
2020-11-05 14:12:47 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-19 22:50:22 +08:00
|
|
|
err = g.Prepare()
|
2021-09-11 20:57:58 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-31 16:40:49 +08:00
|
|
|
var projectCtx *ctx.ProjectContext
|
|
|
|
|
if len(zctx.Module) > 0 {
|
|
|
|
|
projectCtx, err = ctx.PrepareWithModule(abs, zctx.Module)
|
|
|
|
|
} else {
|
|
|
|
|
projectCtx, err = ctx.Prepare(abs)
|
|
|
|
|
}
|
2020-11-05 14:12:47 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
p := parser.NewDefaultProtoParser()
|
2022-07-21 12:47:46 +08:00
|
|
|
proto, err := p.Parse(zctx.Src, zctx.Multiple)
|
2020-11-05 14:12:47 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-19 22:50:22 +08:00
|
|
|
dirCtx, err := mkdir(projectCtx, proto, g.cfg, zctx)
|
2020-11-05 14:12:47 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-19 22:50:22 +08:00
|
|
|
err = g.GenEtc(dirCtx, proto, g.cfg)
|
2020-11-05 14:12:47 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-19 22:50:22 +08:00
|
|
|
err = g.GenPb(dirCtx, zctx)
|
2020-11-05 14:12:47 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-19 22:50:22 +08:00
|
|
|
err = g.GenConfig(dirCtx, proto, g.cfg)
|
2020-11-05 14:12:47 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-19 22:50:22 +08:00
|
|
|
err = g.GenSvc(dirCtx, proto, g.cfg)
|
2020-11-05 14:12:47 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-21 12:47:46 +08:00
|
|
|
err = g.GenLogic(dirCtx, proto, g.cfg, zctx)
|
2020-11-05 14:12:47 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-21 12:47:46 +08:00
|
|
|
err = g.GenServer(dirCtx, proto, g.cfg, zctx)
|
2020-11-05 14:12:47 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-21 12:47:46 +08:00
|
|
|
err = g.GenMain(dirCtx, proto, g.cfg, zctx)
|
2020-11-05 14:12:47 +08:00
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-17 20:52:49 +08:00
|
|
|
if zctx.IsGenClient {
|
|
|
|
|
err = g.GenCall(dirCtx, proto, g.cfg, zctx)
|
|
|
|
|
}
|
2020-11-05 14:12:47 +08:00
|
|
|
|
|
|
|
|
console.NewColorConsole().MarkDone()
|
|
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
}
|