2
0

auto_code_plugin.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. package system
  2. import (
  3. "bytes"
  4. "context"
  5. "fmt"
  6. "github.com/flipped-aurora/gin-vue-admin/server/global"
  7. "github.com/flipped-aurora/gin-vue-admin/server/model/system"
  8. "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
  9. "github.com/flipped-aurora/gin-vue-admin/server/utils"
  10. "github.com/flipped-aurora/gin-vue-admin/server/utils/ast"
  11. "github.com/mholt/archiver/v4"
  12. cp "github.com/otiai10/copy"
  13. "github.com/pkg/errors"
  14. "go.uber.org/zap"
  15. "go/parser"
  16. "go/printer"
  17. "go/token"
  18. "io"
  19. "mime/multipart"
  20. "os"
  21. "path/filepath"
  22. "strings"
  23. )
  24. var AutoCodePlugin = new(autoCodePlugin)
  25. type autoCodePlugin struct{}
  26. // Install 插件安装
  27. func (s *autoCodePlugin) Install(file *multipart.FileHeader) (web, server int, err error) {
  28. const GVAPLUGPINATH = "./gva-plug-temp/"
  29. defer os.RemoveAll(GVAPLUGPINATH)
  30. _, err = os.Stat(GVAPLUGPINATH)
  31. if os.IsNotExist(err) {
  32. os.Mkdir(GVAPLUGPINATH, os.ModePerm)
  33. }
  34. src, err := file.Open()
  35. if err != nil {
  36. return -1, -1, err
  37. }
  38. defer src.Close()
  39. out, err := os.Create(GVAPLUGPINATH + file.Filename)
  40. if err != nil {
  41. return -1, -1, err
  42. }
  43. defer out.Close()
  44. _, err = io.Copy(out, src)
  45. paths, err := utils.Unzip(GVAPLUGPINATH+file.Filename, GVAPLUGPINATH)
  46. paths = filterFile(paths)
  47. var webIndex = -1
  48. var serverIndex = -1
  49. webPlugin := ""
  50. serverPlugin := ""
  51. for i := range paths {
  52. paths[i] = filepath.ToSlash(paths[i])
  53. pathArr := strings.Split(paths[i], "/")
  54. ln := len(pathArr)
  55. if ln < 4 {
  56. continue
  57. }
  58. if pathArr[2]+"/"+pathArr[3] == `server/plugin` && len(serverPlugin) == 0 {
  59. serverPlugin = filepath.Join(pathArr[0], pathArr[1], pathArr[2], pathArr[3])
  60. }
  61. if pathArr[2]+"/"+pathArr[3] == `web/plugin` && len(webPlugin) == 0 {
  62. webPlugin = filepath.Join(pathArr[0], pathArr[1], pathArr[2], pathArr[3])
  63. }
  64. }
  65. if len(serverPlugin) == 0 && len(webPlugin) == 0 {
  66. zap.L().Error("非标准插件,请按照文档自动迁移使用")
  67. return webIndex, serverIndex, errors.New("非标准插件,请按照文档自动迁移使用")
  68. }
  69. if len(serverPlugin) != 0 {
  70. err = installation(serverPlugin, global.GVA_CONFIG.AutoCode.Server, global.GVA_CONFIG.AutoCode.Server)
  71. if err != nil {
  72. return webIndex, serverIndex, err
  73. }
  74. }
  75. if len(webPlugin) != 0 {
  76. err = installation(webPlugin, global.GVA_CONFIG.AutoCode.Server, global.GVA_CONFIG.AutoCode.Web)
  77. if err != nil {
  78. return webIndex, serverIndex, err
  79. }
  80. }
  81. return 1, 1, err
  82. }
  83. func installation(path string, formPath string, toPath string) error {
  84. arr := strings.Split(filepath.ToSlash(path), "/")
  85. ln := len(arr)
  86. if ln < 3 {
  87. return errors.New("arr")
  88. }
  89. name := arr[ln-3]
  90. var form = filepath.Join(global.GVA_CONFIG.AutoCode.Root, formPath, path)
  91. var to = filepath.Join(global.GVA_CONFIG.AutoCode.Root, toPath, "plugin")
  92. _, err := os.Stat(to + name)
  93. if err == nil {
  94. zap.L().Error("autoPath 已存在同名插件,请自行手动安装", zap.String("to", to))
  95. return errors.New(toPath + "已存在同名插件,请自行手动安装")
  96. }
  97. return cp.Copy(form, to, cp.Options{Skip: skipMacSpecialDocument})
  98. }
  99. func filterFile(paths []string) []string {
  100. np := make([]string, 0, len(paths))
  101. for _, path := range paths {
  102. if ok, _ := skipMacSpecialDocument(nil, path, ""); ok {
  103. continue
  104. }
  105. np = append(np, path)
  106. }
  107. return np
  108. }
  109. func skipMacSpecialDocument(_ os.FileInfo, src, _ string) (bool, error) {
  110. if strings.Contains(src, ".DS_Store") || strings.Contains(src, "__MACOSX") {
  111. return true, nil
  112. }
  113. return false, nil
  114. }
  115. func (s *autoCodePlugin) PubPlug(plugName string) (zipPath string, err error) {
  116. if plugName == "" {
  117. return "", errors.New("插件名称不能为空")
  118. }
  119. // 防止路径穿越
  120. plugName = filepath.Clean(plugName)
  121. webPath := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Web, "plugin", plugName)
  122. serverPath := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", plugName)
  123. // 创建一个新的zip文件
  124. // 判断目录是否存在
  125. _, err = os.Stat(webPath)
  126. if err != nil {
  127. return "", errors.New("web路径不存在")
  128. }
  129. _, err = os.Stat(serverPath)
  130. if err != nil {
  131. return "", errors.New("server路径不存在")
  132. }
  133. fileName := plugName + ".zip"
  134. // 创建一个新的zip文件
  135. files, err := archiver.FilesFromDisk(nil, map[string]string{
  136. webPath: plugName + "/web/plugin/" + plugName,
  137. serverPath: plugName + "/server/plugin/" + plugName,
  138. })
  139. // create the output file we'll write to
  140. out, err := os.Create(fileName)
  141. if err != nil {
  142. return
  143. }
  144. defer out.Close()
  145. // we can use the CompressedArchive type to gzip a tarball
  146. // (compression is not required; you could use Tar directly)
  147. format := archiver.Archive{
  148. Archival: archiver.Zip{},
  149. }
  150. // create the archive
  151. err = format.Archive(context.Background(), out, files)
  152. if err != nil {
  153. return
  154. }
  155. return filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, fileName), nil
  156. }
  157. func (s *autoCodePlugin) InitMenu(menuInfo request.InitMenu) (err error) {
  158. menuPath := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", menuInfo.PlugName, "initialize", "menu.go")
  159. src, err := os.ReadFile(menuPath)
  160. if err != nil {
  161. fmt.Println(err)
  162. }
  163. fileSet := token.NewFileSet()
  164. astFile, err := parser.ParseFile(fileSet, "", src, 0)
  165. arrayAst := ast.FindArray(astFile, "model", "SysBaseMenu")
  166. var menus []system.SysBaseMenu
  167. parentMenu := []system.SysBaseMenu{
  168. {
  169. ParentId: 0,
  170. Path: menuInfo.PlugName + "Menu",
  171. Name: menuInfo.PlugName + "Menu",
  172. Hidden: false,
  173. Component: "view/routerHolder.vue",
  174. Sort: 0,
  175. Meta: system.Meta{
  176. Title: menuInfo.ParentMenu,
  177. Icon: "school",
  178. },
  179. },
  180. }
  181. err = global.GVA_DB.Find(&menus, "id in (?)", menuInfo.Menus).Error
  182. if err != nil {
  183. return err
  184. }
  185. menus = append(parentMenu, menus...)
  186. menuExpr := ast.CreateMenuStructAst(menus)
  187. arrayAst.Elts = *menuExpr
  188. var out []byte
  189. bf := bytes.NewBuffer(out)
  190. printer.Fprint(bf, fileSet, astFile)
  191. os.WriteFile(menuPath, bf.Bytes(), 0666)
  192. return nil
  193. }
  194. func (s *autoCodePlugin) InitAPI(apiInfo request.InitApi) (err error) {
  195. apiPath := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, "plugin", apiInfo.PlugName, "initialize", "api.go")
  196. src, err := os.ReadFile(apiPath)
  197. if err != nil {
  198. fmt.Println(err)
  199. }
  200. fileSet := token.NewFileSet()
  201. astFile, err := parser.ParseFile(fileSet, "", src, 0)
  202. arrayAst := ast.FindArray(astFile, "model", "SysApi")
  203. var apis []system.SysApi
  204. err = global.GVA_DB.Find(&apis, "id in (?)", apiInfo.APIs).Error
  205. if err != nil {
  206. return err
  207. }
  208. apisExpr := ast.CreateApiStructAst(apis)
  209. arrayAst.Elts = *apisExpr
  210. var out []byte
  211. bf := bytes.NewBuffer(out)
  212. printer.Fprint(bf, fileSet, astFile)
  213. os.WriteFile(apiPath, bf.Bytes(), 0666)
  214. return nil
  215. }