From 095c6ac0bea473ff47c0cc51fd400e2c8cbdefd0 Mon Sep 17 00:00:00 2001 From: songzhibin97 <718428482@qq.com> Date: Sun, 18 Jul 2021 13:13:48 +0800 Subject: [PATCH 1/3] =?UTF-8?q?fix:=E4=BF=AE=E6=AD=A3mac=E6=83=85=E5=86=B5?= =?UTF-8?q?=E4=B8=8B=E7=9A=84=E6=96=87=E4=BB=B6=E5=86=B2=E7=AA=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/service/system/sys_auto_code.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/service/system/sys_auto_code.go b/server/service/system/sys_auto_code.go index ee2eb3c7..ee53fdf0 100644 --- a/server/service/system/sys_auto_code.go +++ b/server/service/system/sys_auto_code.go @@ -19,7 +19,7 @@ import ( ) const ( - autoPath = "autoCode/" + autoPath = "autocode_template/" basePath = "resource/template" ) From e7e46bb6b8f0496d67593909f2caadb9c8f478c8 Mon Sep 17 00:00:00 2001 From: songzhibin97 <718428482@qq.com> Date: Sun, 18 Jul 2021 14:16:26 +0800 Subject: [PATCH 2/3] =?UTF-8?q?fix:=E5=A2=9E=E5=8A=A0=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8C=96=E4=BB=A3=E7=A0=81=E6=8F=92=E5=85=A5=E7=82=B9&?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=B7=AF=E5=BE=84=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/config.yaml | 2 +- server/service/system/sys_auto_code.go | 33 ++++++++++++++++++++++---- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/server/config.yaml b/server/config.yaml index 112b2624..fa0c233d 100644 --- a/server/config.yaml +++ b/server/config.yaml @@ -73,7 +73,7 @@ local: autocode: transfer-restart: true root: "" - server: /server/autocode + server: /server server-api: /api/v1/autocode server-initialize: /initialize server-model: /model/autocode diff --git a/server/service/system/sys_auto_code.go b/server/service/system/sys_auto_code.go index ee53fdf0..31178fdc 100644 --- a/server/service/system/sys_auto_code.go +++ b/server/service/system/sys_auto_code.go @@ -154,6 +154,14 @@ func (autoCodeService *AutoCodeService) CreateTemp(autoCode system.AutoCodeStruc global.GVA_CONFIG.AutoCode.Server, global.GVA_CONFIG.AutoCode.SInitialize, "gorm.go") initializeRouterFilePath := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, global.GVA_CONFIG.AutoCode.SInitialize, "router.go") + initializeApiFilePath := filepath.Join(global.GVA_CONFIG.AutoCode.Root, + global.GVA_CONFIG.AutoCode.Server, global.GVA_CONFIG.AutoCode.SApi, "enter.go") + + initializeAutoRouterFilePath := filepath.Join(global.GVA_CONFIG.AutoCode.Root, + global.GVA_CONFIG.AutoCode.Server, global.GVA_CONFIG.AutoCode.SRouter, "enter.go") + initializeServiceFilePath := filepath.Join(global.GVA_CONFIG.AutoCode.Root, + global.GVA_CONFIG.AutoCode.Server, global.GVA_CONFIG.AutoCode.SService, "enter.go") + err = utils.AutoInjectionCode(initializeGormFilePath, "MysqlTables", "model."+autoCode.StructName+"{},") if err != nil { return err @@ -162,10 +170,28 @@ func (autoCodeService *AutoCodeService) CreateTemp(autoCode system.AutoCodeStruc if err != nil { return err } + err = utils.AutoInjectionCode(initializeApiFilePath, "ApiGroup", autoCode.StructName+"Api") + if err != nil { + return err + } + err = utils.AutoInjectionCode(initializeAutoRouterFilePath, "RouterGroup", autoCode.StructName+"Router") + if err != nil { + return err + } + err = utils.AutoInjectionCode(initializeServiceFilePath, "ServiceGroup", autoCode.StructName+"Service") + if err != nil { + return err + } injectionCodeMeta.WriteString(fmt.Sprintf("%s@%s@%s", initializeGormFilePath, "MysqlTables", "model."+autoCode.StructName+"{},")) injectionCodeMeta.WriteString(";") injectionCodeMeta.WriteString(fmt.Sprintf("%s@%s@%s", initializeRouterFilePath, "Routers", "router.Init"+autoCode.StructName+"Router(PrivateGroup)")) + injectionCodeMeta.WriteString(";") + injectionCodeMeta.WriteString(fmt.Sprintf("%s@%s@%s", initializeApiFilePath, "ApiGroup", autoCode.StructName+"Api")) + injectionCodeMeta.WriteString(";") + injectionCodeMeta.WriteString(fmt.Sprintf("%s@%s@%s", initializeAutoRouterFilePath, "RouterGroup", autoCode.StructName+"Router")) + injectionCodeMeta.WriteString(";") + injectionCodeMeta.WriteString(fmt.Sprintf("%s@%s@%s", initializeServiceFilePath, "ServiceGroup", autoCode.StructName+"Service")) // 保存生成信息 for _, data := range dataList { @@ -176,9 +202,9 @@ func (autoCodeService *AutoCodeService) CreateTemp(autoCode system.AutoCodeStruc } if global.GVA_CONFIG.AutoCode.TransferRestart { - go func() { - _ = utils.Reload() - }() + //go func() { + // _ = utils.Reload() + //}() } //return errors.New("创建代码成功并移动文件成功") } else { // 打包 @@ -414,7 +440,6 @@ func (autoCodeService *AutoCodeService) getNeedList(autoCode *system.AutoCodeStr // 生成文件路径,填充 autoCodePath 字段,readme.txt.tpl不符合规则,需要特殊处理 // resource/template/web/api.js.tpl -> autoCode/web/autoCode.PackageName/api/autoCode.PackageName.js // resource/template/readme.txt.tpl -> autoCode/readme.txt - autoPath := "autoCode/" for index, value := range dataList { trimBase := strings.TrimPrefix(value.locationPath, basePath+"/") if trimBase == "readme.txt.tpl" { From a2cad44d1620c30a75c215dc40e42f07edb3d903 Mon Sep 17 00:00:00 2001 From: songzhibin97 <718428482@qq.com> Date: Sun, 18 Jul 2021 14:53:11 +0800 Subject: [PATCH 3/3] =?UTF-8?q?feat:=E4=BC=98=E5=8C=96=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/service/system/sys_auto_code.go | 109 +++++++++++++++---------- 1 file changed, 64 insertions(+), 45 deletions(-) diff --git a/server/service/system/sys_auto_code.go b/server/service/system/sys_auto_code.go index 31178fdc..0fb7a143 100644 --- a/server/service/system/sys_auto_code.go +++ b/server/service/system/sys_auto_code.go @@ -23,6 +23,52 @@ const ( basePath = "resource/template" ) +var injectionPaths []injectionMeta + +func Init() { + if len(injectionPaths) != 0 { + return + } + injectionPaths = []injectionMeta{ + { + path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, + global.GVA_CONFIG.AutoCode.Server, global.GVA_CONFIG.AutoCode.SInitialize, "gorm.go"), + funcName: "MysqlTables", + structNameF: "autocode.%s{},", + }, + { + path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, + global.GVA_CONFIG.AutoCode.Server, global.GVA_CONFIG.AutoCode.SInitialize, "router.go"), + funcName: "Routers", + structNameF: "autocodeRouter.Init%sRouter(PrivateGroup)", + }, + { + path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, + global.GVA_CONFIG.AutoCode.Server, global.GVA_CONFIG.AutoCode.SApi, "enter.go"), + funcName: "ApiGroup", + structNameF: "%sApi", + }, + { + path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, + global.GVA_CONFIG.AutoCode.Server, global.GVA_CONFIG.AutoCode.SRouter, "enter.go"), + funcName: "RouterGroup", + structNameF: "%sRouter", + }, + { + path: filepath.Join(global.GVA_CONFIG.AutoCode.Root, + global.GVA_CONFIG.AutoCode.Server, global.GVA_CONFIG.AutoCode.SService, "enter.go"), + funcName: "ServiceGroup", + structNameF: "%sService", + }, + } +} + +type injectionMeta struct { + path string + funcName string + structNameF string // 带格式化的 +} + type tplData struct { template *template.Template locationPath string @@ -142,6 +188,7 @@ func (autoCodeService *AutoCodeService) CreateTemp(autoCode system.AutoCodeStruc idBf.WriteString(";") } if autoCode.AutoMoveFile { // 判断是否需要自动转移 + Init() for index := range dataList { autoCodeService.addAutoMoveFile(&dataList[index]) } @@ -150,49 +197,10 @@ func (autoCodeService *AutoCodeService) CreateTemp(autoCode system.AutoCodeStruc return err } } - initializeGormFilePath := filepath.Join(global.GVA_CONFIG.AutoCode.Root, - global.GVA_CONFIG.AutoCode.Server, global.GVA_CONFIG.AutoCode.SInitialize, "gorm.go") - initializeRouterFilePath := filepath.Join(global.GVA_CONFIG.AutoCode.Root, - global.GVA_CONFIG.AutoCode.Server, global.GVA_CONFIG.AutoCode.SInitialize, "router.go") - initializeApiFilePath := filepath.Join(global.GVA_CONFIG.AutoCode.Root, - global.GVA_CONFIG.AutoCode.Server, global.GVA_CONFIG.AutoCode.SApi, "enter.go") - - initializeAutoRouterFilePath := filepath.Join(global.GVA_CONFIG.AutoCode.Root, - global.GVA_CONFIG.AutoCode.Server, global.GVA_CONFIG.AutoCode.SRouter, "enter.go") - initializeServiceFilePath := filepath.Join(global.GVA_CONFIG.AutoCode.Root, - global.GVA_CONFIG.AutoCode.Server, global.GVA_CONFIG.AutoCode.SService, "enter.go") - - err = utils.AutoInjectionCode(initializeGormFilePath, "MysqlTables", "model."+autoCode.StructName+"{},") - if err != nil { - return err - } - err = utils.AutoInjectionCode(initializeRouterFilePath, "Routers", "router.Init"+autoCode.StructName+"Router(PrivateGroup)") - if err != nil { - return err - } - err = utils.AutoInjectionCode(initializeApiFilePath, "ApiGroup", autoCode.StructName+"Api") - if err != nil { - return err - } - err = utils.AutoInjectionCode(initializeAutoRouterFilePath, "RouterGroup", autoCode.StructName+"Router") - if err != nil { - return err - } - err = utils.AutoInjectionCode(initializeServiceFilePath, "ServiceGroup", autoCode.StructName+"Service") + err = injectionCode(autoCode.StructName, &injectionCodeMeta) if err != nil { - return err + return } - - injectionCodeMeta.WriteString(fmt.Sprintf("%s@%s@%s", initializeGormFilePath, "MysqlTables", "model."+autoCode.StructName+"{},")) - injectionCodeMeta.WriteString(";") - injectionCodeMeta.WriteString(fmt.Sprintf("%s@%s@%s", initializeRouterFilePath, "Routers", "router.Init"+autoCode.StructName+"Router(PrivateGroup)")) - injectionCodeMeta.WriteString(";") - injectionCodeMeta.WriteString(fmt.Sprintf("%s@%s@%s", initializeApiFilePath, "ApiGroup", autoCode.StructName+"Api")) - injectionCodeMeta.WriteString(";") - injectionCodeMeta.WriteString(fmt.Sprintf("%s@%s@%s", initializeAutoRouterFilePath, "RouterGroup", autoCode.StructName+"Router")) - injectionCodeMeta.WriteString(";") - injectionCodeMeta.WriteString(fmt.Sprintf("%s@%s@%s", initializeServiceFilePath, "ServiceGroup", autoCode.StructName+"Service")) - // 保存生成信息 for _, data := range dataList { if len(data.autoMoveFilePath) != 0 { @@ -202,11 +210,10 @@ func (autoCodeService *AutoCodeService) CreateTemp(autoCode system.AutoCodeStruc } if global.GVA_CONFIG.AutoCode.TransferRestart { - //go func() { - // _ = utils.Reload() - //}() + go func() { + _ = utils.Reload() + }() } - //return errors.New("创建代码成功并移动文件成功") } else { // 打包 if err = utils.ZipFiles("./ginvueadmin.zip", fileList, ".", "."); err != nil { return err @@ -472,3 +479,15 @@ func (autoCodeService *AutoCodeService) getNeedList(autoCode *system.AutoCodeStr } return dataList, fileList, needMkdir, err } + +// injectionCode 封装代码注入 +func injectionCode(structName string, bf *strings.Builder) error { + for _, meta := range injectionPaths { + code := fmt.Sprintf(meta.structNameF, structName) + if err := utils.AutoInjectionCode(meta.path, meta.funcName, code); err != nil { + return err + } + bf.WriteString(fmt.Sprintf("%s@%s@%s;", meta.path, meta.funcName, code)) + } + return nil +}