update
This commit is contained in:
@@ -15,6 +15,7 @@ func CommandGen(root *cobra.Command) {
|
|||||||
CommandGenRoute,
|
CommandGenRoute,
|
||||||
CommandGenModel,
|
CommandGenModel,
|
||||||
CommandGenEnum,
|
CommandGenEnum,
|
||||||
|
CommandGenService,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, c := range cmds {
|
for _, c := range cmds {
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ func CommandGenProvider(root *cobra.Command) {
|
|||||||
Aliases: []string{"p"},
|
Aliases: []string{"p"},
|
||||||
Short: "Generate providers",
|
Short: "Generate providers",
|
||||||
Long: `// @provider
|
Long: `// @provider
|
||||||
// @provider:[except|only] [returnType] [group]
|
// @provider(cronjob|job|event|grpc|model):[except|only] [returnType] [group]
|
||||||
// when except add tag: inject:"false"
|
// when except add tag: inject:"false"
|
||||||
// when only add tag: inject:"true"`,
|
// when only add tag: inject:"true"`,
|
||||||
RunE: commandGenProviderE,
|
RunE: commandGenProviderE,
|
||||||
|
|||||||
90
cmd/gen_service.go
Normal file
90
cmd/gen_service.go
Normal file
@@ -0,0 +1,90 @@
|
|||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"embed"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"text/template"
|
||||||
|
|
||||||
|
"github.com/samber/lo"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"go.ipao.vip/atomctl/v2/templates"
|
||||||
|
)
|
||||||
|
|
||||||
|
func CommandGenService(root *cobra.Command) {
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "service",
|
||||||
|
Short: "generate services",
|
||||||
|
RunE: commandGenServiceE,
|
||||||
|
PostRunE: commandGenProviderE,
|
||||||
|
}
|
||||||
|
|
||||||
|
cmd.Flags().String("path", "./app/services", "base path to scan")
|
||||||
|
|
||||||
|
root.AddCommand(cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
func commandGenServiceE(cmd *cobra.Command, args []string) error {
|
||||||
|
path := cmd.Flag("path").Value.String()
|
||||||
|
|
||||||
|
files, err := os.ReadDir(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
type srv struct {
|
||||||
|
CamelName string
|
||||||
|
ServiceName string
|
||||||
|
}
|
||||||
|
|
||||||
|
// get services from files
|
||||||
|
var services []srv
|
||||||
|
for _, file := range files {
|
||||||
|
if file.IsDir() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
name := file.Name()
|
||||||
|
if strings.HasSuffix(name, "_test.go") || strings.HasSuffix(name, ".gen.go") ||
|
||||||
|
!strings.HasSuffix(name, ".go") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
name = strings.TrimSuffix(name, ".go")
|
||||||
|
|
||||||
|
services = append(services, srv{
|
||||||
|
CamelName: lo.PascalCase(name),
|
||||||
|
ServiceName: lo.CamelCase(name),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成 services.gen.go 文件,使用 text/template 渲染 services.go.tpl 模板
|
||||||
|
if err := renderTemplateFS(templates.Services, "services/services.go.tpl", services, filepath.Join(path, "services.gen.go"), true); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// renderTemplateFS 使用 text/template 渲染模板并写入目标文件
|
||||||
|
func renderTemplateFS(fs embed.FS, tplName string, data interface{}, targetPath string, overwrite bool) error {
|
||||||
|
tplContent, err := fs.ReadFile(tplName)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
tpl, err := template.New(tplName).Parse(string(tplContent))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !overwrite {
|
||||||
|
if _, err := os.Stat(targetPath); err == nil {
|
||||||
|
return nil // 文件已存在且不覆盖
|
||||||
|
}
|
||||||
|
}
|
||||||
|
f, err := os.Create(targetPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
return tpl.Execute(f, data)
|
||||||
|
}
|
||||||
34
templates/services/services.go.tpl
Normal file
34
templates/services/services.go.tpl
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package services
|
||||||
|
|
||||||
|
import (
|
||||||
|
"gorm.io/gorm"
|
||||||
|
)
|
||||||
|
|
||||||
|
var _db *gorm.DB
|
||||||
|
|
||||||
|
// exported CamelCase Services
|
||||||
|
var (
|
||||||
|
{{- range . }}
|
||||||
|
{{ .CamelName }} *{{ .ServiceName }}
|
||||||
|
{{- end }}
|
||||||
|
)
|
||||||
|
|
||||||
|
// @provider(model)
|
||||||
|
type services struct {
|
||||||
|
db *gorm.DB
|
||||||
|
// define Services
|
||||||
|
{{- range . }}
|
||||||
|
{{ .ServiceName }} *{{ .ServiceName }}
|
||||||
|
{{- end }}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (svc *services) Prepare() error {
|
||||||
|
_db = svc.db
|
||||||
|
|
||||||
|
// set exported Services here
|
||||||
|
{{- range . }}
|
||||||
|
{{ .CamelName }} = svc.{{ .ServiceName }}
|
||||||
|
{{- end }}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -16,3 +16,6 @@ var Events embed.FS
|
|||||||
|
|
||||||
//go:embed jobs
|
//go:embed jobs
|
||||||
var Jobs embed.FS
|
var Jobs embed.FS
|
||||||
|
|
||||||
|
//go:embed services
|
||||||
|
var Services embed.FS
|
||||||
|
|||||||
Reference in New Issue
Block a user