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/logrusorgru/aurora"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
)
@@ -46,7 +47,7 @@ func ApiCommand(c *cli.Context) error {
return errors.New("missing -o")
}
fp, err := util.CreateIfNotExist(apiFile)
fp, err := pathx.CreateIfNotExist(apiFile)
if err != nil {
return err
}
@@ -62,15 +63,15 @@ func ApiCommand(c *cli.Context) error {
}
if len(home) > 0 {
util.RegisterGoctlHome(home)
pathx.RegisterGoctlHome(home)
}
text, err := util.LoadTemplate(category, apiTemplateFile, apiTemplate)
text, err := pathx.LoadTemplate(category, apiTemplateFile, apiTemplate)
if err != nil {
return err
}
baseName := util.FileNameWithoutExt(filepath.Base(apiFile))
baseName := pathx.FileNameWithoutExt(filepath.Base(apiFile))
if strings.HasSuffix(strings.ToLower(baseName), "-api") {
baseName = baseName[:len(baseName)-4]
} else if strings.HasSuffix(strings.ToLower(baseName), "api") {

View File

@@ -3,7 +3,7 @@ package apigen
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"
)
@@ -23,12 +23,12 @@ func Category() string {
// Clean cleans the generated deployment files.
func Clean() error {
return util.Clean(category)
return pathx.Clean(category)
}
// GenTemplates generates api template files.
func GenTemplates(_ *cli.Context) error {
return util.InitTemplates(category, templates)
return pathx.InitTemplates(category, templates)
}
// RevertTemplate reverts the given template file to the default value.
@@ -37,7 +37,7 @@ 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)
}
// Update updates the template files to the templates built in current goctl.
@@ -47,5 +47,5 @@ func Update() error {
return err
}
return util.InitTemplates(category, templates)
return pathx.InitTemplates(category, templates)
}

View File

@@ -8,7 +8,7 @@ import (
"strings"
"github.com/tal-tech/go-zero/tools/goctl/api/parser"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
)
@@ -28,7 +28,7 @@ func DocCommand(c *cli.Context) error {
}
}
if !util.FileExists(dir) {
if !pathx.FileExists(dir) {
return fmt.Errorf("dir %s not exsit", dir)
}

View File

@@ -14,7 +14,7 @@ import (
"github.com/tal-tech/go-zero/core/errorx"
"github.com/tal-tech/go-zero/tools/goctl/api/parser"
"github.com/tal-tech/go-zero/tools/goctl/api/util"
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
)

View File

@@ -18,6 +18,7 @@ import (
apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util"
"github.com/tal-tech/go-zero/tools/goctl/config"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
)
@@ -40,7 +41,7 @@ func GoCommand(c *cli.Context) error {
}
if len(home) > 0 {
util.RegisterGoctlHome(home)
pathx.RegisterGoctlHome(home)
}
if len(apiFile) == 0 {
return errors.New("missing -api")
@@ -64,7 +65,7 @@ func DoGenProject(apiFile, dir, style string) error {
return err
}
logx.Must(util.MkdirIfNotExist(dir))
logx.Must(pathx.MkdirIfNotExist(dir))
rootPkg, err := getParentPackage(dir)
if err != nil {
return err

View File

@@ -10,6 +10,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/internal/version"
"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/vars"
)
@@ -122,10 +123,10 @@ func genHandlers(dir, rootPkg string, cfg *config.Config, api *spec.ApiSpec) err
func genHandlerImports(group spec.Group, route spec.Route, parentPkg string) string {
var imports []string
imports = append(imports, fmt.Sprintf("\"%s\"",
util.JoinPackages(parentPkg, getLogicFolderPath(group, route))))
imports = append(imports, fmt.Sprintf("\"%s\"", util.JoinPackages(parentPkg, contextDir)))
pathx.JoinPackages(parentPkg, getLogicFolderPath(group, route))))
imports = append(imports, fmt.Sprintf("\"%s\"", pathx.JoinPackages(parentPkg, contextDir)))
if len(route.RequestTypeName()) > 0 {
imports = append(imports, fmt.Sprintf("\"%s\"\n", util.JoinPackages(parentPkg, typesDir)))
imports = append(imports, fmt.Sprintf("\"%s\"\n", pathx.JoinPackages(parentPkg, typesDir)))
}
currentVersion := version.GetGoctlVersion()

View File

@@ -9,8 +9,8 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/gen/api"
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
"github.com/tal-tech/go-zero/tools/goctl/config"
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/format"
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util/pathx"
"github.com/tal-tech/go-zero/tools/goctl/vars"
)

