From 1e1cfbdca6e2746bc5b60d6550c89a32e9668713 Mon Sep 17 00:00:00 2001 From: qqxhb <1252905006@qq.com> Date: Thu, 23 Dec 2021 12:31:09 +0800 Subject: [PATCH 1/3] fix: model pkg path --- generator.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/generator.go b/generator.go index e5448ace..14070e7a 100644 --- a/generator.go +++ b/generator.go @@ -14,6 +14,7 @@ import ( "strings" "text/template" + "golang.org/x/tools/go/packages" "golang.org/x/tools/imports" "gorm.io/gorm" "gorm.io/gorm/schema" @@ -405,6 +406,11 @@ func (g *Generator) generateModelFile() error { if err := os.MkdirAll(modelOutPath, os.ModePerm); err != nil { return fmt.Errorf("create model pkg path(%s) fail: %s", modelOutPath, err) } + p, err := packages.Load(&packages.Config{Dir: modelOutPath}) + if err != nil || len(p) == 0 { + return fmt.Errorf("parse model pkg path(%s) fail: %v", modelOutPath, err) + } + modelPkgPath := p[0].PkgPath errChan := make(chan error) pool := pools.NewPool(concurrent) @@ -412,7 +418,7 @@ func (g *Generator) generateModelFile() error { if data == nil || !data.GenBaseStruct { continue } - + data.StructInfo.PkgPath = modelPkgPath pool.Wait() go func(data *check.BaseStruct) { defer pool.Done() From bb4a74fde6f0400244902fa872e659861fb22af1 Mon Sep 17 00:00:00 2001 From: riverchu Date: Thu, 23 Dec 2021 17:51:33 +0800 Subject: [PATCH 2/3] feat(generate): load pkg after model generated --- config.go | 9 +++------ generator.go | 29 ++++++++++++++++++++--------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/config.go b/config.go index 4bb0eea2..df4eeca0 100644 --- a/config.go +++ b/config.go @@ -38,9 +38,9 @@ type Config struct { Mode GenerateMode // generate mode - queryPkgName string // generated query code's package name - modelImportPath string - dbNameOpts []model.SchemaNameOpt + queryPkgName string // generated query code's package name + modelPkgPath string // model pkg path in target project + dbNameOpts []model.SchemaNameOpt // name strategy for syncing table from db tableNameNS func(tableName string) (targetTableName string) @@ -103,9 +103,6 @@ var moduleFullPath = func() string { func (cfg *Config) Revise() (err error) { if strings.TrimSpace(cfg.ModelPkgPath) == "" { cfg.ModelPkgPath = check.DefaultModelPkg - cfg.modelImportPath = filepath.Dir(filepath.Clean(moduleFullPath+"/"+cfg.OutPath)) + "/" + cfg.ModelPkgPath - } else { - cfg.modelImportPath = filepath.Clean(moduleFullPath + "/" + cfg.ModelPkgPath) } cfg.OutPath, err = filepath.Abs(cfg.OutPath) diff --git a/generator.go b/generator.go index 14070e7a..b5401745 100644 --- a/generator.go +++ b/generator.go @@ -49,7 +49,7 @@ func NewGenerator(cfg Config) *Generator { return &Generator{ Config: cfg, Data: make(map[string]*genInfo), - modelData: map[string]*check.BaseStruct{}, + modelData: make(map[string]*check.BaseStruct), } } @@ -335,7 +335,7 @@ func (g *Generator) generateSingleQueryFile(data *genInfo) (err error) { structPkgPath := data.StructInfo.PkgPath if structPkgPath == "" { - structPkgPath = g.modelImportPath + structPkgPath = g.modelPkgPath } err = render(tmpl.Header, &buf, map[string]string{ "Package": g.queryPkgName, @@ -397,7 +397,7 @@ func (g *Generator) generateQueryUnitTestFile(data *genInfo) (err error) { } // generateModelFile generate model structures and save to file -func (g *Generator) generateModelFile() error { +func (g *Generator) generateModelFile() (err error) { modelOutPath, err := g.getModelOutputPath() if err != nil { return err @@ -406,11 +406,6 @@ func (g *Generator) generateModelFile() error { if err := os.MkdirAll(modelOutPath, os.ModePerm); err != nil { return fmt.Errorf("create model pkg path(%s) fail: %s", modelOutPath, err) } - p, err := packages.Load(&packages.Config{Dir: modelOutPath}) - if err != nil || len(p) == 0 { - return fmt.Errorf("parse model pkg path(%s) fail: %v", modelOutPath, err) - } - modelPkgPath := p[0].PkgPath errChan := make(chan error) pool := pools.NewPool(concurrent) @@ -418,7 +413,6 @@ func (g *Generator) generateModelFile() error { if data == nil || !data.GenBaseStruct { continue } - data.StructInfo.PkgPath = modelPkgPath pool.Wait() go func(data *check.BaseStruct) { defer pool.Done() @@ -441,6 +435,7 @@ func (g *Generator) generateModelFile() error { case err = <-errChan: return err case <-pool.AsyncWaitAll(): + g.fillModelPkgPath(modelOutPath) } return nil } @@ -457,6 +452,22 @@ func (g *Generator) getModelOutputPath() (outPath string, err error) { return outPath + "/", nil } +func (g *Generator) fillModelPkgPath(filePath string) { + pkgs, err := packages.Load(&packages.Config{ + Mode: packages.NeedName, + Dir: filePath, + }) + if err != nil { + g.db.Logger.Error(context.Background(), "parse model pkg path fail: %s", err) + return + } + if len(pkgs) == 0 { + g.db.Logger.Error(context.Background(), "parse model pkg path fail: got 0 packages") + return + } + g.modelPkgPath = pkgs[0].PkgPath +} + // output format and output func (g *Generator) output(fileName string, content []byte) error { result, err := imports.Process(fileName, content, nil) From 47530a7c0a0c178064577befe956c92e18ff2b53 Mon Sep 17 00:00:00 2001 From: riverchu Date: Thu, 23 Dec 2021 17:55:14 +0800 Subject: [PATCH 3/3] style(generat): remove deadcode --- config.go | 9 --------- generator.go | 2 +- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/config.go b/config.go index df4eeca0..3434396c 100644 --- a/config.go +++ b/config.go @@ -3,7 +3,6 @@ package gen import ( "fmt" "path/filepath" - "runtime/debug" "strings" "gorm.io/gen/internal/check" @@ -91,14 +90,6 @@ func (cfg *Config) WithNewTagNameStrategy(ns func(columnName string) (tagContent cfg.fieldNewTagNS = ns } -var moduleFullPath = func() string { - info, ok := debug.ReadBuildInfo() - if !ok { - return "" - } - return info.Path -}() - // Revise format path and db func (cfg *Config) Revise() (err error) { if strings.TrimSpace(cfg.ModelPkgPath) == "" { diff --git a/generator.go b/generator.go index b5401745..3daac08f 100644 --- a/generator.go +++ b/generator.go @@ -465,7 +465,7 @@ func (g *Generator) fillModelPkgPath(filePath string) { g.db.Logger.Error(context.Background(), "parse model pkg path fail: got 0 packages") return } - g.modelPkgPath = pkgs[0].PkgPath + g.Config.modelPkgPath = pkgs[0].PkgPath } // output format and output