Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 41 additions & 5 deletions tool/curd/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import (
"strings"
"text/template"
"unicode"

"github.com/muxi-Infra/muxi-micro/tool/curd/parse"
)

func CreateVar(pkg, dir string, open, cover bool) error {
func CreateVar(pkg, table, dir string, open, cover bool) error {
if cover == false && !CheckExist(dir, "var.go") {
return nil
}
Expand All @@ -34,8 +36,10 @@ func CreateVar(pkg, dir string, open, cover bool) error {

data := struct {
PackageName string
ModelName string
}{
PackageName: pkg,
ModelName: table,
}

if err := t.ExecuteTemplate(file, "var", data); err != nil {
Expand Down Expand Up @@ -84,7 +88,7 @@ func CreateExample(pkg, dir, table string, open, cover bool) error {
return nil
}

func CreateExample_gen(pkg, dir, table string, fields []string, open bool) error {
func CreateExample_gen(pkg, dir, table string, fields []parse.FieldInfo, open bool) error {
var tmplPath []string
if open {
tmplPath = []string{
Expand Down Expand Up @@ -115,15 +119,15 @@ func CreateExample_gen(pkg, dir, table string, fields []string, open bool) error
data := struct {
PackageName string
ModelName string
Fields []string
NotPrs []string
Fields []parse.FieldInfo
NotPrs []parse.FieldInfo
Pr string
}{
PackageName: pkg,
ModelName: table,
Fields: fields,
NotPrs: fields[:len(fields)-1],
Pr: fields[len(fields)-1],
Pr: fields[len(fields)-1].Name,
}

if err := t.ExecuteTemplate(file, "header", data); err != nil {
Expand All @@ -133,6 +137,38 @@ func CreateExample_gen(pkg, dir, table string, fields []string, open bool) error
return nil
}

func CreateTranscation(pkg, dir string, transcation bool) error {
if transcation == false {
return nil
}
var tmplPath string
tmplPath = filepath.Join("curd", "template", "transaction.tpl")
t, err := template.New("header").ParseFiles(tmplPath)
if err != nil {
return err
}

outputPath := filepath.Join(dir, "transaction.go")
file, err := os.Create(outputPath)
if err != nil {
return err
}

defer file.Close()

data := struct {
PackageName string
}{
PackageName: pkg,
}

if err := t.ExecuteTemplate(file, "transaction", data); err != nil {
return err
}

return nil
}

func safeFilename(tableName string) string {
return strings.Map(func(r rune) rune {
if unicode.IsLetter(r) || unicode.IsDigit(r) || r == '_' || r == '-' {
Expand Down
43 changes: 28 additions & 15 deletions tool/curd/curd.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package curd

import (
"fmt"
"os"
"path/filepath"

"github.com/muxi-Infra/muxi-micro/tool/curd/create"
"github.com/muxi-Infra/muxi-micro/tool/curd/parse"
"github.com/spf13/cobra"
"os"
"path/filepath"
)

func InitCurdCobra() *cobra.Command {
Expand All @@ -21,27 +21,39 @@ func InitCurdCobra() *cobra.Command {
dir, _ := cmd.Flags().GetString("dir")
cache, _ := cmd.Flags().GetBool("cache")
cover, _ := cmd.Flags().GetBool("cover")
transcation, _ := cmd.Flags().GetBool("transcation")

modelPath := filepath.Join(dir, "model.go")
if _, err := os.Stat(modelPath); err != nil {
return err
}
table, index, err := parse.ParseStruct(modelPath)
structs, err := parse.ParseStructs(modelPath)
if err != nil {
return err
}

err = create.CreateExample_gen(pkg, dir, table, index, cache)
if err != nil {
return err
}
err = create.CreateExample(pkg, dir, table, cache, cover)
if err != nil {
return err
}
err = create.CreateVar(pkg, dir, cache, cover)
if err != nil {
fmt.Println(err)
for _, v := range structs {
var index []parse.FieldInfo
for _, vv := range v.Index {
index = append(index, vv)
}
index = append(index, v.Primary[0])
err = create.CreateExample_gen(pkg, dir, v.Name, index, cache)
if err != nil {
return err
}
err = create.CreateExample(pkg, dir, v.Name, cache, cover)
if err != nil {
return err
}
err = create.CreateVar(pkg, v.Name, dir, cache, cover)
if err != nil {
return err
}
err = create.CreateTranscation(pkg, dir, transcation)
if err != nil {
return err
}
}
return nil
},
Expand All @@ -51,6 +63,7 @@ func InitCurdCobra() *cobra.Command {
curdCmd.Flags().String("dir", ".", "model文件以及文件生成目录")
curdCmd.Flags().Bool("cache", false, "是否开启缓存")
curdCmd.Flags().Bool("cover", false, "是否覆盖除 _gen.go 外的另外两个文件")
curdCmd.Flags().Bool("transcation", false, "是否生成事务代码")

return curdCmd
}
35 changes: 0 additions & 35 deletions tool/curd/example/UserModel.go

This file was deleted.

Loading