工具
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.

202 lines
3.7 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
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. err = parseTpl(mainTpl, filepath.Join(rootDir, "main.go"), appName)
  51. if err != nil {
  52. return err
  53. }
  54. err = parseTpl(configJsonTpl, filepath.Join(rootDir, "config.json"), appName)
  55. if err != nil {
  56. return err
  57. }
  58. return nil
  59. }
  60. func genApp(appName string) error {
  61. appDir, err := getAppDir(appName)
  62. if err != nil {
  63. return err
  64. }
  65. err = os.Mkdir(appDir, os.ModeDir)
  66. if err != nil {
  67. return err
  68. }
  69. // api.go
  70. err = parseTpl(apiTpl, filepath.Join(appDir, "api.go"), appName)
  71. if err != nil {
  72. return err
  73. }
  74. // api_pre.go
  75. err = parseTpl(apiPreTpl, filepath.Join(appDir, "api_pre.go"), appName)
  76. if err != nil {
  77. return err
  78. }
  79. // api_task.go
  80. err = parseTpl(apiTaskTpl, filepath.Join(appDir, "api_task.go"), appName)
  81. if err != nil {
  82. return err
  83. }
  84. // api_test.go
  85. err = parseTpl(apiTestTpl, filepath.Join(appDir, "api_test.go"), appName)
  86. if err != nil {
  87. return err
  88. }
  89. err = parseTpl(dbModelTpl, filepath.Join(appDir, "db_model.go"), appName)
  90. if err != nil {
  91. return err
  92. }
  93. err = parseTpl(initializeTpl, filepath.Join(appDir, "initialize.go"), appName)
  94. if err != nil {
  95. return err
  96. }
  97. err = parseTpl(adminInitializeTpl, filepath.Join(appDir, "initialize_amdin.go"), appName)
  98. if err != nil {
  99. return err
  100. }
  101. return nil
  102. }
  103. func genOptions(appName string) error {
  104. optionsDir, err := getOptionsDir(appName)
  105. if err != nil {
  106. return err
  107. }
  108. err = os.Mkdir(optionsDir, os.ModeDir)
  109. if err != nil {
  110. return err
  111. }
  112. err = parseTpl(optionsTpl, filepath.Join(optionsDir, "options.go"), appName)
  113. if err != nil {
  114. return err
  115. }
  116. return nil
  117. }
  118. func genExternal(appName string) error {
  119. externalDir, err := getExternal(appName)
  120. if err != nil {
  121. return err
  122. }
  123. err = os.Mkdir(externalDir, os.ModeDir)
  124. if err != nil {
  125. return err
  126. }
  127. err = parseTpl(externalTpl, filepath.Join(externalDir, "extenral.go"), appName)
  128. if err != nil {
  129. return err
  130. }
  131. return nil
  132. }
  133. func getRootDir(appName string) (string, error) {
  134. rootDir := "hasaki-service-" + appName
  135. pwd, err := os.Getwd()
  136. if err != nil {
  137. return "", err
  138. }
  139. path := filepath.Join(pwd, rootDir)
  140. return path, nil
  141. }
  142. func getAppDir(appName string) (string, error) {
  143. appDir := "hasaki_" + appName
  144. rootDir, err := getRootDir(appName)
  145. if err != nil {
  146. return "", err
  147. }
  148. path := filepath.Join(rootDir, appDir)
  149. return path, nil
  150. }
  151. func getOptionsDir(appName string) (string, error) {
  152. optionsDir := "hasaki_" + appName + "_options"
  153. rootDir, err := getRootDir(appName)
  154. if err != nil {
  155. return "", err
  156. }
  157. path := filepath.Join(rootDir, optionsDir)
  158. return path, nil
  159. }
  160. func getExternal(appName string) (string, error) {
  161. externalDir := "hasaki_" + appName + "_external"
  162. rootDir, err := getRootDir(appName)
  163. if err != nil {
  164. return "", err
  165. }
  166. path := filepath.Join(rootDir, externalDir)
  167. return path, nil
  168. }
  169. func parseTpl(tmpl *template.Template, file string, appName string) error {
  170. f, err := os.Create(file)
  171. if err != nil {
  172. return err
  173. }
  174. err = tmpl.Execute(f, map[string]interface{}{"app_name": appName})
  175. if err != nil {
  176. return err
  177. }
  178. return nil
  179. }