工具
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
3.3 KiB

4 years ago
  1. package servicegen
  2. import (
  3. "os"
  4. "path/filepath"
  5. "text/template"
  6. )
  7. /* 生成整个项目结构
  8. - hasaki-service-app
  9. - hasaki-app
  10. - api.go
  11. - api_pre.go
  12. - api_test.go
  13. - api_task.go
  14. - db_model.go
  15. - handler.go
  16. - initialize.go
  17. - hasaki-app-options
  18. - options.go
  19. - hasaki-app-external
  20. - external.go
  21. */
  22. func Gen(name string) error {
  23. err := genService(name)
  24. if err != nil {
  25. return err
  26. }
  27. err = genApp(name)
  28. if err != nil {
  29. return err
  30. }
  31. err = genOptions(name)
  32. if err != nil {
  33. return err
  34. }
  35. err = genExternal(name)
  36. if err != nil {
  37. return err
  38. }
  39. return nil
  40. }
  41. func genService(appName string) error {
  42. rootDir, err := getRootDir(appName)
  43. if err != nil {
  44. return err
  45. }
  46. err = os.Mkdir(rootDir, os.ModeDir)
  47. if err != nil {
  48. return err
  49. }
  50. return nil
  51. }
  52. func genApp(appName string) error {
  53. appDir, err := getAppDir(appName)
  54. if err != nil {
  55. return err
  56. }
  57. err = os.Mkdir(appDir, os.ModeDir)
  58. if err != nil {
  59. return err
  60. }
  61. // api.go
  62. err = parseTmpl(apiTmpl, filepath.Join(appDir, "api.go"), appName)
  63. if err != nil {
  64. return err
  65. }
  66. // api_pre.go
  67. err = parseTmpl(apiPreTmpl, filepath.Join(appDir, "api_pre.go"), appName)
  68. if err != nil {
  69. return err
  70. }
  71. // api_task.go
  72. err = parseTmpl(apiTaskTmpl, filepath.Join(appDir, "api_task.go"), appName)
  73. if err != nil {
  74. return err
  75. }
  76. // api_test.go
  77. err = parseTmpl(apiTestTmpl, filepath.Join(appDir, "api_test.go"), appName)
  78. if err != nil {
  79. return err
  80. }
  81. err = parseTmpl(dbModelTmpl, filepath.Join(appDir, "db_model.go"), appName)
  82. if err != nil {
  83. return err
  84. }
  85. return nil
  86. }
  87. func genOptions(appName string) error {
  88. optionsDir, err := getOptionsDir(appName)
  89. if err != nil {
  90. return err
  91. }
  92. err = os.Mkdir(optionsDir, os.ModeDir)
  93. if err != nil {
  94. return err
  95. }
  96. err = parseTmpl(optionsTmpl, filepath.Join(optionsDir, "options.go"), appName)
  97. if err != nil {
  98. return err
  99. }
  100. return nil
  101. }
  102. func genExternal(appName string) error {
  103. externalDir, err := getExternal(appName)
  104. if err != nil {
  105. return err
  106. }
  107. err = os.Mkdir(externalDir, os.ModeDir)
  108. if err != nil {
  109. return err
  110. }
  111. err = parseTmpl(externalTmpl, filepath.Join(externalDir, "extenral.go"), appName)
  112. if err != nil {
  113. return err
  114. }
  115. return nil
  116. }
  117. func getRootDir(appName string) (string, error) {
  118. rootDir := "hasaki-service-" + appName
  119. pwd, err := os.Getwd()
  120. if err != nil {
  121. return "", err
  122. }
  123. path := filepath.Join(pwd, rootDir)
  124. return path, nil
  125. }
  126. func getAppDir(appName string) (string, error) {
  127. appDir := "hasaki_" + appName
  128. rootDir, err := getRootDir(appName)
  129. if err != nil {
  130. return "", err
  131. }
  132. path := filepath.Join(rootDir, appDir)
  133. return path, nil
  134. }
  135. func getOptionsDir(appName string) (string, error) {
  136. optionsDir := "hasaki_" + appName + "_options"
  137. rootDir, err := getRootDir(appName)
  138. if err != nil {
  139. return "", err
  140. }
  141. path := filepath.Join(rootDir, optionsDir)
  142. return path, nil
  143. }
  144. func getExternal(appName string) (string, error) {
  145. externalDir := "hasaki_" + appName + "_external"
  146. rootDir, err := getRootDir(appName)
  147. if err != nil {
  148. return "", err
  149. }
  150. path := filepath.Join(rootDir, externalDir)
  151. return path, nil
  152. }
  153. func parseTmpl(tmpl *template.Template, file string, appName string) error {
  154. f, err := os.Create(file)
  155. if err != nil {
  156. return err
  157. }
  158. err = tmpl.Execute(f, map[string]interface{}{"app_name": appName})
  159. if err != nil {
  160. return err
  161. }
  162. return nil
  163. }