🎨 移除多余模块

This commit is contained in:
2026-04-08 12:19:24 +08:00
parent 22bb5fdc94
commit 7599146f24
192 changed files with 623 additions and 13983 deletions

View File

@@ -3,7 +3,6 @@ package ast
import (
"bytes"
"fmt"
"github.com/flipped-aurora/gin-vue-admin/server/global"
"go/ast"
"go/parser"
"go/printer"
@@ -22,7 +21,7 @@ func RollGormBack(pk, model string) {
// 首先分析存在多少个ttt作为调用方的node块
// 如果多个 仅仅删除对应块即可
// 如果单个 那么还需要剔除import
path := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "initialize", "gorm_biz.go")
path := filepath.Join(resolveServerRoot(), "initialize", "gorm_biz.go")
src, err := os.ReadFile(path)
if err != nil {
fmt.Println(err)
@@ -99,7 +98,7 @@ func RollRouterBack(pk, model string) {
// 首先抓到所有的代码块结构 {}
// 分析结构中是否存在一个变量叫做 pk+Router
// 然后获取到代码块指针 对内部需要回滚的代码进行剔除
path := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "initialize", "router_biz.go")
path := filepath.Join(resolveServerRoot(), "initialize", "router_biz.go")
src, err := os.ReadFile(path)
if err != nil {
fmt.Println(err)

View File

@@ -20,12 +20,6 @@ func (r Type) Group() string {
return "RouterGroup"
case TypePackageServiceModuleEnter:
return "ServiceGroup"
case TypePluginApiEnter:
return "api"
case TypePluginRouterEnter:
return "router"
case TypePluginServiceEnter:
return "service"
default:
return ""
}
@@ -40,14 +34,4 @@ const (
TypePackageServiceModuleEnter = "PackageServiceModuleEnter" // server/service/{package}/enter.go
TypePackageInitializeGorm = "PackageInitializeGorm" // server/initialize/gorm_biz.go
TypePackageInitializeRouter = "PackageInitializeRouter" // server/initialize/router_biz.go
TypePluginGen = "PluginGen" // server/plugin/{package}/gen/main.go
TypePluginApiEnter = "PluginApiEnter" // server/plugin/{package}/enter.go
TypePluginInitializeV1 = "PluginInitializeV1" // server/initialize/plugin_biz_v1.go
TypePluginInitializeV2 = "PluginInitializeV2" // server/plugin/register.go
TypePluginRouterEnter = "PluginRouterEnter" // server/plugin/{package}/enter.go
TypePluginServiceEnter = "PluginServiceEnter" // server/plugin/{package}/enter.go
TypePluginInitializeApi = "PluginInitializeApi" // server/plugin/{package}/initialize/api.go
TypePluginInitializeGorm = "PluginInitializeGorm" // server/plugin/{package}/initialize/gorm.go
TypePluginInitializeMenu = "PluginInitializeMenu" // server/plugin/{package}/initialize/menu.go
TypePluginInitializeRouter = "PluginInitializeRouter" // server/plugin/{package}/initialize/router.go
)

View File

@@ -1,7 +1,6 @@
package ast
import (
"github.com/flipped-aurora/gin-vue-admin/server/global"
"github.com/pkg/errors"
"go/ast"
"go/format"
@@ -61,7 +60,7 @@ func (a *Base) Format(filename string, writer io.Writer, file *ast.File) error {
// RelativePath 绝对路径转相对路径
func (a *Base) RelativePath(filePath string) string {
server := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server)
server := resolveServerRoot()
hasServer := strings.Index(filePath, server)
if hasServer != -1 {
filePath = strings.TrimPrefix(filePath, server)
@@ -73,9 +72,30 @@ func (a *Base) RelativePath(filePath string) string {
// AbsolutePath 相对路径转绝对路径
func (a *Base) AbsolutePath(filePath string) string {
server := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server)
server := resolveServerRoot()
keys := strings.Split(filePath, "/")
filePath = filepath.Join(keys...)
filePath = filepath.Join(server, filePath)
return filePath
}
func resolveProjectRoot() string {
if cwd, err := os.Getwd(); err == nil {
if filepath.Base(cwd) == "server" {
return filepath.Dir(cwd)
}
if _, err := os.Stat(filepath.Join(cwd, "server")); err == nil {
return cwd
}
return cwd
}
return "."
}
func resolveServerRoot() string {
root := resolveProjectRoot()
if filepath.Base(root) == "server" {
return root
}
return filepath.Join(root, "server")
}

View File

@@ -1,167 +0,0 @@
package ast
import (
"go/ast"
"go/token"
"io"
)
// PluginEnter 插件化入口
// ModuleName := PackageName.GroupName.ServiceName
type PluginEnter struct {
Base
Type Type // 类型
Path string // 文件路径
ImportPath string // 导包路径
RelativePath string // 相对路径
StructName string // 结构体名称
StructCamelName string // 结构体小驼峰名称
ModuleName string // 模块名称
GroupName string // 分组名称
PackageName string // 包名
ServiceName string // 服务名称
}
func (a *PluginEnter) 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 *PluginEnter) Rollback(file *ast.File) error {
//回滚结构体内内容
var structType *ast.StructType
ast.Inspect(file, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.TypeSpec:
if s, ok := x.Type.(*ast.StructType); ok {
structType = s
for i, field := range x.Type.(*ast.StructType).Fields.List {
if len(field.Names) > 0 && field.Names[0].Name == a.StructName {
s.Fields.List = append(s.Fields.List[:i], s.Fields.List[i+1:]...)
return false
}
}
}
}
return true
})
if len(structType.Fields.List) == 0 {
_ = NewImport(a.ImportPath).Rollback(file)
}
if a.Type == TypePluginServiceEnter {
return nil
}
//回滚变量内容
ast.Inspect(file, func(n ast.Node) bool {
genDecl, ok := n.(*ast.GenDecl)
if ok && genDecl.Tok == token.VAR {
for i, spec := range genDecl.Specs {
valueSpec, vsok := spec.(*ast.ValueSpec)
if vsok {
for _, name := range valueSpec.Names {
if name.Name == a.ModuleName {
genDecl.Specs = append(genDecl.Specs[:i], genDecl.Specs[i+1:]...)
return false
}
}
}
}
}
return true
})
return nil
}
func (a *PluginEnter) Injection(file *ast.File) error {
_ = NewImport(a.ImportPath).Injection(file)
has := false
hasVar := false
var firstStruct *ast.StructType
var varSpec *ast.GenDecl
//寻找是否存在结构且定位
ast.Inspect(file, func(n ast.Node) bool {
switch x := n.(type) {
case *ast.TypeSpec:
if s, ok := x.Type.(*ast.StructType); ok {
firstStruct = s
for _, field := range x.Type.(*ast.StructType).Fields.List {
if len(field.Names) > 0 && field.Names[0].Name == a.StructName {
has = true
return false
}
}
}
}
return true
})
if !has {
field := &ast.Field{
Names: []*ast.Ident{{Name: a.StructName}},
Type: &ast.Ident{Name: a.StructCamelName},
}
firstStruct.Fields.List = append(firstStruct.Fields.List, field)
}
if a.Type == TypePluginServiceEnter {
return nil
}
//寻找是否存在变量且定位
ast.Inspect(file, func(n ast.Node) bool {
genDecl, ok := n.(*ast.GenDecl)
if ok && genDecl.Tok == token.VAR {
for _, spec := range genDecl.Specs {
valueSpec, vsok := spec.(*ast.ValueSpec)
if vsok {
varSpec = genDecl
for _, name := range valueSpec.Names {
if name.Name == a.ModuleName {
hasVar = true
return false
}
}
}
}
}
return true
})
if !hasVar {
spec := &ast.ValueSpec{
Names: []*ast.Ident{{Name: a.ModuleName}},
Values: []ast.Expr{
&ast.SelectorExpr{
X: &ast.SelectorExpr{
X: &ast.Ident{Name: a.PackageName},
Sel: &ast.Ident{Name: a.GroupName},
},
Sel: &ast.Ident{Name: a.ServiceName},
},
},
}
varSpec.Specs = append(varSpec.Specs, spec)
}
return nil
}
func (a *PluginEnter) Format(filename string, writer io.Writer, file *ast.File) error {
if filename == "" {
filename = a.Path
}
return a.Base.Format(filename, writer, file)
}

View File

@@ -1,189 +0,0 @@
package ast
import (
"go/ast"
"go/token"
"io"
)
type PluginGen struct {
Base
Type Type // 类型
Path string // 文件路径
ImportPath string // 导包路径
RelativePath string // 相对路径
StructName string // 结构体名称
PackageName string // 包名
IsNew bool // 是否使用new关键字
}
func (a *PluginGen) 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 *PluginGen) 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 {
if v4.Sel.Name != "ApplyBasic" {
continue
}
for k := 0; k < len(v3.Args); k++ {
v5, o5 := v3.Args[k].(*ast.CallExpr)
if o5 {
v6, o6 := v5.Fun.(*ast.Ident)
if o6 {
if v6.Name != "new" {
continue
}
for l := 0; l < len(v5.Args); l++ {
v7, o7 := v5.Args[l].(*ast.SelectorExpr)
if o7 {
v8, o8 := v7.X.(*ast.Ident)
if o8 {
if v8.Name == a.PackageName && v7.Sel.Name == a.StructName {
v3.Args = append(v3.Args[:k], v3.Args[k+1:]...)
continue
}
}
}
}
}
}
if k >= len(v3.Args) {
break
}
v6, o6 := v3.Args[k].(*ast.CompositeLit)
if o6 {
v7, o7 := v6.Type.(*ast.SelectorExpr)
if o7 {
v8, o8 := v7.X.(*ast.Ident)
if o8 {
if v8.Name == a.PackageName && v7.Sel.Name == a.StructName {
v3.Args = append(v3.Args[:k], v3.Args[k+1:]...)
continue
}
}
}
}
}
if len(v3.Args) == 0 {
_ = NewImport(a.ImportPath).Rollback(file)
}
}
}
}
}
}
}
return nil
}
func (a *PluginGen) 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 {
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 {
if v4.Sel.Name != "ApplyBasic" {
continue
}
var has bool
for k := 0; k < len(v3.Args); k++ {
v5, o5 := v3.Args[k].(*ast.CallExpr)
if o5 {
v6, o6 := v5.Fun.(*ast.Ident)
if o6 {
if v6.Name != "new" {
continue
}
for l := 0; l < len(v5.Args); l++ {
v7, o7 := v5.Args[l].(*ast.SelectorExpr)
if o7 {
v8, o8 := v7.X.(*ast.Ident)
if o8 {
if v8.Name == a.PackageName && v7.Sel.Name == a.StructName {
has = true
break
}
}
}
}
}
}
v6, o6 := v3.Args[k].(*ast.CompositeLit)
if o6 {
v7, o7 := v6.Type.(*ast.SelectorExpr)
if o7 {
v8, o8 := v7.X.(*ast.Ident)
if o8 {
if v8.Name == a.PackageName && v7.Sel.Name == a.StructName {
has = true
break
}
}
}
}
}
if !has {
if a.IsNew {
arg := &ast.CallExpr{
Fun: &ast.Ident{Name: "\n\t\tnew"},
Args: []ast.Expr{
&ast.SelectorExpr{
X: &ast.Ident{Name: a.PackageName},
Sel: &ast.Ident{Name: a.StructName},
},
},
}
v3.Args = append(v3.Args, arg)
v3.Args = append(v3.Args, &ast.BasicLit{
Kind: token.STRING,
Value: "\n",
})
break
}
arg := &ast.CompositeLit{
Type: &ast.SelectorExpr{
X: &ast.Ident{Name: a.PackageName},
Sel: &ast.Ident{Name: a.StructName},
},
}
v3.Args = append(v3.Args, arg)
}
}
}
}
}
}
}
return nil
}
func (a *PluginGen) Format(filename string, writer io.Writer, file *ast.File) error {
if filename == "" {
filename = a.Path
}
return a.Base.Format(filename, writer, file)
}

