Skip to content

Commit

Permalink
Merge pull request go-gorm#314 from go-gorm/fix_import_path
Browse files Browse the repository at this point in the history
fix: model pkg path
  • Loading branch information
qqxhb authored Dec 23, 2021
2 parents 65170ad + 47530a7 commit 6189144
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
18 changes: 3 additions & 15 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package gen
import (
"fmt"
"path/filepath"
"runtime/debug"
"strings"

"gorm.io/gen/internal/check"
Expand Down Expand Up @@ -38,9 +37,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)
Expand Down Expand Up @@ -91,21 +90,10 @@ 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) == "" {
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)
Expand Down
25 changes: 21 additions & 4 deletions generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -48,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),
}
}

Expand Down Expand Up @@ -334,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,
Expand Down Expand Up @@ -396,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
Expand All @@ -412,7 +413,6 @@ func (g *Generator) generateModelFile() error {
if data == nil || !data.GenBaseStruct {
continue
}

pool.Wait()
go func(data *check.BaseStruct) {
defer pool.Done()
Expand All @@ -435,6 +435,7 @@ func (g *Generator) generateModelFile() error {
case err = <-errChan:
return err
case <-pool.AsyncWaitAll():
g.fillModelPkgPath(modelOutPath)
}
return nil
}
Expand All @@ -451,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.Config.modelPkgPath = pkgs[0].PkgPath
}

// output format and output
func (g *Generator) output(fileName string, content []byte) error {
result, err := imports.Process(fileName, content, nil)
Expand Down

0 comments on commit 6189144

Please sign in to comment.