project.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package main
  2. import (
  3. "bytes"
  4. "io/ioutil"
  5. "os"
  6. "os/exec"
  7. "strings"
  8. "text/template"
  9. )
  10. // project project config
  11. type project struct {
  12. Department string
  13. Type string
  14. Name string
  15. Owner string
  16. Path string
  17. WithGRPC bool
  18. }
  19. const (
  20. _tplTypeDao = iota
  21. _tplTypeHTTPServer
  22. _tplTypeAPIProto
  23. _tplTypeService
  24. _tplTypeMain
  25. _tplTypeChangeLog
  26. _tplTypeContributors
  27. _tplTypeReadme
  28. _tplTypeAppToml
  29. _tplTypeMySQLToml
  30. _tplTypeMCToml
  31. _tplTypeRedisToml
  32. _tplTypeHTTPToml
  33. _tplTypeGRPCToml
  34. _tplTypeModel
  35. _tplTypeGRPCServer
  36. _tplTypeAPIGenerate
  37. )
  38. var (
  39. p project
  40. // files type => path
  41. files = map[int]string{
  42. // init doc
  43. _tplTypeChangeLog: "/CHANGELOG.md",
  44. _tplTypeContributors: "/CONTRIBUTORS.md",
  45. _tplTypeReadme: "/README.md",
  46. // init project
  47. _tplTypeMain: "/cmd/main.go",
  48. _tplTypeDao: "/internal/dao/dao.go",
  49. _tplTypeHTTPServer: "/internal/server/http/http.go",
  50. _tplTypeService: "/internal/service/service.go",
  51. _tplTypeModel: "/internal/model/model.go",
  52. // init config
  53. _tplTypeAppToml: "/configs/application.toml",
  54. _tplTypeMySQLToml: "/configs/mysql.toml",
  55. _tplTypeMCToml: "/configs/memcache.toml",
  56. _tplTypeRedisToml: "/configs/redis.toml",
  57. _tplTypeHTTPToml: "/configs/http.toml",
  58. _tplTypeGRPCToml: "/configs/grpc.toml",
  59. }
  60. // tpls type => content
  61. tpls = map[int]string{
  62. _tplTypeDao: _tplDao,
  63. _tplTypeHTTPServer: _tplHTTPServer,
  64. _tplTypeAPIProto: _tplAPIProto,
  65. _tplTypeAPIGenerate: _tplAPIGenerate,
  66. _tplTypeMain: _tplMain,
  67. _tplTypeChangeLog: _tplChangeLog,
  68. _tplTypeContributors: _tplContributors,
  69. _tplTypeReadme: _tplReadme,
  70. _tplTypeMySQLToml: _tplMySQLToml,
  71. _tplTypeMCToml: _tplMCToml,
  72. _tplTypeRedisToml: _tplRedisToml,
  73. _tplTypeAppToml: _tplAppToml,
  74. _tplTypeHTTPToml: _tplHTTPToml,
  75. _tplTypeModel: _tplModel,
  76. }
  77. )
  78. func validate() (ok bool) {
  79. if !depts[p.Department] {
  80. println("[-d] Invalid department name.")
  81. return
  82. }
  83. if !types[p.Type] {
  84. println("[-t] Invalid project type.")
  85. return
  86. }
  87. if p.Name == "" {
  88. println("[-n] Invalid project name.")
  89. return
  90. }
  91. // TODO: check project name
  92. return true
  93. }
  94. func create() (err error) {
  95. if p.WithGRPC {
  96. files[_tplTypeGRPCServer] = "/internal/server/grpc/server.go"
  97. files[_tplTypeAPIProto] = "/api/api.proto"
  98. files[_tplTypeAPIGenerate] = "/api/generate.go"
  99. tpls[_tplTypeGRPCServer] = _tplGRPCServer
  100. tpls[_tplTypeGRPCToml] = _tplGRPCToml
  101. tpls[_tplTypeService] = _tplGPRCService
  102. } else {
  103. tpls[_tplTypeService] = _tplService
  104. tpls[_tplTypeMain] = delgrpc(_tplMain)
  105. }
  106. if err = os.MkdirAll(p.Path, 0755); err != nil {
  107. return
  108. }
  109. for t, v := range files {
  110. i := strings.LastIndex(v, "/")
  111. if i > 0 {
  112. dir := v[:i]
  113. if err = os.MkdirAll(p.Path+dir, 0755); err != nil {
  114. return
  115. }
  116. }
  117. if err = write(p.Path+v, tpls[t]); err != nil {
  118. return
  119. }
  120. }
  121. if p.WithGRPC {
  122. if err = genpb(); err != nil {
  123. return
  124. }
  125. }
  126. err = updateProwAction()
  127. return
  128. }
  129. func genpb() error {
  130. cmd := exec.Command("go", "generate", p.Path+"/api/generate.go")
  131. return cmd.Run()
  132. }
  133. func delgrpc(tpl string) string {
  134. var buf bytes.Buffer
  135. lines := strings.Split(tpl, "\n")
  136. for _, l := range lines {
  137. if strings.Contains(l, "grpc") {
  138. continue
  139. }
  140. if strings.Contains(l, "warden") {
  141. continue
  142. }
  143. buf.WriteString(l)
  144. buf.WriteString("\n")
  145. }
  146. return buf.String()
  147. }
  148. func write(name, tpl string) (err error) {
  149. data, err := parse(tpl)
  150. if err != nil {
  151. return
  152. }
  153. return ioutil.WriteFile(name, data, 0644)
  154. }
  155. func parse(s string) ([]byte, error) {
  156. t, err := template.New("").Parse(s)
  157. if err != nil {
  158. return nil, err
  159. }
  160. var buf bytes.Buffer
  161. if err = t.Execute(&buf, p); err != nil {
  162. return nil, err
  163. }
  164. return buf.Bytes(), nil
  165. }