View File

@@ -1,111 +0,0 @@
package ast
import (
"go/ast"
"io"
)
type PluginInitializeGorm struct {
Base
Type Type // 类型
Path string // 文件路径
ImportPath string // 导包路径
RelativePath string // 相对路径
StructName string // 结构体名称
PackageName string // 包名
IsNew bool // 是否使用new关键字 true: new(PackageName.StructName) false: &PackageName.StructName{}
}
func (a *PluginInitializeGorm) 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 *PluginInitializeGorm) Rollback(file *ast.File) error {
var needRollBackImport bool
ast.Inspect(file, func(n ast.Node) bool {
callExpr, ok := n.(*ast.CallExpr)
if !ok {
return true
}
selExpr, seok := callExpr.Fun.(*ast.SelectorExpr)
if !seok || selExpr.Sel.Name != "AutoMigrate" {
return true
}
if len(callExpr.Args) <= 1 {
needRollBackImport = true
}
// 删除指定的参数
for i, arg := range callExpr.Args {
compLit, cok := arg.(*ast.CompositeLit)
if !cok {
continue
}
cselExpr, sok := compLit.Type.(*ast.SelectorExpr)
if !sok {
continue
}
ident, idok := cselExpr.X.(*ast.Ident)
if idok && ident.Name == a.PackageName && cselExpr.Sel.Name == a.StructName {
// 删除参数
callExpr.Args = append(callExpr.Args[:i], callExpr.Args[i+1:]...)
break
}
}
return true
})
if needRollBackImport {
_ = NewImport(a.ImportPath).Rollback(file)
}
return nil
}
func (a *PluginInitializeGorm) Injection(file *ast.File) error {
_ = NewImport(a.ImportPath).Injection(file)
var call *ast.CallExpr
ast.Inspect(file, func(n ast.Node) bool {
callExpr, ok := n.(*ast.CallExpr)
if !ok {
return true
}
selExpr, ok := callExpr.Fun.(*ast.SelectorExpr)
if ok && selExpr.Sel.Name == "AutoMigrate" {
call = callExpr
return false
}
return true
})
arg := &ast.CompositeLit{
Type: &ast.SelectorExpr{
X: &ast.Ident{Name: a.PackageName},
Sel: &ast.Ident{Name: a.StructName},
},
}
call.Args = append(call.Args, arg)
return nil
}
func (a *PluginInitializeGorm) Format(filename string, writer io.Writer, file *ast.File) error {
if filename == "" {
filename = a.Path
}
return a.Base.Format(filename, writer, file)
}

