From 1d12f20ff6fa65a289e8076c18256e322ba05a73 Mon Sep 17 00:00:00 2001 From: kingxt Date: Mon, 7 Sep 2020 18:47:03 +0800 Subject: [PATCH] refactor (#49) * rebase upstream * rebase * trim no need line * trim no need line * trim no need line * refactor gomod module logic Co-authored-by: kingxt --- tools/goctl/api/gogen/util.go | 12 ++++++++++++ tools/goctl/util/project/project.go | 2 ++ 2 files changed, 14 insertions(+) diff --git a/tools/goctl/api/gogen/util.go b/tools/goctl/api/gogen/util.go index 5f82a2ae..f1edd409 100644 --- a/tools/goctl/api/gogen/util.go +++ b/tools/goctl/api/gogen/util.go @@ -4,11 +4,13 @@ import ( "fmt" goformat "go/format" "io" + "path/filepath" "strings" "github.com/tal-tech/go-zero/core/collection" "github.com/tal-tech/go-zero/tools/goctl/api/spec" "github.com/tal-tech/go-zero/tools/goctl/api/util" + goctlutil "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/util/project" ) @@ -17,6 +19,16 @@ func getParentPackage(dir string) (string, error) { if err != nil { return "", err } + if len(p.GoMod.Path) > 0 { + goModePath := filepath.Clean(filepath.Dir(p.GoMod.Path)) + absPath, err := filepath.Abs(dir) + if err != nil { + return "", err + } + parent := filepath.Clean(goctlutil.JoinPackages(p.GoMod.Module, absPath[len(goModePath):])) + parent = strings.ReplaceAll(parent, "\\", "/") + return parent, nil + } return p.GoMod.Module, nil } diff --git a/tools/goctl/util/project/project.go b/tools/goctl/util/project/project.go index 954753a4..8e6da6d0 100644 --- a/tools/goctl/util/project/project.go +++ b/tools/goctl/util/project/project.go @@ -28,6 +28,7 @@ type ( GoMod struct { Module string + Path string } ) @@ -110,6 +111,7 @@ func Prepare(projectDir string, checkGrpcEnv bool) (*Project, error) { Path: path, GoMod: GoMod{ Module: module, + Path: goMod, }, }, nil }