mkdir.go 7.2 KB

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