@@ -5,14 +5,10 @@ import (
5
5
"encoding/json"
6
6
"errors"
7
7
"fmt"
8
- cp "github.com/otiai10/copy"
9
- "go.uber.org/zap"
10
8
"go/ast"
11
9
"go/format"
12
10
"go/parser"
13
11
"go/token"
14
- "golang.org/x/text/cases"
15
- "golang.org/x/text/language"
16
12
"io"
17
13
"io/ioutil"
18
14
"log"
@@ -23,6 +19,11 @@ import (
23
19
"strings"
24
20
"text/template"
25
21
22
+ cp "github.com/otiai10/copy"
23
+ "go.uber.org/zap"
24
+ "golang.org/x/text/cases"
25
+ "golang.org/x/text/language"
26
+
26
27
"github.com/flipped-aurora/gin-vue-admin/server/resource/autocode_template/subcontract"
27
28
28
29
"github.com/flipped-aurora/gin-vue-admin/server/global"
@@ -827,91 +828,112 @@ func (autoCodeService *AutoCodeService) CreatePlug(plug system.AutoPlugReq) erro
827
828
os .MkdirAll (dirPath , 0755 )
828
829
}
829
830
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 ]))
830
- f , _ := os .OpenFile (file , os .O_WRONLY | os .O_CREATE , 0666 )
831
+ f , err := os .OpenFile (file , os .O_WRONLY | os .O_CREATE , 0666 )
832
+ if err != nil {
833
+ zap .L ().Error ("open file" , zap .String ("tpl" , tpl ), zap .Error (err ), zap .Any ("plug" , plug ))
834
+ return err
835
+ }
836
+ defer f .Close ()
837
+
831
838
err = temp .Execute (f , plug )
832
839
if err != nil {
833
840
zap .L ().Error ("exec err" , zap .String ("tpl" , tpl ), zap .Error (err ), zap .Any ("plug" , plug ))
834
841
return err
835
842
}
836
- defer f .Close ()
837
843
}
838
844
return nil
839
845
}
840
846
841
- func (autoCodeService * AutoCodeService ) InstallPlugin (file * multipart.FileHeader ) (err error ) {
842
- const GVAPLUGPATH = "./gva-plug-temp/"
843
- defer os .RemoveAll (GVAPLUGPATH )
844
- _ , err = os .Stat (GVAPLUGPATH )
847
+ func (autoCodeService * AutoCodeService ) InstallPlugin (file * multipart.FileHeader ) (web , server int , err error ) {
848
+ const GVAPLUGPINATH = "./gva-plug-temp/"
849
+ defer os .RemoveAll (GVAPLUGPINATH )
850
+ _ , err = os .Stat (GVAPLUGPINATH )
845
851
if os .IsNotExist (err ) {
846
- os .Mkdir (GVAPLUGPATH , os .ModePerm )
852
+ os .Mkdir (GVAPLUGPINATH , os .ModePerm )
847
853
}
848
854
849
855
src , err := file .Open ()
850
856
if err != nil {
851
- return err
857
+ return - 1 , - 1 , err
852
858
}
853
859
defer src .Close ()
854
860
855
- out , err := os .Create (GVAPLUGPATH + file .Filename )
861
+ out , err := os .Create (GVAPLUGPINATH + file .Filename )
856
862
if err != nil {
857
- return err
863
+ return - 1 , - 1 , err
858
864
}
859
865
defer out .Close ()
860
866
861
867
_ , err = io .Copy (out , src )
862
868
863
- paths , err := utils .Unzip (GVAPLUGPATH + file .Filename , GVAPLUGPATH )
864
- var webIndex = 0
865
- var serverIndex = 0
869
+ paths , err := utils .Unzip (GVAPLUGPINATH + file .Filename , GVAPLUGPINATH )
870
+ paths = filterFile (paths )
871
+ var webIndex = - 1
872
+ var serverIndex = - 1
866
873
for i := range paths {
867
874
paths [i ] = filepath .ToSlash (paths [i ])
868
875
pathArr := strings .Split (paths [i ], "/" )
869
- if pathArr [len (pathArr )- 2 ] == "server" && pathArr [len (pathArr )- 1 ] == "plugin" {
870
- serverIndex = i + 1
876
+ ln := len (pathArr )
877
+ if ln < 2 {
878
+ continue
879
+ }
880
+ if pathArr [ln - 2 ] == "server" && pathArr [ln - 1 ] == "plugin" {
881
+ serverIndex = i
871
882
}
872
- if pathArr [len ( pathArr ) - 2 ] == "web" && pathArr [len ( pathArr ) - 1 ] == "plugin" {
873
- webIndex = i + 1
883
+ if pathArr [ln - 2 ] == "web" && pathArr [ln - 1 ] == "plugin" {
884
+ webIndex = i
874
885
}
875
886
}
876
- if webIndex == 0 && serverIndex == 0 {
877
- fmt . Println ("非标准插件,请按照文档自动迁移使用" )
878
- return errors .New ("非标准插件,请按照文档自动迁移使用" )
887
+ if webIndex == - 1 && serverIndex == - 1 {
888
+ zap . L (). Error ("非标准插件,请按照文档自动迁移使用" )
889
+ return webIndex , serverIndex , errors .New ("非标准插件,请按照文档自动迁移使用" )
879
890
}
880
891
881
- if webIndex != 0 {
882
- webNameArr := strings .Split (filepath .ToSlash (paths [webIndex ]), "/" )
883
- webName := webNameArr [len (webNameArr )- 1 ]
884
- var form = filepath .ToSlash (global .GVA_CONFIG .AutoCode .Root + global .GVA_CONFIG .AutoCode .Server + "/" + paths [webIndex ])
885
- var to = filepath .ToSlash (global .GVA_CONFIG .AutoCode .Root + global .GVA_CONFIG .AutoCode .Web + "/plugin/" + webName )
886
- _ , err := os .Stat (to )
887
- if err == nil {
888
- fmt .Println ("web 已存在同名插件,请自行手动安装" )
889
- return errors .New ("web 已存在同名插件,请自行手动安装" )
890
- }
891
- err = cp .Copy (form , to )
892
+ if webIndex != - 1 {
893
+ err = installation (paths [webIndex ], global .GVA_CONFIG .AutoCode .Server , global .GVA_CONFIG .AutoCode .Web )
892
894
if err != nil {
893
- return err
895
+ return webIndex , serverIndex , err
894
896
}
895
897
}
896
898
897
- if serverIndex != 0 {
898
- serverNameArr := strings .Split (filepath .ToSlash (paths [serverIndex ]), "/" )
899
- serverName := serverNameArr [len (serverNameArr )- 1 ]
900
- var form = filepath .ToSlash (global .GVA_CONFIG .AutoCode .Root + global .GVA_CONFIG .AutoCode .Server + "/" + paths [serverIndex ])
901
- var to = filepath .ToSlash (global .GVA_CONFIG .AutoCode .Root + global .GVA_CONFIG .AutoCode .Server + "/plugin/" + serverName )
902
- _ , err := os .Stat (to )
903
- if err == nil {
904
- fmt .Println ("server 已存在同名插件,请自行手动安装" )
905
- return errors .New ("server 已存在同名插件,请自行手动安装" )
906
- }
907
- err = cp .Copy (form , to )
908
- if err != nil {
909
- return err
899
+ if serverIndex != - 1 {
900
+ err = installation (paths [serverIndex ], global .GVA_CONFIG .AutoCode .Server , global .GVA_CONFIG .AutoCode .Server )
901
+ }
902
+ return webIndex , serverIndex , err
903
+ }
904
+
905
+ func installation (path string , formPath string , toPath string ) error {
906
+ arr := strings .Split (filepath .ToSlash (path ), "/" )
907
+ ln := len (arr )
908
+ if ln < 3 {
909
+ return errors .New ("arr" )
910
+ }
911
+ name := arr [ln - 3 ]
912
+
913
+ var form = filepath .ToSlash (global .GVA_CONFIG .AutoCode .Root + formPath + "/" + path )
914
+ var to = filepath .ToSlash (global .GVA_CONFIG .AutoCode .Root + toPath + "/plugin/" )
915
+ _ , err := os .Stat (to + name )
916
+ if err == nil {
917
+ zap .L ().Error ("autoPath 已存在同名插件,请自行手动安装" , zap .String ("to" , to ))
918
+ return errors .New (toPath + "已存在同名插件,请自行手动安装" )
919
+ }
920
+ return cp .Copy (form , to , cp.Options {Skip : skipMacSpecialDocument })
921
+ }
922
+
923
+ func filterFile (paths []string ) []string {
924
+ np := make ([]string , 0 , len (paths ))
925
+ for _ , path := range paths {
926
+ if ok , _ := skipMacSpecialDocument (path ); ok {
927
+ continue
910
928
}
929
+ np = append (np , path )
911
930
}
912
- if err != nil {
913
- return err
914
- } else {
915
- return nil
931
+ return np
932
+ }
933
+
934
+ func skipMacSpecialDocument (src string ) (bool , error ) {
935
+ if strings .Contains (src , ".DS_Store" ) || strings .Contains (src , "__MACOSX" ) {
936
+ return true , nil
916
937
}
938
+ return false , nil
917
939
}
0 commit comments