mkdir.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. package generator
  2. import (
  3. "path/filepath"
  4. "strings"
  5. conf "github.com/zeromicro/go-zero/tools/goctl/config"
  6. "github.com/zeromicro/go-zero/tools/goctl/rpc/parser"
  7. "github.com/zeromicro/go-zero/tools/goctl/util/ctx"
  8. "github.com/zeromicro/go-zero/tools/goctl/util/pathx"
  9. "github.com/zeromicro/go-zero/tools/goctl/util/stringx"
  10. )
  11. const (
  12. wd = "wd"
  13. etc = "etc"
  14. internal = "internal"
  15. config = "config"
  16. logic = "logic"
  17. server = "server"
  18. svc = "svc"
  19. pb = "pb"
  20. protoGo = "proto-go"
  21. call = "call"
  22. )
  23. type (
  24. // DirContext defines a rpc service directories context
  25. DirContext interface {
  26. GetCall() Dir
  27. GetEtc() Dir
  28. GetInternal() Dir
  29. GetConfig() Dir
  30. GetLogic() Dir
  31. GetServer() Dir
  32. GetSvc() Dir
  33. GetPb() Dir
  34. GetProtoGo() Dir
  35. GetMain() Dir
  36. GetServiceName() stringx.String
  37. SetPbDir(pbDir, grpcDir string)
  38. }
  39. // Dir defines a directory
  40. Dir struct {
  41. Base string
  42. Filename string
  43. Package string
  44. GetChildPackage func(childPath string) (string, error)
  45. }
  46. defaultDirContext struct {
  47. inner map[string]Dir
  48. serviceName stringx.String
  49. ctx *ctx.ProjectContext
  50. }
  51. )
  52. func mkdir(ctx *ctx.ProjectContext, proto parser.Proto, _ *conf.Config, c *ZRpcContext) (DirContext,
  53. error) {
  54. inner := make(map[string]Dir)
  55. etcDir := filepath.Join(ctx.WorkDir, "etc")
  56. clientDir := filepath.Join(ctx.WorkDir, "client")
  57. internalDir := filepath.Join(ctx.WorkDir, "internal")
  58. configDir := filepath.Join(internalDir, "config")
  59. logicDir := filepath.Join(internalDir, "logic")
  60. serverDir := filepath.Join(internalDir, "server")
  61. svcDir := filepath.Join(internalDir, "svc")
  62. pbDir := filepath.Join(ctx.WorkDir, proto.GoPackage)
  63. protoGoDir := pbDir
  64. if c != nil {
  65. pbDir = c.ProtoGenGrpcDir
  66. protoGoDir = c.ProtoGenGoDir
  67. }
  68. getChildPackage := func(parent, childPath string) (string, error) {
  69. child := strings.TrimPrefix(childPath, parent)
  70. abs := filepath.Join(parent, strings.ToLower(child))
  71. if c.Multiple {
  72. if err := pathx.MkdirIfNotExist(abs); err != nil {
  73. return "", err
  74. }
  75. }
  76. childPath = strings.TrimPrefix(abs, ctx.Dir)
  77. pkg := filepath.Join(ctx.Path, childPath)
  78. return filepath.ToSlash(pkg), nil
  79. }
  80. if !c.Multiple {
  81. callDir := filepath.Join(ctx.WorkDir,
  82. strings.ToLower(stringx.From(proto.Service[0].Name).ToCamel()))
  83. if strings.EqualFold(proto.Service[0].Name, filepath.Base(proto.GoPackage)) {
  84. callDir = filepath.Join(ctx.WorkDir,
  85. strings.ToLower(stringx.From(proto.Service[0].Name+"_client").ToCamel()))
  86. }
  87. inner[call] = Dir{
  88. Filename: callDir,
  89. Package: filepath.ToSlash(filepath.Join(ctx.Path,
  90. strings.TrimPrefix(callDir, ctx.Dir))),
  91. Base: filepath.Base(callDir),
  92. GetChildPackage: func(childPath string) (string, error) {
  93. return getChildPackage(callDir, childPath)
  94. },
  95. }
  96. } else {
  97. inner[call] = Dir{
  98. Filename: clientDir,
  99. Package: filepath.ToSlash(filepath.Join(ctx.Path,
  100. strings.TrimPrefix(clientDir, ctx.Dir))),
  101. Base: filepath.Base(clientDir),
  102. GetChildPackage: func(childPath string) (string, error) {
  103. return getChildPackage(clientDir, childPath)
  104. },
  105. }
  106. }
  107. inner[wd] = Dir{
  108. Filename: ctx.WorkDir,
  109. Package: filepath.ToSlash(filepath.Join(ctx.Path,
  110. strings.TrimPrefix(ctx.WorkDir, ctx.Dir))),
  111. Base: filepath.Base(ctx.WorkDir),
  112. GetChildPackage: func(childPath string) (string, error) {
  113. return getChildPackage(ctx.WorkDir, childPath)
  114. },
  115. }
  116. inner[etc] = Dir{
  117. Filename: etcDir,
  118. Package: filepath.ToSlash(filepath.Join(ctx.Path, strings.TrimPrefix(etcDir, ctx.Dir))),
  119. Base: filepath.Base(etcDir),
  120. GetChildPackage: func(childPath string) (string, error) {
  121. return getChildPackage(etcDir, childPath)
  122. },
  123. }
  124. inner[internal] = Dir{
  125. Filename: internalDir,
  126. Package: filepath.ToSlash(filepath.Join(ctx.Path,
  127. strings.TrimPrefix(internalDir, ctx.Dir))),
  128. Base: filepath.Base(internalDir),
  129. GetChildPackage: func(childPath string) (string, error) {
  130. return getChildPackage(internalDir, childPath)
  131. },
  132. }
  133. inner[config] = Dir{
  134. Filename: configDir,
  135. Package: filepath.ToSlash(filepath.Join(ctx.Path, strings.TrimPrefix(configDir, ctx.Dir))),
  136. Base: filepath.Base(configDir),
  137. GetChildPackage: func(childPath string) (string, error) {
  138. return getChildPackage(configDir, childPath)
  139. },
  140. }
  141. inner[logic] = Dir{
  142. Filename: logicDir,
  143. Package: filepath.ToSlash(filepath.Join(ctx.Path, strings.TrimPrefix(logicDir, ctx.Dir))),
  144. Base: filepath.Base(logicDir),
  145. GetChildPackage: func(childPath string) (string, error) {
  146. return getChildPackage(logicDir, childPath)
  147. },
  148. }
  149. inner[server] = Dir{
  150. Filename: serverDir,
  151. Package: filepath.ToSlash(filepath.Join(ctx.Path, strings.TrimPrefix(serverDir, ctx.Dir))),
  152. Base: filepath.Base(serverDir),
  153. GetChildPackage: func(childPath string) (string, error) {
  154. return getChildPackage(serverDir, childPath)
  155. },
  156. }
  157. inner[svc] = Dir{
  158. Filename: svcDir,
  159. Package: filepath.ToSlash(filepath.Join(ctx.Path, strings.TrimPrefix(svcDir, ctx.Dir))),
  160. Base: filepath.Base(svcDir),
  161. GetChildPackage: func(childPath string) (string, error) {
  162. return getChildPackage(svcDir, childPath)
  163. },
  164. }
  165. inner[pb] = Dir{
  166. Filename: pbDir,
  167. Package: filepath.ToSlash(filepath.Join(ctx.Path, strings.TrimPrefix(pbDir, ctx.Dir))),
  168. Base: filepath.Base(pbDir),
  169. GetChildPackage: func(childPath string) (string, error) {
  170. return getChildPackage(pbDir, childPath)
  171. },
  172. }
  173. inner[protoGo] = Dir{
  174. Filename: protoGoDir,
  175. Package: filepath.ToSlash(filepath.Join(ctx.Path,
  176. strings.TrimPrefix(protoGoDir, ctx.Dir))),
  177. Base: filepath.Base(protoGoDir),
  178. GetChildPackage: func(childPath string) (string, error) {
  179. return getChildPackage(protoGoDir, childPath)
  180. },
  181. }
  182. for _, v := range inner {
  183. err := pathx.MkdirIfNotExist(v.Filename)
  184. if err != nil {
  185. return nil, err
  186. }
  187. }
  188. serviceName := strings.TrimSuffix(proto.Name, filepath.Ext(proto.Name))
  189. return &defaultDirContext{
  190. ctx: ctx,
  191. inner: inner,
  192. serviceName: stringx.From(strings.ReplaceAll(serviceName, "-", "")),
  193. }, nil
  194. }
  195. func (d *defaultDirContext) SetPbDir(pbDir, grpcDir string) {
  196. d.inner[pb] = Dir{
  197. Filename: pbDir,
  198. Package: filepath.ToSlash(filepath.Join(d.ctx.Path, strings.TrimPrefix(pbDir, d.ctx.Dir))),
  199. Base: filepath.Base(pbDir),
  200. }
  201. d.inner[protoGo] = Dir{
  202. Filename: grpcDir,
  203. Package: filepath.ToSlash(filepath.Join(d.ctx.Path,
  204. strings.TrimPrefix(grpcDir, d.ctx.Dir))),
  205. Base: filepath.Base(grpcDir),
  206. }
  207. }
  208. func (d *defaultDirContext) GetCall() Dir {
  209. return d.inner[call]
  210. }
  211. func (d *defaultDirContext) GetEtc() Dir {
  212. return d.inner[etc]
  213. }
  214. func (d *defaultDirContext) GetInternal() Dir {
  215. return d.inner[internal]
  216. }
  217. func (d *defaultDirContext) GetConfig() Dir {
  218. return d.inner[config]
  219. }
  220. func (d *defaultDirContext) GetLogic() Dir {
  221. return d.inner[logic]
  222. }
  223. func (d *defaultDirContext) GetServer() Dir {
  224. return d.inner[server]
  225. }
  226. func (d *defaultDirContext) GetSvc() Dir {
  227. return d.inner[svc]
  228. }
  229. func (d *defaultDirContext) GetPb() Dir {
  230. return d.inner[pb]
  231. }
  232. func (d *defaultDirContext) GetProtoGo() Dir {
  233. return d.inner[protoGo]
  234. }
  235. func (d *defaultDirContext) GetMain() Dir {
  236. return d.inner[wd]
  237. }
  238. func (d *defaultDirContext) GetServiceName() stringx.String {
  239. return d.serviceName
  240. }
  241. // Valid returns true if the directory is valid
  242. func (d *Dir) Valid() bool {
  243. return len(d.Filename) > 0 && len(d.Package) > 0
  244. }