feat: remove module command
This commit is contained in:
@@ -6,8 +6,6 @@ import (
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
pgDatabase "go.ipao.vip/atomctl/pkg/postgres"
|
||||
"go.ipao.vip/atomctl/pkg/utils/gomod"
|
||||
"github.com/go-jet/jet/v2/generator/metadata"
|
||||
"github.com/go-jet/jet/v2/generator/postgres"
|
||||
"github.com/go-jet/jet/v2/generator/template"
|
||||
@@ -19,6 +17,9 @@ import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
astModel "go.ipao.vip/atomctl/pkg/ast/model"
|
||||
pgDatabase "go.ipao.vip/atomctl/pkg/postgres"
|
||||
"go.ipao.vip/atomctl/pkg/utils/gomod"
|
||||
)
|
||||
|
||||
func CommandGenModel(root *cobra.Command) {
|
||||
@@ -68,6 +69,8 @@ func commandGenModelE(cmd *cobra.Command, args []string) error {
|
||||
"bool",
|
||||
}
|
||||
|
||||
generatedTables := []string{}
|
||||
|
||||
err = postgres.GenerateDSN(
|
||||
dbConf.DSN(),
|
||||
dbConf.Schema,
|
||||
@@ -108,6 +111,8 @@ func commandGenModelE(cmd *cobra.Command, args []string) error {
|
||||
return tbl
|
||||
}
|
||||
|
||||
generatedTables = append(generatedTables, table.Name)
|
||||
|
||||
return tbl.UseField(func(column metadata.Column) template.TableModelField {
|
||||
defaultTableModelField := template.DefaultTableModelField(column)
|
||||
defaultTableModelField = defaultTableModelField.UseTags(fmt.Sprintf(`json:"%s"`, column.Name))
|
||||
@@ -164,6 +169,5 @@ func commandGenModelE(cmd *cobra.Command, args []string) error {
|
||||
if err := os.Rename(dataPath, "database/schemas"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
return astModel.Generate(generatedTables)
|
||||
}
|
||||
|
||||
@@ -4,21 +4,21 @@ import (
|
||||
"io/fs"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"go.ipao.vip/atomctl/pkg/ast/route"
|
||||
"go.ipao.vip/atomctl/pkg/utils/gomod"
|
||||
"github.com/samber/lo"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
"go.ipao.vip/atomctl/pkg/ast/route"
|
||||
"go.ipao.vip/atomctl/pkg/utils/gomod"
|
||||
)
|
||||
|
||||
func CommandGenRoute(root *cobra.Command) {
|
||||
cmd := &cobra.Command{
|
||||
Use: "route",
|
||||
Short: "generate routes",
|
||||
RunE: commandGenRouteE,
|
||||
PostRunE: commandGenProviderE,
|
||||
Use: "route",
|
||||
Short: "generate routes",
|
||||
RunE: commandGenRouteE,
|
||||
// PostRunE: commandGenProviderE,
|
||||
}
|
||||
|
||||
root.AddCommand(cmd)
|
||||
@@ -51,13 +51,20 @@ func commandGenRouteE(cmd *cobra.Command, args []string) error {
|
||||
log.Fatal("modules dir not exist, ", modulePath)
|
||||
}
|
||||
|
||||
controllerPattern := regexp.MustCompile(`controller(_?\w+)?\.go`)
|
||||
// controllerPattern := regexp.MustCompile(`controller(_?\w+)?\.go`)
|
||||
err = filepath.WalkDir(modulePath, func(path string, d fs.DirEntry, err error) error {
|
||||
if d.IsDir() {
|
||||
return nil
|
||||
}
|
||||
|
||||
if !controllerPattern.MatchString(d.Name()) {
|
||||
// if !controllerPattern.MatchString(d.Name()) {
|
||||
// return nil
|
||||
// }
|
||||
if strings.HasSuffix(path, ".gen.go") {
|
||||
return nil
|
||||
}
|
||||
|
||||
if strings.HasSuffix(path, "_test.go") {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,7 @@ func CommandInit(root *cobra.Command) {
|
||||
|
||||
cmds := []func(*cobra.Command){
|
||||
CommandNewProject,
|
||||
CommandNewModule,
|
||||
// deprecate CommandNewModule,
|
||||
CommandNewProvider,
|
||||
CommandNewEvent,
|
||||
CommandNewJob,
|
||||
|
||||
95
pkg/ast/model/generage.go
Normal file
95
pkg/ast/model/generage.go
Normal file
@@ -0,0 +1,95 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/samber/lo"
|
||||
)
|
||||
|
||||
//go:embed table.go.tpl
|
||||
var tableTpl string
|
||||
|
||||
//go:embed models.gen.go.tpl
|
||||
var modelTpl string
|
||||
|
||||
type TableModelParam struct {
|
||||
CamelTable string // user
|
||||
PascalTable string // User
|
||||
}
|
||||
|
||||
func Generate(tables []string) error {
|
||||
baseDir := "app/models"
|
||||
|
||||
tableTpl := template.Must(template.New("model").Parse(string(tableTpl)))
|
||||
modelTpl := template.Must(template.New("modelGen").Parse(string(modelTpl)))
|
||||
|
||||
items := []TableModelParam{}
|
||||
for _, table := range tables {
|
||||
items = append(items, TableModelParam{
|
||||
CamelTable: lo.CamelCase(table),
|
||||
PascalTable: lo.PascalCase(table),
|
||||
})
|
||||
|
||||
modelFile := fmt.Sprintf("%s/%s.go", baseDir, table)
|
||||
// 如果 modelFile 已存在,则跳过
|
||||
if _, err := os.Stat(modelFile); err == nil {
|
||||
fmt.Printf("Model file %s already exists. Skipping...\n", modelFile)
|
||||
continue
|
||||
}
|
||||
|
||||
// 如果 modelFile 不存在,则创建
|
||||
fd, err := os.Create(modelFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create model file %s: %w", modelFile, err)
|
||||
}
|
||||
defer fd.Close()
|
||||
|
||||
if err := tableTpl.Execute(fd, map[string]string{"CamelTable": lo.CamelCase(table)}); err != nil {
|
||||
return fmt.Errorf("failed to render model template: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 遍历 baseDir 下的所有文件,将不在 tables 中的文件名(不带扩展名)加入
|
||||
files, err := os.ReadDir(baseDir)
|
||||
if err != nil {
|
||||
return fmt.Errorf("遍历目录 %s 失败: %w", baseDir, err)
|
||||
}
|
||||
for _, file := range files {
|
||||
if file.IsDir() {
|
||||
continue
|
||||
}
|
||||
name := file.Name()
|
||||
if strings.HasSuffix(name, ".gen.go") {
|
||||
continue
|
||||
}
|
||||
|
||||
baseName := strings.TrimSuffix(name, filepath.Ext(name))
|
||||
if !lo.Contains(tables, baseName) {
|
||||
items = append(items, TableModelParam{
|
||||
CamelTable: lo.CamelCase(baseName),
|
||||
PascalTable: lo.PascalCase(baseName),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 渲染总的 model 文件
|
||||
|
||||
modelFile := fmt.Sprintf("%s/models.gen.go", baseDir)
|
||||
os.Remove(modelFile)
|
||||
fd, err := os.Create(modelFile)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create model file %s: %w", baseDir, err)
|
||||
}
|
||||
defer fd.Close()
|
||||
|
||||
if err := modelTpl.Execute(fd, items); err != nil {
|
||||
return fmt.Errorf("failed to render model template: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
22
pkg/ast/model/models.gen.go.tpl
Normal file
22
pkg/ast/model/models.gen.go.tpl
Normal file
@@ -0,0 +1,22 @@
|
||||
// Code generated by the atomctl ; DO NOT EDIT.
|
||||
// Code generated by the atomctl ; DO NOT EDIT.
|
||||
// Code generated by the atomctl ; DO NOT EDIT.
|
||||
package models
|
||||
|
||||
{{- range . }}
|
||||
var {{.PascalTable}} *{{.CamelTable}}Model
|
||||
{{- end }}
|
||||
|
||||
// @provider(model)
|
||||
type models struct {
|
||||
{{- range . }}
|
||||
{{.CamelTable}} *{{.CamelTable}}Model
|
||||
{{- end }}
|
||||
}
|
||||
|
||||
func (m *models) Prepare() error {
|
||||
{{- range . }}
|
||||
{{.PascalTable}} = m.{{.CamelTable}}
|
||||
{{- end }}
|
||||
return nil
|
||||
}
|
||||
9
pkg/ast/model/table.go.tpl
Normal file
9
pkg/ast/model/table.go.tpl
Normal file
@@ -0,0 +1,9 @@
|
||||
package models
|
||||
|
||||
// @provider
|
||||
type {{.CamelTable}}Model struct {
|
||||
}
|
||||
|
||||
func (m *{{.CamelTable}}Model) Prepare() error {
|
||||
return nil
|
||||
}
|
||||
@@ -321,6 +321,14 @@ func Parse(source string) []Provider {
|
||||
}
|
||||
}
|
||||
|
||||
if providerDoc.Mode == "model" {
|
||||
provider.Mode = "model"
|
||||
|
||||
provider.ProviderGroup = "atom.GroupInitial"
|
||||
provider.ReturnType = "contracts.Initial"
|
||||
provider.NeedPrepareFunc = true
|
||||
}
|
||||
|
||||
providers = append(providers, provider)
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user