View File

@@ -1,124 +0,0 @@
package ast
import (
"fmt"
"go/ast"
"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 {
funcDecl := FindFunction(file, "Router")
delI := 0
routerNum := 0
for i := len(funcDecl.Body.List) - 1; i >= 0; i-- {
stmt, ok := funcDecl.Body.List[i].(*ast.ExprStmt)
if !ok {
continue
}
callExpr, ok := stmt.X.(*ast.CallExpr)
if !ok {
continue
}
selExpr, ok := callExpr.Fun.(*ast.SelectorExpr)
if !ok {
continue
}
ident, ok := selExpr.X.(*ast.SelectorExpr)
if ok {
if iExpr, ieok := ident.X.(*ast.SelectorExpr); ieok {
if iden, idok := iExpr.X.(*ast.Ident); idok {
if iden.Name == "router" {
routerNum++
}
}
}
if ident.Sel.Name == a.GroupName && selExpr.Sel.Name == a.FunctionName {
// 删除语句
delI = i
}
}
}
funcDecl.Body.List = append(funcDecl.Body.List[:delI], funcDecl.Body.List[delI+1:]...)
if routerNum <= 1 {
_ = NewImport(a.ImportPath).Rollback(file)
}
return nil
}
func (a *PluginInitializeRouter) Injection(file *ast.File) error {
_ = NewImport(a.ImportPath).Injection(file)
funcDecl := FindFunction(file, "Router")
var exists bool
ast.Inspect(funcDecl, func(n ast.Node) bool {
callExpr, ok := n.(*ast.CallExpr)
if !ok {
return true
}
selExpr, ok := callExpr.Fun.(*ast.SelectorExpr)
if !ok {
return true
}
ident, ok := selExpr.X.(*ast.SelectorExpr)
if ok && ident.Sel.Name == a.GroupName && selExpr.Sel.Name == a.FunctionName {
exists = true
return false
}
return true
})
if !exists {
stmtStr := fmt.Sprintf("%s.%s.%s.%s(%s, %s)", a.PackageName, a.AppName, a.GroupName, a.FunctionName, a.LeftRouterGroupName, a.RightRouterGroupName)
stmt := CreateStmt(stmtStr)
funcDecl.Body.List = append(funcDecl.Body.List, stmt)
}
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)
}

