autoCode.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. package autoCodeModel
  2. import (
  3. "fmt"
  4. "gin-vue-admin/tools"
  5. "html/template"
  6. "os"
  7. )
  8. //开发中功能,若您发现这块代码可以研究,可以无视
  9. type AutoCodeStruct struct {
  10. StructName string `json:"structName"`
  11. PackageName string `json:"packageName"`
  12. Abbreviation string `json:"abbreviation"`
  13. Components []Component `json:"components"`
  14. }
  15. type Component struct {
  16. ComponentName string `json:"componentName"`
  17. ComponentType string `json:"componentType"`
  18. ComponentJson string `json:"componentJson"`
  19. Ismultiple bool `json:"isMultiple"`
  20. ComponentShowType string `json:"componentShowType"`
  21. NideDictionary bool `json:"nideDictionary"`
  22. DictionaryName string `json:"dictionaryName"`
  23. ComponentDictionary []Dictionary `json:"dictionary"`
  24. }
  25. type Dictionary struct {
  26. Label string `json:"label"`
  27. Value string `json:"value"`
  28. }
  29. func Temp() {
  30. modelTmpl, modelTplErr := template.ParseFiles("../../tpl/te/model.go.tpl")
  31. apiTmpl, apiTplErr := template.ParseFiles("../../tpl/te/api.go.tpl")
  32. routerTmpl, routerTplErr := template.ParseFiles("../../tpl/te/router.go.tpl")
  33. feapiTmpl, feapiTplErr := template.ParseFiles("../../tpl/fe/api.js.tpl")
  34. fmt.Println(modelTplErr,apiTplErr,routerTplErr,feapiTplErr)
  35. a1 := Component{
  36. ComponentName: "TestComponent",
  37. ComponentType: "string",
  38. ComponentJson: "testComponent",
  39. Ismultiple: false,
  40. ComponentShowType: "",
  41. NideDictionary: false,
  42. DictionaryName: "",
  43. ComponentDictionary: nil,
  44. }
  45. a2 := Component{
  46. ComponentName: "TestBigComponent",
  47. ComponentType: "int",
  48. ComponentJson: "testBigComponent",
  49. Ismultiple: false,
  50. ComponentShowType: "",
  51. NideDictionary: false,
  52. DictionaryName: "",
  53. ComponentDictionary: nil,
  54. }
  55. a := AutoCodeStruct{
  56. StructName: "Test",
  57. PackageName: "autocode",
  58. Abbreviation: "t",
  59. Components: []Component{a1, a2},
  60. }
  61. _autoCode := "../../autoCode/"
  62. _te := "../../autoCode/te/"
  63. _dir := "../../autoCode/te/" + a.PackageName
  64. _modeldir := "../../autoCode/te/" + a.PackageName+"/model"
  65. _apidir := "../../autoCode/te/" + a.PackageName+"/api"
  66. _routerdir := "../../autoCode/te/" + a.PackageName+"/router"
  67. _fe := "../../autoCode/fe/"
  68. _fe_dir:="../../autoCode/fe/" + a.PackageName
  69. _fe_apidir:="../../autoCode/fe/" + a.PackageName+"/api"
  70. mkerr := createDir(_autoCode,_te,_dir,_modeldir,_apidir,_routerdir,_fe,_fe_dir,_fe_apidir)
  71. fmt.Print(mkerr)
  72. model, _ := os.OpenFile("../../autoCode/te/"+a.PackageName+"/model/model.go", os.O_CREATE|os.O_WRONLY, 0755)
  73. api, _ := os.OpenFile("../../autoCode/te/"+a.PackageName+"/api/api.go", os.O_CREATE|os.O_WRONLY, 0755)
  74. router, _ := os.OpenFile("../../autoCode/te/"+a.PackageName+"/router/router.go", os.O_CREATE|os.O_WRONLY, 0755)
  75. feapi ,_ := os.OpenFile("../../autoCode/fe/"+a.PackageName+"/api/api.js", os.O_CREATE|os.O_WRONLY, 0755)
  76. modelErr := modelTmpl.Execute(model, a)
  77. apiErr := apiTmpl.Execute(api, a)
  78. routerErr := routerTmpl.Execute(router, a)
  79. feapiErr := feapiTmpl.Execute(feapi, a)
  80. fmt.Println(modelErr,apiErr,routerErr,feapiErr)
  81. }
  82. //批量创建文件夹
  83. func createDir(dirs ...string)(err error){
  84. for _,v:=range dirs{
  85. exist, err := tools.PathExists(v)
  86. if err != nil {
  87. //log.L.Info(fmt.Sprintf("get dir error![%v]\n", err))
  88. return err
  89. }
  90. if exist {
  91. //log.L.Info(fmt.Sprintf("has dir![%v]\n"+_dir))
  92. } else {
  93. //log.L.Info(fmt.Sprintf("no dir![%v]\n"+_dir))
  94. // 创建文件夹
  95. err = os.Mkdir(v, os.ModePerm)
  96. if err != nil {
  97. //log.L.Error(fmt.Sprintf("mkdir error![%v]\n",err))
  98. } else {
  99. //log.L.Info("mkdir success!\n")
  100. }
  101. }
  102. }
  103. return err
  104. }