文件结构调整,支持插件自动化 (#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:
249
server/utils/ast/plugin_initialize_router.go
Normal file
249
server/utils/ast/plugin_initialize_router.go
Normal file
@@ -0,0 +1,249 @@
|
||||
package ast
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
||||
"go/ast"
|
||||
"go/token"
|
||||
"io"
|
||||
)
|
||||
|
||||
// PluginInitializeRouter 插件初始化路由
|
||||
// PackageName.AppName.GroupName.FunctionName()
|
||||
type PluginInitializeRouter struct {
|
||||
Base
|
||||
Type Type // 类型
|
||||
Path string // 文件路径
|
||||
ImportPath string // 导包路径
|
||||
ImportGlobalPath string // 导包全局变量路径
|
||||
ImportMiddlewarePath string // 导包中间件路径
|
||||
RelativePath string // 相对路径
|
||||
AppName string // 应用名称
|
||||
GroupName string // 分组名称
|
||||
PackageName string // 包名
|
||||
FunctionName string // 函数名
|
||||
LeftRouterGroupName string // 左路由分组名称
|
||||
RightRouterGroupName string // 右路由分组名称
|
||||
}
|
||||
|
||||
func (a *PluginInitializeRouter) Parse(filename string, writer io.Writer) (file *ast.File, err error) {
|
||||
if filename == "" {
|
||||
if a.RelativePath == "" {
|
||||
filename = a.Path
|
||||
a.RelativePath = a.Base.RelativePath(a.Path)
|
||||
return a.Base.Parse(filename, writer)
|
||||
}
|
||||
a.Path = a.Base.AbsolutePath(a.RelativePath)
|
||||
filename = a.Path
|
||||
}
|
||||
return a.Base.Parse(filename, writer)
|
||||
}
|
||||
|
||||
func (a *PluginInitializeRouter) Rollback(file *ast.File) error {
|
||||
for i := 0; i < len(file.Decls); i++ {
|
||||
v1, o1 := file.Decls[i].(*ast.FuncDecl)
|
||||
if o1 {
|
||||
for j := 0; j < len(v1.Body.List); j++ {
|
||||
v2, o2 := v1.Body.List[j].(*ast.ExprStmt)
|
||||
if o2 {
|
||||
v3, o3 := v2.X.(*ast.CallExpr)
|
||||
if o3 {
|
||||
v4, o4 := v3.Fun.(*ast.SelectorExpr)
|
||||
if o4 {
|
||||
v5, o5 := v4.X.(*ast.SelectorExpr)
|
||||
if o5 {
|
||||
v6, o6 := v5.X.(*ast.SelectorExpr)
|
||||
if o6 {
|
||||
v7, o7 := v6.X.(*ast.Ident)
|
||||
if o7 {
|
||||
if v7.Name == a.PackageName && v6.Sel.Name == a.AppName && v5.Sel.Name == a.GroupName && v4.Sel.Name == a.FunctionName {
|
||||
v1.Body.List = append(v1.Body.List[:j], v1.Body.List[j+1:]...)
|
||||
if len(v1.Body.List) == 3 {
|
||||
if a.ImportMiddlewarePath != "" {
|
||||
_ = NewImport(a.ImportMiddlewarePath).Rollback(file)
|
||||
}
|
||||
if a.ImportGlobalPath != "" {
|
||||
_ = NewImport(a.ImportGlobalPath).Rollback(file)
|
||||
}
|
||||
_ = NewImport(a.ImportPath).Rollback(file)
|
||||
v1.Body.List = nil
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *PluginInitializeRouter) Injection(file *ast.File) error {
|
||||
_ = NewImport(a.ImportPath).Injection(file)
|
||||
for i := 0; i < len(file.Decls); i++ {
|
||||
v1, o1 := file.Decls[i].(*ast.FuncDecl)
|
||||
if o1 {
|
||||
var has bool
|
||||
for j := 0; j < len(v1.Body.List); j++ {
|
||||
v2, o2 := v1.Body.List[j].(*ast.ExprStmt)
|
||||
if o2 {
|
||||
v3, o3 := v2.X.(*ast.CallExpr)
|
||||
if o3 {
|
||||
v4, o4 := v3.Fun.(*ast.SelectorExpr)
|
||||
if o4 {
|
||||
v5, o5 := v4.X.(*ast.SelectorExpr)
|
||||
if o5 {
|
||||
v6, o6 := v5.X.(*ast.SelectorExpr)
|
||||
if o6 {
|
||||
v7, o7 := v6.X.(*ast.Ident)
|
||||
if o7 {
|
||||
if v7.Name == a.PackageName && v6.Sel.Name == a.AppName && v5.Sel.Name == a.GroupName && v4.Sel.Name == a.FunctionName {
|
||||
has = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if !has {
|
||||
if v1.Body == nil {
|
||||
v1.Body = new(ast.BlockStmt)
|
||||
}
|
||||
if v1.Body.List == nil {
|
||||
a.ImportMiddlewarePath = fmt.Sprintf(`"%s/middleware"`, global.GVA_CONFIG.AutoCode.Module)
|
||||
_ = NewImport(a.ImportMiddlewarePath).Injection(file)
|
||||
a.ImportGlobalPath = fmt.Sprintf(`"%s/global"`, global.GVA_CONFIG.AutoCode.Module)
|
||||
_ = NewImport(a.ImportGlobalPath).Injection(file)
|
||||
v1.Body.List = make([]ast.Stmt, 0, 3)
|
||||
public := &ast.AssignStmt{
|
||||
Lhs: []ast.Expr{
|
||||
&ast.Ident{Name: a.LeftRouterGroupName, Obj: ast.NewObj(ast.Var, a.LeftRouterGroupName)},
|
||||
},
|
||||
Tok: token.DEFINE,
|
||||
Rhs: []ast.Expr{
|
||||
&ast.CallExpr{
|
||||
Fun: &ast.SelectorExpr{
|
||||
X: &ast.CallExpr{
|
||||
Fun: &ast.SelectorExpr{
|
||||
X: &ast.Ident{Name: "engine"},
|
||||
Sel: &ast.Ident{Name: "Group"},
|
||||
},
|
||||
Args: []ast.Expr{
|
||||
&ast.BasicLit{
|
||||
Kind: token.STRING,
|
||||
Value: `global.GVA_CONFIG.System.RouterPrefix`,
|
||||
},
|
||||
},
|
||||
},
|
||||
Sel: &ast.Ident{Name: "Group"},
|
||||
},
|
||||
Args: []ast.Expr{
|
||||
&ast.BasicLit{
|
||||
Kind: token.STRING,
|
||||
Value: `""`,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
private := &ast.AssignStmt{
|
||||
Lhs: []ast.Expr{
|
||||
&ast.Ident{Name: a.RightRouterGroupName, Obj: ast.NewObj(ast.Var, a.RightRouterGroupName)},
|
||||
},
|
||||
Tok: token.DEFINE,
|
||||
Rhs: []ast.Expr{
|
||||
&ast.CallExpr{
|
||||
Fun: &ast.SelectorExpr{
|
||||
X: &ast.CallExpr{
|
||||
Fun: &ast.SelectorExpr{
|
||||
X: &ast.Ident{Name: "engine"},
|
||||
Sel: &ast.Ident{Name: "Group"},
|
||||
},
|
||||
Args: []ast.Expr{
|
||||
&ast.BasicLit{
|
||||
Kind: token.STRING,
|
||||
Value: `global.GVA_CONFIG.System.RouterPrefix`,
|
||||
},
|
||||
},
|
||||
},
|
||||
Sel: &ast.Ident{Name: "Group"},
|
||||
},
|
||||
Args: []ast.Expr{
|
||||
&ast.BasicLit{
|
||||
Kind: token.STRING,
|
||||
Value: `""`,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
useMiddleware := &ast.ExprStmt{
|
||||
X: &ast.CallExpr{
|
||||
Fun: &ast.SelectorExpr{
|
||||
X: &ast.CallExpr{
|
||||
Fun: &ast.SelectorExpr{
|
||||
X: &ast.Ident{Name: a.RightRouterGroupName},
|
||||
Sel: &ast.Ident{Name: "Use"},
|
||||
},
|
||||
Args: []ast.Expr{
|
||||
&ast.CallExpr{
|
||||
Fun: &ast.SelectorExpr{
|
||||
X: &ast.Ident{Name: "middleware"},
|
||||
Sel: &ast.Ident{Name: "JWTAuth"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Sel: &ast.Ident{Name: "Use"},
|
||||
},
|
||||
Args: []ast.Expr{
|
||||
&ast.CallExpr{
|
||||
Fun: &ast.SelectorExpr{
|
||||
X: &ast.Ident{Name: "middleware"},
|
||||
Sel: &ast.Ident{Name: "CasbinHandler"},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
v1.Body.List = append(v1.Body.List, public)
|
||||
v1.Body.List = append(v1.Body.List, private)
|
||||
v1.Body.List = append(v1.Body.List, useMiddleware)
|
||||
}
|
||||
body := &ast.ExprStmt{
|
||||
X: &ast.CallExpr{
|
||||
Fun: &ast.SelectorExpr{
|
||||
X: &ast.SelectorExpr{
|
||||
X: &ast.SelectorExpr{
|
||||
X: &ast.Ident{Name: a.PackageName},
|
||||
Sel: &ast.Ident{Name: a.AppName},
|
||||
},
|
||||
Sel: &ast.Ident{Name: a.GroupName},
|
||||
},
|
||||
Sel: &ast.Ident{Name: a.FunctionName},
|
||||
},
|
||||
Args: []ast.Expr{
|
||||
&ast.Ident{Name: a.LeftRouterGroupName},
|
||||
&ast.Ident{Name: a.RightRouterGroupName},
|
||||
},
|
||||
},
|
||||
}
|
||||
v1.Body.List = append(v1.Body.List, body)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *PluginInitializeRouter) Format(filename string, writer io.Writer, file *ast.File) error {
|
||||
if filename == "" {
|
||||
filename = a.Path
|
||||
}
|
||||
return a.Base.Format(filename, writer, file)
|
||||
}
|
Reference in New Issue
Block a user