View File

@@ -6,8 +6,8 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
"github.com/tal-tech/go-zero/tools/goctl/config"
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/format"
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util/pathx"
"github.com/tal-tech/go-zero/tools/goctl/vars"
)

View File

@@ -11,8 +11,8 @@ import (
"github.com/tal-tech/go-zero/core/collection"
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
"github.com/tal-tech/go-zero/tools/goctl/config"
"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/vars"
)
@@ -151,7 +151,7 @@ rest.WithPrefix("%s"),`, g.prefix)
func genRouteImports(parentPkg string, api *spec.ApiSpec) string {
importSet := collection.NewSet()
importSet.AddStr(fmt.Sprintf("\"%s\"", util.JoinPackages(parentPkg, contextDir)))
importSet.AddStr(fmt.Sprintf("\"%s\"", pathx.JoinPackages(parentPkg, contextDir)))
for _, group := range api.Service.Groups {
for _, route := range group.Routes {
folder := route.GetAnnotation(groupProperty)
@@ -161,7 +161,7 @@ func genRouteImports(parentPkg string, api *spec.ApiSpec) string {
continue
}
}
importSet.AddStr(fmt.Sprintf("%s \"%s\"", toPrefix(folder), util.JoinPackages(parentPkg, handlerDir, folder)))
importSet.AddStr(fmt.Sprintf("%s \"%s\"", toPrefix(folder), pathx.JoinPackages(parentPkg, handlerDir, folder)))
}
}
imports := importSet.KeysStr()

View File

@@ -6,8 +6,8 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
"github.com/tal-tech/go-zero/tools/goctl/config"
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/format"
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util/pathx"
"github.com/tal-tech/go-zero/tools/goctl/vars"
)

View File

@@ -3,7 +3,7 @@ package gogen
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"
)
@@ -33,12 +33,12 @@ func Category() string {
// Clean cleans the generated deployment files.
func Clean() error {
return util.Clean(category)
return pathx.Clean(category)
}
// GenTemplates generates api template files.
func GenTemplates(_ *cli.Context) error {
return util.InitTemplates(category, templates)
return pathx.InitTemplates(category, templates)
}
// RevertTemplate reverts the given template file to the default value.
@@ -47,7 +47,7 @@ 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)
}
// Update updates the template files to the templates built in current goctl.
@@ -57,5 +57,5 @@ func Update() error {
return err
}
return util.InitTemplates(category, templates)
return pathx.InitTemplates(category, templates)
}

View File

@@ -6,13 +6,13 @@ 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) {
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, "main.tpl")
data, err := ioutil.ReadFile(file)
@@ -22,10 +22,10 @@ func TestGenTemplates(t *testing.T) {
func TestRevertTemplate(t *testing.T) {
name := "main.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)
@@ -33,7 +33,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)
@@ -50,12 +50,12 @@ func TestRevertTemplate(t *testing.T) {
func TestClean(t *testing.T) {
name := "main.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)
@@ -65,10 +65,10 @@ func TestClean(t *testing.T) {
func TestUpdate(t *testing.T) {
name := "main.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)
@@ -76,7 +76,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

@@ -12,8 +12,8 @@ import (
"github.com/tal-tech/go-zero/core/collection"
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
"github.com/tal-tech/go-zero/tools/goctl/api/util"
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/ctx"
ctlutil "github.com/tal-tech/go-zero/tools/goctl/util/pathx"
)
type fileGenConfig struct {

View File

@@ -8,7 +8,7 @@ import (
"github.com/logrusorgru/aurora"
"github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/tools/goctl/api/parser"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
)
@@ -30,7 +30,7 @@ func JavaCommand(c *cli.Context) error {
api.Service = api.Service.JoinPrefix()
packetName := strings.TrimSuffix(api.Service.Name, "-api")
logx.Must(util.MkdirIfNotExist(dir))
logx.Must(pathx.MkdirIfNotExist(dir))
logx.Must(genPacket(dir, packetName, api))
logx.Must(genComponents(dir, packetName, api))

View File

@@ -14,6 +14,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
)
const (
@@ -110,7 +111,7 @@ func (c *componentsContext) createComponent(dir, packetName string, ty spec.Type
modelFile := util.Title(ty.Name()) + ".java"
filename := path.Join(dir, modelDir, modelFile)
if err := util.RemoveOrQuit(filename); err != nil {
if err := pathx.RemoveOrQuit(filename); err != nil {
return err
}
@@ -270,7 +271,7 @@ func (c *componentsContext) buildConstructor() (string, string, error) {
writeIndent(&constructorSetter, 2)
constructorSetter.WriteString(fmt.Sprintf("this.%s = %s;", pn, util.Untitle(member.Name)))
if index != len(c.members)-1 {
constructorSetter.WriteString(util.NL)
constructorSetter.WriteString(pathx.NL)
}
}
return params.String(), constructorSetter.String(), nil

View File

@@ -8,12 +8,13 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
)
func writeProperty(writer io.Writer, member spec.Member, indent int) error {
if len(member.Comment) > 0 {
writeIndent(writer, indent)
fmt.Fprint(writer, member.Comment+util.NL)
fmt.Fprint(writer, member.Comment+pathx.NL)
}
writeIndent(writer, indent)
ty, err := specTypeToJava(member.Type)

View File

@@ -10,6 +10,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/api/gogen"
conf "github.com/tal-tech/go-zero/tools/goctl/config"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
)
@@ -49,7 +50,7 @@ func CreateServiceCommand(c *cli.Context) error {
return err
}
err = util.MkdirIfNotExist(abs)
err = pathx.MkdirIfNotExist(abs)
if err != nil {
return err
}
@@ -74,10 +75,10 @@ func CreateServiceCommand(c *cli.Context) error {
}
if len(home) > 0 {
util.RegisterGoctlHome(home)
pathx.RegisterGoctlHome(home)
}
text, err := util.LoadTemplate(category, apiTemplateFile, apiTemplate)
text, err := pathx.LoadTemplate(category, apiTemplateFile, apiTemplate)
if err != nil {
return err
}

View File

@@ -3,7 +3,7 @@ package new
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"
)
@@ -23,12 +23,12 @@ func Category() string {
// Clean cleans the generated deployment files.
func Clean() error {
return util.Clean(category)
return pathx.Clean(category)
}
// GenTemplates generates api template files.
func GenTemplates(_ *cli.Context) error {
return util.InitTemplates(category, templates)
return pathx.InitTemplates(category, templates)
}
// RevertTemplate reverts the given template file to the default value.
@@ -37,7 +37,7 @@ 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)
}
// Update updates the template files to the templates built in current goctl.
@@ -47,5 +47,5 @@ func Update() error {
return err
}
return util.InitTemplates(category, templates)
return pathx.InitTemplates(category, templates)
}

View File

@@ -9,7 +9,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/tal-tech/go-zero/tools/goctl/api/parser/g4/ast"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
)
var (
@@ -119,7 +119,7 @@ func TestApiParser(t *testing.T) {
})
t.Run("nestedImport", func(t *testing.T) {
file := filepath.Join(util.MustTempDir(), "foo.api")
file := filepath.Join(pathx.MustTempDir(), "foo.api")
err := ioutil.WriteFile(file, []byte(nestedAPIImport), os.ModePerm)
if err != nil {
return
@@ -149,7 +149,7 @@ func TestApiParser(t *testing.T) {
})
t.Run("ambiguousSyntax", func(t *testing.T) {
file := filepath.Join(util.MustTempDir(), "foo.api")
file := filepath.Join(pathx.MustTempDir(), "foo.api")
err := ioutil.WriteFile(file, []byte(ambiguousSyntax), os.ModePerm)
if err != nil {
return
@@ -163,7 +163,7 @@ func TestApiParser(t *testing.T) {
})
t.Run("ambiguousSyntax", func(t *testing.T) {
file := filepath.Join(util.MustTempDir(), "foo.api")
file := filepath.Join(pathx.MustTempDir(), "foo.api")
err := ioutil.WriteFile(file, []byte(ambiguousSyntax), os.ModePerm)
if err != nil {
return
@@ -177,7 +177,7 @@ func TestApiParser(t *testing.T) {
})
t.Run("ambiguousService", func(t *testing.T) {
file := filepath.Join(util.MustTempDir(), "foo.api")
file := filepath.Join(pathx.MustTempDir(), "foo.api")
err := ioutil.WriteFile(file, []byte(ambiguousService), os.ModePerm)
if err != nil {
return
@@ -207,7 +207,7 @@ func TestApiParser(t *testing.T) {
`)
assert.Error(t, err)
file := filepath.Join(util.MustTempDir(), "foo.api")
file := filepath.Join(pathx.MustTempDir(), "foo.api")
err = ioutil.WriteFile(file, []byte(duplicateHandler), os.ModePerm)
if err != nil {
return
@@ -236,7 +236,7 @@ func TestApiParser(t *testing.T) {
`)
assert.Error(t, err)
file := filepath.Join(util.MustTempDir(), "foo.api")
file := filepath.Join(pathx.MustTempDir(), "foo.api")
err = ioutil.WriteFile(file, []byte(duplicateRoute), os.ModePerm)
if err != nil {
return
@@ -260,7 +260,7 @@ func TestApiParser(t *testing.T) {
`)
assert.Error(t, err)
file := filepath.Join(util.MustTempDir(), "foo.api")
file := filepath.Join(pathx.MustTempDir(), "foo.api")
err = ioutil.WriteFile(file, []byte(duplicateType), os.ModePerm)
if err != nil {
return

View File

@@ -7,7 +7,7 @@ import (
"github.com/logrusorgru/aurora"
"github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/tools/goctl/api/parser"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
"github.com/urfave/cli"
)
@@ -33,7 +33,7 @@ func TsCommand(c *cli.Context) error {
}
api.Service = api.Service.JoinPrefix()
logx.Must(util.MkdirIfNotExist(dir))
logx.Must(pathx.MkdirIfNotExist(dir))
logx.Must(genHandler(dir, webAPI, caller, api, unwrapAPI))
logx.Must(genComponents(dir, api))

View File

@@ -7,7 +7,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
)
const (
@@ -30,7 +30,7 @@ func genComponents(dir string, api *spec.ApiSpec) error {
outputFile := apiutil.ComponentName(api) + ".ts"
filename := path.Join(dir, outputFile)
if err := util.RemoveIfExist(filename); err != nil {
if err := pathx.RemoveIfExist(filename); err != nil {
return err
}

View File

@@ -9,6 +9,7 @@ import (
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
apiutil "github.com/tal-tech/go-zero/tools/goctl/api/util"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
)
const (
@@ -20,7 +21,7 @@ const (
func genHandler(dir, webAPI, caller string, api *spec.ApiSpec, unwrapAPI bool) error {
filename := strings.Replace(api.Service.Name, "-api", "", 1) + ".ts"
if err := util.RemoveIfExist(path.Join(dir, filename)); err != nil {
if err := pathx.RemoveIfExist(path.Join(dir, filename)); err != nil {
return err
}
fp, created, err := apiutil.MaybeCreateFile(dir, "", filename)
@@ -46,11 +47,11 @@ func genHandler(dir, webAPI, caller string, api *spec.ApiSpec, unwrapAPI bool) e
if len(api.Types) != 0 {
if len(imports) > 0 {
imports += util.NL
imports += pathx.NL
}
outputFile := apiutil.ComponentName(api)
imports += fmt.Sprintf(`import * as components from "%s"`, "./"+outputFile)
imports += fmt.Sprintf(`%sexport * from "%s"`, util.NL, "./"+outputFile)
imports += fmt.Sprintf(`%sexport * from "%s"`, pathx.NL, "./"+outputFile)
}
apis, err := genAPI(api, caller)

View File

@@ -10,19 +10,19 @@ import (
"github.com/tal-tech/go-zero/core/logx"
"github.com/tal-tech/go-zero/tools/goctl/api/spec"
"github.com/tal-tech/go-zero/tools/goctl/util"
"github.com/tal-tech/go-zero/tools/goctl/util/pathx"
)
// MaybeCreateFile creates file if not exists
func MaybeCreateFile(dir, subdir, file string) (fp *os.File, created bool, err error) {
logx.Must(util.MkdirIfNotExist(path.Join(dir, subdir)))
logx.Must(pathx.MkdirIfNotExist(path.Join(dir, subdir)))
fpath := path.Join(dir, subdir, file)
if util.FileExists(fpath) {
if pathx.FileExists(fpath) {
fmt.Printf("%s exists, ignored generation\n", fpath)
return nil, false, nil
}
fp, err = util.CreateIfNotExist(fpath)
fp, err = pathx.CreateIfNotExist(fpath)
created = err == nil
return
}