Skip to content

Commit

Permalink
feat: 优化more直接生成curd
Browse files Browse the repository at this point in the history
  • Loading branch information
2637309949 committed May 28, 2021
1 parent b255abe commit 8f8ec8b
Show file tree
Hide file tree
Showing 23 changed files with 352 additions and 140 deletions.
32 changes: 20 additions & 12 deletions cmd/dolphin/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ import (

"github.com/2637309949/dolphin/cmd/dolphin/gen/modules"
"github.com/2637309949/dolphin/cmd/dolphin/gen/pipe"
"github.com/2637309949/dolphin/cmd/dolphin/schema"
"github.com/2637309949/dolphin/cmd/dolphin/parser"
"github.com/2637309949/dolphin/cmd/dolphin/utils"
"github.com/sirupsen/logrus"
"github.com/thoas/go-funk"
)

var lines = []pipe.Pipe{
&modules.More{},
&modules.Main{},
&modules.App{},
&modules.Ctr{},
Expand All @@ -42,7 +43,6 @@ var lines = []pipe.Pipe{
&modules.Boilerplate{},
&modules.Table{},
&modules.Deploy{},
&modules.More{},
}

// AddPipe defined addPipe
Expand All @@ -52,25 +52,25 @@ func AddPipe(p pipe.Pipe) {

// GetPipesByName defined getbyname
func GetPipesByName(nm ...string) []pipe.Pipe {
return funk.Filter(lines, func(pipe pipe.Pipe) bool {
for _, v := range nm {
if pipe.Name() == v {
return funk.Chain(lines).Filter(func(pipe pipe.Pipe) bool {
for i := range nm {
if pipe.Name() == nm[i] {
return true
}
}
return false
}).([]pipe.Pipe)
}).Value().([]pipe.Pipe)
}

// Gen struct
type Gen struct {
App *schema.Application
Pipes []pipe.Pipe
Parser *parser.AppParser
Pipes []pipe.Pipe
}

// New defined gen
func New(app *schema.Application) *Gen {
return &Gen{App: app}
func New(parser *parser.AppParser) *Gen {
return &Gen{Parser: parser}
}

// AddPipe add pipe
Expand All @@ -88,17 +88,25 @@ func (gen *Gen) BuildDir(dir string, args []string) (err error) {
// generate code
cfgs := []*pipe.TmplCfg{}
for i := range gen.Pipes {
items, err := gen.Pipes[i].Build(dir, args, gen.App)
cfgs = append(cfgs, items...)
err := gen.Pipes[i].Pre(gen.Parser)
if err != nil {
return err
}
items, err := gen.Pipes[i].Build(dir, args, gen.Parser)
if err != nil {
return err
}
cfgs = append(cfgs, items...)
for j := range items {
err = gen.Build(items[j])
if err != nil {
panic(err)
}
}
err = gen.Pipes[i].After(gen.Parser, items)
if err != nil {
return err
}
}
// fmt code
filePaths := funk.Keys(funk.Map(funk.Filter(cfgs, func(cfg *pipe.TmplCfg) bool { return cfg.GOFmt && path.Ext(cfg.FilePath) == ".go" }), func(cfg *pipe.TmplCfg) (string, string) { return path.Dir(cfg.FilePath), path.Dir(cfg.FilePath) })).([]string)
Expand Down
20 changes: 15 additions & 5 deletions cmd/dolphin/gen/modules/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/2637309949/dolphin/cmd/dolphin/gen/pipe"
"github.com/2637309949/dolphin/cmd/dolphin/gen/template"
"github.com/2637309949/dolphin/cmd/dolphin/schema"
"github.com/2637309949/dolphin/cmd/dolphin/parser"
"github.com/shurcooL/httpfs/vfsutil"
"github.com/spf13/viper"
)
Expand All @@ -23,12 +23,22 @@ func (app *App) Name() string {
return "app"
}

// Pre defined
func (app *App) Pre(*parser.AppParser) error {
return nil
}

// After defined
func (app *App) After(*parser.AppParser, []*pipe.TmplCfg) error {
return nil
}

// Build func
func (app *App) Build(dir string, args []string, node *schema.Application) ([]*pipe.TmplCfg, error) {
func (app *App) Build(dir string, args []string, parser *parser.AppParser) ([]*pipe.TmplCfg, error) {
data := map[string]interface{}{
"PackageName": node.PackageName,
"Name": node.Name,
"Application": node,
"PackageName": parser.PackageName,
"Name": parser.Name,
"Application": parser,
"Viper": viper.GetViper(),
}

Expand Down
24 changes: 17 additions & 7 deletions cmd/dolphin/gen/modules/auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/2637309949/dolphin/cmd/dolphin/gen/pipe"
"github.com/2637309949/dolphin/cmd/dolphin/gen/template"
"github.com/2637309949/dolphin/cmd/dolphin/schema"
"github.com/2637309949/dolphin/cmd/dolphin/parser"
"github.com/shurcooL/httpfs/vfsutil"
"github.com/spf13/viper"
)
Expand All @@ -23,14 +23,24 @@ func (auto *Auto) Name() string {
return "auto"
}

// Pre defined
func (auto *Auto) Pre(*parser.AppParser) error {
return nil
}

// After defined
func (auto *Auto) After(*parser.AppParser, []*pipe.TmplCfg) error {
return nil
}

// Build func
func (auto *Auto) Build(dir string, args []string, node *schema.Application) ([]*pipe.TmplCfg, error) {
func (auto *Auto) Build(dir string, args []string, parser *parser.AppParser) ([]*pipe.TmplCfg, error) {
data := map[string]interface{}{
"PackageName": node.PackageName,
"Name": node.Name,
"Controllers": node.Controllers,
"Services": node.Services,
"Tables": node.Tables,
"PackageName": parser.PackageName,
"Name": parser.Name,
"Controllers": parser.Controllers,
"Services": parser.Services,
"Tables": parser.Tables,
"Viper": viper.GetViper(),
}
autoByte, _ := vfsutil.ReadFile(template.Assets, "auto.tmpl")
Expand Down
24 changes: 17 additions & 7 deletions cmd/dolphin/gen/modules/bean.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/2637309949/dolphin/cmd/dolphin/gen/pipe"
"github.com/2637309949/dolphin/cmd/dolphin/gen/template"
"github.com/2637309949/dolphin/cmd/dolphin/schema"
"github.com/2637309949/dolphin/cmd/dolphin/parser"
"github.com/2637309949/dolphin/cmd/dolphin/utils"
"github.com/shurcooL/httpfs/vfsutil"
"github.com/spf13/viper"
Expand All @@ -24,18 +24,28 @@ func (m *Bean) Name() string {
return "bean"
}

// Pre defined
func (m *Bean) Pre(*parser.AppParser) error {
return nil
}

// After defined
func (m *Bean) After(*parser.AppParser, []*pipe.TmplCfg) error {
return nil
}

// Build func
func (m *Bean) Build(dir string, args []string, node *schema.Application) ([]*pipe.TmplCfg, error) {
func (m *Bean) Build(dir string, args []string, parser *parser.AppParser) ([]*pipe.TmplCfg, error) {
var tmplCfgs []*pipe.TmplCfg
beanByte, _ := vfsutil.ReadFile(template.Assets, "bean.tmpl")
for i := range node.Beans {
for i := range parser.Beans {
data := map[string]interface{}{
"PackageName": node.PackageName,
"Name": node.Name,
"Bean": node.Beans[i],
"PackageName": parser.PackageName,
"Name": parser.Name,
"Bean": parser.Beans[i],
"Viper": viper.GetViper(),
}
filename := utils.FileNameTrimSuffix(node.Beans[i].Path)
filename := utils.FileNameTrimSuffix(parser.Beans[i].Path)
tmplCfg := &pipe.TmplCfg{
Text: string(beanByte),
FilePath: path.Join(dir, viper.GetString("dir.model"), filename+".auto.go"),
Expand Down
20 changes: 15 additions & 5 deletions cmd/dolphin/gen/modules/boil.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

"github.com/2637309949/dolphin/cmd/dolphin/gen/pipe"
"github.com/2637309949/dolphin/cmd/dolphin/gen/template"
"github.com/2637309949/dolphin/cmd/dolphin/schema"
"github.com/2637309949/dolphin/cmd/dolphin/parser"
"github.com/shurcooL/httpfs/vfsutil"
"github.com/spf13/viper"
)
Expand All @@ -27,14 +27,24 @@ func (m *Boilerplate) Name() string {
return "boilerplate"
}

// Pre defined
func (m *Boilerplate) Pre(*parser.AppParser) error {
return nil
}

// After defined
func (m *Boilerplate) After(*parser.AppParser, []*pipe.TmplCfg) error {
return nil
}

// Build func
func (m *Boilerplate) Build(dir string, args []string, node *schema.Application) ([]*pipe.TmplCfg, error) {
func (m *Boilerplate) Build(dir string, args []string, parser *parser.AppParser) ([]*pipe.TmplCfg, error) {
cfgs := []*pipe.TmplCfg{}
data := map[string]interface{}{
"lt": ht.HTML("<"),
"PackageName": node.PackageName,
"Name": node.Name,
"Desc": node.Desc,
"PackageName": parser.PackageName,
"Name": parser.Name,
"Desc": parser.Desc,
"Viper": viper.GetViper(),
}
walkFn := func(p string, fi os.FileInfo, err error) error {
Expand Down
26 changes: 18 additions & 8 deletions cmd/dolphin/gen/modules/ctr.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/2637309949/dolphin/cmd/dolphin/gen/pipe"
"github.com/2637309949/dolphin/cmd/dolphin/gen/template"
"github.com/2637309949/dolphin/cmd/dolphin/schema"
"github.com/2637309949/dolphin/cmd/dolphin/parser"
"github.com/2637309949/dolphin/cmd/dolphin/utils"
"github.com/shurcooL/httpfs/vfsutil"
"github.com/spf13/viper"
Expand All @@ -24,19 +24,29 @@ func (ctr *Ctr) Name() string {
return "ctr"
}

// Pre defined
func (ctr *Ctr) Pre(*parser.AppParser) error {
return nil
}

// After defined
func (ctr *Ctr) After(*parser.AppParser, []*pipe.TmplCfg) error {
return nil
}

// Build func
func (ctr *Ctr) Build(dir string, args []string, node *schema.Application) ([]*pipe.TmplCfg, error) {
func (ctr *Ctr) Build(dir string, args []string, parser *parser.AppParser) ([]*pipe.TmplCfg, error) {
var tmplCfgs []*pipe.TmplCfg
ctrByte, _ := vfsutil.ReadFile(template.Assets, "ctr.tmpl")
for i := range node.Controllers {
for i := range parser.Controllers {
data := map[string]interface{}{
"PackageName": node.PackageName,
"Tables": node.Tables,
"Name": node.Name,
"Controller": node.Controllers[i],
"PackageName": parser.PackageName,
"Tables": parser.Tables,
"Name": parser.Name,
"Controller": parser.Controllers[i],
"Viper": viper.GetViper(),
}
filename := utils.FileNameTrimSuffix(node.Controllers[i].Path)
filename := utils.FileNameTrimSuffix(parser.Controllers[i].Path)
tmplCfg := &pipe.TmplCfg{
Text: string(ctrByte),
FilePath: path.Join(dir, viper.GetString("dir.app"), filename+".go"),
Expand Down
14 changes: 12 additions & 2 deletions cmd/dolphin/gen/modules/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"path"

"github.com/2637309949/dolphin/cmd/dolphin/gen/pipe"
"github.com/2637309949/dolphin/cmd/dolphin/schema"
"github.com/2637309949/dolphin/cmd/dolphin/parser"
swag "github.com/2637309949/dolphin/packages/swag/gen"
"github.com/spf13/viper"
)
Expand All @@ -22,8 +22,18 @@ func (m *Doc) Name() string {
return "doc"
}

// Pre defined
func (m *Doc) Pre(*parser.AppParser) error {
return nil
}

// After defined
func (m *Doc) After(*parser.AppParser, []*pipe.TmplCfg) error {
return nil
}

// Build func
func (m *Doc) Build(dir string, args []string, node *schema.Application) ([]*pipe.TmplCfg, error) {
func (m *Doc) Build(dir string, args []string, parser *parser.AppParser) ([]*pipe.TmplCfg, error) {
return []*pipe.TmplCfg{}, swag.New().Build(&swag.Config{
SearchDir: dir,
MainAPIFile: "main.go",
Expand Down
18 changes: 14 additions & 4 deletions cmd/dolphin/gen/modules/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/2637309949/dolphin/cmd/dolphin/gen/pipe"
"github.com/2637309949/dolphin/cmd/dolphin/gen/template"
"github.com/2637309949/dolphin/cmd/dolphin/schema"
"github.com/2637309949/dolphin/cmd/dolphin/parser"
"github.com/shurcooL/httpfs/vfsutil"
"github.com/spf13/viper"
)
Expand All @@ -23,11 +23,21 @@ func (dp *Deploy) Name() string {
return "deploy"
}

// Pre defined
func (dp *Deploy) Pre(*parser.AppParser) error {
return nil
}

// After defined
func (dp *Deploy) After(*parser.AppParser, []*pipe.TmplCfg) error {
return nil
}

// Build func
func (dp *Deploy) Build(dir string, args []string, node *schema.Application) ([]*pipe.TmplCfg, error) {
func (dp *Deploy) Build(dir string, args []string, parser *parser.AppParser) ([]*pipe.TmplCfg, error) {
data := map[string]interface{}{
"PackageName": node.PackageName,
"Name": node.Name,
"PackageName": parser.PackageName,
"Name": parser.Name,
"Viper": viper.GetViper(),
}
cfgByte, _ := vfsutil.ReadFile(template.Assets, "k8s.cfg.tmpl")
Expand Down
18 changes: 14 additions & 4 deletions cmd/dolphin/gen/modules/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/2637309949/dolphin/cmd/dolphin/gen/pipe"
"github.com/2637309949/dolphin/cmd/dolphin/gen/template"
"github.com/2637309949/dolphin/cmd/dolphin/schema"
"github.com/2637309949/dolphin/cmd/dolphin/parser"
"github.com/shurcooL/httpfs/vfsutil"
"github.com/spf13/viper"
)
Expand All @@ -23,11 +23,21 @@ func (m *Main) Name() string {
return "main"
}

// Pre defined
func (m *Main) Pre(*parser.AppParser) error {
return nil
}

// After defined
func (m *Main) After(*parser.AppParser, []*pipe.TmplCfg) error {
return nil
}

// Build func
func (m *Main) Build(dir string, args []string, node *schema.Application) ([]*pipe.TmplCfg, error) {
func (m *Main) Build(dir string, args []string, parser *parser.AppParser) ([]*pipe.TmplCfg, error) {
data := map[string]interface{}{
"PackageName": node.PackageName,
"Name": node.Name,
"PackageName": parser.PackageName,
"Name": parser.Name,
"Viper": viper.GetViper(),
}
mainByte, _ := vfsutil.ReadFile(template.Assets, "main.tmpl")
Expand Down
Loading

0 comments on commit 8f8ec8b

Please sign in to comment.