文件结构调整,支持插件自动化 (#1824)
* Update index.vue 多个详情页之间切换tab,页面没有重新渲染 * feature:新增同步API功能 * feature: 同步表数据 * feature:新增同步API功能 * feature: 增加V2插件注册 * feature:给Enter的依赖结构增加单独的New 方便引用 * feature: 调整关联属性的选择模式 * feature: 增加component组件和name的映射插件,防止keepalive的懒加载失效。 * update: PluginInitializeRouter && 修复TypePluginInitializeMenu ast 类型错误 * update: 测试文件的astType 类型错误 * feature: 文件变更自动同步componentName.json。 * feature: 文件变更自动同步componentName.json。 * feat: UI美化 * feat: 自动化页面顺序调整 * feature:修改404页面 * update: PluginInitializeMenu * update: Plugin template * fixed systemApi 重复声明 * api.vue:update:修改API分组为下拉列表 * update: import添加注释 * update: plugin_enter_test.go 增加测试用例 * update: ast 预览文件路径 * update: config Autocode 新增Module字段以及如果为空的情况下自动获取运行目录下的go.mod文件 * update: auto_code_package.go 完善调用ast工具类的封装使用 * update: auto_code_template.go Create方法和修正SysAutoCodeHistory * feat:调整自动化package为模板,增加初始化配置信息,调整页面信息。 * update: ast PreviewPath MkdirAll * update: ast type错误, PluginEnter and PackageModuleEnter add TemplatePath模版路径 * update: autoCodePackage and autoCodeTemplate bug修正 * update: PackageInitializeRouter 传入两个路由组 * update: PackageModuleEnter 处理空变量时与type冲突注入 * update: Package 模版更新 * update: utils/ast 优化统一 * update: 注入内容修复错误 * fix: 修复注释错误 * update: plugin 模版 完成 * feature: 文件watch功能只在development下开启 * update: viper.go.template 因为viper不区分配置的key的大小写所以用package * update: ast 测试代码规范化 * update: package 删除api和router多余导包 * update: plugin template * update: auto_code_package 问题修复 * update: ast 测试插件的预览功能 * update: gorm_biz 更新注册方式 * update: go.mod tidy * remove: plugin template gen main.go.template * update: ast 重构, 分离读取和写入步骤支持 * update: AutoCodePackageApi 传入参数错误修复 * rename: sys_autocode_history.go => sys_auto_code_history.go * update: 预览无需落盘, 创建落盘,抽离公共参数 * update: api.go.tpl 导包位置fmt 和package js位置存放错误 * update: 测试用例修复 and PackageInitializeGorm 重构 * update: ast 新增相对路径, 代码生成器历史回滚功能 * update: ast 工具类回滚失败修复以及测试文件 * update: 代码生成器历史 回滚问题修复 * update: 代码生成器模版忽略.DS_Store * featute: 自动化GORM结构的注入和剔除 * feature: 插件模板调整 * feature: 增加公告插件示例,调整代码模板。 * feature: 自动注册插件V2。 --------- Co-authored-by: zayn <972858472@qq.com> Co-authored-by: SliverHorn <sliver_horn@qq.com> Co-authored-by: krank <emosick@qq.com> Co-authored-by: cjk <wlicjk@126.com> Co-authored-by: piexlMax(奇淼 <qimiaojiangjizhao@gmail.com> Co-authored-by: maxwell <zhong.maxwell@gmail.com>
This commit is contained in:
579
server/service/system/auto_code_package.go
Normal file
579
server/service/system/auto_code_package.go
Normal file
@@ -0,0 +1,579 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
||||
common "github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
|
||||
model "github.com/flipped-aurora/gin-vue-admin/server/model/system"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/utils/ast"
|
||||
"github.com/pkg/errors"
|
||||
"go/token"
|
||||
"gorm.io/gorm"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
var AutoCodePackage = new(autoCodePackage)
|
||||
|
||||
type autoCodePackage struct{}
|
||||
|
||||
// Create 创建包信息
|
||||
// @author: [piexlmax](https://github.com/piexlmax)
|
||||
// @author: [SliverHorn](https://github.com/SliverHorn)
|
||||
func (s *autoCodePackage) Create(ctx context.Context, info *request.SysAutoCodePackageCreate) error {
|
||||
switch {
|
||||
case info.Template == "":
|
||||
return errors.New("模板不能为空!")
|
||||
case info.Template == "page":
|
||||
return errors.New("page为表单生成器!")
|
||||
case info.PackageName == "":
|
||||
return errors.New("PackageName不能为空!")
|
||||
case token.IsKeyword(info.PackageName):
|
||||
return errors.Errorf("%s为go的关键字!", info.PackageName)
|
||||
case info.Template == "package":
|
||||
if info.PackageName == "system" || info.PackageName == "example" {
|
||||
return errors.New("不能使用已保留的package name")
|
||||
}
|
||||
default:
|
||||
break
|
||||
}
|
||||
if !errors.Is(global.GVA_DB.Where("package_name = ? and template = ?", info.PackageName, info.Template).First(&model.SysAutoCodePackage{}).Error, gorm.ErrRecordNotFound) {
|
||||
return errors.New("存在相同PackageName")
|
||||
}
|
||||
create := info.Create()
|
||||
return global.GVA_DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
err := tx.Create(&create).Error
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "创建失败!")
|
||||
}
|
||||
code := info.AutoCode()
|
||||
_, asts, creates, err := s.templates(ctx, create, code)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for key, value := range creates { // key 为 模版绝对路径
|
||||
var files *template.Template
|
||||
files, err = template.ParseFiles(key)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "[filepath:%s]读取模版文件失败!", key)
|
||||
}
|
||||
err = os.MkdirAll(filepath.Dir(value), os.ModePerm)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "[filepath:%s]创建文件夹失败!", value)
|
||||
}
|
||||
var file *os.File
|
||||
file, err = os.Create(value)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "[filepath:%s]创建文件夹失败!", value)
|
||||
}
|
||||
err = files.Execute(file, code)
|
||||
_ = file.Close()
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "[filepath:%s]生成失败!", value)
|
||||
}
|
||||
fmt.Printf("[template:%s][filepath:%s]生成成功!\n", key, value)
|
||||
}
|
||||
for key, value := range asts {
|
||||
keys := strings.Split(key, "=>")
|
||||
if len(keys) == 2 {
|
||||
if keys[1] == ast.TypePluginInitializeV2 {
|
||||
file, _ := value.Parse("", nil)
|
||||
if file != nil {
|
||||
err = value.Injection(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = value.Format("", nil, file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
fmt.Printf("[type:%s]注入成功!\n", key)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// Delete 删除包记录
|
||||
// @author: [piexlmax](https://github.com/piexlmax)
|
||||
// @author: [SliverHorn](https://github.com/SliverHorn)
|
||||
func (s *autoCodePackage) Delete(ctx context.Context, info common.GetById) error {
|
||||
err := global.GVA_DB.WithContext(ctx).Delete(&model.SysAutoCodePackage{}, info.Uint()).Error
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "删除失败!")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// All 获取所有包
|
||||
// @author: [piexlmax](https://github.com/piexlmax)
|
||||
// @author: [SliverHorn](https://github.com/SliverHorn)
|
||||
func (s *autoCodePackage) All(ctx context.Context) (entities []model.SysAutoCodePackage, err error) {
|
||||
err = global.GVA_DB.WithContext(ctx).Find(&entities).Error
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "获取所有包失败!")
|
||||
}
|
||||
return entities, nil
|
||||
}
|
||||
|
||||
// Templates 获取所有模版文件夹
|
||||
// @author: [SliverHorn](https://github.com/SliverHorn)
|
||||
func (s *autoCodePackage) Templates(ctx context.Context) ([]string, error) {
|
||||
templates := make([]string, 0)
|
||||
entries, err := os.ReadDir("resource")
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "读取模版文件夹失败!")
|
||||
}
|
||||
for i := 0; i < len(entries); i++ {
|
||||
if entries[i].IsDir() {
|
||||
if entries[i].Name() == "page" {
|
||||
continue
|
||||
} // page 为表单生成器
|
||||
if entries[i].Name() == "preview" {
|
||||
continue
|
||||
} // preview 为预览代码生成器的代码
|
||||
templates = append(templates, entries[i].Name())
|
||||
}
|
||||
}
|
||||
return templates, nil
|
||||
}
|
||||
|
||||
func (s *autoCodePackage) templates(ctx context.Context, entity model.SysAutoCodePackage, info request.AutoCode) (code map[string]string, asts map[string]ast.Ast, creates map[string]string, err error) {
|
||||
code = make(map[string]string)
|
||||
asts = make(map[string]ast.Ast)
|
||||
creates = make(map[string]string)
|
||||
templateDir := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "resource", entity.Template)
|
||||
templateDirs, err := os.ReadDir(templateDir)
|
||||
if err != nil {
|
||||
return nil, nil, nil, errors.Wrapf(err, "读取模版文件夹[%s]失败!", templateDir)
|
||||
}
|
||||
for i := 0; i < len(templateDirs); i++ {
|
||||
second := filepath.Join(templateDir, templateDirs[i].Name())
|
||||
switch templateDirs[i].Name() {
|
||||
case "server":
|
||||
var secondDirs []os.DirEntry
|
||||
secondDirs, err = os.ReadDir(second)
|
||||
if err != nil {
|
||||
return nil, nil, nil, errors.Wrapf(err, "读取模版文件夹[%s]失败!", second)
|
||||
}
|
||||
for j := 0; j < len(secondDirs); j++ {
|
||||
if secondDirs[j].Name() == ".DS_Store" {
|
||||
continue
|
||||
}
|
||||
three := filepath.Join(second, secondDirs[j].Name())
|
||||
if !secondDirs[j].IsDir() {
|
||||
ext := filepath.Ext(secondDirs[j].Name())
|
||||
if ext != ".template" && ext != ".tpl" {
|
||||
return nil, nil, nil, errors.Errorf("[filpath:%s]非法模版后缀!", three)
|
||||
}
|
||||
name := strings.TrimSuffix(secondDirs[j].Name(), ext)
|
||||
if name == "main.go" || name == "plugin.go" {
|
||||
pluginInitialize := &ast.PluginInitializeV2{
|
||||
Type: ast.TypePluginInitializeV2,
|
||||
Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", entity.PackageName, name),
|
||||
PluginPath: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "initialize", "plugin_biz_v2.go"),
|
||||
ImportPath: fmt.Sprintf(`"%s/plugin/%s"`, global.GVA_CONFIG.AutoCode.Module, entity.PackageName),
|
||||
PackageName: entity.PackageName,
|
||||
}
|
||||
asts[pluginInitialize.PluginPath+"=>"+pluginInitialize.Type.String()] = pluginInitialize
|
||||
creates[three] = pluginInitialize.Path
|
||||
continue
|
||||
}
|
||||
return nil, nil, nil, errors.Errorf("[filpath:%s]非法模版文件!", three)
|
||||
}
|
||||
switch secondDirs[j].Name() {
|
||||
case "api", "router", "service":
|
||||
var threeDirs []os.DirEntry
|
||||
threeDirs, err = os.ReadDir(three)
|
||||
if err != nil {
|
||||
return nil, nil, nil, errors.Wrapf(err, "读取模版文件夹[%s]失败!", three)
|
||||
}
|
||||
for k := 0; k < len(threeDirs); k++ {
|
||||
if threeDirs[k].Name() == ".DS_Store" {
|
||||
continue
|
||||
}
|
||||
four := filepath.Join(three, threeDirs[k].Name())
|
||||
if threeDirs[k].IsDir() {
|
||||
return nil, nil, nil, errors.Errorf("[filpath:%s]非法模版文件夹!", four)
|
||||
}
|
||||
ext := filepath.Ext(four)
|
||||
if ext != ".template" && ext != ".tpl" {
|
||||
return nil, nil, nil, errors.Errorf("[filpath:%s]非法模版后缀!", four)
|
||||
}
|
||||
api := strings.Index(threeDirs[k].Name(), "api")
|
||||
hasEnter := strings.Index(threeDirs[k].Name(), "enter")
|
||||
router := strings.Index(threeDirs[k].Name(), "router")
|
||||
service := strings.Index(threeDirs[k].Name(), "service")
|
||||
if router == -1 && api == -1 && service == -1 && hasEnter == -1 {
|
||||
return nil, nil, nil, errors.Errorf("[filpath:%s]非法模版文件!", four)
|
||||
}
|
||||
if entity.Template == "package" {
|
||||
create := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, secondDirs[j].Name(), entity.PackageName, info.HumpPackageName+".go")
|
||||
if api != -1 {
|
||||
create = filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, secondDirs[j].Name(), "v1", entity.PackageName, info.HumpPackageName+".go")
|
||||
}
|
||||
if hasEnter != -1 {
|
||||
isApi := strings.Index(secondDirs[j].Name(), "api")
|
||||
isRouter := strings.Index(secondDirs[j].Name(), "router")
|
||||
isService := strings.Index(secondDirs[j].Name(), "service")
|
||||
if isApi != -1 {
|
||||
packageApiEnter := &ast.PackageEnter{
|
||||
Type: ast.TypePackageApiEnter,
|
||||
Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, secondDirs[j].Name(), "v1", "enter.go"),
|
||||
ImportPath: fmt.Sprintf(`"%s/%s/%s/%s"`, global.GVA_CONFIG.AutoCode.Module, "api", "v1", entity.PackageName),
|
||||
StructName: info.PackageT + "ApiGroup",
|
||||
PackageName: entity.PackageName,
|
||||
PackageStructName: "ApiGroup",
|
||||
}
|
||||
asts[packageApiEnter.Path+"=>"+packageApiEnter.Type.String()] = packageApiEnter
|
||||
packageApiModuleEnter := &ast.PackageModuleEnter{
|
||||
Type: ast.TypePackageApiModuleEnter,
|
||||
Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, secondDirs[j].Name(), "v1", entity.PackageName, "enter.go"),
|
||||
ImportPath: fmt.Sprintf(`"%s/service"`, global.GVA_CONFIG.AutoCode.Module),
|
||||
StructName: info.StructName + "Api",
|
||||
AppName: "ServiceGroupApp",
|
||||
GroupName: info.PackageT + "ServiceGroup",
|
||||
ModuleName: info.Abbreviation + "Service",
|
||||
PackageName: "service",
|
||||
ServiceName: info.StructName + "Service",
|
||||
}
|
||||
asts[packageApiModuleEnter.Path+"=>"+packageApiModuleEnter.Type.String()] = packageApiModuleEnter
|
||||
creates[four] = packageApiModuleEnter.Path
|
||||
}
|
||||
if isRouter != -1 {
|
||||
packageRouterEnter := &ast.PackageEnter{
|
||||
Type: ast.TypePackageRouterEnter,
|
||||
Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, secondDirs[j].Name(), "enter.go"),
|
||||
ImportPath: fmt.Sprintf(`"%s/%s/%s"`, global.GVA_CONFIG.AutoCode.Module, secondDirs[j].Name(), entity.PackageName),
|
||||
StructName: info.PackageT,
|
||||
PackageName: entity.PackageName,
|
||||
PackageStructName: "RouterGroup",
|
||||
}
|
||||
asts[packageRouterEnter.Path+"=>"+packageRouterEnter.Type.String()] = packageRouterEnter
|
||||
packageRouterModuleEnter := &ast.PackageModuleEnter{
|
||||
Type: ast.TypePackageRouterModuleEnter,
|
||||
Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, secondDirs[j].Name(), entity.PackageName, "enter.go"),
|
||||
ImportPath: fmt.Sprintf(`api "%s/api/v1"`, global.GVA_CONFIG.AutoCode.Module),
|
||||
StructName: info.StructName + "Router",
|
||||
AppName: "ApiGroupApp",
|
||||
GroupName: info.PackageT + "ApiGroup",
|
||||
ModuleName: info.Abbreviation + "Api",
|
||||
PackageName: "api",
|
||||
ServiceName: info.StructName + "Api",
|
||||
}
|
||||
creates[four] = packageRouterModuleEnter.Path
|
||||
asts[packageRouterModuleEnter.Path+"=>"+packageRouterModuleEnter.Type.String()] = packageRouterModuleEnter
|
||||
packageInitializeRouter := &ast.PackageInitializeRouter{
|
||||
Type: ast.TypePackageInitializeRouter,
|
||||
Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "initialize", "router_biz.go"),
|
||||
ImportPath: fmt.Sprintf(`"%s/router"`, global.GVA_CONFIG.AutoCode.Module),
|
||||
AppName: "RouterGroupApp",
|
||||
GroupName: info.PackageT,
|
||||
ModuleName: entity.PackageName + "Router",
|
||||
PackageName: "router",
|
||||
FunctionName: "Init" + info.StructName + "Router",
|
||||
LeftRouterGroupName: "privateGroup",
|
||||
RightRouterGroupName: "publicGroup",
|
||||
}
|
||||
asts[packageInitializeRouter.Path+"=>"+packageInitializeRouter.Type.String()] = packageInitializeRouter
|
||||
}
|
||||
if isService != -1 {
|
||||
path := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, secondDirs[j].Name(), strings.TrimSuffix(threeDirs[k].Name(), ext))
|
||||
importPath := fmt.Sprintf(`"%s/service/%s"`, global.GVA_CONFIG.AutoCode.Module, entity.PackageName)
|
||||
packageServiceEnter := &ast.PackageEnter{
|
||||
Type: ast.TypePackageServiceEnter,
|
||||
Path: path,
|
||||
ImportPath: importPath,
|
||||
StructName: info.PackageT + "ServiceGroup",
|
||||
PackageName: entity.PackageName,
|
||||
PackageStructName: "ServiceGroup",
|
||||
}
|
||||
asts[packageServiceEnter.Path+"=>"+packageServiceEnter.Type.String()] = packageServiceEnter
|
||||
packageServiceModuleEnter := &ast.PackageModuleEnter{
|
||||
Type: ast.TypePackageServiceModuleEnter,
|
||||
Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, secondDirs[j].Name(), entity.PackageName, "enter.go"),
|
||||
StructName: info.StructName + "Service",
|
||||
}
|
||||
asts[packageServiceModuleEnter.Path+"=>"+packageServiceModuleEnter.Type.String()] = packageServiceModuleEnter
|
||||
creates[four] = packageServiceModuleEnter.Path
|
||||
}
|
||||
continue
|
||||
}
|
||||
code[four] = create
|
||||
continue
|
||||
}
|
||||
if hasEnter != -1 {
|
||||
isApi := strings.Index(secondDirs[j].Name(), "api")
|
||||
isRouter := strings.Index(secondDirs[j].Name(), "router")
|
||||
isService := strings.Index(secondDirs[j].Name(), "service")
|
||||
if isRouter != -1 {
|
||||
pluginRouterEnter := &ast.PluginEnter{
|
||||
Type: ast.TypePluginRouterEnter,
|
||||
Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", entity.PackageName, secondDirs[j].Name(), strings.TrimSuffix(threeDirs[k].Name(), ext)),
|
||||
ImportPath: fmt.Sprintf(`"%s/plugin/%s/api"`, global.GVA_CONFIG.AutoCode.Module, entity.PackageName),
|
||||
StructName: info.StructName,
|
||||
StructCamelName: info.Abbreviation,
|
||||
ModuleName: "api" + info.StructName,
|
||||
GroupName: "Api",
|
||||
PackageName: "api",
|
||||
ServiceName: info.StructName,
|
||||
}
|
||||
asts[pluginRouterEnter.Path+"=>"+pluginRouterEnter.Type.String()] = pluginRouterEnter
|
||||
creates[four] = pluginRouterEnter.Path
|
||||
}
|
||||
if isApi != -1 {
|
||||
pluginApiEnter := &ast.PluginEnter{
|
||||
Type: ast.TypePluginApiEnter,
|
||||
Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", entity.PackageName, secondDirs[j].Name(), strings.TrimSuffix(threeDirs[k].Name(), ext)),
|
||||
ImportPath: fmt.Sprintf(`"%s/plugin/%s/service"`, global.GVA_CONFIG.AutoCode.Module, entity.PackageName),
|
||||
StructName: info.StructName,
|
||||
StructCamelName: info.Abbreviation,
|
||||
ModuleName: "service" + info.StructName,
|
||||
GroupName: "Service",
|
||||
PackageName: "service",
|
||||
ServiceName: info.StructName,
|
||||
}
|
||||
asts[pluginApiEnter.Path+"=>"+pluginApiEnter.Type.String()] = pluginApiEnter
|
||||
creates[four] = pluginApiEnter.Path
|
||||
}
|
||||
if isService != -1 {
|
||||
pluginServiceEnter := &ast.PluginEnter{
|
||||
Type: ast.TypePluginServiceEnter,
|
||||
Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", entity.PackageName, secondDirs[j].Name(), strings.TrimSuffix(threeDirs[k].Name(), ext)),
|
||||
StructName: info.StructName,
|
||||
StructCamelName: info.Abbreviation,
|
||||
}
|
||||
asts[pluginServiceEnter.Path+"=>"+pluginServiceEnter.Type.String()] = pluginServiceEnter
|
||||
creates[four] = pluginServiceEnter.Path
|
||||
}
|
||||
continue
|
||||
} // enter.go
|
||||
create := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", entity.PackageName, secondDirs[j].Name(), info.HumpPackageName+".go")
|
||||
code[four] = create
|
||||
}
|
||||
case "gen", "config", "initialize", "plugin", "response":
|
||||
if entity.Template == "package" {
|
||||
continue
|
||||
} // package模板不需要生成gen, config, initialize
|
||||
var threeDirs []os.DirEntry
|
||||
threeDirs, err = os.ReadDir(three)
|
||||
if err != nil {
|
||||
return nil, nil, nil, errors.Wrapf(err, "读取模版文件夹[%s]失败!", three)
|
||||
}
|
||||
for k := 0; k < len(threeDirs); k++ {
|
||||
if threeDirs[k].Name() == ".DS_Store" {
|
||||
continue
|
||||
}
|
||||
four := filepath.Join(three, threeDirs[k].Name())
|
||||
if threeDirs[k].IsDir() {
|
||||
return nil, nil, nil, errors.Errorf("[filpath:%s]非法模版文件夹!", four)
|
||||
}
|
||||
ext := filepath.Ext(four)
|
||||
if ext != ".template" && ext != ".tpl" {
|
||||
return nil, nil, nil, errors.Errorf("[filpath:%s]非法模版后缀!", four)
|
||||
}
|
||||
gen := strings.Index(threeDirs[k].Name(), "gen")
|
||||
api := strings.Index(threeDirs[k].Name(), "api")
|
||||
menu := strings.Index(threeDirs[k].Name(), "menu")
|
||||
viper := strings.Index(threeDirs[k].Name(), "viper")
|
||||
plugin := strings.Index(threeDirs[k].Name(), "plugin")
|
||||
config := strings.Index(threeDirs[k].Name(), "config")
|
||||
router := strings.Index(threeDirs[k].Name(), "router")
|
||||
hasGorm := strings.Index(threeDirs[k].Name(), "gorm")
|
||||
response := strings.Index(threeDirs[k].Name(), "response")
|
||||
if gen != -1 && api != -1 && menu != -1 && viper != -1 && plugin != -1 && config != -1 && router != -1 && hasGorm != -1 && response != -1 {
|
||||
return nil, nil, nil, errors.Errorf("[filpath:%s]非法模版文件!", four)
|
||||
}
|
||||
if api != -1 || menu != -1 || viper != -1 || response != -1 || plugin != -1 || config != -1 {
|
||||
creates[four] = filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", entity.PackageName, secondDirs[j].Name(), strings.TrimSuffix(threeDirs[k].Name(), ext))
|
||||
}
|
||||
if gen != -1 {
|
||||
pluginGen := &ast.PluginGen{
|
||||
Type: ast.TypePluginGen,
|
||||
Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", entity.PackageName, secondDirs[j].Name(), strings.TrimSuffix(threeDirs[k].Name(), ext)),
|
||||
ImportPath: fmt.Sprintf(`"%s/plugin/%s/model"`, global.GVA_CONFIG.AutoCode.Module, entity.PackageName),
|
||||
StructName: info.StructName,
|
||||
PackageName: "model",
|
||||
IsNew: true,
|
||||
}
|
||||
asts[pluginGen.Path+"=>"+pluginGen.Type.String()] = pluginGen
|
||||
creates[four] = pluginGen.Path
|
||||
}
|
||||
if hasGorm != -1 {
|
||||
pluginInitializeGorm := &ast.PluginInitializeGorm{
|
||||
Type: ast.TypePluginInitializeGorm,
|
||||
Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", entity.PackageName, secondDirs[j].Name(), strings.TrimSuffix(threeDirs[k].Name(), ext)),
|
||||
ImportPath: fmt.Sprintf(`"%s/plugin/%s/model"`, global.GVA_CONFIG.AutoCode.Module, entity.PackageName),
|
||||
StructName: info.StructName,
|
||||
PackageName: "model",
|
||||
IsNew: true,
|
||||
}
|
||||
asts[pluginInitializeGorm.Path+"=>"+pluginInitializeGorm.Type.String()] = pluginInitializeGorm
|
||||
creates[four] = pluginInitializeGorm.Path
|
||||
}
|
||||
if router != -1 {
|
||||
pluginInitializeRouter := &ast.PluginInitializeRouter{
|
||||
Type: ast.TypePluginInitializeRouter,
|
||||
Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", entity.PackageName, secondDirs[j].Name(), strings.TrimSuffix(threeDirs[k].Name(), ext)),
|
||||
ImportPath: fmt.Sprintf(`"%s/plugin/%s/router"`, global.GVA_CONFIG.AutoCode.Module, entity.PackageName),
|
||||
AppName: "Router",
|
||||
GroupName: info.StructName,
|
||||
PackageName: "router",
|
||||
FunctionName: "Init",
|
||||
LeftRouterGroupName: "public",
|
||||
RightRouterGroupName: "private",
|
||||
}
|
||||
asts[pluginInitializeRouter.Path+"=>"+pluginInitializeRouter.Type.String()] = pluginInitializeRouter
|
||||
creates[four] = pluginInitializeRouter.Path
|
||||
}
|
||||
}
|
||||
case "model":
|
||||
var threeDirs []os.DirEntry
|
||||
threeDirs, err = os.ReadDir(three)
|
||||
if err != nil {
|
||||
return nil, nil, nil, errors.Wrapf(err, "读取模版文件夹[%s]失败!", three)
|
||||
}
|
||||
for k := 0; k < len(threeDirs); k++ {
|
||||
if threeDirs[k].Name() == ".DS_Store" {
|
||||
continue
|
||||
}
|
||||
four := filepath.Join(three, threeDirs[k].Name())
|
||||
if threeDirs[k].IsDir() {
|
||||
var fourDirs []os.DirEntry
|
||||
fourDirs, err = os.ReadDir(four)
|
||||
if err != nil {
|
||||
return nil, nil, nil, errors.Wrapf(err, "读取模版文件夹[%s]失败!", four)
|
||||
}
|
||||
for l := 0; l < len(fourDirs); l++ {
|
||||
if fourDirs[l].Name() == ".DS_Store" {
|
||||
continue
|
||||
}
|
||||
five := filepath.Join(four, fourDirs[l].Name())
|
||||
if fourDirs[l].IsDir() {
|
||||
return nil, nil, nil, errors.Errorf("[filpath:%s]非法模版文件夹!", five)
|
||||
}
|
||||
ext := filepath.Ext(five)
|
||||
if ext != ".template" && ext != ".tpl" {
|
||||
return nil, nil, nil, errors.Errorf("[filpath:%s]非法模版后缀!", five)
|
||||
}
|
||||
hasRequest := strings.Index(fourDirs[l].Name(), "request")
|
||||
if hasRequest == -1 {
|
||||
return nil, nil, nil, errors.Errorf("[filpath:%s]非法模版文件!", five)
|
||||
}
|
||||
create := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", entity.PackageName, secondDirs[j].Name(), threeDirs[k].Name(), info.HumpPackageName+".go")
|
||||
if entity.Template == "package" {
|
||||
create = filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, secondDirs[j].Name(), entity.PackageName, threeDirs[k].Name(), info.HumpPackageName+".go")
|
||||
}
|
||||
code[five] = create
|
||||
}
|
||||
continue
|
||||
}
|
||||
ext := filepath.Ext(threeDirs[k].Name())
|
||||
if ext != ".template" && ext != ".tpl" {
|
||||
return nil, nil, nil, errors.Errorf("[filpath:%s]非法模版后缀!", four)
|
||||
}
|
||||
hasModel := strings.Index(threeDirs[k].Name(), "model")
|
||||
if hasModel == -1 {
|
||||
return nil, nil, nil, errors.Errorf("[filpath:%s]非法模版文件!", four)
|
||||
}
|
||||
create := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", entity.PackageName, secondDirs[j].Name(), info.HumpPackageName+".go")
|
||||
if entity.Template == "package" {
|
||||
packageInitializeGorm := &ast.PackageInitializeGorm{
|
||||
Type: ast.TypePackageInitializeGorm,
|
||||
Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "initialize", "gorm_biz.go"),
|
||||
ImportPath: fmt.Sprintf(`"%s/model/%s"`, global.GVA_CONFIG.AutoCode.Module, entity.PackageName),
|
||||
Business: info.BusinessDB,
|
||||
StructName: info.StructName,
|
||||
PackageName: entity.PackageName,
|
||||
IsNew: true,
|
||||
}
|
||||
code[four] = packageInitializeGorm.Path
|
||||
asts[packageInitializeGorm.Path+"=>"+packageInitializeGorm.Type.String()] = packageInitializeGorm
|
||||
create = filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, secondDirs[j].Name(), entity.PackageName, info.HumpPackageName+".go")
|
||||
}
|
||||
code[four] = create
|
||||
}
|
||||
default:
|
||||
return nil, nil, nil, errors.Errorf("[filpath:%s]非法模版文件夹!", three)
|
||||
}
|
||||
}
|
||||
case "web":
|
||||
var secondDirs []os.DirEntry
|
||||
secondDirs, err = os.ReadDir(second)
|
||||
if err != nil {
|
||||
return nil, nil, nil, errors.Wrapf(err, "读取模版文件夹[%s]失败!", second)
|
||||
}
|
||||
for j := 0; j < len(secondDirs); j++ {
|
||||
if secondDirs[j].Name() == ".DS_Store" {
|
||||
continue
|
||||
}
|
||||
three := filepath.Join(second, secondDirs[j].Name())
|
||||
if !secondDirs[j].IsDir() {
|
||||
return nil, nil, nil, errors.Errorf("[filpath:%s]非法模版文件!", three)
|
||||
}
|
||||
switch secondDirs[j].Name() {
|
||||
case "api", "form", "view", "table":
|
||||
var threeDirs []os.DirEntry
|
||||
threeDirs, err = os.ReadDir(three)
|
||||
if err != nil {
|
||||
return nil, nil, nil, errors.Wrapf(err, "读取模版文件夹[%s]失败!", three)
|
||||
}
|
||||
for k := 0; k < len(threeDirs); k++ {
|
||||
if threeDirs[k].Name() == ".DS_Store" {
|
||||
continue
|
||||
}
|
||||
four := filepath.Join(three, threeDirs[k].Name())
|
||||
if threeDirs[k].IsDir() {
|
||||
return nil, nil, nil, errors.Errorf("[filpath:%s]非法模版文件夹!", four)
|
||||
}
|
||||
ext := filepath.Ext(four)
|
||||
if ext != ".template" && ext != ".tpl" {
|
||||
return nil, nil, nil, errors.Errorf("[filpath:%s]非法模版后缀!", four)
|
||||
}
|
||||
api := strings.Index(threeDirs[k].Name(), "api")
|
||||
form := strings.Index(threeDirs[k].Name(), "form")
|
||||
view := strings.Index(threeDirs[k].Name(), "view")
|
||||
table := strings.Index(threeDirs[k].Name(), "table")
|
||||
if api == -1 && form == -1 && view == -1 && table == -1 {
|
||||
return nil, nil, nil, errors.Errorf("[filpath:%s]非法模版文件!", four)
|
||||
}
|
||||
if entity.Template == "package" {
|
||||
if view != -1 || table != -1 {
|
||||
formPath := filepath.Join(three, "form.vue"+ext)
|
||||
value, ok := code[formPath]
|
||||
if ok {
|
||||
value = filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.WebRoot(), secondDirs[j].Name(), entity.PackageName, info.PackageName, info.Abbreviation+"Form"+filepath.Ext(strings.TrimSuffix(threeDirs[k].Name(), ext)))
|
||||
code[formPath] = value
|
||||
}
|
||||
}
|
||||
create := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.WebRoot(), secondDirs[j].Name(), entity.PackageName, info.PackageName, info.Abbreviation+filepath.Ext(strings.TrimSuffix(threeDirs[k].Name(), ext)))
|
||||
if api != -1 {
|
||||
create = filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.WebRoot(), secondDirs[j].Name(), entity.PackageName, info.Abbreviation+filepath.Ext(strings.TrimSuffix(threeDirs[k].Name(), ext)))
|
||||
}
|
||||
code[four] = create
|
||||
continue
|
||||
}
|
||||
create := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.WebRoot(), "plugin", entity.PackageName, secondDirs[j].Name(), info.Abbreviation+filepath.Ext(strings.TrimSuffix(threeDirs[k].Name(), ext)))
|
||||
code[four] = create
|
||||
}
|
||||
default:
|
||||
return nil, nil, nil, errors.Errorf("[filpath:%s]非法模版文件夹!", three)
|
||||
}
|
||||
}
|
||||
case "readme.txt.tpl", "readme.txt.template":
|
||||
continue
|
||||
default:
|
||||
if templateDirs[i].Name() == ".DS_Store" {
|
||||
continue
|
||||
}
|
||||
return nil, nil, nil, errors.Errorf("[filpath:%s]非法模版文件!", second)
|
||||
}
|
||||
}
|
||||
return code, asts, creates, nil
|
||||
}
|
Reference in New Issue
Block a user