autoCode.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package autoCodeModel
  2. import (
  3. "fmt"
  4. log "gin-vue-admin/init/initlog"
  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. tmpl, err := template.ParseFiles("../../tpl/te/struct.go.tpl")
  31. fmt.Println(tmpl, err)
  32. a1 := Component{
  33. ComponentName: "TestComponent",
  34. ComponentType: "string",
  35. ComponentJson: "testComponent",
  36. Ismultiple: false,
  37. ComponentShowType: "",
  38. NideDictionary: false,
  39. DictionaryName: "",
  40. ComponentDictionary: nil,
  41. }
  42. a2 := Component{
  43. ComponentName: "TestBigComponent",
  44. ComponentType: "int",
  45. ComponentJson: "testBigComponent",
  46. Ismultiple: false,
  47. ComponentShowType: "",
  48. NideDictionary: false,
  49. DictionaryName: "",
  50. ComponentDictionary: nil,
  51. }
  52. a := AutoCodeStruct{
  53. StructName: "Test",
  54. PackageName: "autocode",
  55. Components: []Component{a1, a2},
  56. }
  57. _dir := "../" + a.PackageName
  58. exist, err := pathExists(_dir)
  59. if err != nil {
  60. log.L.Info("get dir error![%v]\n", err)
  61. return
  62. }
  63. if exist {
  64. log.L.Info("has dir![%v]\n", _dir)
  65. } else {
  66. log.L.Info("no dir![%v]\n", _dir)
  67. // 创建文件夹
  68. err := os.Mkdir(_dir, os.ModePerm)
  69. if err != nil {
  70. log.L.Error("mkdir failed![%v]\n", err)
  71. } else {
  72. log.L.Info("mkdir success!\n")
  73. }
  74. }
  75. file, err := os.OpenFile("../"+a.PackageName+"/struct.go", os.O_CREATE|os.O_WRONLY, 0755)
  76. err = tmpl.Execute(file, a)
  77. }
  78. // 判断文件夹是否存在
  79. func pathExists(path string) (bool, error) {
  80. _, err := os.Stat(path)
  81. if err == nil {
  82. return true, nil
  83. }
  84. if os.IsNotExist(err) {
  85. return false, nil
  86. }
  87. return false, err
  88. }