From ba636187ce344dcf8a0290508de96bf91c011349 Mon Sep 17 00:00:00 2001 From: kevin Date: Mon, 10 Aug 2020 17:26:47 +0800 Subject: [PATCH] fix goctl path issue on windows --- tools/goctl/api/gogen/genhandlers.go | 7 +++-- tools/goctl/api/gogen/genlogic.go | 6 ++-- tools/goctl/api/gogen/genmain.go | 8 ++--- tools/goctl/api/gogen/genroutes.go | 5 ++-- tools/goctl/api/gogen/genservicecontext.go | 4 +-- tools/goctl/util/env.go | 35 ---------------------- tools/goctl/util/path.go | 6 ++++ 7 files changed, 22 insertions(+), 49 deletions(-) delete mode 100644 tools/goctl/util/env.go diff --git a/tools/goctl/api/gogen/genhandlers.go b/tools/goctl/api/gogen/genhandlers.go index 56d5ed11..1015a502 100644 --- a/tools/goctl/api/gogen/genhandlers.go +++ b/tools/goctl/api/gogen/genhandlers.go @@ -163,11 +163,12 @@ func genHandlers(dir string, api *spec.ApiSpec) error { func genHandlerImports(group spec.Group, route spec.Route, parentPkg string) string { var imports []string imports = append(imports, fmt.Sprintf("\"%s/rest/httpx\"", vars.ProjectOpenSourceUrl)) - imports = append(imports, fmt.Sprintf("\"%s\"", path.Join(parentPkg, contextDir))) + imports = append(imports, fmt.Sprintf("\"%s\"", util.JoinPackages(parentPkg, contextDir))) if len(route.RequestType.Name) > 0 || len(route.ResponseType.Name) > 0 { - imports = append(imports, fmt.Sprintf("\"%s\"", path.Join(parentPkg, typesDir))) + imports = append(imports, fmt.Sprintf("\"%s\"", util.JoinPackages(parentPkg, typesDir))) } - imports = append(imports, fmt.Sprintf("\"%s\"", path.Join(parentPkg, getLogicFolderPath(group, route)))) + imports = append(imports, fmt.Sprintf("\"%s\"", + util.JoinPackages(parentPkg, getLogicFolderPath(group, route)))) sort.Strings(imports) return strings.Join(imports, "\n\t") diff --git a/tools/goctl/api/gogen/genlogic.go b/tools/goctl/api/gogen/genlogic.go index 257fc37c..2daff8ba 100644 --- a/tools/goctl/api/gogen/genlogic.go +++ b/tools/goctl/api/gogen/genlogic.go @@ -9,6 +9,7 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/api/spec" "github.com/tal-tech/go-zero/tools/goctl/api/util" + ctlutil "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/vars" ) @@ -34,7 +35,6 @@ func New{{.logic}}(ctx context.Context, svcCtx *svc.ServiceContext) {{.logic}} { func (l *{{.logic}}) {{.function}}({{.request}}) {{.responseType}} { {{.returnString}} } - ` func genLogic(dir string, api *spec.ApiSpec) error { @@ -124,8 +124,8 @@ func genLogicImports(route spec.Route, parentPkg string) string { imports = append(imports, "\n") imports = append(imports, fmt.Sprintf("\"%s/core/logx\"", vars.ProjectOpenSourceUrl)) if len(route.ResponseType.Name) > 0 || len(route.RequestType.Name) > 0 { - imports = append(imports, fmt.Sprintf("\"%s\"", path.Join(parentPkg, typesDir))) + imports = append(imports, fmt.Sprintf("\"%s\"", ctlutil.JoinPackages(parentPkg, typesDir))) } - imports = append(imports, fmt.Sprintf("\"%s\"", path.Join(parentPkg, contextDir))) + imports = append(imports, fmt.Sprintf("\"%s\"", ctlutil.JoinPackages(parentPkg, contextDir))) return strings.Join(imports, "\n\t") } diff --git a/tools/goctl/api/gogen/genmain.go b/tools/goctl/api/gogen/genmain.go index 55d46e10..1426489c 100644 --- a/tools/goctl/api/gogen/genmain.go +++ b/tools/goctl/api/gogen/genmain.go @@ -3,13 +3,13 @@ package gogen import ( "bytes" "fmt" - "path" "sort" "strings" "text/template" "github.com/tal-tech/go-zero/tools/goctl/api/spec" "github.com/tal-tech/go-zero/tools/goctl/api/util" + ctlutil "github.com/tal-tech/go-zero/tools/goctl/util" "github.com/tal-tech/go-zero/tools/goctl/vars" ) @@ -77,9 +77,9 @@ func genMainImports(parentPkg string) string { fmt.Sprintf("\"%s/core/conf\"", vars.ProjectOpenSourceUrl), fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceUrl), } - imports = append(imports, fmt.Sprintf("\"%s\"", path.Join(parentPkg, configDir))) - imports = append(imports, fmt.Sprintf("\"%s\"", path.Join(parentPkg, handlerDir))) - imports = append(imports, fmt.Sprintf("\"%s\"", path.Join(parentPkg, contextDir))) + imports = append(imports, fmt.Sprintf("\"%s\"", ctlutil.JoinPackages(parentPkg, configDir))) + imports = append(imports, fmt.Sprintf("\"%s\"", ctlutil.JoinPackages(parentPkg, handlerDir))) + imports = append(imports, fmt.Sprintf("\"%s\"", ctlutil.JoinPackages(parentPkg, contextDir))) sort.Strings(imports) return strings.Join(imports, "\n\t") } diff --git a/tools/goctl/api/gogen/genroutes.go b/tools/goctl/api/gogen/genroutes.go index 9296729f..b52f2e06 100644 --- a/tools/goctl/api/gogen/genroutes.go +++ b/tools/goctl/api/gogen/genroutes.go @@ -131,7 +131,7 @@ func genRoutes(dir string, api *spec.ApiSpec) error { func genRouteImports(parentPkg string, api *spec.ApiSpec) string { var importSet = collection.NewSet() importSet.AddStr(fmt.Sprintf("\"%s/rest\"", vars.ProjectOpenSourceUrl)) - importSet.AddStr(fmt.Sprintf("\"%s\"", path.Join(parentPkg, contextDir))) + importSet.AddStr(fmt.Sprintf("\"%s\"", util.JoinPackages(parentPkg, contextDir))) for _, group := range api.Service.Groups { for _, route := range group.Routes { folder, ok := apiutil.GetAnnotationValue(route.Annotations, "server", folderProperty) @@ -141,7 +141,8 @@ func genRouteImports(parentPkg string, api *spec.ApiSpec) string { continue } } - importSet.AddStr(fmt.Sprintf("%s \"%s\"", folder, path.Join(parentPkg, handlerDir, folder))) + importSet.AddStr(fmt.Sprintf("%s \"%s\"", folder, + util.JoinPackages(parentPkg, handlerDir, folder))) } } imports := importSet.KeysStr() diff --git a/tools/goctl/api/gogen/genservicecontext.go b/tools/goctl/api/gogen/genservicecontext.go index b890c020..0c380971 100644 --- a/tools/goctl/api/gogen/genservicecontext.go +++ b/tools/goctl/api/gogen/genservicecontext.go @@ -3,11 +3,11 @@ package gogen import ( "bytes" "fmt" - "path" "text/template" "github.com/tal-tech/go-zero/tools/goctl/api/spec" "github.com/tal-tech/go-zero/tools/goctl/api/util" + ctlutil "github.com/tal-tech/go-zero/tools/goctl/util" ) const ( @@ -47,7 +47,7 @@ func genServiceContext(dir string, api *spec.ApiSpec) error { if err != nil { return err } - var configImport = "\"" + path.Join(parentPkg, configDir) + "\"" + var configImport = "\"" + ctlutil.JoinPackages(parentPkg, configDir) + "\"" t := template.Must(template.New("contextTemplate").Parse(contextTemplate)) buffer := new(bytes.Buffer) err = t.Execute(buffer, map[string]string{ diff --git a/tools/goctl/util/env.go b/tools/goctl/util/env.go deleted file mode 100644 index 0b4b0935..00000000 --- a/tools/goctl/util/env.go +++ /dev/null @@ -1,35 +0,0 @@ -package util - -import ( - "fmt" - "os" - "path" - "strings" -) - -func GetFullPackage(pkg string) (string, error) { - dir, err := os.Getwd() - if err != nil { - return "", err - } - - pkgPath := path.Join(dir, pkg) - info, err := os.Stat(pkgPath) - if err != nil { - return "", err - } - - if !info.IsDir() { - return "", fmt.Errorf("%s is not a directory", pkg) - } - - gopath := os.Getenv("GOPATH") - parent := path.Join(gopath, "src") - pos := strings.Index(pkgPath, parent) - if pos < 0 { - return "", fmt.Errorf("%s is not a correct package", pkg) - } - - // skip slash - return pkgPath[len(parent)+1:], nil -} diff --git a/tools/goctl/util/path.go b/tools/goctl/util/path.go index f5739a86..8446b4bb 100644 --- a/tools/goctl/util/path.go +++ b/tools/goctl/util/path.go @@ -10,6 +10,12 @@ import ( "github.com/tal-tech/go-zero/tools/goctl/vars" ) +const pkgSep = "/" + +func JoinPackages(pkgs ...string) string { + return strings.Join(pkgs, pkgSep) +} + func MkdirIfNotExist(dir string) error { if len(dir) == 0 { return nil