feat: Skip source directory for gen addition

This commit is contained in:
熊闯 2022-06-15 21:53:51 +08:00
parent 8d45714628
commit 17b47b1184

View File

@ -12,6 +12,8 @@ import (
)
var gormSourceDir string
var skipSourceDirs []string
var matchSkipSourceDir = false
func init() {
_, file, _, _ := runtime.Caller(0)
@ -19,13 +21,31 @@ func init() {
gormSourceDir = regexp.MustCompile(`utils.utils\.go`).ReplaceAllString(file, "")
}
func AddSkipSourceDir(dirs ...string) {
skipSourceDirs = append(skipSourceDirs, dirs...)
if len(skipSourceDirs) > 0 {
matchSkipSourceDir = true
}
}
// FileWithLineNum return the file name and line number of the current file
func FileWithLineNum() string {
// the second caller usually from gorm internal, so set i start from 2
for i := 2; i < 15; i++ {
_, file, line, ok := runtime.Caller(i)
if ok && (!strings.HasPrefix(file, gormSourceDir) || strings.HasSuffix(file, "_test.go")) {
return file + ":" + strconv.FormatInt(int64(line), 10)
matchSkip := false
if matchSkipSourceDir {
for _, dir := range skipSourceDirs {
if strings.HasPrefix(file, dir) {
matchSkip = true
break
}
}
}
if !matchSkip {
return file + ":" + strconv.FormatInt(int64(line), 10)
}
}
}