Skip to content

Commit fa9957c

Browse files
author
piexlMax(奇淼
committed
feat: 调整自动化插件的注册逻辑
1 parent 00224ef commit fa9957c

File tree

4 files changed

+47
-10
lines changed

4 files changed

+47
-10
lines changed

server/service/system/auto_code_package.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ func (s *autoCodePackage) templates(ctx context.Context, entity model.SysAutoCod
337337
pluginInitialize := &ast.PluginInitializeV2{
338338
Type: ast.TypePluginInitializeV2,
339339
Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", entity.PackageName, name),
340-
PluginPath: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "initialize", "plugin_biz_v2.go"),
340+
PluginPath: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "register.go"),
341341
ImportPath: fmt.Sprintf(`"%s/plugin/%s"`, global.GVA_CONFIG.AutoCode.Module, entity.PackageName),
342342
PackageName: entity.PackageName,
343343
}

server/utils/ast/ast_type.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ const (
4343
TypePluginGen = "PluginGen" // server/plugin/{package}/gen/main.go
4444
TypePluginApiEnter = "PluginApiEnter" // server/plugin/{package}/enter.go
4545
TypePluginInitializeV1 = "PluginInitializeV1" // server/initialize/plugin_biz_v1.go
46-
TypePluginInitializeV2 = "PluginInitializeV2" // server/initialize/plugin_biz_v2.go
46+
TypePluginInitializeV2 = "PluginInitializeV2" // server/plugin/register.go
4747
TypePluginRouterEnter = "PluginRouterEnter" // server/plugin/{package}/enter.go
4848
TypePluginServiceEnter = "PluginServiceEnter" // server/plugin/{package}/enter.go
4949
TypePluginInitializeApi = "PluginInitializeApi" // server/plugin/{package}/initialize/api.go

server/utils/ast/plugin_initialize_v2.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package ast
22

33
import (
44
"go/ast"
5+
"go/token"
56
"io"
7+
"strconv"
8+
"strings"
69
)
710

811
type PluginInitializeV2 struct {
@@ -30,6 +33,40 @@ func (a *PluginInitializeV2) Parse(filename string, writer io.Writer) (file *ast
3033
}
3134

3235
func (a *PluginInitializeV2) Injection(file *ast.File) error {
36+
importPath := strings.TrimSpace(a.ImportPath)
37+
if importPath == "" {
38+
return nil
39+
}
40+
importPath = strings.Trim(importPath, "\"")
41+
if importPath == "" || CheckImport(file, importPath) {
42+
return nil
43+
}
44+
45+
importSpec := &ast.ImportSpec{
46+
Name: ast.NewIdent("_"),
47+
Path: &ast.BasicLit{Kind: token.STRING, Value: strconv.Quote(importPath)},
48+
}
49+
var importDecl *ast.GenDecl
50+
for _, decl := range file.Decls {
51+
genDecl, ok := decl.(*ast.GenDecl)
52+
if !ok {
53+
continue
54+
}
55+
if genDecl.Tok == token.IMPORT {
56+
importDecl = genDecl
57+
break
58+
}
59+
}
60+
if importDecl == nil {
61+
file.Decls = append([]ast.Decl{
62+
&ast.GenDecl{
63+
Tok: token.IMPORT,
64+
Specs: []ast.Spec{importSpec},
65+
},
66+
}, file.Decls...)
67+
return nil
68+
}
69+
importDecl.Specs = append(importDecl.Specs, importSpec)
3370
return nil
3471
}
3572

server/utils/ast/plugin_initialize_v2_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ func TestPluginInitialize_Injection(t *testing.T) {
2222
name: "测试 Gva插件 注册注入",
2323
fields: fields{
2424
Type: TypePluginInitializeV2,
25-
Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "initialize", "plugin_biz_v2.go"),
26-
PluginPath: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "gva", "plugin.go"),
25+
Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "gva", "plugin.go"),
26+
PluginPath: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "register.go"),
2727
ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/plugin/gva"`,
2828
},
2929
wantErr: false,
@@ -37,12 +37,12 @@ func TestPluginInitialize_Injection(t *testing.T) {
3737
PluginPath: tt.fields.PluginPath,
3838
ImportPath: tt.fields.ImportPath,
3939
}
40-
file, err := a.Parse(a.Path, nil)
40+
file, err := a.Parse("", nil)
4141
if err != nil {
4242
t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
4343
}
4444
a.Injection(file)
45-
err = a.Format(a.Path, nil, file)
45+
err = a.Format("", nil, file)
4646
if (err != nil) != tt.wantErr {
4747
t.Errorf("Injection() error = %v, wantErr %v", err, tt.wantErr)
4848
}
@@ -69,8 +69,8 @@ func TestPluginInitialize_Rollback(t *testing.T) {
6969
name: "测试 Gva插件 回滚",
7070
fields: fields{
7171
Type: TypePluginInitializeV2,
72-
Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "initialize", "plugin_biz_v2.go"),
73-
PluginPath: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "gva", "plugin.go"),
72+
Path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "gva", "plugin.go"),
73+
PluginPath: filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", "register.go"),
7474
ImportPath: `"github.com/flipped-aurora/gin-vue-admin/server/plugin/gva"`,
7575
},
7676
wantErr: false,
@@ -86,12 +86,12 @@ func TestPluginInitialize_Rollback(t *testing.T) {
8686
StructName: "Plugin",
8787
PackageName: "gva",
8888
}
89-
file, err := a.Parse(a.Path, nil)
89+
file, err := a.Parse("", nil)
9090
if err != nil {
9191
t.Errorf("Parse() error = %v, wantErr %v", err, tt.wantErr)
9292
}
9393
a.Rollback(file)
94-
err = a.Format(a.Path, nil, file)
94+
err = a.Format("", nil, file)
9595
if (err != nil) != tt.wantErr {
9696
t.Errorf("Rollback() error = %v, wantErr %v", err, tt.wantErr)
9797
}

0 commit comments

Comments
 (0)