From 7fb5bab26b9ab41b0398411325deb09d3a2debaa Mon Sep 17 00:00:00 2001 From: anqiansong Date: Sun, 19 Sep 2021 08:53:41 +0800 Subject: [PATCH] fix #1058 (#1064) --- tools/goctl/api/gogen/util.go | 15 ++++++++++++++- tools/goctl/util/file.go | 18 ++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/tools/goctl/api/gogen/util.go b/tools/goctl/api/gogen/util.go index 106688b1..aa9ba8fc 100644 --- a/tools/goctl/api/gogen/util.go +++ b/tools/goctl/api/gogen/util.go @@ -70,7 +70,20 @@ func getParentPackage(dir string) (string, error) { return "", err } - return filepath.ToSlash(filepath.Join(projectCtx.Path, strings.TrimPrefix(projectCtx.WorkDir, projectCtx.Dir))), nil + // fix https://github.com/zeromicro/go-zero/issues/1058 + wd := projectCtx.WorkDir + d := projectCtx.Dir + same, err := ctlutil.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 { diff --git a/tools/goctl/util/file.go b/tools/goctl/util/file.go index b8ef6bf2..149d2a83 100644 --- a/tools/goctl/util/file.go +++ b/tools/goctl/util/file.go @@ -148,6 +148,24 @@ func LoadTemplate(category, file, builtin string) (string, error) { return string(content), nil } +// SameFile compares the between path if the same path, +// it maybe the same path in case case-ignore, such as: +// /Users/go_zero and /Users/Go_zero, as far as we know, +// this case maybe appear on macOS and Windows. +func SameFile(path1, path2 string) (bool, error) { + stat1, err := os.Stat(path1) + if err != nil { + return false, err + } + + stat2, err := os.Stat(path2) + if err != nil { + return false, err + } + + return os.SameFile(stat1, stat2), nil +} + func createTemplate(file, content string, force bool) error { if FileExists(file) && !force { return nil