gendir.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package gen
  2. import (
  3. "path/filepath"
  4. "runtime"
  5. "strings"
  6. "github.com/tal-tech/go-zero/tools/goctl/util"
  7. )
  8. // target
  9. // ├── etc
  10. // ├── internal
  11. // │   ├── config
  12. // │   ├── handler
  13. // │   ├── logic
  14. // │   ├── pb
  15. // │   └── svc
  16. func (g *defaultRpcGenerator) createDir() error {
  17. ctx := g.Ctx
  18. m := make(map[string]string)
  19. m[dirTarget] = ctx.TargetDir
  20. m[dirEtc] = filepath.Join(ctx.TargetDir, dirEtc)
  21. m[dirInternal] = filepath.Join(ctx.TargetDir, dirInternal)
  22. m[dirConfig] = filepath.Join(ctx.TargetDir, dirInternal, dirConfig)
  23. m[dirServer] = filepath.Join(ctx.TargetDir, dirInternal, dirServer)
  24. m[dirLogic] = filepath.Join(ctx.TargetDir, dirInternal, dirLogic)
  25. m[dirPb] = filepath.Join(ctx.TargetDir, dirPb)
  26. m[dirSvc] = filepath.Join(ctx.TargetDir, dirInternal, dirSvc)
  27. for _, d := range m {
  28. err := util.MkdirIfNotExist(d)
  29. if err != nil {
  30. return err
  31. }
  32. }
  33. g.dirM = m
  34. return nil
  35. }
  36. func (g *defaultRpcGenerator) mustGetPackage(dir string) string {
  37. target := g.dirM[dir]
  38. projectPath := g.Ctx.ProjectPath
  39. relativePath := strings.TrimPrefix(target, projectPath)
  40. os := runtime.GOOS
  41. switch os {
  42. case "windows":
  43. relativePath = filepath.ToSlash(relativePath)
  44. case "darwin", "linux":
  45. default:
  46. g.Ctx.Fatalln("unexpected os: %s", os)
  47. }
  48. return g.Ctx.Module + relativePath
  49. }