自动插件模式鉴定调整 (#1161)
* fix: #1160 * 自动插件模式鉴定调整 Co-authored-by: songzhibin97 <718428482@qq.com>
This commit is contained in:
@@ -5,14 +5,10 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
cp "github.com/otiai10/copy"
|
||||
"go.uber.org/zap"
|
||||
"go/ast"
|
||||
"go/format"
|
||||
"go/parser"
|
||||
"go/token"
|
||||
"golang.org/x/text/cases"
|
||||
"golang.org/x/text/language"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
@@ -23,6 +19,11 @@ import (
|
||||
"strings"
|
||||
"text/template"
|
||||
|
||||
cp "github.com/otiai10/copy"
|
||||
"go.uber.org/zap"
|
||||
"golang.org/x/text/cases"
|
||||
"golang.org/x/text/language"
|
||||
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/resource/autocode_template/subcontract"
|
||||
|
||||
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
||||
@@ -827,91 +828,112 @@ func (autoCodeService *AutoCodeService) CreatePlug(plug system.AutoPlugReq) erro
|
||||
os.MkdirAll(dirPath, 0755)
|
||||
}
|
||||
file := filepath.Join(global.GVA_CONFIG.AutoCode.Root, global.GVA_CONFIG.AutoCode.Server, fmt.Sprintf(global.GVA_CONFIG.AutoCode.SPlug, plug.Snake+"/"+tpl[len(plugPath):len(tpl)-4]))
|
||||
f, _ := os.OpenFile(file, os.O_WRONLY|os.O_CREATE, 0666)
|
||||
f, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE, 0666)
|
||||
if err != nil {
|
||||
zap.L().Error("open file", zap.String("tpl", tpl), zap.Error(err), zap.Any("plug", plug))
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
err = temp.Execute(f, plug)
|
||||
if err != nil {
|
||||
zap.L().Error("exec err", zap.String("tpl", tpl), zap.Error(err), zap.Any("plug", plug))
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (autoCodeService *AutoCodeService) InstallPlugin(file *multipart.FileHeader) (err error) {
|
||||
const GVAPLUGPATH = "./gva-plug-temp/"
|
||||
defer os.RemoveAll(GVAPLUGPATH)
|
||||
_, err = os.Stat(GVAPLUGPATH)
|
||||
func (autoCodeService *AutoCodeService) InstallPlugin(file *multipart.FileHeader) (web, server int, err error) {
|
||||
const GVAPLUGPINATH = "./gva-plug-temp/"
|
||||
defer os.RemoveAll(GVAPLUGPINATH)
|
||||
_, err = os.Stat(GVAPLUGPINATH)
|
||||
if os.IsNotExist(err) {
|
||||
os.Mkdir(GVAPLUGPATH, os.ModePerm)
|
||||
os.Mkdir(GVAPLUGPINATH, os.ModePerm)
|
||||
}
|
||||
|
||||
src, err := file.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
return -1, -1, err
|
||||
}
|
||||
defer src.Close()
|
||||
|
||||
out, err := os.Create(GVAPLUGPATH + file.Filename)
|
||||
out, err := os.Create(GVAPLUGPINATH + file.Filename)
|
||||
if err != nil {
|
||||
return err
|
||||
return -1, -1, err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
_, err = io.Copy(out, src)
|
||||
|
||||
paths, err := utils.Unzip(GVAPLUGPATH+file.Filename, GVAPLUGPATH)
|
||||
var webIndex = 0
|
||||
var serverIndex = 0
|
||||
paths, err := utils.Unzip(GVAPLUGPINATH+file.Filename, GVAPLUGPINATH)
|
||||
paths = filterFile(paths)
|
||||
var webIndex = -1
|
||||
var serverIndex = -1
|
||||
for i := range paths {
|
||||
paths[i] = filepath.ToSlash(paths[i])
|
||||
pathArr := strings.Split(paths[i], "/")
|
||||
if pathArr[len(pathArr)-2] == "server" && pathArr[len(pathArr)-1] == "plugin" {
|
||||
serverIndex = i + 1
|
||||
ln := len(pathArr)
|
||||
if ln < 2 {
|
||||
continue
|
||||
}
|
||||
if pathArr[len(pathArr)-2] == "web" && pathArr[len(pathArr)-1] == "plugin" {
|
||||
webIndex = i + 1
|
||||
if pathArr[ln-2] == "server" && pathArr[ln-1] == "plugin" {
|
||||
serverIndex = i
|
||||
}
|
||||
if pathArr[ln-2] == "web" && pathArr[ln-1] == "plugin" {
|
||||
webIndex = i
|
||||
}
|
||||
}
|
||||
if webIndex == 0 && serverIndex == 0 {
|
||||
fmt.Println("非标准插件,请按照文档自动迁移使用")
|
||||
return errors.New("非标准插件,请按照文档自动迁移使用")
|
||||
if webIndex == -1 && serverIndex == -1 {
|
||||
zap.L().Error("非标准插件,请按照文档自动迁移使用")
|
||||
return webIndex, serverIndex, errors.New("非标准插件,请按照文档自动迁移使用")
|
||||
}
|
||||
|
||||
if webIndex != 0 {
|
||||
webNameArr := strings.Split(filepath.ToSlash(paths[webIndex]), "/")
|
||||
webName := webNameArr[len(webNameArr)-1]
|
||||
var form = filepath.ToSlash(global.GVA_CONFIG.AutoCode.Root + global.GVA_CONFIG.AutoCode.Server + "/" + paths[webIndex])
|
||||
var to = filepath.ToSlash(global.GVA_CONFIG.AutoCode.Root + global.GVA_CONFIG.AutoCode.Web + "/plugin/" + webName)
|
||||
_, err := os.Stat(to)
|
||||
if err == nil {
|
||||
fmt.Println("web 已存在同名插件,请自行手动安装")
|
||||
return errors.New("web 已存在同名插件,请自行手动安装")
|
||||
}
|
||||
err = cp.Copy(form, to)
|
||||
if webIndex != -1 {
|
||||
err = installation(paths[webIndex], global.GVA_CONFIG.AutoCode.Server, global.GVA_CONFIG.AutoCode.Web)
|
||||
if err != nil {
|
||||
return err
|
||||
return webIndex, serverIndex, err
|
||||
}
|
||||
}
|
||||
|
||||
if serverIndex != 0 {
|
||||
serverNameArr := strings.Split(filepath.ToSlash(paths[serverIndex]), "/")
|
||||
serverName := serverNameArr[len(serverNameArr)-1]
|
||||
var form = filepath.ToSlash(global.GVA_CONFIG.AutoCode.Root + global.GVA_CONFIG.AutoCode.Server + "/" + paths[serverIndex])
|
||||
var to = filepath.ToSlash(global.GVA_CONFIG.AutoCode.Root + global.GVA_CONFIG.AutoCode.Server + "/plugin/" + serverName)
|
||||
_, err := os.Stat(to)
|
||||
if err == nil {
|
||||
fmt.Println("server 已存在同名插件,请自行手动安装")
|
||||
return errors.New("server 已存在同名插件,请自行手动安装")
|
||||
}
|
||||
err = cp.Copy(form, to)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
return nil
|
||||
if serverIndex != -1 {
|
||||
err = installation(paths[serverIndex], global.GVA_CONFIG.AutoCode.Server, global.GVA_CONFIG.AutoCode.Server)
|
||||
}
|
||||
return webIndex, serverIndex, err
|
||||
}
|
||||
|
||||
func installation(path string, formPath string, toPath string) error {
|
||||
arr := strings.Split(filepath.ToSlash(path), "/")
|
||||
ln := len(arr)
|
||||
if ln < 3 {
|
||||
return errors.New("arr")
|
||||
}
|
||||
name := arr[ln-3]
|
||||
|
||||
var form = filepath.ToSlash(global.GVA_CONFIG.AutoCode.Root + formPath + "/" + path)
|
||||
var to = filepath.ToSlash(global.GVA_CONFIG.AutoCode.Root + toPath + "/plugin/")
|
||||
_, err := os.Stat(to + name)
|
||||
if err == nil {
|
||||
zap.L().Error("autoPath 已存在同名插件,请自行手动安装", zap.String("to", to))
|
||||
return errors.New(toPath + "已存在同名插件,请自行手动安装")
|
||||
}
|
||||
return cp.Copy(form, to, cp.Options{Skip: skipMacSpecialDocument})
|
||||
}
|
||||
|
||||
func filterFile(paths []string) []string {
|
||||
np := make([]string, 0, len(paths))
|
||||
for _, path := range paths {
|
||||
if ok, _ := skipMacSpecialDocument(path); ok {
|
||||
continue
|
||||
}
|
||||
np = append(np, path)
|
||||
}
|
||||
return np
|
||||
}
|
||||
|
||||
func skipMacSpecialDocument(src string) (bool, error) {
|
||||
if strings.Contains(src, ".DS_Store") || strings.Contains(src, "__MACOSX") {
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user