go项目脚手架
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
2.6 KiB

package main
import (
"bytes"
"fmt"
"os"
"strings"
)
var repoStub = `package repositories
import (
"gorm.io/gorm"
"sorbet/internal/entities"
"sorbet/pkg/db"
)
type {{pascal}}Repository struct {
*db.Repository[entities.{{pascal}}]
}
// New{{pascal}}Repository 创建{{label}}仓库
func New{{pascal}}Repository(orm *gorm.DB) *{{pascal}}Repository {
return &{{pascal}}Repository{
db.NewRepositoryWith[entities.{{pascal}}](orm, "id"),
}
}
`
var labels = map[string]string{
"Company": "公司",
"CompanyDepartment": "公司部门",
"CompanyEmployee": "公司员工",
"Config": "配置",
"ConfigGroup": "配置组",
"Feature": "栏目",
"FeatureCategory": "栏目分类",
"FeatureConfig": "栏目配置",
"FeatureContent": "栏目内容",
"FeatureContentChapter": "栏目内容章回",
"FeatureContentDetail": "栏目内容详情",
"Resource": "资源",
"ResourceCategory": "资源分类",
"SystemLog": "系统日志",
"SystemMenu": "系统菜单",
"SystemPermission": "系统权限",
"SystemRole": "系统用户角色",
"SystemRolePower": "角色授权",
"SystemUser": "系统用户",
}
func main() {
dirs, err := os.ReadDir("../internal/entities")
if err != nil {
panic(err)
}
_ = os.MkdirAll("../internal/repositories", os.ModePerm)
var camels []string
var pascals []string
var createdCount int
for _, dir := range dirs {
name := dir.Name()
if !strings.HasSuffix(name, ".go") {
continue
}
name = strings.TrimSuffix(name, ".go")
parts := strings.Split(name, "_")
for i, part := range parts {
parts[i] = string(append(bytes.ToUpper([]byte{part[0]}), part[1:]...))
}
pascal := strings.Join(parts, "")
camel := string(bytes.ToLower([]byte{pascal[0]})) + pascal[1:]
pascals = append(pascals, pascal)
camels = append(camels, camel)
label, ok := labels[pascal]
if !ok {
fmt.Println("跳过 " + dir.Name())
continue
}
repository := "../internal/repositories/" + dir.Name()
if PathExists(repository) {
fmt.Println("跳过 " + dir.Name())
} else {
code := strings.ReplaceAll(repoStub, "{{pascal}}", pascal)
code = strings.ReplaceAll(code, "{{camel}}", camel)
code = strings.ReplaceAll(code, "{{label}}", label)
_ = os.WriteFile("../internal/repositories/"+dir.Name(), []byte(code), os.ModePerm)
fmt.Println("创建 " + dir.Name())
createdCount++
}
}
}
func PathExists(path string) bool {
_, err := os.Stat(path)
if err == nil {
return true
}
if os.IsNotExist(err) {
return false
}
panic(err)
}