diff --git a/cmd/dolphin/gen/gen.go b/cmd/dolphin/gen/gen.go index 344c7205..2fca9790 100644 --- a/cmd/dolphin/gen/gen.go +++ b/cmd/dolphin/gen/gen.go @@ -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{}, @@ -42,7 +43,6 @@ var lines = []pipe.Pipe{ &modules.Boilerplate{}, &modules.Table{}, &modules.Deploy{}, - &modules.More{}, } // AddPipe defined addPipe @@ -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 @@ -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) diff --git a/cmd/dolphin/gen/modules/app.go b/cmd/dolphin/gen/modules/app.go index 986a681c..4acf3d86 100644 --- a/cmd/dolphin/gen/modules/app.go +++ b/cmd/dolphin/gen/modules/app.go @@ -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" ) @@ -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(), } diff --git a/cmd/dolphin/gen/modules/auto.go b/cmd/dolphin/gen/modules/auto.go index e9f70e5e..f14abe4d 100644 --- a/cmd/dolphin/gen/modules/auto.go +++ b/cmd/dolphin/gen/modules/auto.go @@ -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" ) @@ -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") diff --git a/cmd/dolphin/gen/modules/bean.go b/cmd/dolphin/gen/modules/bean.go index e83409d2..9c26dc37 100644 --- a/cmd/dolphin/gen/modules/bean.go +++ b/cmd/dolphin/gen/modules/bean.go @@ -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" @@ -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"), diff --git a/cmd/dolphin/gen/modules/boil.go b/cmd/dolphin/gen/modules/boil.go index 0d4d0437..ac21e9e5 100644 --- a/cmd/dolphin/gen/modules/boil.go +++ b/cmd/dolphin/gen/modules/boil.go @@ -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" ) @@ -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 { diff --git a/cmd/dolphin/gen/modules/ctr.go b/cmd/dolphin/gen/modules/ctr.go index f99c1f55..af6ad112 100644 --- a/cmd/dolphin/gen/modules/ctr.go +++ b/cmd/dolphin/gen/modules/ctr.go @@ -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" @@ -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"), diff --git a/cmd/dolphin/gen/modules/doc.go b/cmd/dolphin/gen/modules/doc.go index 7e2d4b22..026bd1df 100644 --- a/cmd/dolphin/gen/modules/doc.go +++ b/cmd/dolphin/gen/modules/doc.go @@ -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" ) @@ -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", diff --git a/cmd/dolphin/gen/modules/k8s.go b/cmd/dolphin/gen/modules/k8s.go index 7b246506..2b4b5fbc 100644 --- a/cmd/dolphin/gen/modules/k8s.go +++ b/cmd/dolphin/gen/modules/k8s.go @@ -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" ) @@ -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") diff --git a/cmd/dolphin/gen/modules/main.go b/cmd/dolphin/gen/modules/main.go index 244b17f2..a311df53 100644 --- a/cmd/dolphin/gen/modules/main.go +++ b/cmd/dolphin/gen/modules/main.go @@ -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" ) @@ -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") diff --git a/cmd/dolphin/gen/modules/model.go b/cmd/dolphin/gen/modules/model.go index 98721702..2ee9ba67 100644 --- a/cmd/dolphin/gen/modules/model.go +++ b/cmd/dolphin/gen/modules/model.go @@ -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" @@ -24,18 +24,28 @@ func (m *Model) Name() string { return "model" } +// Pre defined +func (m *Model) Pre(*parser.AppParser) error { + return nil +} + +// After defined +func (m *Model) After(*parser.AppParser, []*pipe.TmplCfg) error { + return nil +} + // Build func -func (m *Model) Build(dir string, args []string, node *schema.Application) ([]*pipe.TmplCfg, error) { +func (m *Model) Build(dir string, args []string, parser *parser.AppParser) ([]*pipe.TmplCfg, error) { var tmplCfgs []*pipe.TmplCfg modelByte, _ := vfsutil.ReadFile(template.Assets, "model.tmpl") - for i := range node.Tables { + for i := range parser.Tables { data := map[string]interface{}{ - "PackageName": node.PackageName, - "Name": node.Name, - "Table": node.Tables[i], + "PackageName": parser.PackageName, + "Name": parser.Name, + "Table": parser.Tables[i], "Viper": viper.GetViper(), } - filename := utils.FileNameTrimSuffix(node.Tables[i].Path) + filename := utils.FileNameTrimSuffix(parser.Tables[i].Path) tmplCfg := &pipe.TmplCfg{ Text: string(modelByte), FilePath: path.Join(dir, viper.GetString("dir.model"), filename+".auto.go"), diff --git a/cmd/dolphin/gen/modules/more.go b/cmd/dolphin/gen/modules/more.go index 7fd1ac70..161d3ff9 100644 --- a/cmd/dolphin/gen/modules/more.go +++ b/cmd/dolphin/gen/modules/more.go @@ -11,11 +11,12 @@ 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/sirupsen/logrus" "github.com/spf13/viper" + "github.com/thoas/go-funk" ) // More struct @@ -27,8 +28,19 @@ func (m *More) Name() string { return "more" } +// Pre defined +func (m *More) Pre(*parser.AppParser) error { + return nil +} + +// After defined +func (m *More) After(parser *parser.AppParser, cfgs []*pipe.TmplCfg) error { + parser.WalkXML(funk.Map(cfgs, func(cfg *pipe.TmplCfg) string { return cfg.FilePath }).([]string)...) + return nil +} + // Build func -func (m *More) Build(dir string, args []string, node *schema.Application) ([]*pipe.TmplCfg, error) { +func (m *More) Build(dir string, args []string, parser *parser.AppParser) ([]*pipe.TmplCfg, error) { var tmplCfgs []*pipe.TmplCfg ctrByte, _ := vfsutil.ReadFile(template.Assets, "more.ctr.tmpl") tbByte, _ := vfsutil.ReadFile(template.Assets, "more.tb.tmpl") @@ -45,8 +57,8 @@ func (m *More) Build(dir string, args []string, node *schema.Application) ([]*pi return strings.Contains(string(data), "