
* 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>
218 lines
7.5 KiB
Go
218 lines
7.5 KiB
Go
package system
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"github.com/flipped-aurora/gin-vue-admin/server/utils/ast"
|
|
"github.com/pkg/errors"
|
|
"path"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"time"
|
|
|
|
"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"
|
|
request "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
|
|
"github.com/flipped-aurora/gin-vue-admin/server/utils"
|
|
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
var AutocodeHistory = new(autoCodeHistory)
|
|
|
|
type autoCodeHistory struct{}
|
|
|
|
// Create 创建代码生成器历史记录
|
|
// Author [SliverHorn](https://github.com/SliverHorn)
|
|
// Author [songzhibin97](https://github.com/songzhibin97)
|
|
func (s *autoCodeHistory) Create(ctx context.Context, info request.SysAutoHistoryCreate) error {
|
|
create := info.Create()
|
|
err := global.GVA_DB.WithContext(ctx).Create(&create).Error
|
|
if err != nil {
|
|
return errors.Wrap(err, "创建失败!")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// First 根据id获取代码生成器历史的数据
|
|
// Author [SliverHorn](https://github.com/SliverHorn)
|
|
// Author [songzhibin97](https://github.com/songzhibin97)
|
|
func (s *autoCodeHistory) First(ctx context.Context, info common.GetById) (string, error) {
|
|
var meta string
|
|
err := global.GVA_DB.WithContext(ctx).Model(model.SysAutoCodeHistory{}).Where("id = ?", info.ID).Pluck("request", &meta).Error
|
|
if err != nil {
|
|
return "", errors.Wrap(err, "获取失败!")
|
|
}
|
|
return meta, nil
|
|
}
|
|
|
|
// Repeat 检测重复
|
|
// Author [SliverHorn](https://github.com/SliverHorn)
|
|
// Author [songzhibin97](https://github.com/songzhibin97)
|
|
func (s *autoCodeHistory) Repeat(businessDB, structName, Package string) bool {
|
|
var count int64
|
|
global.GVA_DB.Model(&model.SysAutoCodeHistory{}).Where("business_db = ? and struct_name = ? and package = ? and flag = 0", businessDB, structName, Package).Count(&count)
|
|
return count > 0
|
|
}
|
|
|
|
// RollBack 回滚
|
|
// Author [SliverHorn](https://github.com/SliverHorn)
|
|
// Author [songzhibin97](https://github.com/songzhibin97)
|
|
func (s *autoCodeHistory) RollBack(ctx context.Context, info request.SysAutoHistoryRollBack) error {
|
|
var history model.SysAutoCodeHistory
|
|
err := global.GVA_DB.Where("id = ?", info.ID).First(&history).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if info.DeleteApi {
|
|
ids := info.ApiIds(history)
|
|
err = ApiServiceApp.DeleteApisByIds(ids)
|
|
if err != nil {
|
|
global.GVA_LOG.Error("ClearTag DeleteApiByIds:", zap.Error(err))
|
|
}
|
|
} // 清除API表
|
|
if info.DeleteMenu {
|
|
err = BaseMenuServiceApp.DeleteBaseMenu(int(history.MenuID))
|
|
if err != nil {
|
|
return errors.Wrap(err, "删除菜单失败!")
|
|
}
|
|
} // 清除菜单表
|
|
if info.DeleteTable {
|
|
err = s.DropTable(history.BusinessDB, history.Table)
|
|
if err != nil {
|
|
return errors.Wrap(err, "删除表失败!")
|
|
}
|
|
} // 删除表
|
|
templates := make(map[string]string, len(history.Templates))
|
|
for key, template := range history.Templates {
|
|
{
|
|
server := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server)
|
|
keys := strings.Split(key, "/")
|
|
key = filepath.Join(keys...)
|
|
key = strings.TrimPrefix(key, server)
|
|
} // key
|
|
{
|
|
web := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.WebRoot())
|
|
server := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server)
|
|
slices := strings.Split(template, "/")
|
|
template = filepath.Join(slices...)
|
|
ext := path.Ext(template)
|
|
switch ext {
|
|
case ".js", ".vue":
|
|
template = filepath.Join(web, template)
|
|
case ".go":
|
|
template = filepath.Join(server, template)
|
|
}
|
|
} // value
|
|
templates[key] = template
|
|
}
|
|
history.Templates = templates
|
|
for key, value := range history.Injections {
|
|
var injection ast.Ast
|
|
switch key {
|
|
case ast.TypePackageApiEnter, ast.TypePackageRouterEnter, ast.TypePackageServiceEnter:
|
|
var entity ast.PackageEnter
|
|
_ = json.Unmarshal([]byte(value), &entity)
|
|
injection = &entity
|
|
case ast.TypePackageApiModuleEnter, ast.TypePackageRouterModuleEnter, ast.TypePackageServiceModuleEnter:
|
|
var entity ast.PackageModuleEnter
|
|
_ = json.Unmarshal([]byte(value), &entity)
|
|
injection = &entity
|
|
case ast.TypePackageInitializeGorm:
|
|
var entity ast.PackageInitializeGorm
|
|
_ = json.Unmarshal([]byte(value), &entity)
|
|
injection = &entity
|
|
case ast.TypePackageInitializeRouter:
|
|
var entity ast.PackageInitializeRouter
|
|
_ = json.Unmarshal([]byte(value), &entity)
|
|
injection = &entity
|
|
case ast.TypePluginGen:
|
|
var entity ast.PluginGen
|
|
_ = json.Unmarshal([]byte(value), &entity)
|
|
injection = &entity
|
|
case ast.TypePluginApiEnter, ast.TypePluginRouterEnter, ast.TypePluginServiceEnter:
|
|
var entity ast.PluginEnter
|
|
_ = json.Unmarshal([]byte(value), &entity)
|
|
injection = &entity
|
|
case ast.TypePluginInitializeV2:
|
|
var entity ast.PluginInitializeV2
|
|
_ = json.Unmarshal([]byte(value), &entity)
|
|
injection = &entity
|
|
case ast.TypePluginInitializeGorm:
|
|
var entity ast.PluginInitializeGorm
|
|
_ = json.Unmarshal([]byte(value), &entity)
|
|
injection = &entity
|
|
case ast.TypePluginInitializeRouter:
|
|
var entity ast.PluginInitializeRouter
|
|
_ = json.Unmarshal([]byte(value), &entity)
|
|
injection = &entity
|
|
}
|
|
if injection == nil {
|
|
continue
|
|
}
|
|
file, _ := injection.Parse("", nil)
|
|
if file != nil {
|
|
_ = injection.Rollback(file)
|
|
err = injection.Format("", nil, file)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Printf("[filepath:%s]回滚注入代码成功!\n", key)
|
|
}
|
|
} // 清除注入代码
|
|
removeBasePath := filepath.Join(global.GVA_CONFIG.AutoCode.Root, "rm_file", strconv.FormatInt(int64(time.Now().Nanosecond()), 10))
|
|
for _, value := range history.Templates {
|
|
if !filepath.IsAbs(value) {
|
|
continue
|
|
}
|
|
removePath := filepath.Join(removeBasePath, strings.TrimPrefix(value, global.GVA_CONFIG.AutoCode.Root))
|
|
err = utils.FileMove(value, removePath)
|
|
if err != nil {
|
|
return errors.Wrapf(err, "[src:%s][dst:%s]文件移动失败!", value, removePath)
|
|
}
|
|
} // 移动文件
|
|
err = global.GVA_DB.WithContext(ctx).Model(&model.SysAutoCodeHistory{}).Where("id = ?", info.ID).Update("flag", 1).Error
|
|
if err != nil {
|
|
return errors.Wrap(err, "更新失败!")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Delete 删除历史数据
|
|
// Author [SliverHorn](https://github.com/SliverHorn)
|
|
// Author [songzhibin97](https://github.com/songzhibin97)
|
|
func (s *autoCodeHistory) Delete(ctx context.Context, info common.GetById) error {
|
|
err := global.GVA_DB.WithContext(ctx).Where("id = ?", info.Uint()).Delete(&model.SysAutoCodeHistory{}).Error
|
|
if err != nil {
|
|
return errors.Wrap(err, "删除失败!")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetList 获取系统历史数据
|
|
// Author [SliverHorn](https://github.com/SliverHorn)
|
|
// Author [songzhibin97](https://github.com/songzhibin97)
|
|
func (s *autoCodeHistory) GetList(ctx context.Context, info common.PageInfo) (list []model.SysAutoCodeHistory, total int64, err error) {
|
|
var entities []model.SysAutoCodeHistory
|
|
db := global.GVA_DB.WithContext(ctx).Model(&model.SysAutoCodeHistory{})
|
|
err = db.Count(&total).Error
|
|
if err != nil {
|
|
return nil, total, err
|
|
}
|
|
err = db.Scopes(info.Paginate()).Order("updated_at desc").Find(&entities).Error
|
|
return entities, total, err
|
|
}
|
|
|
|
// DropTable 获取指定数据库和指定数据表的所有字段名,类型值等
|
|
// @author: [piexlmax](https://github.com/piexlmax)
|
|
func (s *autoCodeHistory) DropTable(BusinessDb, tableName string) error {
|
|
if BusinessDb != "" {
|
|
return global.MustGetGlobalDBByDBName(BusinessDb).Exec("DROP TABLE " + tableName).Error
|
|
} else {
|
|
return global.GVA_DB.Exec("DROP TABLE " + tableName).Error
|
|
}
|
|
}
|