refactor file|path (#1409)

Co-authored-by: anqiansong <anqiansong@bytedance.com>
This commit is contained in:
anqiansong
2022-01-03 21:32:40 +08:00
committed by GitHub
parent 290de6aa96
commit 89ce5e492b
81 changed files with 279 additions and 245 deletions

View File

@@ -15,6 +15,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/model/sql/util"
file "github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/console"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
)
@@ -50,7 +51,7 @@ func MysqlDDL(ctx *cli.Context) error {
}
}
if len(home) > 0 {
file.RegisterGoctlHome(home)
pathx.RegisterGoctlHome(home)
}
cfg, err := config.NewConfig(style)
if err != nil {
@@ -76,7 +77,7 @@ func MySqlDataSource(ctx *cli.Context) error {
}
}
if len(home) > 0 {
file.RegisterGoctlHome(home)
pathx.RegisterGoctlHome(home)
}
pattern := strings.TrimSpace(ctx.String(flagTable))
@@ -105,7 +106,7 @@ func PostgreSqlDataSource(ctx *cli.Context) error {
}
}
if len(home) > 0 {
file.RegisterGoctlHome(home)
pathx.RegisterGoctlHome(home)
}
if len(schema) == 0 {

View File

@@ -9,7 +9,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/tal-tech/go-zero/tools/goctl/config"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/gen"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
)
var (
@@ -23,12 +23,12 @@ func TestFromDDl(t *testing.T) {
err := gen.Clean()
assert.Nil(t, err)
err = fromDDL("./user.sql", util.MustTempDir(), cfg, true, false, "go_zero")
err = fromDDL("./user.sql", pathx.MustTempDir(), cfg, true, false, "go_zero")
assert.Equal(t, errNotMatched, err)
// case dir is not exists
unknownDir := filepath.Join(util.MustTempDir(), "test", "user.sql")
err = fromDDL(unknownDir, util.MustTempDir(), cfg, true, false, "go_zero")
unknownDir := filepath.Join(pathx.MustTempDir(), "test", "user.sql")
err = fromDDL(unknownDir, pathx.MustTempDir(), cfg, true, false, "go_zero")
assert.True(t, func() bool {
switch err.(type) {
case *os.PathError:
@@ -39,13 +39,13 @@ func TestFromDDl(t *testing.T) {
}())
// case empty src
err = fromDDL("", util.MustTempDir(), cfg, true, false, "go_zero")
err = fromDDL("", pathx.MustTempDir(), cfg, true, false, "go_zero")
if err != nil {
assert.Equal(t, "expected path or path globbing patterns, but nothing found", err.Error())
}
tempDir := filepath.Join(util.MustTempDir(), "test")
err = util.MkdirIfNotExist(tempDir)
tempDir := filepath.Join(pathx.MustTempDir(), "test")
err = pathx.MkdirIfNotExist(tempDir)
if err != nil {
return
}

View File

@@ -6,6 +6,7 @@ import (
"github.com/tal-tech/go-zero/core/collection"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
)
@@ -20,7 +21,7 @@ func genDelete(table Table, withCache, postgreSql bool) (string, string, error)
}
camel := table.Name.ToCamel()
text, err := util.LoadTemplate(category, deleteTemplateFile, template.Delete)
text, err := pathx.LoadTemplate(category, deleteTemplateFile, template.Delete)
if err != nil {
return "", "", err
}
@@ -43,7 +44,7 @@ func genDelete(table Table, withCache, postgreSql bool) (string, string, error)
}
// interface method
text, err = util.LoadTemplate(category, deleteMethodTemplateFile, template.DeleteMethod)
text, err = pathx.LoadTemplate(category, deleteMethodTemplateFile, template.DeleteMethod)
if err != nil {
return "", "", err
}

View File

@@ -6,6 +6,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/model/sql/parser"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
)
func genFields(fields []*parser.Field) (string, error) {
@@ -29,7 +30,7 @@ func genField(field *parser.Field) (string, error) {
return "", err
}
text, err := util.LoadTemplate(category, fieldTemplateFile, template.Field)
text, err := pathx.LoadTemplate(category, fieldTemplateFile, template.Field)
if err != nil {
return "", err
}

View File

@@ -3,12 +3,13 @@ package gen
import (
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
)
func genFindOne(table Table, withCache, postgreSql bool) (string, string, error) {
camel := table.Name.ToCamel()
text, err := util.LoadTemplate(category, findOneTemplateFile, template.FindOne)
text, err := pathx.LoadTemplate(category, findOneTemplateFile, template.FindOne)
if err != nil {
return "", "", err
}
@@ -30,7 +31,7 @@ func genFindOne(table Table, withCache, postgreSql bool) (string, string, error)
return "", "", err
}
text, err = util.LoadTemplate(category, findOneMethodTemplateFile, template.FindOneMethod)
text, err = pathx.LoadTemplate(category, findOneMethodTemplateFile, template.FindOneMethod)
if err != nil {
return "", "", err
}

View File

@@ -6,6 +6,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
)
@@ -16,7 +17,7 @@ type findOneCode struct {
}
func genFindOneByField(table Table, withCache, postgreSql bool) (*findOneCode, error) {
text, err := util.LoadTemplate(category, findOneByFieldTemplateFile, template.FindOneByField)
text, err := pathx.LoadTemplate(category, findOneByFieldTemplateFile, template.FindOneByField)
if err != nil {
return nil, err
}
@@ -47,7 +48,7 @@ func genFindOneByField(table Table, withCache, postgreSql bool) (*findOneCode, e
list = append(list, output.String())
}
text, err = util.LoadTemplate(category, findOneByFieldMethodTemplateFile, template.FindOneByFieldMethod)
text, err = pathx.LoadTemplate(category, findOneByFieldMethodTemplateFile, template.FindOneByFieldMethod)
if err != nil {
return nil, err
}
@@ -79,7 +80,7 @@ func genFindOneByField(table Table, withCache, postgreSql bool) (*findOneCode, e
}
if withCache {
text, err := util.LoadTemplate(category, findOneByFieldExtraMethodTemplateFile, template.FindOneByFieldExtraMethod)
text, err := pathx.LoadTemplate(category, findOneByFieldExtraMethodTemplateFile, template.FindOneByFieldExtraMethod)
if err != nil {
return nil, err
}
@@ -96,15 +97,15 @@ func genFindOneByField(table Table, withCache, postgreSql bool) (*findOneCode, e
}
return &findOneCode{
findOneMethod: strings.Join(list, util.NL),
findOneInterfaceMethod: strings.Join(listMethod, util.NL),
findOneMethod: strings.Join(list, pathx.NL),
findOneInterfaceMethod: strings.Join(listMethod, pathx.NL),
cacheExtra: out.String(),
}, nil
}
return &findOneCode{
findOneMethod: strings.Join(list, util.NL),
findOneInterfaceMethod: strings.Join(listMethod, util.NL),
findOneMethod: strings.Join(list, pathx.NL),
findOneInterfaceMethod: strings.Join(listMethod, pathx.NL),
}, nil
}

View File

@@ -16,6 +16,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/console"
"github.com/tal-tech/go-zero/tools/goctl/util/format"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
)
@@ -62,7 +63,7 @@ func NewDefaultGenerator(dir string, cfg *config.Config, opt ...Option) (*defaul
dir = dirAbs
pkg := filepath.Base(dirAbs)
err = util.MkdirIfNotExist(dir)
err = pathx.MkdirIfNotExist(dir)
if err != nil {
return nil, err
}
@@ -134,7 +135,7 @@ func (g *defaultGenerator) createFile(modelList map[string]string) error {
g.dir = dirAbs
g.pkg = filepath.Base(dirAbs)
err = util.MkdirIfNotExist(dirAbs)
err = pathx.MkdirIfNotExist(dirAbs)
if err != nil {
return err
}
@@ -148,7 +149,7 @@ func (g *defaultGenerator) createFile(modelList map[string]string) error {
name := util.SafeString(modelFilename) + ".go"
filename := filepath.Join(dirAbs, name)
if util.FileExists(filename) {
if pathx.FileExists(filename) {
g.Warning("%s already exists, ignored.", name)
continue
}
@@ -165,7 +166,7 @@ func (g *defaultGenerator) createFile(modelList map[string]string) error {
}
filename := filepath.Join(dirAbs, varFilename+".go")
text, err := util.LoadTemplate(category, errTemplateFile, template.Error)
text, err := pathx.LoadTemplate(category, errTemplateFile, template.Error)
if err != nil {
return err
}
@@ -261,7 +262,7 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
var list []string
list = append(list, insertCodeMethod, findOneCodeMethod, ret.findOneInterfaceMethod, updateCodeMethod, deleteCodeMethod)
typesCode, err := genTypes(table, strings.Join(modelutil.TrimStringSlice(list), util.NL), withCache)
typesCode, err := genTypes(table, strings.Join(modelutil.TrimStringSlice(list), pathx.NL), withCache)
if err != nil {
return "", err
}
@@ -292,7 +293,7 @@ func (g *defaultGenerator) genModel(in parser.Table, withCache bool) (string, er
}
func (g *defaultGenerator) executeModel(code *code) (*bytes.Buffer, error) {
text, err := util.LoadTemplate(category, modelTemplateFile, template.Model)
text, err := pathx.LoadTemplate(category, modelTemplateFile, template.Model)
if err != nil {
return nil, err
}

View File

@@ -14,7 +14,7 @@ import (
"github.com/tal-tech/go-zero/core/stringx"
"github.com/tal-tech/go-zero/tools/goctl/config"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/builderx"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
)
var source = "CREATE TABLE `test_user` (\n `id` bigint NOT NULL AUTO_INCREMENT,\n `mobile` varchar(255) COLLATE utf8mb4_bin NOT NULL,\n `class` bigint NOT NULL,\n `name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,\n `create_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP,\n `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,\n PRIMARY KEY (`id`),\n UNIQUE KEY `mobile_unique` (`mobile`),\n UNIQUE KEY `class_name_unique` (`class`,`name`),\n KEY `create_index` (`create_time`),\n KEY `name_index` (`name`)\n) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;"
@@ -23,11 +23,11 @@ func TestCacheModel(t *testing.T) {
logx.Disable()
_ = Clean()
sqlFile := filepath.Join(util.MustTempDir(), "tmp.sql")
sqlFile := filepath.Join(pathx.MustTempDir(), "tmp.sql")
err := ioutil.WriteFile(sqlFile, []byte(source), 0o777)
assert.Nil(t, err)
dir := filepath.Join(util.MustTempDir(), "./testmodel")
dir := filepath.Join(pathx.MustTempDir(), "./testmodel")
cacheDir := filepath.Join(dir, "cache")
noCacheDir := filepath.Join(dir, "nocache")
g, err := NewDefaultGenerator(cacheDir, &config.Config{
@@ -58,7 +58,7 @@ func TestNamingModel(t *testing.T) {
logx.Disable()
_ = Clean()
sqlFile := filepath.Join(util.MustTempDir(), "tmp.sql")
sqlFile := filepath.Join(pathx.MustTempDir(), "tmp.sql")
err := ioutil.WriteFile(sqlFile, []byte(source), 0o777)
assert.Nil(t, err)

View File

@@ -3,11 +3,12 @@ package gen
import (
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
)
func genImports(withCache, timeImport bool) (string, error) {
if withCache {
text, err := util.LoadTemplate(category, importsTemplateFile, template.Imports)
text, err := pathx.LoadTemplate(category, importsTemplateFile, template.Imports)
if err != nil {
return "", err
}
@@ -22,7 +23,7 @@ func genImports(withCache, timeImport bool) (string, error) {
return buffer.String(), nil
}
text, err := util.LoadTemplate(category, importsWithNoCacheTemplateFile, template.ImportsNoCache)
text, err := pathx.LoadTemplate(category, importsWithNoCacheTemplateFile, template.ImportsNoCache)
if err != nil {
return "", err
}

View File

@@ -7,6 +7,7 @@ import (
"github.com/tal-tech/go-zero/core/collection"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
)
@@ -45,7 +46,7 @@ func genInsert(table Table, withCache, postgreSql bool) (string, string, error)
}
camel := table.Name.ToCamel()
text, err := util.LoadTemplate(category, insertTemplateFile, template.Insert)
text, err := pathx.LoadTemplate(category, insertTemplateFile, template.Insert)
if err != nil {
return "", "", err
}
@@ -67,7 +68,7 @@ func genInsert(table Table, withCache, postgreSql bool) (string, string, error)
}
// interface method
text, err = util.LoadTemplate(category, insertTemplateMethodFile, template.InsertMethod)
text, err = pathx.LoadTemplate(category, insertTemplateMethodFile, template.InsertMethod)
if err != nil {
return "", "", err
}

View File

@@ -5,10 +5,11 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
)
func genNew(table Table, withCache, postgreSql bool) (string, error) {
text, err := util.LoadTemplate(category, modelNewTemplateFile, template.New)
text, err := pathx.LoadTemplate(category, modelNewTemplateFile, template.New)
if err != nil {
return "", err
}

View File

@@ -3,6 +3,7 @@ package gen
import (
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
)
func genTag(in string) (string, error) {
@@ -10,7 +11,7 @@ func genTag(in string) (string, error) {
return in, nil
}
text, err := util.LoadTemplate(category, tagTemplateFile, template.Tag)
text, err := pathx.LoadTemplate(category, tagTemplateFile, template.Tag)
if err != nil {
return "", err
}

View File

@@ -4,7 +4,7 @@ import (
"fmt"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
)
@@ -62,12 +62,12 @@ func Category() string {
// Clean deletes all template files
func Clean() error {
return util.Clean(category)
return pathx.Clean(category)
}
// GenTemplates creates template files if not exists
func GenTemplates(_ *cli.Context) error {
return util.InitTemplates(category, templates)
return pathx.InitTemplates(category, templates)
}
// RevertTemplate recovers the delete template files
@@ -77,7 +77,7 @@ func RevertTemplate(name string) error {
return fmt.Errorf("%s: no such file name", name)
}
return util.CreateTemplate(category, name, content)
return pathx.CreateTemplate(category, name, content)
}
// Update provides template clean and init
@@ -87,5 +87,5 @@ func Update() error {
return err
}
return util.InitTemplates(category, templates)
return pathx.InitTemplates(category, templates)
}

View File

@@ -7,13 +7,13 @@ import (
"github.com/stretchr/testify/assert"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
)
func TestGenTemplates(t *testing.T) {
err := util.InitTemplates(category, templates)
err := pathx.InitTemplates(category, templates)
assert.Nil(t, err)
dir, err := util.GetTemplateDir(category)
dir, err := pathx.GetTemplateDir(category)
assert.Nil(t, err)
file := filepath.Join(dir, "model-new.tpl")
data, err := ioutil.ReadFile(file)
@@ -23,10 +23,10 @@ func TestGenTemplates(t *testing.T) {
func TestRevertTemplate(t *testing.T) {
name := "model-new.tpl"
err := util.InitTemplates(category, templates)
err := pathx.InitTemplates(category, templates)
assert.Nil(t, err)
dir, err := util.GetTemplateDir(category)
dir, err := pathx.GetTemplateDir(category)
assert.Nil(t, err)
file := filepath.Join(dir, name)
@@ -34,7 +34,7 @@ func TestRevertTemplate(t *testing.T) {
assert.Nil(t, err)
modifyData := string(data) + "modify"
err = util.CreateTemplate(category, name, modifyData)
err = pathx.CreateTemplate(category, name, modifyData)
assert.Nil(t, err)
data, err = ioutil.ReadFile(file)
@@ -51,12 +51,12 @@ func TestRevertTemplate(t *testing.T) {
func TestClean(t *testing.T) {
name := "model-new.tpl"
err := util.InitTemplates(category, templates)
err := pathx.InitTemplates(category, templates)
assert.Nil(t, err)
assert.Nil(t, Clean())
dir, err := util.GetTemplateDir(category)
dir, err := pathx.GetTemplateDir(category)
assert.Nil(t, err)
file := filepath.Join(dir, name)
@@ -66,10 +66,10 @@ func TestClean(t *testing.T) {
func TestUpdate(t *testing.T) {
name := "model-new.tpl"
err := util.InitTemplates(category, templates)
err := pathx.InitTemplates(category, templates)
assert.Nil(t, err)
dir, err := util.GetTemplateDir(category)
dir, err := pathx.GetTemplateDir(category)
assert.Nil(t, err)
file := filepath.Join(dir, name)
@@ -77,7 +77,7 @@ func TestUpdate(t *testing.T) {
assert.Nil(t, err)
modifyData := string(data) + "modify"
err = util.CreateTemplate(category, name, modifyData)
err = pathx.CreateTemplate(category, name, modifyData)
assert.Nil(t, err)
data, err = ioutil.ReadFile(file)

View File

@@ -3,6 +3,7 @@ package gen
import (
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
)
func genTypes(table Table, methods string, withCache bool) (string, error) {
@@ -12,7 +13,7 @@ func genTypes(table Table, methods string, withCache bool) (string, error) {
return "", err
}
text, err := util.LoadTemplate(category, typesTemplateFile, template.Types)
text, err := pathx.LoadTemplate(category, typesTemplateFile, template.Types)
if err != nil {
return "", err
}

View File

@@ -6,6 +6,7 @@ import (
"github.com/tal-tech/go-zero/core/collection"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
)
@@ -39,7 +40,7 @@ func genUpdate(table Table, withCache, postgreSql bool) (string, string, error)
expressionValues = append(expressionValues, "data."+table.PrimaryKey.Name.ToCamel())
}
camelTableName := table.Name.ToCamel()
text, err := util.LoadTemplate(category, updateTemplateFile, template.Update)
text, err := pathx.LoadTemplate(category, updateTemplateFile, template.Update)
if err != nil {
return "", "", err
}
@@ -63,7 +64,7 @@ func genUpdate(table Table, withCache, postgreSql bool) (string, string, error)
}
// update interface method
text, err = util.LoadTemplate(category, updateMethodTemplateFile, template.UpdateMethod)
text, err = pathx.LoadTemplate(category, updateMethodTemplateFile, template.UpdateMethod)
if err != nil {
return "", "", err
}

View File

@@ -5,6 +5,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/model/sql/template"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
)
@@ -16,7 +17,7 @@ func genVars(table Table, withCache, postgreSql bool) (string, error) {
}
camel := table.Name.ToCamel()
text, err := util.LoadTemplate(category, varTemplateFile, template.Vars)
text, err := pathx.LoadTemplate(category, varTemplateFile, template.Vars)
if err != nil {
return "", err
}

View File

@@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/model"
"github.com/tal-tech/go-zero/tools/goctl/model/sql/util"
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util/pathx"
)
func TestParsePlainText(t *testing.T) {