Skip to content

Commit

Permalink
fix: generated dao go files forcely cleared by command gf gen dao i…
Browse files Browse the repository at this point in the history
…f clear set true (#3337)
  • Loading branch information
gqcn authored Mar 5, 2024
1 parent 4feb821 commit 680ae86
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 47 deletions.
25 changes: 7 additions & 18 deletions cmd/gf/internal/cmd/gendao/gendao.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ type (
NoModelComment bool `name:"noModelComment" short:"m" brief:"{CGenDaoBriefNoModelComment}" orphan:"true"`
Clear bool `name:"clear" short:"a" brief:"{CGenDaoBriefClear}" orphan:"true"`

TypeMapping map[DBFieldTypeName]CustomAttributeType `name:"typeMapping" short:"y" brief:"{CGenDaoBriefTypeMapping}" orphan:"true"`
generatedFilePaths *CGenDaoInternalGeneratedFilePaths
TypeMapping map[DBFieldTypeName]CustomAttributeType `name:"typeMapping" short:"y" brief:"{CGenDaoBriefTypeMapping}" orphan:"true"`
genItems *CGenDaoInternalGenItems
}
CGenDaoOutput struct{}

Expand All @@ -213,13 +213,6 @@ type (
NewTableNames []string
}

CGenDaoInternalGeneratedFilePaths struct {
DaoFilePaths []string
DaoInternalFilePaths []string
DoFilePaths []string
EntityFilePaths []string
}

DBFieldTypeName = string
CustomAttributeType struct {
Type string `brief:"custom attribute type name"`
Expand All @@ -228,12 +221,7 @@ type (
)

func (c CGenDao) Dao(ctx context.Context, in CGenDaoInput) (out *CGenDaoOutput, err error) {
in.generatedFilePaths = &CGenDaoInternalGeneratedFilePaths{
DaoFilePaths: make([]string, 0),
DaoInternalFilePaths: make([]string, 0),
DoFilePaths: make([]string, 0),
EntityFilePaths: make([]string, 0),
}
in.genItems = newCGenDaoInternalGenItems()
if g.Cfg().Available(ctx) {
v := g.Cfg().MustGet(ctx, CGenDaoConfig)
if v.IsSlice() {
Expand All @@ -246,6 +234,7 @@ func (c CGenDao) Dao(ctx context.Context, in CGenDaoInput) (out *CGenDaoOutput,
} else {
doGenDaoForArray(ctx, -1, in)
}
doClear(in.genItems)
mlog.Print("done!")
return
}
Expand Down Expand Up @@ -326,6 +315,8 @@ func doGenDaoForArray(ctx context.Context, index int, in CGenDaoInput) {
newTableNames[i] = newTableName
}

in.genItems.Scale()

// Dao: index and internal.
generateDao(ctx, CGenDaoInternalInput{
CGenDaoInput: in,
Expand All @@ -348,9 +339,7 @@ func doGenDaoForArray(ctx context.Context, index int, in CGenDaoInput) {
NewTableNames: newTableNames,
})

if in.Clear {
doClear(ctx, in)
}
in.genItems.SetClear(in.Clear)
}

func getImportPartContent(ctx context.Context, source string, isDo bool, appendImports []string) string {
Expand Down
39 changes: 26 additions & 13 deletions cmd/gf/internal/cmd/gendao/gendao_clear.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,40 @@
package gendao

import (
"context"

"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/text/gstr"

"github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
)

func doClear(ctx context.Context, in CGenDaoInput) {
filePaths, err := gfile.ScanDirFile(in.Path, "*.go", true)
if err != nil {
mlog.Fatal(err)
}
func doClear(items *CGenDaoInternalGenItems) {
var allGeneratedFilePaths = make([]string, 0)
allGeneratedFilePaths = append(allGeneratedFilePaths, in.generatedFilePaths.DaoFilePaths...)
allGeneratedFilePaths = append(allGeneratedFilePaths, in.generatedFilePaths.DaoInternalFilePaths...)
allGeneratedFilePaths = append(allGeneratedFilePaths, in.generatedFilePaths.EntityFilePaths...)
allGeneratedFilePaths = append(allGeneratedFilePaths, in.generatedFilePaths.DoFilePaths...)
for _, filePath := range filePaths {
for _, item := range items.Items {
allGeneratedFilePaths = append(allGeneratedFilePaths, item.GeneratedFilePaths...)
}
for i, v := range allGeneratedFilePaths {
allGeneratedFilePaths[i] = gfile.RealPath(v)
}
for _, item := range items.Items {
if !item.Clear {
continue
}
doClearItem(item, allGeneratedFilePaths)
}
}

func doClearItem(item CGenDaoInternalGenItem, allGeneratedFilePaths []string) {
var generatedFilePaths = make([]string, 0)
for _, dirPath := range item.StorageDirPaths {
filePaths, err := gfile.ScanDirFile(dirPath, "*.go", true)
if err != nil {
mlog.Fatal(err)
}
generatedFilePaths = append(generatedFilePaths, filePaths...)
}
for _, filePath := range generatedFilePaths {
if !gstr.InArray(allGeneratedFilePaths, filePath) {
if err = gfile.Remove(filePath); err != nil {
if err := gfile.Remove(filePath); err != nil {
mlog.Print(err)
}
}
Expand Down
11 changes: 3 additions & 8 deletions cmd/gf/internal/cmd/gendao/gendao_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func generateDao(ctx context.Context, in CGenDaoInternalInput) {
dirPathDao = gfile.Join(in.Path, in.DaoPath)
dirPathDaoInternal = gfile.Join(dirPathDao, "internal")
)
in.genItems.AppendDirPath(dirPathDao)
for i := 0; i < len(in.TableNames); i++ {
generateDaoSingle(ctx, generateDaoSingleInput{
CGenDaoInternalInput: in,
Expand Down Expand Up @@ -106,10 +107,7 @@ type generateDaoIndexInput struct {
func generateDaoIndex(in generateDaoIndexInput) {
path := filepath.FromSlash(gfile.Join(in.DirPathDao, in.FileName+".go"))
// It should add path to result slice whenever it would generate the path file or not.
in.generatedFilePaths.DaoFilePaths = append(
in.generatedFilePaths.DaoFilePaths,
path,
)
in.genItems.AppendGeneratedFilePath(path)
if in.OverwriteDao || !gfile.Exists(path) {
indexContent := gstr.ReplaceByMap(
getTemplateFromPathOrDefault(in.TplDaoIndexPath, consts.TemplateGenDaoIndexContent),
Expand Down Expand Up @@ -153,10 +151,7 @@ func generateDaoInternal(in generateDaoInternalInput) {
tplVarColumnNames: gstr.Trim(generateColumnNamesForDao(in.FieldMap, removeFieldPrefixArray)),
})
modelContent = replaceDefaultVar(in.CGenDaoInternalInput, modelContent)
in.generatedFilePaths.DaoInternalFilePaths = append(
in.generatedFilePaths.DaoInternalFilePaths,
path,
)
in.genItems.AppendGeneratedFilePath(path)
if err := gfile.PutContents(path, strings.TrimSpace(modelContent)); err != nil {
mlog.Fatalf("writing content to '%s' failed: %v", path, err)
} else {
Expand Down
6 changes: 2 additions & 4 deletions cmd/gf/internal/cmd/gendao/gendao_do.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

func generateDo(ctx context.Context, in CGenDaoInternalInput) {
var dirPathDo = filepath.FromSlash(gfile.Join(in.Path, in.DoPath))
in.genItems.AppendDirPath(dirPathDo)
in.NoJsonTag = true
in.DescriptionTag = false
in.NoModelComment = false
Expand Down Expand Up @@ -63,10 +64,7 @@ func generateDo(ctx context.Context, in CGenDaoInternalInput) {
gstr.CaseCamel(newTableName),
structDefinition,
)
in.generatedFilePaths.DoFilePaths = append(
in.generatedFilePaths.DoFilePaths,
doFilePath,
)
in.genItems.AppendGeneratedFilePath(doFilePath)
err = gfile.PutContents(doFilePath, strings.TrimSpace(modelContent))
if err != nil {
mlog.Fatalf(`writing content to "%s" failed: %v`, doFilePath, err)
Expand Down
6 changes: 2 additions & 4 deletions cmd/gf/internal/cmd/gendao/gendao_entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

func generateEntity(ctx context.Context, in CGenDaoInternalInput) {
var dirPathEntity = gfile.Join(in.Path, in.EntityPath)
in.genItems.AppendDirPath(dirPathEntity)
// Model content.
for i, tableName := range in.TableNames {
fieldMap, err := in.DB.TableFields(ctx, tableName)
Expand All @@ -48,10 +49,7 @@ func generateEntity(ctx context.Context, in CGenDaoInternalInput) {
appendImports,
)
)
in.generatedFilePaths.EntityFilePaths = append(
in.generatedFilePaths.EntityFilePaths,
entityFilePath,
)
in.genItems.AppendGeneratedFilePath(entityFilePath)
err = gfile.PutContents(entityFilePath, strings.TrimSpace(entityContent))
if err != nil {
mlog.Fatalf("writing content to '%s' failed: %v", entityFilePath, err)
Expand Down
53 changes: 53 additions & 0 deletions cmd/gf/internal/cmd/gendao/gendao_gen_item.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.

package gendao

type (
CGenDaoInternalGenItems struct {
index int
Items []CGenDaoInternalGenItem
}
CGenDaoInternalGenItem struct {
Clear bool
StorageDirPaths []string
GeneratedFilePaths []string
}
)

func newCGenDaoInternalGenItems() *CGenDaoInternalGenItems {
return &CGenDaoInternalGenItems{
index: -1,
Items: make([]CGenDaoInternalGenItem, 0),
}
}

func (i *CGenDaoInternalGenItems) Scale() {
i.Items = append(i.Items, CGenDaoInternalGenItem{
StorageDirPaths: make([]string, 0),
GeneratedFilePaths: make([]string, 0),
Clear: false,
})
i.index++
}

func (i *CGenDaoInternalGenItems) SetClear(clear bool) {
i.Items[i.index].Clear = clear
}

func (i CGenDaoInternalGenItems) AppendDirPath(storageDirPath string) {
i.Items[i.index].StorageDirPaths = append(
i.Items[i.index].StorageDirPaths,
storageDirPath,
)
}

func (i CGenDaoInternalGenItems) AppendGeneratedFilePath(generatedFilePath string) {
i.Items[i.index].GeneratedFilePaths = append(
i.Items[i.index].GeneratedFilePaths,
generatedFilePath,
)
}

0 comments on commit 680ae86

Please sign in to comment.