mkdir.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package generator
  2. import (
  3. "path/filepath"
  4. "strings"
  5. "git.i2edu.net/i2/go-zero/tools/goctl/rpc/parser"
  6. "git.i2edu.net/i2/go-zero/tools/goctl/util"
  7. "git.i2edu.net/i2/go-zero/tools/goctl/util/ctx"
  8. "git.i2edu.net/i2/go-zero/tools/goctl/util/stringx"
  9. )
  10. const (
  11. wd = "wd"
  12. etc = "etc"
  13. internal = "internal"
  14. config = "config"
  15. logic = "logic"
  16. server = "server"
  17. svc = "svc"
  18. pb = "pb"
  19. call = "call"
  20. )
  21. type (
  22. // DirContext defines a rpc service directories context
  23. DirContext interface {
  24. GetCall() Dir
  25. GetEtc() Dir
  26. GetInternal() Dir
  27. GetConfig() Dir
  28. GetLogic() Dir
  29. GetServer() Dir
  30. GetSvc() Dir
  31. GetPb() Dir
  32. GetMain() Dir
  33. GetServiceName() stringx.String
  34. }
  35. // Dir defines a directory
  36. Dir struct {
  37. Base string
  38. Filename string
  39. Package string
  40. }
  41. defaultDirContext struct {
  42. inner map[string]Dir
  43. serviceName stringx.String
  44. }
  45. )
  46. func mkdir(ctx *ctx.ProjectContext, proto parser.Proto) (DirContext, error) {
  47. inner := make(map[string]Dir)
  48. etcDir := filepath.Join(ctx.WorkDir, "etc")
  49. internalDir := filepath.Join(ctx.WorkDir, "internal")
  50. configDir := filepath.Join(internalDir, "config")
  51. logicDir := filepath.Join(internalDir, "logic")
  52. serverDir := filepath.Join(internalDir, "server")
  53. svcDir := filepath.Join(internalDir, "svc")
  54. pbDir := filepath.Join(ctx.WorkDir, proto.GoPackage)
  55. callDir := filepath.Join(ctx.WorkDir, strings.ToLower(stringx.From(proto.Service.Name).ToCamel()))
  56. if strings.ToLower(proto.Service.Name) == strings.ToLower(proto.GoPackage) {
  57. callDir = filepath.Join(ctx.WorkDir, strings.ToLower(stringx.From(proto.Service.Name+"_client").ToCamel()))
  58. }
  59. inner[wd] = Dir{
  60. Filename: ctx.WorkDir,
  61. Package: filepath.ToSlash(filepath.Join(ctx.Path, strings.TrimPrefix(ctx.WorkDir, ctx.Dir))),
  62. Base: filepath.Base(ctx.WorkDir),
  63. }
  64. inner[etc] = Dir{
  65. Filename: etcDir,
  66. Package: filepath.ToSlash(filepath.Join(ctx.Path, strings.TrimPrefix(etcDir, ctx.Dir))),
  67. Base: filepath.Base(etcDir),
  68. }
  69. inner[internal] = Dir{
  70. Filename: internalDir,
  71. Package: filepath.ToSlash(filepath.Join(ctx.Path, strings.TrimPrefix(internalDir, ctx.Dir))),
  72. Base: filepath.Base(internalDir),
  73. }
  74. inner[config] = Dir{
  75. Filename: configDir,
  76. Package: filepath.ToSlash(filepath.Join(ctx.Path, strings.TrimPrefix(configDir, ctx.Dir))),
  77. Base: filepath.Base(configDir),
  78. }
  79. inner[logic] = Dir{
  80. Filename: logicDir,
  81. Package: filepath.ToSlash(filepath.Join(ctx.Path, strings.TrimPrefix(logicDir, ctx.Dir))),
  82. Base: filepath.Base(logicDir),
  83. }
  84. inner[server] = Dir{
  85. Filename: serverDir,
  86. Package: filepath.ToSlash(filepath.Join(ctx.Path, strings.TrimPrefix(serverDir, ctx.Dir))),
  87. Base: filepath.Base(serverDir),
  88. }
  89. inner[svc] = Dir{
  90. Filename: svcDir,
  91. Package: filepath.ToSlash(filepath.Join(ctx.Path, strings.TrimPrefix(svcDir, ctx.Dir))),
  92. Base: filepath.Base(svcDir),
  93. }
  94. inner[pb] = Dir{
  95. Filename: pbDir,
  96. Package: filepath.ToSlash(filepath.Join(ctx.Path, strings.TrimPrefix(pbDir, ctx.Dir))),
  97. Base: filepath.Base(pbDir),
  98. }
  99. inner[call] = Dir{
  100. Filename: callDir,
  101. Package: filepath.ToSlash(filepath.Join(ctx.Path, strings.TrimPrefix(callDir, ctx.Dir))),
  102. Base: filepath.Base(callDir),
  103. }
  104. for _, v := range inner {
  105. err := util.MkdirIfNotExist(v.Filename)
  106. if err != nil {
  107. return nil, err
  108. }
  109. }
  110. serviceName := strings.TrimSuffix(proto.Name, filepath.Ext(proto.Name))
  111. return &defaultDirContext{
  112. inner: inner,
  113. serviceName: stringx.From(strings.ReplaceAll(serviceName, "-", "")),
  114. }, nil
  115. }
  116. func (d *defaultDirContext) GetCall() Dir {
  117. return d.inner[call]
  118. }
  119. func (d *defaultDirContext) GetEtc() Dir {
  120. return d.inner[etc]
  121. }
  122. func (d *defaultDirContext) GetInternal() Dir {
  123. return d.inner[internal]
  124. }
  125. func (d *defaultDirContext) GetConfig() Dir {
  126. return d.inner[config]
  127. }
  128. func (d *defaultDirContext) GetLogic() Dir {
  129. return d.inner[logic]
  130. }
  131. func (d *defaultDirContext) GetServer() Dir {
  132. return d.inner[server]
  133. }
  134. func (d *defaultDirContext) GetSvc() Dir {
  135. return d.inner[svc]
  136. }
  137. func (d *defaultDirContext) GetPb() Dir {
  138. return d.inner[pb]
  139. }
  140. func (d *defaultDirContext) GetMain() Dir {
  141. return d.inner[wd]
  142. }
  143. func (d *defaultDirContext) GetServiceName() stringx.String {
  144. return d.serviceName
  145. }
  146. // Valid returns true if the directory is valid
  147. func (d *Dir) Valid() bool {
  148. return len(d.Filename) > 0 && len(d.Package) > 0
  149. }