View File

@@ -1,82 +0,0 @@
package ast
import (
"go/ast"
"go/token"
"io"
"strconv"
"strings"
)
type PluginInitializeV2 struct {
Base
Type Type // 类型
Path string // 文件路径
PluginPath string // 插件路径
RelativePath string // 相对路径
ImportPath string // 导包路径
StructName string // 结构体名称
PackageName string // 包名
}
func (a *PluginInitializeV2) Parse(filename string, writer io.Writer) (file *ast.File, err error) {
if filename == "" {
if a.RelativePath == "" {
filename = a.PluginPath
a.RelativePath = a.Base.RelativePath(a.PluginPath)
return a.Base.Parse(filename, writer)
}
a.PluginPath = a.Base.AbsolutePath(a.RelativePath)
filename = a.PluginPath
}
return a.Base.Parse(filename, writer)
}
func (a *PluginInitializeV2) Injection(file *ast.File) error {
importPath := strings.TrimSpace(a.ImportPath)
if importPath == "" {
return nil
}
importPath = strings.Trim(importPath, "\"")
if importPath == "" || CheckImport(file, importPath) {
return nil
}
importSpec := &ast.ImportSpec{
Name: ast.NewIdent("_"),
Path: &ast.BasicLit{Kind: token.STRING, Value: strconv.Quote(importPath)},
}
var importDecl *ast.GenDecl
for _, decl := range file.Decls {
genDecl, ok := decl.(*ast.GenDecl)
if !ok {
continue
}
if genDecl.Tok == token.IMPORT {
importDecl = genDecl
break
}
}
if importDecl == nil {
file.Decls = append([]ast.Decl{
&ast.GenDecl{
Tok: token.IMPORT,
Specs: []ast.Spec{importSpec},
},
}, file.Decls...)
return nil
}
importDecl.Specs = append(importDecl.Specs, importSpec)
return nil
}
func (a *PluginInitializeV2) Rollback(file *ast.File) error {
return nil
}
func (a *PluginInitializeV2) Format(filename string, writer io.Writer, file *ast.File) error {
if filename == "" {
filename = a.PluginPath
}
return a.Base.Format(filename, writer, file)
}