From fa9957c43c3dc758034f1c35a3ca277a43e54dd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?piexlMax=28=E5=A5=87=E6=B7=BC?= Date: Thu, 5 Feb 2026 14:08:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=B0=83=E6=95=B4=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8C=96=E6=8F=92=E4=BB=B6=E7=9A=84=E6=B3=A8=E5=86=8C=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/service/system/auto_code_package.go | 2 +- server/utils/ast/ast_type.go | 2 +- server/utils/ast/plugin_initialize_v2.go | 37 +++++++++++++++++++ server/utils/ast/plugin_initialize_v2_test.go | 16 ++++---- 4 files changed, 47 insertions(+), 10 deletions(-) diff --git a/server/service/system/auto_code_package.go b/server/service/system/auto_code_package.go index c8b7bb03a..763c56fa4 100644 --- a/server/service/system/auto_code_package.go +++ b/server/service/system/auto_code_package.go @@ -337,7 +337,7 @@ func (s *autoCodePackage) templates(ctx context.Context, entity model.SysAutoCod 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"), + PluginPath: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "register.go"), ImportPath: fmt.Sprintf(`"%s/plugin/%s"`, global.GVA_CONFIG.AutoCode.Module, entity.PackageName), PackageName: entity.PackageName, } diff --git a/server/utils/ast/ast_type.go b/server/utils/ast/ast_type.go index c4e905eda..43285c910 100644 --- a/server/utils/ast/ast_type.go +++ b/server/utils/ast/ast_type.go @@ -43,7 +43,7 @@ const ( 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/initialize/plugin_biz_v2.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 diff --git a/server/utils/ast/plugin_initialize_v2.go b/server/utils/ast/plugin_initialize_v2.go index a85e48574..974f51307 100644 --- a/server/utils/ast/plugin_initialize_v2.go +++ b/server/utils/ast/plugin_initialize_v2.go @@ -2,7 +2,10 @@ package ast import ( "go/ast" + "go/token" "io" + "strconv" + "strings" ) type PluginInitializeV2 struct { @@ -30,6 +33,40 @@ func (a *PluginInitializeV2) Parse(filename string, writer io.Writer) (file *ast } 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 } diff --git a/server/utils/ast/plugin_initialize_v2_test.go b/server/utils/ast/plugin_initialize_v2_test.go index 4e99c6dae..ceb2a7438 100644 --- a/server/utils/ast/plugin_initialize_v2_test.go +++ b/server/utils/ast/plugin_initialize_v2_test.go @@ -22,8 +22,8 @@ func TestPluginInitialize_Injection(t *testing.T) { name: "测试 Gva插件 注册注入", fields: fields{ Type: TypePluginInitializeV2, - Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "initialize", "plugin_biz_v2.go"), - PluginPath: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "gva", "plugin.go"), + Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "gva", "plugin.go"), + PluginPath: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "register.go"), ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/plugin/gva"`, }, wantErr: false, @@ -37,12 +37,12 @@ func TestPluginInitialize_Injection(t *testing.T) { PluginPath: tt.fields.PluginPath, ImportPath: tt.fields.ImportPath, } - file, err := a.Parse(a.Path, nil) + file, err := a.Parse("", nil) if err != nil { t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr) } a.Injection(file) - err = a.Format(a.Path, nil, file) + err = a.Format("", nil, file) if (err != nil) != tt.wantErr { t.Errorf("Injection() error = %v, wantErr %v", err, tt.wantErr) } @@ -69,8 +69,8 @@ func TestPluginInitialize_Rollback(t *testing.T) { name: "测试 Gva插件 回滚", fields: fields{ Type: TypePluginInitializeV2, - Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "initialize", "plugin_biz_v2.go"), - PluginPath: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "gva", "plugin.go"), + Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "gva", "plugin.go"), + PluginPath: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "register.go"), ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/plugin/gva"`, }, wantErr: false, @@ -86,12 +86,12 @@ func TestPluginInitialize_Rollback(t *testing.T) { StructName: "Plugin", PackageName: "gva", } - file, err := a.Parse(a.Path, nil) + file, err := a.Parse("", nil) if err != nil { t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr) } a.Rollback(file) - err = a.Format(a.Path, nil, file) + err = a.Format("", nil, file) if (err != nil) != tt.wantErr { t.Errorf("Rollback() error = %v, wantErr %v", err, tt.wantErr) }