feat: remove module command

This commit is contained in:
Rogee
2025-02-13 18:44:23 +08:00
parent e37cc85794
commit e2b09420a0
7 changed files with 159 additions and 14 deletions

View File

@@ -6,8 +6,6 @@ import (
"regexp" "regexp"
"strings" "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/metadata"
"github.com/go-jet/jet/v2/generator/postgres" "github.com/go-jet/jet/v2/generator/postgres"
"github.com/go-jet/jet/v2/generator/template" "github.com/go-jet/jet/v2/generator/template"
@@ -19,6 +17,9 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "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) { func CommandGenModel(root *cobra.Command) {
@@ -68,6 +69,8 @@ func commandGenModelE(cmd *cobra.Command, args []string) error {
"bool", "bool",
} }
generatedTables := []string{}
err = postgres.GenerateDSN( err = postgres.GenerateDSN(
dbConf.DSN(), dbConf.DSN(),
dbConf.Schema, dbConf.Schema,
@@ -108,6 +111,8 @@ func commandGenModelE(cmd *cobra.Command, args []string) error {
return tbl return tbl
} }
generatedTables = append(generatedTables, table.Name)
return tbl.UseField(func(column metadata.Column) template.TableModelField { return tbl.UseField(func(column metadata.Column) template.TableModelField {
defaultTableModelField := template.DefaultTableModelField(column) defaultTableModelField := template.DefaultTableModelField(column)
defaultTableModelField = defaultTableModelField.UseTags(fmt.Sprintf(`json:"%s"`, column.Name)) 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 { if err := os.Rename(dataPath, "database/schemas"); err != nil {
return err return err
} }
return astModel.Generate(generatedTables)
return nil
} }

View File

@@ -4,21 +4,21 @@ import (
"io/fs" "io/fs"
"os" "os"
"path/filepath" "path/filepath"
"regexp" "strings"
"go.ipao.vip/atomctl/pkg/ast/route"
"go.ipao.vip/atomctl/pkg/utils/gomod"
"github.com/samber/lo" "github.com/samber/lo"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"go.ipao.vip/atomctl/pkg/ast/route"
"go.ipao.vip/atomctl/pkg/utils/gomod"
) )
func CommandGenRoute(root *cobra.Command) { func CommandGenRoute(root *cobra.Command) {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "route", Use: "route",
Short: "generate routes", Short: "generate routes",
RunE: commandGenRouteE, RunE: commandGenRouteE,
PostRunE: commandGenProviderE, // PostRunE: commandGenProviderE,
} }
root.AddCommand(cmd) root.AddCommand(cmd)
@@ -51,13 +51,20 @@ func commandGenRouteE(cmd *cobra.Command, args []string) error {
log.Fatal("modules dir not exist, ", modulePath) 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 { err = filepath.WalkDir(modulePath, func(path string, d fs.DirEntry, err error) error {
if d.IsDir() { if d.IsDir() {
return nil 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 return nil
} }

View File

@@ -14,7 +14,7 @@ func CommandInit(root *cobra.Command) {
cmds := []func(*cobra.Command){ cmds := []func(*cobra.Command){
CommandNewProject, CommandNewProject,
CommandNewModule, // deprecate CommandNewModule,
CommandNewProvider, CommandNewProvider,
CommandNewEvent, CommandNewEvent,
CommandNewJob, CommandNewJob,

95
pkg/ast/model/generage.go Normal file
View 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
}

View 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
}

View File

@@ -0,0 +1,9 @@
package models
// @provider
type {{.CamelTable}}Model struct {
}
func (m *{{.CamelTable}}Model) Prepare() error {
return nil
}

View File

@@ -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) providers = append(providers, provider)
} }