mkdir.go 4.2 KB

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