anqiansong 3 年之前
父節點
當前提交
7fb5bab26b
共有 2 個文件被更改,包括 32 次插入1 次删除
  1. 14 1
      tools/goctl/api/gogen/util.go
  2. 18 0
      tools/goctl/util/file.go

+ 14 - 1
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 {

+ 18 - 0
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