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

@@ -9,6 +9,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/rpc/generator"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/env"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
)
@@ -34,7 +35,7 @@ func RPC(c *cli.Context) error {
}
}
if len(home) > 0 {
util.RegisterGoctlHome(home)
pathx.RegisterGoctlHome(home)
}
if len(src) == 0 {
@@ -87,7 +88,7 @@ func RPCNew(c *cli.Context) error {
}
}
if len(home) > 0 {
util.RegisterGoctlHome(home)
pathx.RegisterGoctlHome(home)
}
protoName := rpcname + ".proto"
@@ -122,7 +123,7 @@ func RPCTemplate(c *cli.Context) error {
}
}
if len(home) > 0 {
util.RegisterGoctlHome(home)
pathx.RegisterGoctlHome(home)
}
if len(protoFile) == 0 {

View File

@@ -8,7 +8,7 @@ import (
"runtime"
"strings"
"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/vars"
)
@@ -39,10 +39,10 @@ func Run(arg, dir string, in ...*bytes.Buffer) (string, error) {
err := cmd.Run()
if err != nil {
if stderr.Len() > 0 {
return "", errors.New(strings.TrimSuffix(stderr.String(), util.NL))
return "", errors.New(strings.TrimSuffix(stderr.String(), pathx.NL))
}
return "", err
}
return strings.TrimSuffix(stdout.String(), util.NL), nil
return strings.TrimSuffix(stdout.String(), pathx.NL), nil
}

View File

@@ -5,9 +5,9 @@ import (
conf "github.com/tal-tech/go-zero/tools/goctl/config"
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
"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/ctx"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
)
// RPCGenerator defines a generator and configure
@@ -42,7 +42,7 @@ func (g *RPCGenerator) Generate(src, target string, protoImportPath []string, go
return err
}
err = util.MkdirIfNotExist(abs)
err = pathx.MkdirIfNotExist(abs)
if err != nil {
return err
}

View File

@@ -12,7 +12,7 @@ import (
"github.com/tal-tech/go-zero/core/stringx"
conf "github.com/tal-tech/go-zero/tools/goctl/config"
"github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
)
var cfg = &conf.Config{
@@ -58,7 +58,7 @@ func TestRpcGenerate(t *testing.T) {
// case go mod
t.Run("GOMOD", func(t *testing.T) {
workDir := util.MustTempDir()
workDir := pathx.MustTempDir()
name := filepath.Base(workDir)
_, err = execx.Run("go mod init "+name, workDir)
if err != nil {

View File

@@ -12,6 +12,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
"github.com/tal-tech/go-zero/tools/goctl/util"
"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"
)
@@ -85,7 +86,7 @@ func (g *DefaultGenerator) GenCall(ctx DirContext, proto parser.Proto, cfg *conf
return err
}
text, err := util.LoadTemplate(category, callTemplateFile, callTemplateText)
text, err := pathx.LoadTemplate(category, callTemplateFile, callTemplateText)
if err != nil {
return err
}
@@ -100,13 +101,13 @@ func (g *DefaultGenerator) GenCall(ctx DirContext, proto parser.Proto, cfg *conf
sort.Strings(aliasKeys)
err = util.With("shared").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
"name": callFilename,
"alias": strings.Join(aliasKeys, util.NL),
"alias": strings.Join(aliasKeys, pathx.NL),
"head": head,
"filePackage": dir.Base,
"package": fmt.Sprintf(`"%s"`, ctx.GetPb().Package),
"serviceName": stringx.From(service.Name).ToCamel(),
"functions": strings.Join(functions, util.NL),
"interface": strings.Join(iFunctions, util.NL),
"functions": strings.Join(functions, pathx.NL),
"interface": strings.Join(iFunctions, pathx.NL),
}, filename, true)
return err
}
@@ -137,7 +138,7 @@ func (g *DefaultGenerator) genFunction(goPackage string, service parser.Service)
functions := make([]string, 0)
for _, rpc := range service.RPC {
text, err := util.LoadTemplate(category, callFunctionTemplateFile, callFunctionTemplate)
text, err := pathx.LoadTemplate(category, callFunctionTemplateFile, callFunctionTemplate)
if err != nil {
return nil, err
}
@@ -171,7 +172,7 @@ func (g *DefaultGenerator) getInterfaceFuncs(goPackage string, service parser.Se
functions := make([]string, 0)
for _, rpc := range service.RPC {
text, err := util.LoadTemplate(category, callInterfaceFunctionTemplateFile, callInterfaceFunctionTemplate)
text, err := pathx.LoadTemplate(category, callInterfaceFunctionTemplateFile, callInterfaceFunctionTemplate)
if err != nil {
return nil, err
}

View File

@@ -7,8 +7,8 @@ import (
conf "github.com/tal-tech/go-zero/tools/goctl/config"
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/format"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
)
const configTemplate = `package config
@@ -32,11 +32,11 @@ func (g *DefaultGenerator) GenConfig(ctx DirContext, _ parser.Proto, cfg *conf.C
}
fileName := filepath.Join(dir.Filename, configFilename+".go")
if util.FileExists(fileName) {
if pathx.FileExists(fileName) {
return nil
}
text, err := util.LoadTemplate(category, configTemplateFileFile, configTemplate)
text, err := pathx.LoadTemplate(category, configTemplateFileFile, configTemplate)
if err != nil {
return err
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
"github.com/tal-tech/go-zero/tools/goctl/util"
"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"
)
@@ -31,7 +32,7 @@ func (g *DefaultGenerator) GenEtc(ctx DirContext, _ parser.Proto, cfg *conf.Conf
fileName := filepath.Join(dir.Filename, fmt.Sprintf("%v.yaml", etcFilename))
text, err := util.LoadTemplate(category, etcTemplateFileFile, etcTemplate)
text, err := pathx.LoadTemplate(category, etcTemplateFileFile, etcTemplate)
if err != nil {
return err
}

View File

@@ -10,6 +10,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
"github.com/tal-tech/go-zero/tools/goctl/util"
"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"
)
@@ -67,14 +68,14 @@ func (g *DefaultGenerator) GenLogic(ctx DirContext, proto parser.Proto, cfg *con
imports := collection.NewSet()
imports.AddStr(fmt.Sprintf(`"%v"`, ctx.GetSvc().Package))
imports.AddStr(fmt.Sprintf(`"%v"`, ctx.GetPb().Package))
text, err := util.LoadTemplate(category, logicTemplateFileFile, logicTemplate)
text, err := pathx.LoadTemplate(category, logicTemplateFileFile, logicTemplate)
if err != nil {
return err
}
err = util.With("logic").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
"logicName": fmt.Sprintf("%sLogic", stringx.From(rpc.Name).ToCamel()),
"functions": functions,
"imports": strings.Join(imports.KeysStr(), util.NL),
"imports": strings.Join(imports.KeysStr(), pathx.NL),
}, filename, false)
if err != nil {
return err
@@ -85,7 +86,7 @@ func (g *DefaultGenerator) GenLogic(ctx DirContext, proto parser.Proto, cfg *con
func (g *DefaultGenerator) genLogicFunction(serviceName, goPackage string, rpc *parser.RPC) (string, error) {
functions := make([]string, 0)
text, err := util.LoadTemplate(category, logicFuncTemplateFileFile, logicFunctionTemplate)
text, err := pathx.LoadTemplate(category, logicFuncTemplateFileFile, logicFunctionTemplate)
if err != nil {
return "", err
}
@@ -111,5 +112,5 @@ func (g *DefaultGenerator) genLogicFunction(serviceName, goPackage string, rpc *
}
functions = append(functions, buffer.String())
return strings.Join(functions, util.NL), nil
return strings.Join(functions, pathx.NL), nil
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
"github.com/tal-tech/go-zero/tools/goctl/util"
"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"
)
@@ -65,7 +66,7 @@ func (g *DefaultGenerator) GenMain(ctx DirContext, proto parser.Proto, cfg *conf
remoteImport := fmt.Sprintf(`"%v"`, ctx.GetServer().Package)
configImport := fmt.Sprintf(`"%v"`, ctx.GetConfig().Package)
imports = append(imports, configImport, pbImport, remoteImport, svcImport)
text, err := util.LoadTemplate(category, mainTemplateFile, mainTemplate)
text, err := pathx.LoadTemplate(category, mainTemplateFile, mainTemplate)
if err != nil {
return err
}
@@ -77,7 +78,7 @@ func (g *DefaultGenerator) GenMain(ctx DirContext, proto parser.Proto, cfg *conf
return util.With("main").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
"serviceName": etcFileName,
"imports": strings.Join(imports, util.NL),
"imports": strings.Join(imports, pathx.NL),
"pkg": proto.PbPackage,
"serviceNew": stringx.From(proto.Service.Name).ToCamel(),
"service": parser.CamelCase(proto.Service.Name),

View File

@@ -10,6 +10,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
"github.com/tal-tech/go-zero/tools/goctl/util"
"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"
)
@@ -68,7 +69,7 @@ func (g *DefaultGenerator) GenServer(ctx DirContext, proto parser.Proto, cfg *co
return err
}
text, err := util.LoadTemplate(category, serverTemplateFile, serverTemplate)
text, err := pathx.LoadTemplate(category, serverTemplateFile, serverTemplate)
if err != nil {
return err
}
@@ -84,8 +85,8 @@ func (g *DefaultGenerator) GenServer(ctx DirContext, proto parser.Proto, cfg *co
err = util.With("server").GoFmt(true).Parse(text).SaveTo(map[string]interface{}{
"head": head,
"server": stringx.From(service.Name).ToCamel(),
"imports": strings.Join(imports.KeysStr(), util.NL),
"funcs": strings.Join(funcList, util.NL),
"imports": strings.Join(imports.KeysStr(), pathx.NL),
"funcs": strings.Join(funcList, pathx.NL),
"notStream": notStream,
}, serverFile, true)
return err
@@ -94,7 +95,7 @@ func (g *DefaultGenerator) GenServer(ctx DirContext, proto parser.Proto, cfg *co
func (g *DefaultGenerator) genFunctions(goPackage string, service parser.Service) ([]string, error) {
var functionList []string
for _, rpc := range service.RPC {
text, err := util.LoadTemplate(category, serverFuncTemplateFile, functionTemplate)
text, err := pathx.LoadTemplate(category, serverFuncTemplateFile, functionTemplate)
if err != nil {
return nil, err
}

View File

@@ -8,6 +8,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/format"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
)
const svcTemplate = `package svc
@@ -35,7 +36,7 @@ func (g *DefaultGenerator) GenSvc(ctx DirContext, _ parser.Proto, cfg *conf.Conf
}
fileName := filepath.Join(dir.Filename, svcFilename+".go")
text, err := util.LoadTemplate(category, svcTemplateFile, svcTemplate)
text, err := pathx.LoadTemplate(category, svcTemplateFile, svcTemplate)
if err != nil {
return err
}

View File

@@ -6,8 +6,8 @@ import (
conf "github.com/tal-tech/go-zero/tools/goctl/config"
"github.com/tal-tech/go-zero/tools/goctl/rpc/parser"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/ctx"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
"github.com/tal-tech/go-zero/tools/goctl/util/stringx"
)
@@ -111,7 +111,7 @@ func mkdir(ctx *ctx.ProjectContext, proto parser.Proto, _ *conf.Config) (DirCont
Base: filepath.Base(callDir),
}
for _, v := range inner {
err := util.MkdirIfNotExist(v.Filename)
err := pathx.MkdirIfNotExist(v.Filename)
if err != nil {
return nil, err
}

View File

@@ -5,6 +5,7 @@ import (
"strings"
"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"
)
@@ -29,13 +30,13 @@ service {{.serviceName}} {
func ProtoTmpl(out string) error {
protoFilename := filepath.Base(out)
serviceName := stringx.From(strings.TrimSuffix(protoFilename, filepath.Ext(protoFilename)))
text, err := util.LoadTemplate(category, rpcTemplateFile, rpcTemplateText)
text, err := pathx.LoadTemplate(category, rpcTemplateFile, rpcTemplateText)
if err != nil {
return err
}
dir := filepath.Dir(out)
err = util.MkdirIfNotExist(dir)
err = pathx.MkdirIfNotExist(dir)
if err != nil {
return err
}

View File

@@ -5,17 +5,17 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
)
func TestProtoTmpl(t *testing.T) {
_ = Clean()
// exists dir
err := ProtoTmpl(util.MustTempDir())
err := ProtoTmpl(pathx.MustTempDir())
assert.Nil(t, err)
// not exist dir
dir := filepath.Join(util.MustTempDir(), "test")
dir := filepath.Join(pathx.MustTempDir(), "test")
err = ProtoTmpl(dir)
assert.Nil(t, err)
}

View File

@@ -3,7 +3,7 @@ package generator
import (
"fmt"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
)
@@ -41,7 +41,7 @@ var templates = map[string]string{
// GenTemplates is the entry for command goctl template,
// it will create the specified category
func GenTemplates(_ *cli.Context) error {
return util.InitTemplates(category, templates)
return pathx.InitTemplates(category, templates)
}
// RevertTemplate restores the deleted template files
@@ -50,12 +50,12 @@ func RevertTemplate(name string) error {
if !ok {
return fmt.Errorf("%s: no such file name", name)
}
return util.CreateTemplate(category, name, content)
return pathx.CreateTemplate(category, name, content)
}
// Clean deletes all template files
func Clean() error {
return util.Clean(category)
return pathx.Clean(category)
}
// Update is used to update the template files, it will delete the existing old templates at first,
@@ -66,7 +66,7 @@ func Update() error {
return err
}
return util.InitTemplates(category, templates)
return pathx.InitTemplates(category, templates)
}
// Category returns a const string value for rpc template category

View File

@@ -7,7 +7,7 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
)
func TestGenTemplates(t *testing.T) {
@@ -20,7 +20,7 @@ func TestRevertTemplate(t *testing.T) {
_ = Clean()
err := GenTemplates(nil)
assert.Nil(t, err)
fp, err := util.GetTemplateDir(category)
fp, err := pathx.GetTemplateDir(category)
if err != nil {
return
}
@@ -61,7 +61,7 @@ func TestClean(t *testing.T) {
_ = Clean()
err := GenTemplates(nil)
assert.Nil(t, err)
fp, err := util.GetTemplateDir(category)
fp, err := pathx.GetTemplateDir(category)
if err != nil {
return
}
@@ -80,7 +80,7 @@ func TestUpdate(t *testing.T) {
_ = Clean()
err := GenTemplates(nil)
assert.Nil(t, err)
fp, err := util.GetTemplateDir(category)
fp, err := pathx.GetTemplateDir(category)
if err != nil {
return
}