1
1
package system
2
2
3
3
import (
4
- "bytes"
5
4
"encoding/json"
6
5
"errors"
7
6
"fmt"
8
- "go/ast"
9
- "go/format"
10
- "go/parser"
11
- "go/token"
7
+ ast2 "github.com/flipped-aurora/gin-vue-admin/server/utils/ast"
8
+ "golang.org/x/text/cases"
9
+ "golang.org/x/text/language"
12
10
"io"
13
- "log"
14
11
"mime/multipart"
15
12
"os"
16
13
"path/filepath"
17
14
"strconv"
18
15
"strings"
19
16
"text/template"
20
17
18
+ "github.com/flipped-aurora/gin-vue-admin/server/resource/autocode_template/subcontract"
21
19
cp "github.com/otiai10/copy"
22
20
"go.uber.org/zap"
23
- "golang.org/x/text/cases"
24
- "golang.org/x/text/language"
25
-
26
- "github.com/flipped-aurora/gin-vue-admin/server/resource/autocode_template/subcontract"
27
21
28
22
"github.com/flipped-aurora/gin-vue-admin/server/global"
29
23
"github.com/flipped-aurora/gin-vue-admin/server/model/system"
@@ -53,23 +47,10 @@ type autoPackage struct {
53
47
var (
54
48
packageInjectionMap map [string ]astInjectionMeta
55
49
injectionPaths []injectionMeta
56
- caser = cases .Title (language .English )
57
50
)
58
51
59
52
func Init (Package string ) {
60
53
injectionPaths = []injectionMeta {
61
- {
62
- path : filepath .Join (global .GVA_CONFIG .AutoCode .Root ,
63
- global .GVA_CONFIG .AutoCode .Server , global .GVA_CONFIG .AutoCode .SInitialize , "gorm.go" ),
64
- funcName : "MysqlTables" ,
65
- structNameF : Package + ".%s{}," ,
66
- },
67
- {
68
- path : filepath .Join (global .GVA_CONFIG .AutoCode .Root ,
69
- global .GVA_CONFIG .AutoCode .Server , global .GVA_CONFIG .AutoCode .SInitialize , "router.go" ),
70
- funcName : "Routers" ,
71
- structNameF : Package + "Router.Init%sRouter(PrivateGroup)" ,
72
- },
73
54
{
74
55
path : filepath .Join (global .GVA_CONFIG .AutoCode .Root ,
75
56
global .GVA_CONFIG .AutoCode .Server , fmt .Sprintf (global .GVA_CONFIG .AutoCode .SApi , Package ), "enter.go" ),
@@ -313,6 +294,21 @@ func (autoCodeService *AutoCodeService) CreateTemp(autoCode system.AutoCodeStruc
313
294
return err
314
295
}
315
296
}
297
+
298
+ {
299
+ // 在gorm.go 注入 自动迁移
300
+ path := filepath .Join (global .GVA_CONFIG .AutoCode .Root ,
301
+ global .GVA_CONFIG .AutoCode .Server , global .GVA_CONFIG .AutoCode .SInitialize , "gorm.go" )
302
+ ast2 .AddRegisterTablesAst (path , "RegisterTables" , autoCode .Package , autoCode .BusinessDB , autoCode .StructName )
303
+ }
304
+
305
+ {
306
+ // router.go 注入 自动迁移
307
+ path := filepath .Join (global .GVA_CONFIG .AutoCode .Root ,
308
+ global .GVA_CONFIG .AutoCode .Server , global .GVA_CONFIG .AutoCode .SInitialize , "router.go" )
309
+ ast2 .AddRouterCode (path , "Routers" , autoCode .Package , autoCode .StructName )
310
+ }
311
+ //给各个enter进行注入
316
312
err = injectionCode (autoCode .StructName , & injectionCodeMeta )
317
313
if err != nil {
318
314
return
@@ -324,15 +320,6 @@ func (autoCodeService *AutoCodeService) CreateTemp(autoCode system.AutoCodeStruc
324
320
bf .WriteString (";" )
325
321
}
326
322
}
327
-
328
- var gormPath = filepath .Join (global .GVA_CONFIG .AutoCode .Root ,
329
- global .GVA_CONFIG .AutoCode .Server , global .GVA_CONFIG .AutoCode .SInitialize , "gorm.go" )
330
- var routePath = filepath .Join (global .GVA_CONFIG .AutoCode .Root ,
331
- global .GVA_CONFIG .AutoCode .Server , global .GVA_CONFIG .AutoCode .SInitialize , "router.go" )
332
- var imporStr = fmt .Sprintf ("github.com/flipped-aurora/gin-vue-admin/server/model/%s" , autoCode .Package )
333
- _ = ImportReference (routePath , "" , "" , autoCode .Package , "" )
334
- _ = ImportReference (gormPath , imporStr , "" , "" , "" )
335
-
336
323
} else { // 打包
337
324
if err = utils .ZipFiles ("./ginvueadmin.zip" , fileList , "." , "." ); err != nil {
338
325
return err
@@ -581,9 +568,7 @@ func (autoCodeService *AutoCodeService) getNeedList(autoCode *system.AutoCodeStr
581
568
func injectionCode (structName string , bf * strings.Builder ) error {
582
569
for _ , meta := range injectionPaths {
583
570
code := fmt .Sprintf (meta .structNameF , structName )
584
- if err := utils .AutoInjectionCode (meta .path , meta .funcName , code ); err != nil {
585
- return err
586
- }
571
+ ast2 .ImportForAutoEnter (meta .path , meta .funcName , code )
587
572
bf .WriteString (fmt .Sprintf ("%s@%s@%s;" , meta .path , meta .funcName , code ))
588
573
}
589
574
return nil
@@ -657,179 +642,13 @@ func (autoCodeService *AutoCodeService) CreatePackageTemp(packageName string) er
657
642
// 创建完成后在对应的位置插入结构代码
658
643
for _ , v := range pendingTemp {
659
644
meta := packageInjectionMap [v .name ]
660
- if err := ImportReference (meta .path , fmt .Sprintf (meta .importCodeF , v .name , packageName ), fmt .Sprintf (meta .structNameF , caser .String (packageName )), fmt .Sprintf (meta .packageNameF , packageName ), meta .groupName ); err != nil {
645
+ if err := ast2 . ImportReference (meta .path , fmt .Sprintf (meta .importCodeF , v .name , packageName ), fmt .Sprintf (meta .structNameF , cases . Title ( language . English ) .String (packageName )), fmt .Sprintf (meta .packageNameF , packageName ), meta .groupName ); err != nil {
661
646
return err
662
647
}
663
648
}
664
649
return nil
665
650
}
666
651
667
- type Visitor struct {
668
- ImportCode string
669
- StructName string
670
- PackageName string
671
- GroupName string
672
- }
673
-
674
- func (vi * Visitor ) Visit (node ast.Node ) ast.Visitor {
675
- switch n := node .(type ) {
676
- case * ast.GenDecl :
677
- // 查找有没有import context包
678
- // Notice:没有考虑没有import任何包的情况
679
- if n .Tok == token .IMPORT && vi .ImportCode != "" {
680
- vi .addImport (n )
681
- // 不需要再遍历子树
682
- return nil
683
- }
684
- if n .Tok == token .TYPE && vi .StructName != "" && vi .PackageName != "" && vi .GroupName != "" {
685
- vi .addStruct (n )
686
- return nil
687
- }
688
- case * ast.FuncDecl :
689
- if n .Name .Name == "Routers" {
690
- vi .addFuncBodyVar (n )
691
- return nil
692
- }
693
-
694
- }
695
- return vi
696
- }
697
-
698
- func (vi * Visitor ) addStruct (genDecl * ast.GenDecl ) ast.Visitor {
699
- for i := range genDecl .Specs {
700
- switch n := genDecl .Specs [i ].(type ) {
701
- case * ast.TypeSpec :
702
- if strings .Index (n .Name .Name , "Group" ) > - 1 {
703
- switch t := n .Type .(type ) {
704
- case * ast.StructType :
705
- f := & ast.Field {
706
- Names : []* ast.Ident {
707
- {
708
- Name : vi .StructName ,
709
- Obj : & ast.Object {
710
- Kind : ast .Var ,
711
- Name : vi .StructName ,
712
- },
713
- },
714
- },
715
- Type : & ast.SelectorExpr {
716
- X : & ast.Ident {
717
- Name : vi .PackageName ,
718
- },
719
- Sel : & ast.Ident {
720
- Name : vi .GroupName ,
721
- },
722
- },
723
- }
724
- t .Fields .List = append (t .Fields .List , f )
725
- }
726
- }
727
- }
728
- }
729
- return vi
730
- }
731
-
732
- func (vi * Visitor ) addImport (genDecl * ast.GenDecl ) ast.Visitor {
733
- // 是否已经import
734
- hasImported := false
735
- for _ , v := range genDecl .Specs {
736
- importSpec := v .(* ast.ImportSpec )
737
- // 如果已经包含
738
- if importSpec .Path .Value == strconv .Quote (vi .ImportCode ) {
739
- hasImported = true
740
- }
741
- }
742
- if ! hasImported {
743
- genDecl .Specs = append (genDecl .Specs , & ast.ImportSpec {
744
- Path : & ast.BasicLit {
745
- Kind : token .STRING ,
746
- Value : strconv .Quote (vi .ImportCode ),
747
- },
748
- })
749
- }
750
- return vi
751
- }
752
-
753
- func (vi * Visitor ) addFuncBodyVar (funDecl * ast.FuncDecl ) ast.Visitor {
754
- hasVar := false
755
- for _ , v := range funDecl .Body .List {
756
- switch varSpec := v .(type ) {
757
- case * ast.AssignStmt :
758
- for i := range varSpec .Lhs {
759
- switch nn := varSpec .Lhs [i ].(type ) {
760
- case * ast.Ident :
761
- if nn .Name == vi .PackageName + "Router" {
762
- hasVar = true
763
- }
764
- }
765
- }
766
- }
767
- }
768
- if ! hasVar {
769
- assignStmt := & ast.AssignStmt {
770
- Lhs : []ast.Expr {
771
- & ast.Ident {
772
- Name : vi .PackageName + "Router" ,
773
- Obj : & ast.Object {
774
- Kind : ast .Var ,
775
- Name : vi .PackageName + "Router" ,
776
- },
777
- },
778
- },
779
- Tok : token .DEFINE ,
780
- Rhs : []ast.Expr {
781
- & ast.SelectorExpr {
782
- X : & ast.SelectorExpr {
783
- X : & ast.Ident {
784
- Name : "router" ,
785
- },
786
- Sel : & ast.Ident {
787
- Name : "RouterGroupApp" ,
788
- },
789
- },
790
- Sel : & ast.Ident {
791
- Name : caser .String (vi .PackageName ),
792
- },
793
- },
794
- },
795
- }
796
- funDecl .Body .List = append (funDecl .Body .List , funDecl .Body .List [1 ])
797
- index := 1
798
- copy (funDecl .Body .List [index + 1 :], funDecl .Body .List [index :])
799
- funDecl .Body .List [index ] = assignStmt
800
- }
801
- return vi
802
- }
803
-
804
- func ImportReference (filepath , importCode , structName , packageName , groupName string ) error {
805
- fSet := token .NewFileSet ()
806
- fParser , err := parser .ParseFile (fSet , filepath , nil , parser .ParseComments )
807
- if err != nil {
808
- return err
809
- }
810
- importCode = strings .TrimSpace (importCode )
811
- v := & Visitor {
812
- ImportCode : importCode ,
813
- StructName : structName ,
814
- PackageName : packageName ,
815
- GroupName : groupName ,
816
- }
817
- if importCode == "" {
818
- ast .Print (fSet , fParser )
819
- }
820
-
821
- ast .Walk (v , fParser )
822
-
823
- var output []byte
824
- buffer := bytes .NewBuffer (output )
825
- err = format .Node (buffer , fSet , fParser )
826
- if err != nil {
827
- log .Fatal (err )
828
- }
829
- // 写回数据
830
- return os .WriteFile (filepath , buffer .Bytes (), 0o600 )
831
- }
832
-
833
652
// CreatePlug 自动创建插件模板
834
653
func (autoCodeService * AutoCodeService ) CreatePlug (plug system.AutoPlugReq ) error {
835
654
// 检查列表参数是否有效
0 commit comments