From 927758a2a9e444aea016b06c2db7d9f02530741b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B5=B7=E4=BA=AE?= <739476267@qq.com> Date: Wed, 25 Oct 2023 21:16:05 +0800 Subject: [PATCH] fix separator issue in windows os for command `gf gen` (#3088) --- cmd/gf/internal/cmd/cmd_gen_dao_test.go | 33 ++++++++++--------- .../cmd/genctrl/genctrl_generate_ctrl.go | 9 ++--- .../cmd/genctrl/genctrl_generate_interface.go | 5 +-- .../cmd/genctrl/genctrl_generate_sdk.go | 9 ++--- cmd/gf/internal/cmd/gendao/gendao_dao.go | 5 +-- cmd/gf/internal/cmd/gendao/gendao_do.go | 3 +- cmd/gf/internal/cmd/gendao/gendao_entity.go | 3 +- .../internal/cmd/genpbentity/genpbentity.go | 3 +- 8 files changed, 39 insertions(+), 31 deletions(-) diff --git a/cmd/gf/internal/cmd/cmd_gen_dao_test.go b/cmd/gf/internal/cmd/cmd_gen_dao_test.go index 92ae5818660..f56ca031cd3 100644 --- a/cmd/gf/internal/cmd/cmd_gen_dao_test.go +++ b/cmd/gf/internal/cmd/cmd_gen_dao_test.go @@ -9,6 +9,7 @@ package cmd import ( "context" "fmt" + "path/filepath" "testing" "github.com/gogf/gf/cmd/gf/v2/internal/cmd/gendao" @@ -105,18 +106,18 @@ func Test_Gen_Dao_Default(t *testing.T) { files, err := gfile.ScanDir(path, "*.go", true) t.AssertNil(err) t.Assert(files, []string{ - path + "/dao/internal/table_user.go", - path + "/dao/table_user.go", - path + "/model/do/table_user.go", - path + "/model/entity/table_user.go", + filepath.FromSlash(path + "/dao/internal/table_user.go"), + filepath.FromSlash(path + "/dao/table_user.go"), + filepath.FromSlash(path + "/model/do/table_user.go"), + filepath.FromSlash(path + "/model/entity/table_user.go"), }) // content testPath := gtest.DataPath("gendao", "generated_user") expectFiles := []string{ - testPath + "/dao/internal/table_user.go", - testPath + "/dao/table_user.go", - testPath + "/model/do/table_user.go", - testPath + "/model/entity/table_user.go", + filepath.FromSlash(testPath + "/dao/internal/table_user.go"), + filepath.FromSlash(testPath + "/dao/table_user.go"), + filepath.FromSlash(testPath + "/model/do/table_user.go"), + filepath.FromSlash(testPath + "/model/entity/table_user.go"), } for i, _ := range files { t.Assert(gfile.GetContents(files[i]), gfile.GetContents(expectFiles[i])) @@ -209,18 +210,18 @@ func Test_Gen_Dao_TypeMapping(t *testing.T) { files, err := gfile.ScanDir(path, "*.go", true) t.AssertNil(err) t.Assert(files, []string{ - path + "/dao/internal/table_user.go", - path + "/dao/table_user.go", - path + "/model/do/table_user.go", - path + "/model/entity/table_user.go", + filepath.FromSlash(path + "/dao/internal/table_user.go"), + filepath.FromSlash(path + "/dao/table_user.go"), + filepath.FromSlash(path + "/model/do/table_user.go"), + filepath.FromSlash(path + "/model/entity/table_user.go"), }) // content testPath := gtest.DataPath("gendao", "generated_user_type_mapping") expectFiles := []string{ - testPath + "/dao/internal/table_user.go", - testPath + "/dao/table_user.go", - testPath + "/model/do/table_user.go", - testPath + "/model/entity/table_user.go", + filepath.FromSlash(testPath + "/dao/internal/table_user.go"), + filepath.FromSlash(testPath + "/dao/table_user.go"), + filepath.FromSlash(testPath + "/model/do/table_user.go"), + filepath.FromSlash(testPath + "/model/entity/table_user.go"), } for i, _ := range files { t.Assert(gfile.GetContents(files[i]), gfile.GetContents(expectFiles[i])) diff --git a/cmd/gf/internal/cmd/genctrl/genctrl_generate_ctrl.go b/cmd/gf/internal/cmd/genctrl/genctrl_generate_ctrl.go index 0c18f4e119b..9e88a723048 100644 --- a/cmd/gf/internal/cmd/genctrl/genctrl_generate_ctrl.go +++ b/cmd/gf/internal/cmd/genctrl/genctrl_generate_ctrl.go @@ -8,6 +8,7 @@ package genctrl import ( "fmt" + "path/filepath" "github.com/gogf/gf/cmd/gf/v2/internal/consts" "github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog" @@ -64,8 +65,8 @@ func (c *controllerGenerator) doGenerateCtrlNewByModuleAndVersion( dstModuleFolderPath, module, version, importPath string, ) (err error) { var ( - moduleFilePath = gfile.Join(dstModuleFolderPath, module+".go") - moduleFilePathNew = gfile.Join(dstModuleFolderPath, module+"_new.go") + moduleFilePath = filepath.FromSlash(gfile.Join(dstModuleFolderPath, module+".go")) + moduleFilePathNew = filepath.FromSlash(gfile.Join(dstModuleFolderPath, module+"_new.go")) ctrlName = fmt.Sprintf(`Controller%s`, gstr.UcFirst(version)) interfaceName = fmt.Sprintf(`%s.I%s%s`, module, gstr.CaseCamel(module), gstr.UcFirst(version)) newFuncName = fmt.Sprintf(`New%s`, gstr.UcFirst(version)) @@ -119,9 +120,9 @@ func (c *controllerGenerator) doGenerateCtrlItem(dstModuleFolderPath string, ite var ( methodNameSnake = gstr.CaseSnake(item.MethodName) ctrlName = fmt.Sprintf(`Controller%s`, gstr.UcFirst(item.Version)) - methodFilePath = gfile.Join(dstModuleFolderPath, fmt.Sprintf( + methodFilePath = filepath.FromSlash(gfile.Join(dstModuleFolderPath, fmt.Sprintf( `%s_%s_%s.go`, item.Module, item.Version, methodNameSnake, - )) + ))) ) var content string diff --git a/cmd/gf/internal/cmd/genctrl/genctrl_generate_interface.go b/cmd/gf/internal/cmd/genctrl/genctrl_generate_interface.go index 8d99299df08..90af387e236 100644 --- a/cmd/gf/internal/cmd/genctrl/genctrl_generate_interface.go +++ b/cmd/gf/internal/cmd/genctrl/genctrl_generate_interface.go @@ -8,6 +8,7 @@ package genctrl import ( "fmt" + "path/filepath" "github.com/gogf/gf/cmd/gf/v2/internal/consts" "github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog" @@ -39,14 +40,14 @@ func (c *apiInterfaceGenerator) Generate(apiModuleFolderPath string, apiModuleAp func (c *apiInterfaceGenerator) doGenerate(apiModuleFolderPath string, module string, items []apiItem) (err error) { var ( - moduleFilePath = gfile.Join(apiModuleFolderPath, fmt.Sprintf(`%s.go`, module)) + moduleFilePath = filepath.FromSlash(gfile.Join(apiModuleFolderPath, fmt.Sprintf(`%s.go`, module))) importPathMap = gmap.NewListMap() importPaths []string ) // if there's already exist file that with the same but not auto generated go file, // it uses another file name. if !utils.IsFileDoNotEdit(moduleFilePath) { - moduleFilePath = gfile.Join(apiModuleFolderPath, fmt.Sprintf(`%s.if.go`, module)) + moduleFilePath = filepath.FromSlash(gfile.Join(apiModuleFolderPath, fmt.Sprintf(`%s.if.go`, module))) } // all import paths. importPathMap.Set("\t"+`"context"`, 1) diff --git a/cmd/gf/internal/cmd/genctrl/genctrl_generate_sdk.go b/cmd/gf/internal/cmd/genctrl/genctrl_generate_sdk.go index c59cea82880..25a628e771d 100644 --- a/cmd/gf/internal/cmd/genctrl/genctrl_generate_sdk.go +++ b/cmd/gf/internal/cmd/genctrl/genctrl_generate_sdk.go @@ -8,6 +8,7 @@ package genctrl import ( "fmt" + "path/filepath" "github.com/gogf/gf/cmd/gf/v2/internal/consts" "github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog" @@ -54,7 +55,7 @@ func (c *apiSdkGenerator) Generate(apiModuleApiItems []apiItem, sdkFolderPath st func (c *apiSdkGenerator) doGenerateSdkPkgFile(sdkFolderPath string) (err error) { var ( pkgName = gfile.Basename(sdkFolderPath) - pkgFilePath = gfile.Join(sdkFolderPath, fmt.Sprintf(`%s.go`, pkgName)) + pkgFilePath = filepath.FromSlash(gfile.Join(sdkFolderPath, fmt.Sprintf(`%s.go`, pkgName))) fileContent string ) if gfile.Exists(pkgFilePath) { @@ -79,7 +80,7 @@ func (c *apiSdkGenerator) doGenerateSdkIClient( funcName = gstr.CaseCamel(module) + gstr.UcFirst(version) interfaceName = fmt.Sprintf(`I%s`, funcName) moduleImportPath = gstr.Replace(fmt.Sprintf(`"%s"`, gfile.Dir(versionImportPath)), "\\", "/", -1) - iClientFilePath = gfile.Join(sdkFolderPath, fmt.Sprintf(`%s.iclient.go`, pkgName)) + iClientFilePath = filepath.FromSlash(gfile.Join(sdkFolderPath, fmt.Sprintf(`%s.iclient.go`, pkgName))) interfaceFuncDefinition = fmt.Sprintf( `%s() %s.%s`, gstr.CaseCamel(module)+gstr.UcFirst(version), module, interfaceName, @@ -145,9 +146,9 @@ func (c *apiSdkGenerator) doGenerateSdkImplementer( moduleImportPath = gstr.Replace(gfile.Dir(versionImportPath), "\\", "/", -1) versionPrefix = "" implementerName = moduleNameCamel + gstr.UcFirst(version) - implementerFilePath = gfile.Join(sdkFolderPath, fmt.Sprintf( + implementerFilePath = filepath.FromSlash(gfile.Join(sdkFolderPath, fmt.Sprintf( `%s_%s_%s.go`, pkgName, moduleNameSnake, version, - )) + ))) ) if sdkNoV1 && version == "v1" { implementerName = moduleNameCamel diff --git a/cmd/gf/internal/cmd/gendao/gendao_dao.go b/cmd/gf/internal/cmd/gendao/gendao_dao.go index 6759890d8d3..de3bdd528f7 100644 --- a/cmd/gf/internal/cmd/gendao/gendao_dao.go +++ b/cmd/gf/internal/cmd/gendao/gendao_dao.go @@ -10,6 +10,7 @@ import ( "bytes" "context" "fmt" + "path/filepath" "strings" "github.com/olekukonko/tablewriter" @@ -106,7 +107,7 @@ type generateDaoIndexInput struct { } func generateDaoIndex(in generateDaoIndexInput) { - path := gfile.Join(in.DirPathDao, in.FileName+".go") + path := filepath.FromSlash(gfile.Join(in.DirPathDao, in.FileName+".go")) if in.OverwriteDao || !gfile.Exists(path) { indexContent := gstr.ReplaceByMap( getTemplateFromPathOrDefault(in.TplDaoIndexPath, consts.TemplateGenDaoIndexContent), @@ -136,7 +137,7 @@ type generateDaoInternalInput struct { } func generateDaoInternal(in generateDaoInternalInput) { - path := gfile.Join(in.DirPathDaoInternal, in.FileName+".go") + path := filepath.FromSlash(gfile.Join(in.DirPathDaoInternal, in.FileName+".go")) modelContent := gstr.ReplaceByMap( getTemplateFromPathOrDefault(in.TplDaoInternalPath, consts.TemplateGenDaoInternalContent), g.MapStrStr{ diff --git a/cmd/gf/internal/cmd/gendao/gendao_do.go b/cmd/gf/internal/cmd/gendao/gendao_do.go index ccec96b7adc..85828d4d48f 100644 --- a/cmd/gf/internal/cmd/gendao/gendao_do.go +++ b/cmd/gf/internal/cmd/gendao/gendao_do.go @@ -9,6 +9,7 @@ package gendao import ( "context" "fmt" + "path/filepath" "strings" "github.com/gogf/gf/v2/frame/g" @@ -22,7 +23,7 @@ import ( ) func generateDo(ctx context.Context, in CGenDaoInternalInput) { - var dirPathDo = gfile.Join(in.Path, in.DoPath) + var dirPathDo = filepath.FromSlash(gfile.Join(in.Path, in.DoPath)) if in.Clear { doClear(ctx, dirPathDo, false) } diff --git a/cmd/gf/internal/cmd/gendao/gendao_entity.go b/cmd/gf/internal/cmd/gendao/gendao_entity.go index 06a843f6eaa..00e9239e62e 100644 --- a/cmd/gf/internal/cmd/gendao/gendao_entity.go +++ b/cmd/gf/internal/cmd/gendao/gendao_entity.go @@ -8,6 +8,7 @@ package gendao import ( "context" + "path/filepath" "strings" "github.com/gogf/gf/v2/frame/g" @@ -33,7 +34,7 @@ func generateEntity(ctx context.Context, in CGenDaoInternalInput) { var ( newTableName = in.NewTableNames[i] - entityFilePath = gfile.Join(dirPathEntity, gstr.CaseSnake(newTableName)+".go") + entityFilePath = filepath.FromSlash(gfile.Join(dirPathEntity, gstr.CaseSnake(newTableName)+".go")) structDefinition, appendImports = generateStructDefinition(ctx, generateStructDefinitionInput{ CGenDaoInternalInput: in, TableName: tableName, diff --git a/cmd/gf/internal/cmd/genpbentity/genpbentity.go b/cmd/gf/internal/cmd/genpbentity/genpbentity.go index 967743785d5..caf06556b7a 100644 --- a/cmd/gf/internal/cmd/genpbentity/genpbentity.go +++ b/cmd/gf/internal/cmd/genpbentity/genpbentity.go @@ -10,6 +10,7 @@ import ( "bytes" "context" "fmt" + "path/filepath" "strings" "github.com/olekukonko/tablewriter" @@ -246,7 +247,7 @@ func generatePbEntityContentFile(ctx context.Context, in CGenPbEntityInternalInp tableNameSnakeCase = gstr.CaseSnake(newTableName) entityMessageDefine = generateEntityMessageDefinition(tableNameCamelCase, fieldMap, in) fileName = gstr.Trim(tableNameSnakeCase, "-_.") - path = gfile.Join(in.Path, fileName+".proto") + path = filepath.FromSlash(gfile.Join(in.Path, fileName+".proto")) ) if gstr.Contains(entityMessageDefine, "google.protobuf.Timestamp") { imports = `import "google/protobuf/timestamp.proto";`