project.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package project
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "os/exec"
  6. "path/filepath"
  7. "regexp"
  8. "strings"
  9. "github.com/tal-tech/go-zero/tools/goctl/rpc/execx"
  10. )
  11. const (
  12. constGo = "go"
  13. constProtoC = "protoc"
  14. constGoMod = "go env GOMOD"
  15. constGoPath = "go env GOPATH"
  16. constProtoCGenGo = "protoc-gen-go"
  17. )
  18. type (
  19. Project struct {
  20. Path string
  21. Name string
  22. GoMod GoMod
  23. }
  24. GoMod struct {
  25. Module string
  26. Path string
  27. }
  28. )
  29. func Prepare(projectDir string, checkGrpcEnv bool) (*Project, error) {
  30. _, err := exec.LookPath(constGo)
  31. if err != nil {
  32. return nil, err
  33. }
  34. if checkGrpcEnv {
  35. _, err = exec.LookPath(constProtoC)
  36. if err != nil {
  37. return nil, err
  38. }
  39. _, err = exec.LookPath(constProtoCGenGo)
  40. if err != nil {
  41. return nil, err
  42. }
  43. }
  44. var (
  45. goMod, module string
  46. goPath string
  47. name, path string
  48. )
  49. ret, err := execx.Run(constGoMod)
  50. if err != nil {
  51. return nil, err
  52. }
  53. goMod = strings.TrimSpace(ret)
  54. ret, err = execx.Run(constGoPath)
  55. if err != nil {
  56. return nil, err
  57. }
  58. goPath = strings.TrimSpace(ret)
  59. src := filepath.Join(goPath, "src")
  60. if len(goMod) > 0 {
  61. path = filepath.Dir(goMod)
  62. name = filepath.Base(path)
  63. data, err := ioutil.ReadFile(goMod)
  64. if err != nil {
  65. return nil, err
  66. }
  67. module, err = matchModule(data)
  68. if err != nil {
  69. return nil, err
  70. }
  71. } else {
  72. pwd, err := os.Getwd()
  73. if err != nil {
  74. return nil, err
  75. }
  76. if !strings.HasPrefix(pwd, src) {
  77. absPath, err := filepath.Abs(projectDir)
  78. if err != nil {
  79. return nil, err
  80. }
  81. name = filepath.Clean(filepath.Base(absPath))
  82. path = projectDir
  83. } else {
  84. r := strings.TrimPrefix(pwd, src+string(filepath.Separator))
  85. name = filepath.Dir(r)
  86. if name == "." {
  87. name = r
  88. }
  89. path = filepath.Join(src, name)
  90. }
  91. module = name
  92. }
  93. return &Project{
  94. Name: name,
  95. Path: path,
  96. GoMod: GoMod{
  97. Module: module,
  98. Path: goMod,
  99. },
  100. }, nil
  101. }
  102. func matchModule(data []byte) (string, error) {
  103. text := string(data)
  104. re := regexp.MustCompile(`(?m)^\s*module\s+[a-z0-9/\-.]+$`)
  105. matches := re.FindAllString(text, -1)
  106. if len(matches) == 1 {
  107. target := matches[0]
  108. index := strings.Index(target, "module")
  109. return strings.TrimSpace(target[index+6:]), nil
  110. }
  111. return "", nil
  112. }