diff --git a/tools/goctl/api/gogen/gen.go b/tools/goctl/api/gogen/gen.go index 02c5186f..55a07e95 100644 --- a/tools/goctl/api/gogen/gen.go +++ b/tools/goctl/api/gogen/gen.go @@ -18,6 +18,7 @@ import ( "github.com/zeromicro/go-zero/tools/goctl/api/parser" apiutil "github.com/zeromicro/go-zero/tools/goctl/api/util" "github.com/zeromicro/go-zero/tools/goctl/config" + "github.com/zeromicro/go-zero/tools/goctl/pkg/golang" "github.com/zeromicro/go-zero/tools/goctl/util" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" ) @@ -85,7 +86,7 @@ func DoGenProject(apiFile, dir, style string) error { } logx.Must(pathx.MkdirIfNotExist(dir)) - rootPkg, err := getParentPackage(dir) + rootPkg, err := golang.GetParentPackage(dir) if err != nil { return err } diff --git a/tools/goctl/api/gogen/genhandlers.go b/tools/goctl/api/gogen/genhandlers.go index d073bf98..1d5b1265 100644 --- a/tools/goctl/api/gogen/genhandlers.go +++ b/tools/goctl/api/gogen/genhandlers.go @@ -9,6 +9,7 @@ import ( "github.com/zeromicro/go-zero/tools/goctl/api/spec" "github.com/zeromicro/go-zero/tools/goctl/config" "github.com/zeromicro/go-zero/tools/goctl/internal/version" + "github.com/zeromicro/go-zero/tools/goctl/pkg/golang" "github.com/zeromicro/go-zero/tools/goctl/util" "github.com/zeromicro/go-zero/tools/goctl/util/format" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" @@ -42,7 +43,7 @@ func genHandler(dir, rootPkg string, cfg *config.Config, group spec.Group, route handler = strings.Title(handler) logicName = pkgName } - parentPkg, err := getParentPackage(dir) + parentPkg, err := golang.GetParentPackage(dir) if err != nil { return err } diff --git a/tools/goctl/api/gogen/util.go b/tools/goctl/api/gogen/util.go index cb4ac877..a8798ebd 100644 --- a/tools/goctl/api/gogen/util.go +++ b/tools/goctl/api/gogen/util.go @@ -3,16 +3,14 @@ package gogen import ( "bytes" "fmt" - goformat "go/format" "io" - "path/filepath" "strings" "text/template" "github.com/zeromicro/go-zero/core/collection" "github.com/zeromicro/go-zero/tools/goctl/api/spec" "github.com/zeromicro/go-zero/tools/goctl/api/util" - "github.com/zeromicro/go-zero/tools/goctl/util/ctx" + "github.com/zeromicro/go-zero/tools/goctl/pkg/golang" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" ) @@ -54,38 +52,11 @@ func genFile(c fileGenConfig) error { return err } - code := formatCode(buffer.String()) + code := golang.FormatCode(buffer.String()) _, err = fp.WriteString(code) return err } -func getParentPackage(dir string) (string, error) { - abs, err := filepath.Abs(dir) - if err != nil { - return "", err - } - - projectCtx, err := ctx.Prepare(abs) - if err != nil { - return "", err - } - - // fix https://github.com/zeromicro/go-zero/issues/1058 - wd := projectCtx.WorkDir - d := projectCtx.Dir - same, err := pathx.SameFile(wd, d) - if err != nil { - return "", err - } - - trim := strings.TrimPrefix(projectCtx.WorkDir, projectCtx.Dir) - if same { - trim = strings.TrimPrefix(strings.ToLower(projectCtx.WorkDir), strings.ToLower(projectCtx.Dir)) - } - - return filepath.ToSlash(filepath.Join(projectCtx.Path, trim)), nil -} - func writeProperty(writer io.Writer, name, tag, comment string, tp spec.Type, indent int) error { util.WriteIndent(writer, indent) var err error @@ -136,15 +107,6 @@ func getMiddleware(api *spec.ApiSpec) []string { return result.KeysStr() } -func formatCode(code string) string { - ret, err := goformat.Source([]byte(code)) - if err != nil { - return code - } - - return string(ret) -} - func responseGoTypeName(r spec.Route, pkg ...string) string { if r.ResponseType == nil { return "" diff --git a/tools/goctl/api/gogen/vars.go b/tools/goctl/api/gogen/vars.go index f7f94cf7..c55142da 100644 --- a/tools/goctl/api/gogen/vars.go +++ b/tools/goctl/api/gogen/vars.go @@ -1,13 +1,13 @@ package gogen const ( - interval = "internal/" + internal = "internal/" typesPacket = "types" - configDir = interval + "config" - contextDir = interval + "svc" - handlerDir = interval + "handler" - logicDir = interval + "logic" - middlewareDir = interval + "middleware" - typesDir = interval + typesPacket + configDir = internal + "config" + contextDir = internal + "svc" + handlerDir = internal + "handler" + logicDir = internal + "logic" + middlewareDir = internal + "middleware" + typesDir = internal + typesPacket groupProperty = "group" ) diff --git a/tools/goctl/pkg/golang/format.go b/tools/goctl/pkg/golang/format.go new file mode 100644 index 00000000..d7af3c24 --- /dev/null +++ b/tools/goctl/pkg/golang/format.go @@ -0,0 +1,12 @@ +package golang + +import goformat "go/format" + +func FormatCode(code string) string { + ret, err := goformat.Source([]byte(code)) + if err != nil { + return code + } + + return string(ret) +} diff --git a/tools/goctl/pkg/golang/path.go b/tools/goctl/pkg/golang/path.go new file mode 100644 index 00000000..1f0a2bb1 --- /dev/null +++ b/tools/goctl/pkg/golang/path.go @@ -0,0 +1,36 @@ +package golang + +import ( + "path/filepath" + "strings" + + "github.com/zeromicro/go-zero/tools/goctl/util/ctx" + "github.com/zeromicro/go-zero/tools/goctl/util/pathx" +) + +func GetParentPackage(dir string) (string, error) { + abs, err := filepath.Abs(dir) + if err != nil { + return "", err + } + + projectCtx, err := ctx.Prepare(abs) + if err != nil { + return "", err + } + + // fix https://github.com/zeromicro/go-zero/issues/1058 + wd := projectCtx.WorkDir + d := projectCtx.Dir + same, err := pathx.SameFile(wd, d) + if err != nil { + return "", err + } + + trim := strings.TrimPrefix(projectCtx.WorkDir, projectCtx.Dir) + if same { + trim = strings.TrimPrefix(strings.ToLower(projectCtx.WorkDir), strings.ToLower(projectCtx.Dir)) + } + + return filepath.ToSlash(filepath.Join(projectCtx.Path, trim)), nil +} diff --git a/tools/goctl/quickstart/idl/apilogic.tpl b/tools/goctl/quickstart/idl/apilogic.tpl index 6cc01fbf..9605f952 100644 --- a/tools/goctl/quickstart/idl/apilogic.tpl +++ b/tools/goctl/quickstart/idl/apilogic.tpl @@ -4,9 +4,10 @@ import ( "context" "github.com/zeromicro/go-zero/core/logx" - "greet/api/internal/svc" - "greet/api/internal/types"{{if .callRPC}} - "greet/rpc/greet"{{end}} + + "{{.svcPkg}}" + "{{.typesPkg}}"{{if .callRPC}} + "{{.rpcClientPkg}}"{{end}} ) type PingLogic struct { diff --git a/tools/goctl/quickstart/idl/svc.tpl b/tools/goctl/quickstart/idl/svc.tpl index 36cac723..4d7b96e6 100644 --- a/tools/goctl/quickstart/idl/svc.tpl +++ b/tools/goctl/quickstart/idl/svc.tpl @@ -1,22 +1,22 @@ package svc import ( + "{{.configPkg}}"{{if .callRPC}} "github.com/zeromicro/go-zero/zrpc" - "greet/api/internal/config" - "greet/rpc/greet" + "{{.rpcClientPkg}}"{{end}} ) type ServiceContext struct { - Config config.Config - GreetRpc greet.Greet + Config config.Config{{if .callRPC}} + GreetRpc greet.Greet{{end}} } func NewServiceContext(c config.Config) *ServiceContext { - client := zrpc.MustNewClient(zrpc.RpcClientConf{ + {{if .callRPC}}client := zrpc.MustNewClient(zrpc.RpcClientConf{ Target: "127.0.0.1:8080", - }) + }){{end}} return &ServiceContext{ Config: c, - GreetRpc: greet.NewGreet(client), + {{if .callRPC}}GreetRpc: greet.NewGreet(client),{{end}} } } diff --git a/tools/goctl/quickstart/micro.go b/tools/goctl/quickstart/micro.go index 68e995dd..08a10c0a 100644 --- a/tools/goctl/quickstart/micro.go +++ b/tools/goctl/quickstart/micro.go @@ -17,8 +17,7 @@ var ( protocContent string //go:embed idl/rpc.yaml rpcEtcContent string - - zRPCWorkDir string + zrpcWorkDir string ) type serviceImpl struct { @@ -32,12 +31,12 @@ func (s serviceImpl) Start() { func (s serviceImpl) Stop() {} func initRPCProto() error { - zRPCWorkDir = filepath.Join(projectDir, "rpc") - if err := pathx.MkdirIfNotExist(zRPCWorkDir); err != nil { + zrpcWorkDir = filepath.Join(projectDir, "rpc") + if err := pathx.MkdirIfNotExist(zrpcWorkDir); err != nil { return err } - protoFilename := filepath.Join(zRPCWorkDir, protoName) + protoFilename := filepath.Join(zrpcWorkDir, protoName) rpcBytes := []byte(protocContent) return ioutil.WriteFile(protoFilename, rpcBytes, 0666) } @@ -54,8 +53,8 @@ func (m micro) mustStartRPCProject() { logx.Must(initRPCProto()) log.Debug(">> Generating quickstart zRPC project...") arg := "goctl rpc protoc " + protoName + " --go_out=. --go-grpc_out=. --zrpc_out=. --verbose" - execCommand(zRPCWorkDir, arg) - etcFile := filepath.Join(zRPCWorkDir, "etc", "greet.yaml") + execCommand(zrpcWorkDir, arg) + etcFile := filepath.Join(zrpcWorkDir, "etc", "greet.yaml") logx.Must(ioutil.WriteFile(etcFile, []byte(rpcEtcContent), 0666)) } @@ -65,7 +64,7 @@ func (m micro) start() { sg := service.NewServiceGroup() sg.Add(serviceImpl{func() { log.Debug(">> Ready to start a zRPC server...") - goStart(zRPCWorkDir) + goStart(zrpcWorkDir) }}) sg.Add(serviceImpl{func() { mono.start() diff --git a/tools/goctl/quickstart/mono.go b/tools/goctl/quickstart/mono.go index 9331a390..30f5deda 100644 --- a/tools/goctl/quickstart/mono.go +++ b/tools/goctl/quickstart/mono.go @@ -7,6 +7,7 @@ import ( "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/tools/goctl/api/gogen" + "github.com/zeromicro/go-zero/tools/goctl/pkg/golang" "github.com/zeromicro/go-zero/tools/goctl/util" "github.com/zeromicro/go-zero/tools/goctl/util/pathx" ) @@ -22,9 +23,11 @@ var ( apiEtcContent string apiWorkDir string + rpcWorkDir string ) func initAPIFlags() error { + rpcWorkDir = filepath.Join(projectDir, "rpc") apiWorkDir = filepath.Join(projectDir, "api") if err := pathx.MkdirIfNotExist(apiWorkDir); err != nil { return err @@ -58,15 +61,36 @@ func (m mono) createAPIProject() { etcFile := filepath.Join(apiWorkDir, "etc", "greet.yaml") logx.Must(ioutil.WriteFile(etcFile, []byte(apiEtcContent), 0666)) logicFile := filepath.Join(apiWorkDir, "internal", "logic", "pinglogic.go") + svcFile := filepath.Join(apiWorkDir, "internal", "svc", "servicecontext.go") + configPath := filepath.Join(apiWorkDir, "internal", "config") + svcPath := filepath.Join(apiWorkDir, "internal", "svc") + typesPath := filepath.Join(apiWorkDir, "internal", "types") + svcPkg, err := golang.GetParentPackage(svcPath) + logx.Must(err) + typesPkg, err := golang.GetParentPackage(typesPath) + logx.Must(err) + configPkg, err := golang.GetParentPackage(configPath) + logx.Must(err) - logx.Must(util.With("logic").Parse(apiLogicContent).SaveTo(map[string]bool{ - "callRPC": m.callRPC, - }, logicFile, true)) - + var rpcClientPkg string if m.callRPC { - svcFile := filepath.Join(apiWorkDir, "internal", "svc", "servicecontext.go") - logx.Must(ioutil.WriteFile(svcFile, []byte(svcContent), 0666)) + rpcClientPath := filepath.Join(rpcWorkDir, "greet") + rpcClientPkg, err = golang.GetParentPackage(rpcClientPath) + logx.Must(err) } + + logx.Must(util.With("logic").Parse(apiLogicContent).SaveTo(map[string]interface{}{ + "svcPkg": svcPkg, + "typesPkg": typesPkg, + "rpcClientPkg": rpcClientPkg, + "callRPC": m.callRPC, + }, logicFile, true)) + + logx.Must(util.With("svc").Parse(svcContent).SaveTo(map[string]interface{}{ + "rpcClientPkg": rpcClientPkg, + "configPkg": configPkg, + "callRPC": m.callRPC, + }, svcFile, true)) } func (m mono) start() { diff --git a/tools/goctl/util/pathx/path.go b/tools/goctl/util/pathx/path.go index 06a1638d..8b5ced37 100644 --- a/tools/goctl/util/pathx/path.go +++ b/tools/goctl/util/pathx/path.go @@ -51,8 +51,8 @@ func PathFromGoSrc() (string, error) { return dir[len(parent)+1:], nil } -// FindGoModPath returns the path in project where has file go.mod, it maybe return empty string if -// there is no go.mod file in project +// FindGoModPath returns the path in project where has file go.mod, +// it returns empty string if there is no go.mod file in project. func FindGoModPath(dir string) (string, bool) { absDir, err := filepath.Abs(dir) if err != nil {