add auto package name

This commit is contained in:
谢小军
2019-09-23 19:10:25 +08:00
parent 7940413ee2
commit 47f44b3cd8
4 changed files with 41 additions and 11 deletions

View File

@@ -3,6 +3,7 @@ package gtools
import (
"fmt"
"os/exec"
"strings"
"github.com/xxjwxc/gormt/data/config"
@@ -22,7 +23,7 @@ func Execute() {
// fmt.Println(tt)
pkg := OnGetPackageInfo(orm, OnGetTables(orm))
pkg.SetPackage("model")
pkg.SetPackage(getPkgName())
str := pkg.Generate()
path := config.GetOutDir() + "/" + config.GetMysqlDbInfo().Database + ".go"
@@ -36,5 +37,29 @@ func Execute() {
fmt.Println("formatting differs from gofmt's:")
cmd, _ = exec.Command("gofmt", "-l", "-w", path).Output()
fmt.Println(string(cmd))
}
// 通过config outdir 配置获取报名
func getPkgName() string {
dir := config.GetOutDir()
dir = strings.Replace(dir, "\\", "/", -1)
if len(dir) > 0 {
if dir[len(dir)-1] == '/' {
dir = dir[:(len(dir) - 1)]
}
}
var pkgName string
list := strings.Split(dir, "/")
if len(list) > 0 {
pkgName = list[len(list)-1]
}
if len(pkgName) == 0 || pkgName == "." {
list = strings.Split(tools.GetModelPath(), "/")
if len(list) > 0 {
pkgName = list[len(list)-1]
}
}
return pkgName
}